mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-03-17 22:06:59 -04:00
* [ci] Add Travis * Exclude some libs * Semi-auto whitespace fixes * line length in SQLite * Exclude tests from line length * Feed.php line length * Feed.php: get rid of unnecessary concat * Feed.php: line length * bootstrap.php: no newline at end of file * Allow concatenating across multiple lines * Add Travis badge * do-install line length * update-or-create-user line length * cli/create-user line length * tests/app/Models/SearchTest.php fix indentation * tests/app/Models/UserQueryTest.php fix indentation * tests/app/Models/CategoryTest.php fix indentation * [fix] PHP 5.3 on precise * cli/do-install no spaces * cli/list-users line length * cli/reconfigure line length * empty catch statements * api/index line length nonsense * spaces before semicolon * app/Models/EntryDAO bunch of indentation * extra blank lines * spaces before comma in function call * testing tabwidth * increase to 10 * comment out tabwidth line * try older phpcs version 3.0.0RC4 * line length exception for app/install.php * proper spaces * stray spaces in i18n * Minz/ModelPdo line length * Minz whitespace * greader line length * greader elseif placement * app/Models/Feed.php spacing in function argument * ignore php 5.3 * app/Models/ConfigurationSetter.php stray whitespace * EntryDAOSQLite line length * I vote for higher max line length =P * ignore SQL * remove classname complaint * line length/more legible SQL * ignore line length nonsense * greader line length * feedController issues * uppercase TRUE, FALSE, NULL * revert * importExportController lowercase null * Share.php default value not necessary because ! is_array () a few lines down * CategoryDAO constants should be UPPERCASE * EntryDAO reduce line length * contentious autofix * Allow failures on all versions of PHP except 7.1 because reasons
111 lines
3.2 KiB
PHP
111 lines
3.2 KiB
PHP
<?php
|
|
$favicons_dir = DATA_PATH . '/favicons/';
|
|
$default_favicon = PUBLIC_PATH . '/themes/icons/default_favicon.ico';
|
|
|
|
function isImgMime($content) {
|
|
//Based on https://github.com/ArthurHoaro/favicon/blob/3a4f93da9bb24915b21771eb7873a21bde26f5d1/src/Favicon/Favicon.php#L311-L319
|
|
if ($content == '') {
|
|
return false;
|
|
}
|
|
if (!extension_loaded('fileinfo')) {
|
|
return true;
|
|
}
|
|
$isImage = true;
|
|
try {
|
|
$fInfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$isImage = strpos(finfo_buffer($fInfo, $content), 'image') !== false;
|
|
finfo_close($fInfo);
|
|
} catch (Exception $e) {
|
|
echo 'Caught exception: ', $e->getMessage(), "\n";
|
|
}
|
|
return $isImage;
|
|
}
|
|
|
|
function downloadHttp(&$url, $curlOptions = array()) {
|
|
syslog(LOG_INFO, 'FreshRSS Favicon GET ' . $url);
|
|
if (substr($url, 0, 2) === '//') {
|
|
$url = 'https:' . $favicon;
|
|
}
|
|
if ($url == '' || filter_var($url, FILTER_VALIDATE_URL) === false) {
|
|
return '';
|
|
}
|
|
$ch = curl_init($url);
|
|
curl_setopt_array($ch, array(
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_MAXREDIRS => 10,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => 15,
|
|
CURLOPT_USERAGENT => 'FreshRSS/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ')',
|
|
));
|
|
if (defined('CURLOPT_ENCODING')) {
|
|
curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
|
|
}
|
|
curl_setopt_array($ch, $curlOptions);
|
|
$response = curl_exec($ch);
|
|
$info = curl_getinfo($ch);
|
|
curl_close($ch);
|
|
if (!empty($info['url']) && (filter_var($info['url'], FILTER_VALIDATE_URL) !== false)) {
|
|
$url = $info['url']; //Possible redirect
|
|
}
|
|
return $info['http_code'] == 200 ? $response : '';
|
|
}
|
|
|
|
function searchFavicon(&$url) {
|
|
$dom = new DOMDocument();
|
|
$html = downloadHttp($url);
|
|
if ($html != '' && @$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
|
|
$rels = array('shortcut icon', 'icon');
|
|
$links = $dom->getElementsByTagName('link');
|
|
foreach ($rels as $rel) {
|
|
foreach ($links as $link) {
|
|
if ($link->hasAttribute('rel') && $link->hasAttribute('href') &&
|
|
strtolower(trim($link->getAttribute('rel'))) === $rel) {
|
|
$href = trim($link->getAttribute('href'));
|
|
if (substr($href, 0, 2) === '//') {
|
|
// Case of protocol-relative URLs
|
|
if (preg_match('%^(https?:)//%i', $url, $matches)) {
|
|
$href = $matches[1] . $href;
|
|
} else {
|
|
$href = 'https:' . $href;
|
|
}
|
|
}
|
|
if (filter_var($href, FILTER_VALIDATE_URL) === false) {
|
|
$href = SimplePie_IRI::absolutize($url, $href);
|
|
}
|
|
$favicon = downloadHttp($href, array(
|
|
CURLOPT_REFERER => $url,
|
|
));
|
|
if (isImgMime($favicon)) {
|
|
return $favicon;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
function download_favicon($url, $dest) {
|
|
global $default_favicon;
|
|
$url = trim($url);
|
|
$favicon = searchFavicon($url);
|
|
if ($favicon == '') {
|
|
$rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
|
|
if ($rootUrl != $url) {
|
|
$url = $rootUrl;
|
|
$favicon = searchFavicon($url);
|
|
}
|
|
if ($favicon == '') {
|
|
$link = $rootUrl . 'favicon.ico';
|
|
$favicon = downloadHttp($link, array(
|
|
CURLOPT_REFERER => $url,
|
|
));
|
|
if (!isImgMime($favicon)) {
|
|
$favicon = '';
|
|
}
|
|
}
|
|
}
|
|
return ($favicon != '' && file_put_contents($dest, $favicon)) ||
|
|
@copy($default_favicon, $dest);
|
|
}
|