f.php 1.8 KB

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