favicons.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. finfo_close($fInfo);
  20. return $isImage;
  21. }
  22. function faviconCachePath(string $url): string {
  23. return CACHE_PATH . '/' . sha1($url) . '.ico';
  24. }
  25. function searchFavicon(string $url): string {
  26. $dom = new DOMDocument();
  27. ['body' => $html, 'effective_url' => $effective_url, 'fail' => $fail] = httpGet($url, cachePath: CACHE_PATH . '/' . sha1($url) . '.html', type: 'html');
  28. if ($fail || $html === '' || !@$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  29. return '';
  30. }
  31. $xpath = new DOMXPath($dom);
  32. $links = $xpath->query('//link[@href][translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="shortcut icon"'
  33. . ' or translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="icon"]');
  34. if (!($links instanceof DOMNodeList)) {
  35. return '';
  36. }
  37. // Use the base element for relative paths, if there is one
  38. $baseElements = $xpath->query('//base[@href]');
  39. $baseElement = ($baseElements !== false && $baseElements->length > 0) ? $baseElements->item(0) : null;
  40. $baseUrl = ($baseElement instanceof DOMElement) ? $baseElement->getAttribute('href') : $effective_url;
  41. foreach ($links as $link) {
  42. if (!$link instanceof DOMElement) {
  43. continue;
  44. }
  45. $href = trim($link->getAttribute('href'));
  46. $urlParts = parse_url($effective_url);
  47. // Handle protocol-relative URLs by adding the current URL's scheme
  48. if (substr($href, 0, 2) === '//') {
  49. $href = ($urlParts['scheme'] ?? 'https') . ':' . $href;
  50. }
  51. $href = \SimplePie\IRI::absolutize($baseUrl, $href);
  52. if ($href == false) {
  53. return '';
  54. }
  55. $iri = $href->get_iri();
  56. if ($iri == false) {
  57. return '';
  58. }
  59. $favicon = httpGet($iri, faviconCachePath($iri), 'ico', curl_options: [
  60. CURLOPT_REFERER => $effective_url,
  61. ])['body'];
  62. if (isImgMime($favicon)) {
  63. return $favicon;
  64. }
  65. }
  66. return '';
  67. }
  68. function download_favicon(string $url, string $dest): bool {
  69. $url = trim($url);
  70. $favicon = searchFavicon($url);
  71. if ($favicon == '') {
  72. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url) ?? $url;
  73. if ($rootUrl != $url) {
  74. $url = $rootUrl;
  75. $favicon = searchFavicon($url);
  76. }
  77. if ($favicon == '') {
  78. $link = $rootUrl . 'favicon.ico';
  79. $favicon = httpGet($link, faviconCachePath($link), 'ico', curl_options: [
  80. CURLOPT_REFERER => $url,
  81. ])['body'];
  82. if (!isImgMime($favicon)) {
  83. $favicon = '';
  84. }
  85. }
  86. }
  87. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  88. @copy(DEFAULT_FAVICON, $dest);
  89. }
  90. function contentType(string $ico): string {
  91. $ico_content_type = 'image/x-icon';
  92. if (function_exists('mime_content_type')) {
  93. $ico_content_type = mime_content_type($ico) ?: $ico_content_type;
  94. }
  95. switch ($ico_content_type) {
  96. case 'image/svg':
  97. $ico_content_type = 'image/svg+xml';
  98. break;
  99. }
  100. return $ico_content_type;
  101. }