f.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. require('../constants.php');
  3. include(LIB_PATH . '/Favicon/Favicon.php');
  4. include(LIB_PATH . '/Favicon/DataAccess.php');
  5. require(LIB_PATH . '/http-conditional.php');
  6. $favicons_dir = DATA_PATH . '/favicons/';
  7. $default_favicon = PUBLIC_PATH . '/themes/icons/default_favicon.ico';
  8. /* Télécharge le favicon d'un site et le place sur le serveur */
  9. function download_favicon($website, $dest) {
  10. global $favicons_dir, $default_favicon;
  11. syslog(LOG_DEBUG, 'FreshRSS Favicon discovery GET ' . $website);
  12. $favicon_getter = new \Favicon\Favicon();
  13. $favicon_getter->setCacheDir($favicons_dir);
  14. $favicon_url = $favicon_getter->get($website);
  15. if ($favicon_url === false) {
  16. return @copy($default_favicon, $dest);
  17. }
  18. syslog(LOG_DEBUG, 'FreshRSS Favicon GET ' . $favicon_url);
  19. $c = curl_init($favicon_url);
  20. curl_setopt($c, CURLOPT_HEADER, false);
  21. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  22. curl_setopt($c, CURLOPT_BINARYTRANSFER, true);
  23. $img_raw = curl_exec($c);
  24. $status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  25. curl_close($c);
  26. if ($status_code === 200) {
  27. $file = fopen($dest, 'w');
  28. if ($file !== false) {
  29. fwrite($file, $img_raw);
  30. fclose($file);
  31. return true;
  32. }
  33. }
  34. return false;
  35. }
  36. function show_default_favicon($cacheSeconds = 3600) {
  37. global $default_favicon;
  38. header('Content-Disposition: inline; filename="default_favicon.ico"');
  39. $default_mtime = @filemtime($default_favicon);
  40. if (!httpConditional($default_mtime, $cacheSeconds, 2)) {
  41. readfile($default_favicon);
  42. }
  43. }
  44. $id = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '0';
  45. if (!ctype_xdigit($id)) {
  46. $id = '0';
  47. }
  48. $txt = $favicons_dir . $id . '.txt';
  49. $ico = $favicons_dir . $id . '.ico';
  50. $ico_mtime = @filemtime($ico);
  51. $txt_mtime = @filemtime($txt);
  52. header('Content-Type: image/x-icon');
  53. if ($ico_mtime == false || $txt_mtime > $ico_mtime || ($ico_mtime < time() - 15 * 86400)) {
  54. if ($txt_mtime == false) {
  55. show_default_favicon(1800);
  56. exit();
  57. }
  58. // no ico file or we should download a new one.
  59. $url = file_get_contents($txt);
  60. if (!download_favicon($url, $ico)) {
  61. // Download failed
  62. if ($ico_mtime == false) {
  63. show_default_favicon(86400);
  64. exit();
  65. } else {
  66. touch($ico);
  67. }
  68. }
  69. }
  70. header('Content-Disposition: inline; filename="' . $id . '.ico"');
  71. if (!httpConditional($ico_mtime, 2592000, 2)) {
  72. readfile($ico);
  73. }