favicons.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if (FreshRSS_Context::hasSystemConf()) {
  45. curl_setopt_array($ch, FreshRSS_Context::systemConf()->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. if ($href == false) {
  90. return '';
  91. }
  92. $iri = $href->get_iri();
  93. $favicon = downloadHttp($iri, array(CURLOPT_REFERER => $url));
  94. if (isImgMime($favicon)) {
  95. return $favicon;
  96. }
  97. }
  98. return '';
  99. }
  100. function download_favicon(string $url, string $dest): bool {
  101. $url = trim($url);
  102. $favicon = searchFavicon($url);
  103. if ($favicon == '') {
  104. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
  105. if ($rootUrl != $url) {
  106. $url = $rootUrl;
  107. $favicon = searchFavicon($url);
  108. }
  109. if ($favicon == '') {
  110. $link = $rootUrl . 'favicon.ico';
  111. $favicon = downloadHttp($link, array(
  112. CURLOPT_REFERER => $url,
  113. ));
  114. if (!isImgMime($favicon)) {
  115. $favicon = '';
  116. }
  117. }
  118. }
  119. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  120. @copy(DEFAULT_FAVICON, $dest);
  121. }