favicons.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. echo 'Caught exception: ', $e->getMessage(), "\n";
  19. }
  20. return $isImage;
  21. }
  22. function downloadHttp(&$url, $curlOptions = array()) {
  23. prepareSyslog();
  24. syslog(LOG_INFO, 'FreshRSS Favicon GET ' . $url);
  25. if (substr($url, 0, 2) === '//') {
  26. $url = 'https:' . $url;
  27. }
  28. if ($url == '' || filter_var($url, FILTER_VALIDATE_URL) === false) {
  29. return '';
  30. }
  31. $ch = curl_init($url);
  32. curl_setopt_array($ch, array(
  33. CURLOPT_RETURNTRANSFER => true,
  34. CURLOPT_TIMEOUT => 15,
  35. CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
  36. CURLOPT_MAXREDIRS => 10,
  37. ));
  38. if (version_compare(PHP_VERSION, '5.6.0') >= 0 || ini_get('open_basedir') == '') {
  39. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Keep option separated for open_basedir PHP bug 65646
  40. }
  41. if (defined('CURLOPT_ENCODING')) {
  42. curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
  43. }
  44. curl_setopt_array($ch, $curlOptions);
  45. $response = curl_exec($ch);
  46. $info = curl_getinfo($ch);
  47. curl_close($ch);
  48. if (!empty($info['url']) && (filter_var($info['url'], FILTER_VALIDATE_URL) !== false)) {
  49. $url = $info['url']; //Possible redirect
  50. }
  51. return $info['http_code'] == 200 ? $response : '';
  52. }
  53. function searchFavicon(&$url) {
  54. $dom = new DOMDocument();
  55. $html = downloadHttp($url);
  56. if ($html != '' && @$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
  57. $rels = array('shortcut icon', 'icon');
  58. $links = $dom->getElementsByTagName('link');
  59. foreach ($rels as $rel) {
  60. foreach ($links as $link) {
  61. if ($link->hasAttribute('rel') && $link->hasAttribute('href') &&
  62. strtolower(trim($link->getAttribute('rel'))) === $rel) {
  63. $href = trim($link->getAttribute('href'));
  64. if (substr($href, 0, 2) === '//') {
  65. // Case of protocol-relative URLs
  66. if (preg_match('%^(https?:)//%i', $url, $matches)) {
  67. $href = $matches[1] . $href;
  68. } else {
  69. $href = 'https:' . $href;
  70. }
  71. }
  72. if (filter_var($href, FILTER_VALIDATE_URL) === false) {
  73. $href = SimplePie_IRI::absolutize($url, $href);
  74. }
  75. $favicon = downloadHttp($href, array(
  76. CURLOPT_REFERER => $url,
  77. ));
  78. if (isImgMime($favicon)) {
  79. return $favicon;
  80. }
  81. }
  82. }
  83. }
  84. }
  85. return '';
  86. }
  87. function download_favicon($url, $dest) {
  88. global $default_favicon;
  89. $url = trim($url);
  90. $favicon = searchFavicon($url);
  91. if ($favicon == '') {
  92. $rootUrl = preg_replace('%^(https?://[^/]+).*$%i', '$1/', $url);
  93. if ($rootUrl != $url) {
  94. $url = $rootUrl;
  95. $favicon = searchFavicon($url);
  96. }
  97. if ($favicon == '') {
  98. $link = $rootUrl . 'favicon.ico';
  99. $favicon = downloadHttp($link, array(
  100. CURLOPT_REFERER => $url,
  101. ));
  102. if (!isImgMime($favicon)) {
  103. $favicon = '';
  104. }
  105. }
  106. }
  107. return ($favicon != '' && file_put_contents($dest, $favicon)) ||
  108. @copy($default_favicon, $dest);
  109. }