favicons.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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) {
  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. curl_setopt_array($ch, $curlOptions);
  43. /** @var string $response */
  44. $response = curl_exec($ch);
  45. $info = curl_getinfo($ch);
  46. curl_close($ch);
  47. if (!empty($info['url'])) {
  48. $url2 = checkUrl($info['url']);
  49. if ($url2 != '') {
  50. $url = $url2; //Possible redirect
  51. }
  52. }
  53. return $info['http_code'] == 200 ? $response : '';
  54. }
  55. function searchFavicon(string &$url): string {
  56. $dom = new DOMDocument();
  57. $html = downloadHttp($url);
  58. if ($html != '' && @$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  59. $rels = array('shortcut icon', 'icon');
  60. $links = $dom->getElementsByTagName('link');
  61. foreach ($rels as $rel) {
  62. foreach ($links as $link) {
  63. if ($link->hasAttribute('rel') && $link->hasAttribute('href') &&
  64. strtolower(trim($link->getAttribute('rel'))) === $rel) {
  65. $href = trim($link->getAttribute('href'));
  66. if (substr($href, 0, 2) === '//') {
  67. // Case of protocol-relative URLs
  68. if (preg_match('%^(https?:)//%i', $url, $matches)) {
  69. $href = $matches[1] . $href;
  70. } else {
  71. $href = 'https:' . $href;
  72. }
  73. }
  74. if (!checkUrl($href, false)) {
  75. $href = SimplePie_IRI::absolutize($url, $href);
  76. }
  77. $favicon = downloadHttp($href, array(
  78. CURLOPT_REFERER => $url,
  79. ));
  80. if (isImgMime($favicon)) {
  81. return $favicon;
  82. }
  83. }
  84. }
  85. }
  86. }
  87. return '';
  88. }
  89. function download_favicon(string $url, string $dest): bool {
  90. $url = trim($url);
  91. $favicon = searchFavicon($url);
  92. if ($favicon == '') {
  93. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
  94. if ($rootUrl != $url) {
  95. $url = $rootUrl;
  96. $favicon = searchFavicon($url);
  97. }
  98. if ($favicon == '') {
  99. $link = $rootUrl . 'favicon.ico';
  100. $favicon = downloadHttp($link, array(
  101. CURLOPT_REFERER => $url,
  102. ));
  103. if (!isImgMime($favicon)) {
  104. $favicon = '';
  105. }
  106. }
  107. }
  108. return ($favicon != '' && file_put_contents($dest, $favicon)) ||
  109. @copy(DEFAULT_FAVICON, $dest);
  110. }