favicons.php 3.7 KB

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