favicons.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. const FAVICONS_DIR = DATA_PATH . '/favicons/';
  4. const DEFAULT_FAVICON = PUBLIC_PATH . '/themes/icons/default_favicon.ico';
  5. function isImgMime(string $content): bool {
  6. //Based on https://github.com/ArthurHoaro/favicon/blob/3a4f93da9bb24915b21771eb7873a21bde26f5d1/src/Favicon/Favicon.php#L311-L319
  7. if ($content == '') {
  8. return false;
  9. }
  10. if (!extension_loaded('fileinfo')) {
  11. return true;
  12. }
  13. $fInfo = finfo_open(FILEINFO_MIME_TYPE);
  14. if ($fInfo === false) {
  15. return true;
  16. }
  17. $content = finfo_buffer($fInfo, $content);
  18. $isImage = str_contains($content ?: '', 'image');
  19. return $isImage;
  20. }
  21. function faviconCachePath(string $url): string {
  22. return CACHE_PATH . '/' . sha1($url) . '.ico';
  23. }
  24. function searchFavicon(string $url): string {
  25. $url = trim($url);
  26. if ($url === '') {
  27. return '';
  28. }
  29. ['body' => $html, 'effective_url' => $effective_url, 'fail' => $fail] =
  30. FreshRSS_http_Util::httpGet($url, cachePath: CACHE_PATH . '/' . sha1($url) . '.html', type: 'html');
  31. if ($fail || $html === '') {
  32. return '';
  33. }
  34. $dom = Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
  35. $links = $dom->querySelectorAll('link[href][rel~="icon" i]');
  36. // Use the base element for relative paths, if there is one
  37. $baseElement = $dom->querySelector('base[href]');
  38. $baseUrl = ($baseElement !== null) ? ($baseElement->getAttribute('href') ?? '') : $effective_url;
  39. foreach ($links as $link) {
  40. if (!$link instanceof Dom\Element) {
  41. continue;
  42. }
  43. $href = trim($link->getAttribute('href') ?? '');
  44. $urlParts = parse_url($effective_url);
  45. // Handle protocol-relative URLs by adding the current URL's scheme
  46. if (substr($href, 0, 2) === '//') {
  47. $href = ($urlParts['scheme'] ?? 'https') . ':' . $href;
  48. }
  49. $href = \SimplePie\IRI::absolutize($baseUrl, $href);
  50. if ($href == false) {
  51. return '';
  52. }
  53. $iri = $href->get_iri();
  54. if ($iri == false) {
  55. continue;
  56. }
  57. $iri = FreshRSS_http_Util::checkUrl($iri, fixScheme: false);
  58. if (!is_string($iri) || $iri === '') {
  59. continue;
  60. }
  61. $favicon = FreshRSS_http_Util::httpGet($iri, faviconCachePath($iri), 'ico', curl_options: [
  62. CURLOPT_REFERER => $effective_url,
  63. ])['body'];
  64. if (isImgMime($favicon)) {
  65. return $favicon;
  66. }
  67. }
  68. return '';
  69. }
  70. /**
  71. * Downloads a favicon directly from a known image URL (e.g. from a feed's <image><url> or icon field).
  72. * Returns false without any fallback if the URL does not point to a valid image.
  73. */
  74. function download_favicon_from_image_url(string $imageUrl, string $dest): bool {
  75. $imageUrl = FreshRSS_http_Util::checkUrl($imageUrl);
  76. if (!is_string($imageUrl) || $imageUrl === '') {
  77. return false;
  78. }
  79. $favicon = FreshRSS_http_Util::httpGet($imageUrl, faviconCachePath($imageUrl), 'ico')['body'];
  80. if (!isImgMime($favicon)) {
  81. return false;
  82. }
  83. return file_put_contents($dest, $favicon) > 0;
  84. }
  85. function download_favicon(string $url, string $dest): bool {
  86. $url = FreshRSS_http_Util::checkUrl($url);
  87. if (!is_string($url) || $url === '') {
  88. return @copy(DEFAULT_FAVICON, $dest);
  89. }
  90. $favicon = searchFavicon($url);
  91. if ($favicon == '') {
  92. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url) ?? $url;
  93. if ($rootUrl != $url) {
  94. $url = $rootUrl;
  95. $favicon = searchFavicon($url);
  96. }
  97. if ($favicon == '') {
  98. $link = FreshRSS_http_Util::checkUrl($rootUrl . 'favicon.ico', fixScheme: false) ?: '';
  99. $favicon = $link === '' ? '' : FreshRSS_http_Util::httpGet($link, faviconCachePath($link), 'ico', curl_options: [
  100. CURLOPT_REFERER => $url,
  101. ])['body'];
  102. if (!isImgMime($favicon)) {
  103. $favicon = '';
  104. }
  105. }
  106. }
  107. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  108. @copy(DEFAULT_FAVICON, $dest);
  109. }
  110. function contentType(string $ico): string {
  111. $ico_content_type = 'image/x-icon';
  112. if (function_exists('mime_content_type')) {
  113. $ico_content_type = mime_content_type($ico) ?: $ico_content_type;
  114. }
  115. switch ($ico_content_type) {
  116. case 'image/svg':
  117. $ico_content_type = 'image/svg+xml';
  118. break;
  119. }
  120. return $ico_content_type;
  121. }