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\Helper;
|
2016-12-26 15:44:53 +01:00
|
|
|
use Miniflux\Model;
|
2016-01-10 01:04:42 +01:00
|
|
|
use PicoDb\Database;
|
|
|
|
use PicoFeed\Reader\Favicon;
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
const TABLE = 'favicons';
|
|
|
|
const JOIN_TABLE = 'favicons_feeds';
|
|
|
|
|
2016-04-18 01:44:45 +02:00
|
|
|
function create_feed_favicon($feed_id, $site_url, $icon_link)
|
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
$favicon = fetch_favicon($feed_id, $site_url, $icon_link);
|
2016-01-10 01:04:42 +01:00
|
|
|
if ($favicon === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
$favicon_id = store_favicon($favicon->getType(), $favicon->getContent());
|
2016-01-10 01:04:42 +01:00
|
|
|
if ($favicon_id === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(JOIN_TABLE)
|
|
|
|
->save(array(
|
|
|
|
'feed_id' => $feed_id,
|
|
|
|
'favicon_id' => $favicon_id
|
|
|
|
));
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function fetch_favicon($feed_id, $site_url, $icon_link)
|
2016-01-10 01:04:42 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
if (Helper\bool_config('favicons') && ! has_favicon($feed_id)) {
|
|
|
|
$favicon = new Favicon();
|
2016-01-10 01:04:42 +01:00
|
|
|
$favicon->find($site_url, $icon_link);
|
|
|
|
return $favicon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function store_favicon($mime_type, $blob)
|
2016-01-10 01:04:42 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
if (empty($blob)) {
|
2016-01-10 01:04:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
$hash = sha1($blob);
|
2016-01-10 01:04:42 +01:00
|
|
|
$favicon_id = get_favicon_id($hash);
|
|
|
|
|
|
|
|
if ($favicon_id) {
|
|
|
|
return $favicon_id;
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
$file = $hash.Helper\favicon_extension($mime_type);
|
|
|
|
if (file_put_contents(FAVICON_DIRECTORY.DIRECTORY_SEPARATOR.$file, $blob) === false) {
|
2016-01-10 01:04:42 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->persist(array(
|
|
|
|
'hash' => $hash,
|
|
|
|
'type' => $mime_type
|
|
|
|
));
|
|
|
|
}
|
2016-01-10 01:04:42 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_favicon_data_url($filename, $mime_type)
|
|
|
|
{
|
|
|
|
$blob = base64_encode(file_get_contents(FAVICON_DIRECTORY.DIRECTORY_SEPARATOR.$filename));
|
|
|
|
return sprintf('data:%s;base64,%s', $mime_type, $blob);
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
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')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
|
|
|
->eq('hash', $hash)
|
|
|
|
->findOneColumn('id');
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function delete_favicon(array $favicon)
|
2016-01-10 01:04:42 +01:00
|
|
|
{
|
|
|
|
unlink(FAVICON_DIRECTORY.DIRECTORY_SEPARATOR.$favicon['hash'].Helper\favicon_extension($favicon['type']));
|
2016-12-26 15:44:53 +01:00
|
|
|
|
2016-01-10 01:04:42 +01:00
|
|
|
Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
2016-01-10 01:04:42 +01:00
|
|
|
->eq('hash', $favicon['hash'])
|
|
|
|
->remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
function has_favicon($feed_id)
|
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
return Database::getInstance('db')
|
|
|
|
->table(JOIN_TABLE)
|
|
|
|
->eq('feed_id', $feed_id)
|
|
|
|
->exists();
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_favicons_by_feed_ids(array $feed_ids)
|
2016-01-10 01:04:42 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
$result = array();
|
|
|
|
|
|
|
|
if (! Helper\bool_config('favicons')) {
|
|
|
|
return $result;
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
$favicons = Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->columns(
|
|
|
|
'favicons.type',
|
|
|
|
'favicons.hash',
|
|
|
|
'favicons_feeds.feed_id'
|
|
|
|
)
|
|
|
|
->join('favicons_feeds', 'favicon_id', 'id')
|
|
|
|
->in('favicons_feeds.feed_id', $feed_ids)
|
|
|
|
->findAll();
|
2016-01-10 01:04:42 +01:00
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
foreach ($favicons as $favicon) {
|
|
|
|
$result[$favicon['feed_id']] = $favicon;
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_items_favicons(array $items)
|
2016-01-10 01:04:42 +01:00
|
|
|
{
|
|
|
|
$feed_ids = array();
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
2016-12-26 15:44:53 +01:00
|
|
|
$feed_ids[] = $item['feed_id'];
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
return get_favicons_by_feed_ids(array_unique($feed_ids));
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
function get_feeds_favicons(array $feeds)
|
2016-01-10 01:04:42 +01:00
|
|
|
{
|
2016-12-26 15:44:53 +01:00
|
|
|
$feed_ids = array();
|
|
|
|
|
|
|
|
foreach ($feeds as $feed) {
|
|
|
|
$feed_ids[] = $feed['id'];
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
return get_favicons_by_feed_ids($feed_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_favicons_with_data_url($user_id)
|
|
|
|
{
|
|
|
|
$favicons = Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->columns(JOIN_TABLE.'.feed_id', TABLE.'.file', TABLE.'.type')
|
|
|
|
->join(JOIN_TABLE, 'favicon_id', 'id', TABLE)
|
|
|
|
->join(Model\Feed\TABLE, 'id', 'feed_id')
|
|
|
|
->eq(Model\Feed\TABLE.'.user_id', $user_id)
|
|
|
|
->findAll();
|
|
|
|
|
|
|
|
foreach ($favicons as &$favicon) {
|
|
|
|
$favicon['url'] = get_favicon_data_url($favicon['file'], $favicon['mime_type']);
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
return $favicons;
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|