favicons.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. $fInfo = finfo_open(FILEINFO_MIME_TYPE);
  15. $isImage = strpos(finfo_buffer($fInfo, $content), 'image') !== false;
  16. finfo_close($fInfo);
  17. } catch (Exception $e) {
  18. echo 'Caught exception: ', $e->getMessage(), "\n";
  19. }
  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) {
  27. return '';
  28. }
  29. $ch = curl_init($url);
  30. curl_setopt_array($ch, [
  31. CURLOPT_RETURNTRANSFER => true,
  32. CURLOPT_TIMEOUT => 15,
  33. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  34. CURLOPT_MAXREDIRS => 10,
  35. CURLOPT_FOLLOWLOCATION => true,
  36. CURLOPT_ENCODING => '', //Enable all encodings
  37. ]);
  38. curl_setopt_array($ch, $curlOptions);
  39. $response = curl_exec($ch);
  40. $info = curl_getinfo($ch);
  41. curl_close($ch);
  42. if (!empty($info['url'])) {
  43. $url2 = checkUrl($info['url']);
  44. if ($url2 != '') {
  45. $url = $url2; //Possible redirect
  46. }
  47. }
  48. return $info['http_code'] == 200 ? $response : '';
  49. }
  50. function searchFavicon(string &$url): string {
  51. $dom = new DOMDocument();
  52. $html = downloadHttp($url);
  53. if ($html != '' && @$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  54. $rels = array('shortcut icon', 'icon');
  55. $links = $dom->getElementsByTagName('link');
  56. foreach ($rels as $rel) {
  57. foreach ($links as $link) {
  58. if ($link->hasAttribute('rel') && $link->hasAttribute('href') &&
  59. strtolower(trim($link->getAttribute('rel'))) === $rel) {
  60. $href = trim($link->getAttribute('href'));
  61. if (substr($href, 0, 2) === '//') {
  62. // Case of protocol-relative URLs
  63. if (preg_match('%^(https?:)//%i', $url, $matches)) {
  64. $href = $matches[1] . $href;
  65. } else {
  66. $href = 'https:' . $href;
  67. }
  68. }
  69. if (!checkUrl($href, false)) {
  70. $href = SimplePie_IRI::absolutize($url, $href);
  71. }
  72. $favicon = downloadHttp($href, array(
  73. CURLOPT_REFERER => $url,
  74. ));
  75. if (isImgMime($favicon)) {
  76. return $favicon;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. return '';
  83. }
  84. function download_favicon(string $url, string $dest): bool {
  85. $url = trim($url);
  86. $favicon = searchFavicon($url);
  87. if ($favicon == '') {
  88. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
  89. if ($rootUrl != $url) {
  90. $url = $rootUrl;
  91. $favicon = searchFavicon($url);
  92. }
  93. if ($favicon == '') {
  94. $link = $rootUrl . 'favicon.ico';
  95. $favicon = downloadHttp($link, array(
  96. CURLOPT_REFERER => $url,
  97. ));
  98. if (!isImgMime($favicon)) {
  99. $favicon = '';
  100. }
  101. }
  102. }
  103. return ($favicon != '' && file_put_contents($dest, $favicon)) ||
  104. @copy(DEFAULT_FAVICON, $dest);
  105. }