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;
|
|
|
|
|
2016-12-26 15:44:53 +01:00
|
|
|
const TABLE = 'favicons';
|
|
|
|
const JOIN_TABLE = 'favicons_feeds';
|
|
|
|
|
2016-12-26 22:54:44 +01:00
|
|
|
function create_feed_favicon($feed_id, $mime_type, $blob)
|
2016-04-18 01:44:45 +02:00
|
|
|
{
|
2016-12-26 22:54:44 +01:00
|
|
|
$favicon_id = store_favicon($mime_type, $blob);
|
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)
|
2016-12-26 22:54:44 +01:00
|
|
|
->insert(array(
|
2016-12-26 15:44:53 +01:00
|
|
|
'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 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-12-26 22:54:44 +01:00
|
|
|
if (file_put_contents(get_favicon_filename($hash, $mime_type), $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 22:54:44 +01:00
|
|
|
function purge_favicons()
|
2016-12-26 15:44:53 +01:00
|
|
|
{
|
2016-12-26 22:54:44 +01:00
|
|
|
$favicons = Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(TABLE)
|
2016-12-26 22:54:44 +01:00
|
|
|
->join(JOIN_TABLE, 'favicon_id', 'id')
|
|
|
|
->isNull('feed_id')
|
|
|
|
->findAll();
|
2016-12-26 15:44:53 +01:00
|
|
|
|
2016-12-26 22:54:44 +01:00
|
|
|
foreach ($favicons as $favicon) {
|
|
|
|
$filename = get_favicon_filename($favicon['hash'], $favicon['type']);
|
|
|
|
Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $favicon['id'])
|
|
|
|
->remove();
|
|
|
|
|
|
|
|
if (file_exists($filename)) {
|
|
|
|
unlink($filename);
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 01:04:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function has_favicon($feed_id)
|
|
|
|
{
|
2016-12-26 22:54:44 +01:00
|
|
|
$favicon = Database::getInstance('db')
|
2016-12-26 15:44:53 +01:00
|
|
|
->table(JOIN_TABLE)
|
|
|
|
->eq('feed_id', $feed_id)
|
2016-12-26 22:54:44 +01:00
|
|
|
->join(TABLE, 'id', 'favicon_id')
|
|
|
|
->findOne();
|
|
|
|
|
|
|
|
$has_favicon = ! empty($favicon);
|
|
|
|
|
|
|
|
if ($has_favicon && ! file_exists(get_favicon_filename($favicon['hash'], $favicon['type']))) {
|
|
|
|
Database::getInstance('db')
|
|
|
|
->table(TABLE)
|
|
|
|
->eq('id', $favicon['id'])
|
|
|
|
->remove();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $has_favicon;
|
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();
|
|
|
|
$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)
|
2016-12-26 22:54:44 +01:00
|
|
|
->columns('feed_id', 'hash', 'type')
|
|
|
|
->join(JOIN_TABLE, 'favicon_id', 'id')
|
|
|
|
->join(Model\Feed\TABLE, 'id', 'feed_id', JOIN_TABLE)
|
2016-12-26 15:44:53 +01:00
|
|
|
->eq(Model\Feed\TABLE.'.user_id', $user_id)
|
2016-12-26 22:54:44 +01:00
|
|
|
->asc(TABLE.'.id')
|
2016-12-26 15:44:53 +01:00
|
|
|
->findAll();
|
|
|
|
|
|
|
|
foreach ($favicons as &$favicon) {
|
2016-12-26 22:54:44 +01:00
|
|
|
$favicon['url'] = get_favicon_data_url($favicon['hash'], $favicon['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
|
|
|
}
|
2016-12-26 22:54:44 +01:00
|
|
|
|
|
|
|
function get_favicon_filename($hash, $mime_type)
|
|
|
|
{
|
|
|
|
return FAVICON_DIRECTORY.DIRECTORY_SEPARATOR.$hash.Helper\favicon_extension($mime_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_favicon_data_url($hash, $mime_type)
|
|
|
|
{
|
|
|
|
$blob = base64_encode(file_get_contents(get_favicon_filename($hash, $mime_type)));
|
|
|
|
return sprintf('data:%s;base64,%s', $mime_type, $blob);
|
|
|
|
}
|