f.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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('HTTP/1.1 404 Not Found');
  36. header('Content-Type: image/ico');
  37. readfile($default_favicon);
  38. die();
  39. }
  40. $id = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '0';
  41. if (!ctype_xdigit($id)) {
  42. $id = '0';
  43. }
  44. $txt = $favicons_dir . $id . '.txt';
  45. $ico = $favicons_dir . $id . '.ico';
  46. $ico_mtime = @filemtime($ico);
  47. $txt_mtime = @filemtime($txt);
  48. if (($ico_mtime == false) || ($txt_mtime > $ico_mtime)) {
  49. if ($txt_mtime == false) {
  50. show_default_favicon();
  51. }
  52. $url = file_get_contents($txt);
  53. if (!download_favicon($url, $ico)) {
  54. show_default_favicon();
  55. }
  56. }
  57. require(LIB_PATH . '/http-conditional.php');
  58. header('Content-Type: image/x-icon');
  59. header('Content-Disposition: inline; filename="' . $id . '.ico"');
  60. if (!httpConditional($ico_mtime, 2592000, 2)) {
  61. readfile($ico);
  62. }