2016-01-10 01:04:42 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-25 03:17:58 +02:00
|
|
|
namespace Miniflux\Model\Favicon;
|
2016-01-10 01:04:42 +01:00
|
|
|
|
2016-08-25 03:17:58 +02:00
|
|
|
use Miniflux\Model\Config;
|
|
|
|
use Miniflux\Model\Group;
|
|
|
|
use Miniflux\Helper;
|
2016-01-10 01:04:42 +01:00
|
|
|
use PicoDb\Database;
|
|
|
|
use PicoFeed\Reader\Favicon;
|
|
|
|
|
|
|
|
// Create a favicons
|
2016-04-18 01:44:45 +02:00
|
|
|
function create_feed_favicon($feed_id, $site_url, $icon_link)
|
|
|
|
{
|
2016-01-10 01:04:42 +01:00
|
|
|
if (has_favicon($feed_id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$favicon = fetch($feed_id, $site_url, $icon_link);
|
|
|
|
|
|
|
|
if ($favicon === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$favicon_id = store($favicon->getType(), $favicon->getContent());
|
|
|
|
|
|
|
|
if ($favicon_id === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Database::getInstance('db')
|
|
|
|
->table('favicons_feeds')
|
|
|
|
->save(array(
|
|
|
|
'feed_id' => $feed_id,
|
|
|
|
'favicon_id' => $favicon_id
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Download a favicon
|
|
|
|
function fetch($feed_id, $site_url, $icon_link)
|
|
|
|
{
|
|
|
|
if (Config\get('favicons') == 1 && ! has_favicon($feed_id)) {
|
|
|
|
$favicon = new Favicon;
|
|
|
|
$favicon->find($site_url, $icon_link);
|
|
|
|
return $favicon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the favicon (only if it does not exist yet)
|
|
|
|
function store($type, $icon)
|
|
|
|
{
|
2016-01-12 01:53:18 +01:00
|
|
|
if ($icon === '') {
|
2016-01-10 01:04:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$hash = sha1($icon);
|
|
|
|
|
|
|
|
$favicon_id = get_favicon_id($hash);
|
|
|
|
|
|
|
|
if ($favicon_id) {
|
|
|
|
return $favicon_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = $hash.Helper\favicon_extension($type);
|
|
|
|
|
|
|
|
if (file_put_contents(FAVICON_DIRECTORY.DIRECTORY_SEPARATOR.$file, $icon) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$saved = Database::getInstance('db')
|
|
|
|
->table('favicons')
|
|
|
|
->save(array(
|
|
|
|
'hash' => $hash,
|
|
|
|
'type' => $type
|
|
|
|
));
|
|
|
|
|
|
|
|
if ($saved === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_favicon_id($hash);
|
|
|
|
}
|
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
function get_favicon_id($hash)
|
|
|
|
{
|
2016-01-10 01:04:42 +01:00
|
|
|
return Database::getInstance('db')
|
|
|
|
->table('favicons')
|
|
|
|
->eq('hash', $hash)
|
|
|
|
->findOneColumn('id');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the favicon
|
|
|
|
function delete_favicon($favicon)
|
|
|
|
{
|
|
|
|
unlink(FAVICON_DIRECTORY.DIRECTORY_SEPARATOR.$favicon['hash'].Helper\favicon_extension($favicon['type']));
|
|
|
|
Database::getInstance('db')
|
|
|
|
->table('favicons')
|
|
|
|
->eq('hash', $favicon['hash'])
|
|
|
|
->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Purge orphaned favicons from database
|
|
|
|
function purge_favicons()
|
|
|
|
{
|
|
|
|
$favicons = Database::getInstance('db')
|
|
|
|
->table('favicons')
|
|
|
|
->columns(
|
|
|
|
'favicons.type',
|
|
|
|
'favicons.hash',
|
|
|
|
'favicons_feeds.feed_id'
|
|
|
|
)
|
|
|
|
->join('favicons_feeds', 'favicon_id', 'id')
|
2016-01-12 01:53:18 +01:00
|
|
|
->isNull('favicons_feeds.feed_id')
|
2016-01-10 01:04:42 +01:00
|
|
|
->findAll();
|
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
foreach ($favicons as $favicon) {
|
|
|
|
delete_favicon($favicon);
|
|
|
|
}
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the feed has a favicon
|
|
|
|
function has_favicon($feed_id)
|
|
|
|
{
|
|
|
|
return Database::getInstance('db')->table('favicons_feeds')->eq('feed_id', $feed_id)->count() === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get favicons for those feeds
|
|
|
|
function get_favicons(array $feed_ids)
|
|
|
|
{
|
|
|
|
if (Config\get('favicons') == 0) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
foreach ($feed_ids as $feed_id) {
|
|
|
|
$result[$feed_id] = Database::getInstance('db')
|
|
|
|
->table('favicons')
|
|
|
|
->columns(
|
|
|
|
'favicons.type',
|
|
|
|
'favicons.hash'
|
|
|
|
)
|
|
|
|
->join('favicons_feeds', 'favicon_id', 'id')
|
|
|
|
->eq('favicons_feeds.feed_id', $feed_id)
|
|
|
|
->findOne();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all favicons for a list of items
|
|
|
|
function get_item_favicons(array $items)
|
|
|
|
{
|
|
|
|
$feed_ids = array();
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$feed_ids[$item['feed_id']] = $item['feed_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return get_favicons($feed_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all favicons
|
|
|
|
function get_all_favicons()
|
|
|
|
{
|
|
|
|
if (Config\get('favicons') == 0) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = Database::getInstance('db')
|
|
|
|
->table('favicons')
|
|
|
|
->columns(
|
|
|
|
'favicons_feeds.feed_id',
|
|
|
|
'favicons.type',
|
|
|
|
'favicons.hash'
|
|
|
|
)
|
|
|
|
->join('favicons_feeds', 'favicon_id', 'id')
|
|
|
|
->findAll();
|
2016-01-12 01:53:18 +01:00
|
|
|
|
2016-01-10 01:04:42 +01:00
|
|
|
$map = array();
|
|
|
|
|
|
|
|
foreach ($result as $row) {
|
2016-04-18 01:44:45 +02:00
|
|
|
$map[$row['feed_id']] = array(
|
2016-01-10 01:04:42 +01:00
|
|
|
"type" => $row['type'],
|
|
|
|
"hash" => $row['hash']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $map;
|
|
|
|
}
|