favicons.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. $isImage = true;
  14. /** @var finfo $fInfo */
  15. $fInfo = finfo_open(FILEINFO_MIME_TYPE);
  16. /** @var string $content */
  17. $content = finfo_buffer($fInfo, $content);
  18. $isImage = strpos($content, 'image') !== false;
  19. finfo_close($fInfo);
  20. return $isImage;
  21. }
  22. /** @param array<int,int|bool> $curlOptions */
  23. function downloadHttp(string &$url, array $curlOptions = []): string {
  24. syslog(LOG_INFO, 'FreshRSS Favicon GET ' . $url);
  25. $url = checkUrl($url);
  26. if ($url == false) {
  27. return '';
  28. }
  29. /** @var CurlHandle $ch */
  30. $ch = curl_init($url);
  31. curl_setopt_array($ch, [
  32. CURLOPT_RETURNTRANSFER => true,
  33. CURLOPT_TIMEOUT => 15,
  34. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  35. CURLOPT_MAXREDIRS => 10,
  36. CURLOPT_FOLLOWLOCATION => true,
  37. CURLOPT_ENCODING => '', //Enable all encodings
  38. //CURLOPT_VERBOSE => 1, // To debug sent HTTP headers
  39. ]);
  40. FreshRSS_Context::initSystem();
  41. if (FreshRSS_Context::hasSystemConf()) {
  42. curl_setopt_array($ch, FreshRSS_Context::systemConf()->curl_options);
  43. }
  44. curl_setopt_array($ch, $curlOptions);
  45. $response = curl_exec($ch);
  46. if (!is_string($response)) {
  47. $response = '';
  48. }
  49. $info = curl_getinfo($ch);
  50. curl_close($ch);
  51. if (!empty($info['url'])) {
  52. $url2 = checkUrl($info['url']);
  53. if ($url2 != '') {
  54. $url = $url2; //Possible redirect
  55. }
  56. }
  57. return $info['http_code'] == 200 ? $response : '';
  58. }
  59. function searchFavicon(string &$url): string {
  60. $dom = new DOMDocument();
  61. $html = downloadHttp($url);
  62. if ($html == '' || !@$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  63. return '';
  64. }
  65. $xpath = new DOMXPath($dom);
  66. $links = $xpath->query('//link[@href][translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="shortcut icon"'
  67. . ' or translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="icon"]');
  68. if (!($links instanceof DOMNodeList)) {
  69. return '';
  70. }
  71. // Use the base element for relative paths, if there is one
  72. $baseElements = $xpath->query('//base[@href]');
  73. $baseElement = ($baseElements !== false && $baseElements->length > 0) ? $baseElements->item(0) : null;
  74. $baseUrl = ($baseElement instanceof DOMElement) ? $baseElement->getAttribute('href') : $url;
  75. foreach ($links as $link) {
  76. if (!$link instanceof DOMElement) {
  77. continue;
  78. }
  79. $href = trim($link->getAttribute('href'));
  80. $urlParts = parse_url($url);
  81. // Handle protocol-relative URLs by adding the current URL's scheme
  82. if (substr($href, 0, 2) === '//') {
  83. $href = ($urlParts['scheme'] ?? 'https') . ':' . $href;
  84. }
  85. $href = SimplePie_IRI::absolutize($baseUrl, $href);
  86. if ($href == false) {
  87. return '';
  88. }
  89. $iri = $href->get_iri();
  90. $favicon = downloadHttp($iri, array(CURLOPT_REFERER => $url));
  91. if (isImgMime($favicon)) {
  92. return $favicon;
  93. }
  94. }
  95. return '';
  96. }
  97. function download_favicon(string $url, string $dest): bool {
  98. $url = trim($url);
  99. $favicon = searchFavicon($url);
  100. if ($favicon == '') {
  101. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
  102. if ($rootUrl != $url) {
  103. $url = $rootUrl;
  104. $favicon = searchFavicon($url);
  105. }
  106. if ($favicon == '') {
  107. $link = $rootUrl . 'favicon.ico';
  108. $favicon = downloadHttp($link, array(
  109. CURLOPT_REFERER => $url,
  110. ));
  111. if (!isImgMime($favicon)) {
  112. $favicon = '';
  113. }
  114. }
  115. }
  116. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  117. @copy(DEFAULT_FAVICON, $dest);
  118. }
  119. function contentType(string $ico): string {
  120. $ico_content_type = 'image/x-icon';
  121. if (function_exists('mime_content_type')) {
  122. $ico_content_type = mime_content_type($ico) ?: $ico_content_type;
  123. }
  124. switch ($ico_content_type) {
  125. case 'image/svg':
  126. $ico_content_type = 'image/svg+xml';
  127. break;
  128. }
  129. return $ico_content_type;
  130. }