favicons.php 4.0 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. $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. return '';
  62. }
  63. $favicon = FreshRSS_http_Util::httpGet($iri, faviconCachePath($iri), 'ico', curl_options: [
  64. CURLOPT_REFERER => $effective_url,
  65. ])['body'];
  66. if (isImgMime($favicon)) {
  67. return $favicon;
  68. }
  69. }
  70. return '';
  71. }
  72. /**
  73. * Downloads a favicon directly from a known image URL (e.g. from a feed's <image><url> or icon field).
  74. * Returns false without any fallback if the URL does not point to a valid image.
  75. */
  76. function download_favicon_from_image_url(string $imageUrl, string $dest): bool {
  77. $imageUrl = trim($imageUrl);
  78. if ($imageUrl === '') {
  79. return false;
  80. }
  81. $favicon = FreshRSS_http_Util::httpGet($imageUrl, faviconCachePath($imageUrl), 'ico')['body'];
  82. if (!isImgMime($favicon)) {
  83. return false;
  84. }
  85. return file_put_contents($dest, $favicon) > 0;
  86. }
  87. function download_favicon(string $url, string $dest): bool {
  88. $url = trim($url);
  89. $favicon = searchFavicon($url);
  90. if ($favicon == '') {
  91. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url) ?? $url;
  92. if ($rootUrl != $url) {
  93. $url = $rootUrl;
  94. $favicon = searchFavicon($url);
  95. }
  96. if ($favicon == '') {
  97. $link = $rootUrl . 'favicon.ico';
  98. $favicon = FreshRSS_http_Util::httpGet($link, faviconCachePath($link), 'ico', curl_options: [
  99. CURLOPT_REFERER => $url,
  100. ])['body'];
  101. if (!isImgMime($favicon)) {
  102. $favicon = '';
  103. }
  104. }
  105. }
  106. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  107. @copy(DEFAULT_FAVICON, $dest);
  108. }
  109. function contentType(string $ico): string {
  110. $ico_content_type = 'image/x-icon';
  111. if (function_exists('mime_content_type')) {
  112. $ico_content_type = mime_content_type($ico) ?: $ico_content_type;
  113. }
  114. switch ($ico_content_type) {
  115. case 'image/svg':
  116. $ico_content_type = 'image/svg+xml';
  117. break;
  118. }
  119. return $ico_content_type;
  120. }