favicons.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. $rels = array('shortcut icon', 'icon');
  67. $links = $dom->getElementsByTagName('link');
  68. foreach ($rels as $rel) {
  69. foreach ($links as $link) {
  70. if ($link->hasAttribute('rel') && $link->hasAttribute('href') &&
  71. strtolower(trim($link->getAttribute('rel'))) === $rel) {
  72. $href = trim($link->getAttribute('href'));
  73. if (substr($href, 0, 2) === '//') {
  74. // Case of protocol-relative URLs
  75. if (preg_match('%^(https?:)//%i', $url, $matches) === 1) {
  76. $href = $matches[1] . $href;
  77. } else {
  78. $href = 'https:' . $href;
  79. }
  80. }
  81. $checkUrl = checkUrl($href, false);
  82. if (is_string($checkUrl)) {
  83. $href = SimplePie_IRI::absolutize($url, $href);
  84. }
  85. $favicon = downloadHttp($href, array(
  86. CURLOPT_REFERER => $url,
  87. ));
  88. if (isImgMime($favicon)) {
  89. return $favicon;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return '';
  96. }
  97. function download_favicon(string $url, string $dest): bool {
  98. $url = trim($url);
  99. $favicon = searchFavicon($url);
  100. if ($favicon == '') {
  101. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
  102. if ($rootUrl != $url) {
  103. $url = $rootUrl;
  104. $favicon = searchFavicon($url);
  105. }
  106. if ($favicon == '') {
  107. $link = $rootUrl . 'favicon.ico';
  108. $favicon = downloadHttp($link, array(
  109. CURLOPT_REFERER => $url,
  110. ));
  111. if (!isImgMime($favicon)) {
  112. $favicon = '';
  113. }
  114. }
  115. }
  116. return ($favicon != '' && file_put_contents($dest, $favicon) > 0) ||
  117. @copy(DEFAULT_FAVICON, $dest);
  118. }