favicons.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. $favicons_dir = DATA_PATH . '/favicons/';
  3. $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. }
  19. return $isImage;
  20. }
  21. function downloadHttp(&$url, $curlOptions = array()) {
  22. syslog(LOG_INFO, 'FreshRSS Favicon GET ' . $url);
  23. if (substr($url, 0, 2) === '//') {
  24. $url = 'https:' . $favicon;
  25. }
  26. if ($url == '' || filter_var($url, FILTER_VALIDATE_URL) === false) {
  27. return '';
  28. }
  29. $ch = curl_init($url);
  30. curl_setopt_array($ch, array(
  31. CURLOPT_FOLLOWLOCATION => true,
  32. CURLOPT_MAXREDIRS => 10,
  33. CURLOPT_RETURNTRANSFER => true,
  34. CURLOPT_TIMEOUT => 15,
  35. CURLOPT_USERAGENT => 'FreshRSS/' . FRESHRSS_VERSION . ' (' . PHP_OS . '; ' . FRESHRSS_WEBSITE . ')',
  36. ));
  37. if (defined('CURLOPT_ENCODING')) {
  38. curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
  39. }
  40. curl_setopt_array($ch, $curlOptions);
  41. $response = curl_exec($ch);
  42. $info = curl_getinfo($ch);
  43. curl_close($ch);
  44. if (!empty($info['url']) && (filter_var($info['url'], FILTER_VALIDATE_URL) !== false)) {
  45. $url = $info['url']; //Possible redirect
  46. }
  47. return $info['http_code'] == 200 ? $response : '';
  48. }
  49. function searchFavicon(&$url) {
  50. $dom = new DOMDocument();
  51. $html = downloadHttp($url);
  52. if ($html != '' && @$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  53. $rels = array('shortcut icon', 'icon');
  54. $links = $dom->getElementsByTagName('link');
  55. foreach ($rels as $rel) {
  56. foreach ($links as $link) {
  57. if ($link->hasAttribute('rel') && $link->hasAttribute('href') &&
  58. strtolower(trim($link->getAttribute('rel'))) === $rel) {
  59. $href = trim($link->getAttribute('href'));
  60. if (substr($href, 0, 2) === '//') {
  61. // Case of protocol-relative URLs
  62. if (preg_match('%^(https?:)//%i', $url, $matches)) {
  63. $href = $matches[1] . $href;
  64. } else {
  65. $href = 'https:' . $href;
  66. }
  67. }
  68. if (filter_var($href, FILTER_VALIDATE_URL) === false) {
  69. $href = SimplePie_IRI::absolutize($url, $href);
  70. }
  71. $favicon = downloadHttp($href, array(
  72. CURLOPT_REFERER => $url,
  73. ));
  74. if (isImgMime($favicon)) {
  75. return $favicon;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. return '';
  82. }
  83. function download_favicon($url, $dest) {
  84. global $default_favicon;
  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. }