f.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $favicon_getter = new \Favicon\Favicon();
  12. $favicon_getter->setCacheDir($favicons_dir);
  13. $favicon_url = $favicon_getter->get($website);
  14. if ($favicon_url === false) {
  15. return @copy($default_favicon, $dest);
  16. }
  17. $c = curl_init($favicon_url);
  18. curl_setopt($c, CURLOPT_HEADER, false);
  19. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  20. curl_setopt($c, CURLOPT_BINARYTRANSFER, true);
  21. $img_raw = curl_exec($c);
  22. $status_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
  23. curl_close($c);
  24. if ($status_code === 200) {
  25. $file = fopen($dest, 'w');
  26. if ($file !== false) {
  27. fwrite($file, $img_raw);
  28. fclose($file);
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. function show_default_favicon($cacheSeconds = 3600) {
  35. global $default_favicon;
  36. header('Content-Disposition: inline; filename="default_favicon.ico"');
  37. $default_mtime = @filemtime($default_favicon);
  38. if (!httpConditional($default_mtime, $cacheSeconds, 2)) {
  39. readfile($default_favicon);
  40. }
  41. }
  42. $id = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '0';
  43. if (!ctype_xdigit($id)) {
  44. $id = '0';
  45. }
  46. $txt = $favicons_dir . $id . '.txt';
  47. $ico = $favicons_dir . $id . '.ico';
  48. $ico_mtime = @filemtime($ico);
  49. $txt_mtime = @filemtime($txt);
  50. header('Content-Type: image/x-icon');
  51. if ($ico_mtime == false || $txt_mtime > $ico_mtime) {
  52. if ($txt_mtime == false) {
  53. show_default_favicon(1800);
  54. return;
  55. }
  56. // no ico file or we should download a new one.
  57. $url = file_get_contents($txt);
  58. if (!download_favicon($url, $ico)) {
  59. // Download failed, show the default favicon
  60. show_default_favicon(86400);
  61. return;
  62. }
  63. }
  64. header('Content-Disposition: inline; filename="' . $id . '.ico"');
  65. if (!httpConditional($ico_mtime, 2592000, 2)) {
  66. readfile($ico);
  67. }