favicons.php 3.8 KB

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