favicons.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. function download_favicon(string $url, string $dest): bool {
  73. $url = trim($url);
  74. $favicon = searchFavicon($url);
  75. if ($favicon == '') {
  76. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url) ?? $url;
  77. if ($rootUrl != $url) {
  78. $url = $rootUrl;
  79. $favicon = searchFavicon($url);
  80. }
  81. if ($favicon == '') {
  82. $link = $rootUrl . 'favicon.ico';
  83. $favicon = FreshRSS_http_Util::httpGet($link, faviconCachePath($link), 'ico', curl_options: [
  84. CURLOPT_REFERER => $url,
  85. ])['body'];
  86. if (!isImgMime($favicon)) {
  87. $favicon = '';
  88. }
  89. }
  90. }
  91. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  92. @copy(DEFAULT_FAVICON, $dest);
  93. }
  94. function contentType(string $ico): string {
  95. $ico_content_type = 'image/x-icon';
  96. if (function_exists('mime_content_type')) {
  97. $ico_content_type = mime_content_type($ico) ?: $ico_content_type;
  98. }
  99. switch ($ico_content_type) {
  100. case 'image/svg':
  101. $ico_content_type = 'image/svg+xml';
  102. break;
  103. }
  104. return $ico_content_type;
  105. }