f.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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() {
  35. global $default_favicon;
  36. header('Content-Type: image/x-icon');
  37. header('Content-Disposition: inline; filename="default_favicon.ico"');
  38. $default_mtime = @filemtime($default_favicon);
  39. if (!httpConditional($default_mtime, 2592000, 2)) {
  40. readfile($default_favicon);
  41. }
  42. }
  43. $id = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '0';
  44. if (!ctype_xdigit($id)) {
  45. $id = '0';
  46. }
  47. $txt = $favicons_dir . $id . '.txt';
  48. $ico = $favicons_dir . $id . '.ico';
  49. $ico_mtime = @filemtime($ico);
  50. $txt_mtime = @filemtime($txt);
  51. if ($ico_mtime == false || $txt_mtime > $ico_mtime) {
  52. if ($txt_mtime == false) {
  53. show_default_favicon();
  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();
  61. return;
  62. }
  63. }
  64. header('Content-Type: image/x-icon');
  65. header('Content-Disposition: inline; filename="' . $id . '.ico"');
  66. if (!httpConditional($ico_mtime, 2592000, 2)) {
  67. readfile($ico);
  68. }