favicons.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ]);
  39. FreshRSS_Context::initSystem();
  40. if (FreshRSS_Context::hasSystemConf()) {
  41. curl_setopt_array($ch, FreshRSS_Context::systemConf()->curl_options);
  42. }
  43. curl_setopt_array($ch, $curlOptions);
  44. $response = curl_exec($ch);
  45. if (!is_string($response)) {
  46. $response = '';
  47. }
  48. $info = curl_getinfo($ch);
  49. curl_close($ch);
  50. if (!empty($info['url'])) {
  51. $url2 = checkUrl($info['url']);
  52. if ($url2 != '') {
  53. $url = $url2; //Possible redirect
  54. }
  55. }
  56. return $info['http_code'] == 200 ? $response : '';
  57. }
  58. function searchFavicon(string &$url): string {
  59. $dom = new DOMDocument();
  60. $html = downloadHttp($url);
  61. if ($html == '' || !@$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  62. return '';
  63. }
  64. $xpath = new DOMXPath($dom);
  65. $links = $xpath->query('//link[@href][translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="shortcut icon"'
  66. . ' or translate(@rel, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")="icon"]');
  67. if (!$links) {
  68. return '';
  69. }
  70. // Use the base element for relative paths, if there is one
  71. $baseElements = $xpath->query('//base[@href]');
  72. $baseElement = ($baseElements !== false && $baseElements->length > 0) ? $baseElements->item(0) : null;
  73. $baseUrl = ($baseElement instanceof DOMElement) ? $baseElement->getAttribute('href') : $url;
  74. foreach ($links as $link) {
  75. if (!$link instanceof DOMElement) {
  76. continue;
  77. }
  78. $href = trim($link->getAttribute('href'));
  79. $urlParts = parse_url($url);
  80. // Handle protocol-relative URLs by adding the current URL's scheme
  81. if (substr($href, 0, 2) === '//') {
  82. $href = ($urlParts['scheme'] ?? 'https') . ':' . $href;
  83. }
  84. $href = SimplePie_IRI::absolutize($baseUrl, $href);
  85. if ($href == false) {
  86. return '';
  87. }
  88. $iri = $href->get_iri();
  89. $favicon = downloadHttp($iri, 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. }