4
0

favicons.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. $dom = new DOMDocument();
  30. ['body' => $html, 'effective_url' => $effective_url, 'fail' => $fail] =
  31. FreshRSS_http_Util::httpGet($url, cachePath: CACHE_PATH . '/' . sha1($url) . '.html', type: 'html');
  32. if ($fail || $html === '' || !@$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  33. return '';
  34. }
  35. $xpath = new DOMXPath($dom);
  36. $links = $xpath->query('//link[@href][translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="shortcut icon"'
  37. . ' or translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="icon"]');
  38. if (!($links instanceof DOMNodeList)) {
  39. return '';
  40. }
  41. // Use the base element for relative paths, if there is one
  42. $baseElements = $xpath->query('//base[@href]');
  43. $baseElement = ($baseElements !== false && $baseElements->length > 0) ? $baseElements->item(0) : null;
  44. $baseUrl = ($baseElement instanceof DOMElement) ? $baseElement->getAttribute('href') : $effective_url;
  45. foreach ($links as $link) {
  46. if (!$link instanceof DOMElement) {
  47. continue;
  48. }
  49. $href = trim($link->getAttribute('href'));
  50. $urlParts = parse_url($effective_url);
  51. // Handle protocol-relative URLs by adding the current URL's scheme
  52. if (substr($href, 0, 2) === '//') {
  53. $href = ($urlParts['scheme'] ?? 'https') . ':' . $href;
  54. }
  55. $href = \SimplePie\IRI::absolutize($baseUrl, $href);
  56. if ($href == false) {
  57. return '';
  58. }
  59. $iri = $href->get_iri();
  60. if ($iri == false) {
  61. continue;
  62. }
  63. $iri = FreshRSS_http_Util::checkUrl($iri, fixScheme: false);
  64. if (!is_string($iri) || $iri === '') {
  65. continue;
  66. }
  67. $favicon = FreshRSS_http_Util::httpGet($iri, faviconCachePath($iri), 'ico', curl_options: [
  68. CURLOPT_REFERER => $effective_url,
  69. ])['body'];
  70. if (isImgMime($favicon)) {
  71. return $favicon;
  72. }
  73. }
  74. return '';
  75. }
  76. /**
  77. * Downloads a favicon directly from a known image URL (e.g. from a feed's <image><url> or icon field).
  78. * Returns false without any fallback if the URL does not point to a valid image.
  79. */
  80. function download_favicon_from_image_url(string $imageUrl, string $dest): bool {
  81. $imageUrl = FreshRSS_http_Util::checkUrl($imageUrl);
  82. if (!is_string($imageUrl) || $imageUrl === '') {
  83. return false;
  84. }
  85. $favicon = FreshRSS_http_Util::httpGet($imageUrl, faviconCachePath($imageUrl), 'ico')['body'];
  86. if (!isImgMime($favicon)) {
  87. return false;
  88. }
  89. return file_put_contents($dest, $favicon) > 0;
  90. }
  91. function download_favicon(string $url, string $dest): bool {
  92. $url = FreshRSS_http_Util::checkUrl($url);
  93. if (!is_string($url) || $url === '') {
  94. return @copy(DEFAULT_FAVICON, $dest);
  95. }
  96. $favicon = searchFavicon($url);
  97. if ($favicon == '') {
  98. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url) ?? $url;
  99. if ($rootUrl != $url) {
  100. $url = $rootUrl;
  101. $favicon = searchFavicon($url);
  102. }
  103. if ($favicon == '') {
  104. $link = FreshRSS_http_Util::checkUrl($rootUrl . 'favicon.ico', fixScheme: false) ?: '';
  105. $favicon = $link === '' ? '' : FreshRSS_http_Util::httpGet($link, faviconCachePath($link), 'ico', curl_options: [
  106. CURLOPT_REFERER => $url,
  107. ])['body'];
  108. if (!isImgMime($favicon)) {
  109. $favicon = '';
  110. }
  111. }
  112. }
  113. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  114. @copy(DEFAULT_FAVICON, $dest);
  115. }
  116. function contentType(string $ico): string {
  117. $ico_content_type = 'image/x-icon';
  118. if (function_exists('mime_content_type')) {
  119. $ico_content_type = mime_content_type($ico) ?: $ico_content_type;
  120. }
  121. switch ($ico_content_type) {
  122. case 'image/svg':
  123. $ico_content_type = 'image/svg+xml';
  124. break;
  125. }
  126. return $ico_content_type;
  127. }