miniflux-legacy/app/models/favicon.php

169 lines
3.9 KiB
PHP
Raw Permalink Normal View History

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;
use Miniflux\Model;
2016-01-10 01:04:42 +01:00
use PicoDb\Database;
const TABLE = 'favicons';
const JOIN_TABLE = 'favicons_feeds';
function create_feed_favicon($feed_id, $mime_type, $blob)
2016-04-18 01:44:45 +02: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')
->table(JOIN_TABLE)
->insert(array(
'feed_id' => $feed_id,
'favicon_id' => $favicon_id
));
2016-01-10 01:04:42 +01:00
}
function store_favicon($mime_type, $blob)
2016-01-10 01:04:42 +01:00
{
if (empty($blob)) {
2016-01-10 01:04:42 +01:00
return false;
}
$hash = sha1($blob);
2017-01-03 02:34:41 +01:00
$favicon_id = Database::getInstance('db')
->table(TABLE)
->eq('hash', $hash)
->findOneColumn('id');
if ($favicon_id) {
return $favicon_id;
}
if (file_put_contents(get_favicon_filename($hash, $mime_type), $blob) === false) {
2016-01-10 01:04:42 +01:00
return false;
}
return Database::getInstance('db')
->table(TABLE)
->persist(array(
'hash' => $hash,
'type' => $mime_type
));
}
2016-01-10 01:04:42 +01:00
function purge_favicons()
{
$favicons = Database::getInstance('db')
->table(TABLE)
->join(JOIN_TABLE, 'favicon_id', 'id')
->isNull('feed_id')
->findAll();
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)
{
$favicon = Database::getInstance('db')
->table(JOIN_TABLE)
->eq('feed_id', $feed_id)
->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
}
function get_favicons_by_feed_ids(array $feed_ids)
2016-01-10 01:04:42 +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
foreach ($favicons as $favicon) {
$result[$favicon['feed_id']] = $favicon;
2016-01-10 01:04:42 +01:00
}
return $result;
}
function get_items_favicons(array $items)
2016-01-10 01:04:42 +01:00
{
$feed_ids = array();
foreach ($items as $item) {
$feed_ids[] = $item['feed_id'];
2016-01-10 01:04:42 +01:00
}
return get_favicons_by_feed_ids(array_unique($feed_ids));
2016-01-10 01:04:42 +01:00
}
function get_feeds_favicons(array $feeds)
2016-01-10 01:04:42 +01:00
{
$feed_ids = array();
foreach ($feeds as $feed) {
$feed_ids[] = $feed['id'];
2016-01-10 01:04:42 +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('feed_id', 'hash', 'type')
->join(JOIN_TABLE, 'favicon_id', 'id')
->join(Model\Feed\TABLE, 'id', 'feed_id', JOIN_TABLE)
->eq(Model\Feed\TABLE.'.user_id', $user_id)
->asc(TABLE.'.id')
->findAll();
foreach ($favicons as &$favicon) {
2016-12-29 21:45:09 +01:00
$favicon['data_url'] = get_favicon_data_url($favicon['hash'], $favicon['type']);
2016-01-10 01:04:42 +01:00
}
return $favicons;
2016-01-10 01:04:42 +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);
}