favicons.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. const FAVICONS_DIR = DATA_PATH . '/favicons/';
  3. const DEFAULT_FAVICON = PUBLIC_PATH . '/themes/icons/default_favicon.ico';
  4. function isImgMime($content) {
  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. function downloadHttp(&$url, $curlOptions = array()) {
  23. syslog(LOG_INFO, 'FreshRSS Favicon GET ' . $url);
  24. if (substr($url, 0, 2) === '//') {
  25. $url = 'https:' . $url;
  26. }
  27. if ($url == '' || filter_var($url, FILTER_VALIDATE_URL) === false) {
  28. return '';
  29. }
  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. curl_setopt_array($ch, $curlOptions);
  40. $response = curl_exec($ch);
  41. $info = curl_getinfo($ch);
  42. curl_close($ch);
  43. if (!empty($info['url']) && (filter_var($info['url'], FILTER_VALIDATE_URL) !== false)) {
  44. $url = $info['url']; //Possible redirect
  45. }
  46. return $info['http_code'] == 200 ? $response : '';
  47. }
  48. function searchFavicon(&$url) {
  49. $dom = new DOMDocument();
  50. $html = downloadHttp($url);
  51. if ($html != '' && @$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  52. $rels = array('shortcut icon', 'icon');
  53. $links = $dom->getElementsByTagName('link');
  54. foreach ($rels as $rel) {
  55. foreach ($links as $link) {
  56. if ($link->hasAttribute('rel') && $link->hasAttribute('href') &&
  57. strtolower(trim($link->getAttribute('rel'))) === $rel) {
  58. $href = trim($link->getAttribute('href'));
  59. if (substr($href, 0, 2) === '//') {
  60. // Case of protocol-relative URLs
  61. if (preg_match('%^(https?:)//%i', $url, $matches)) {
  62. $href = $matches[1] . $href;
  63. } else {
  64. $href = 'https:' . $href;
  65. }
  66. }
  67. if (filter_var($href, FILTER_VALIDATE_URL) === false) {
  68. $href = SimplePie_IRI::absolutize($url, $href);
  69. }
  70. $favicon = downloadHttp($href, array(
  71. CURLOPT_REFERER => $url,
  72. ));
  73. if (isImgMime($favicon)) {
  74. return $favicon;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. return '';
  81. }
  82. function download_favicon($url, $dest) {
  83. $url = trim($url);
  84. $favicon = searchFavicon($url);
  85. if ($favicon == '') {
  86. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
  87. if ($rootUrl != $url) {
  88. $url = $rootUrl;
  89. $favicon = searchFavicon($url);
  90. }
  91. if ($favicon == '') {
  92. $link = $rootUrl . 'favicon.ico';
  93. $favicon = downloadHttp($link, array(
  94. CURLOPT_REFERER => $url,
  95. ));
  96. if (!isImgMime($favicon)) {
  97. $favicon = '';
  98. }
  99. }
  100. }
  101. return ($favicon != '' && file_put_contents($dest, $favicon)) ||
  102. @copy(DEFAULT_FAVICON, $dest);
  103. }