f.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. require(__DIR__ . '/../constants.php');
  3. require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader
  4. require(LIB_PATH . '/favicons.php');
  5. require(LIB_PATH . '/http-conditional.php');
  6. function show_default_favicon(int $cacheSeconds = 3600): void {
  7. $default_mtime = @filemtime(DEFAULT_FAVICON) ?: 0;
  8. if (!httpConditional($default_mtime, $cacheSeconds, 2)) {
  9. header('Content-Type: image/x-icon');
  10. header('Content-Disposition: inline; filename="default_favicon.ico"');
  11. readfile(DEFAULT_FAVICON);
  12. }
  13. }
  14. $id = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : '0';
  15. if (!ctype_xdigit($id)) {
  16. $id = '0';
  17. }
  18. $txt = FAVICONS_DIR . $id . '.txt';
  19. $ico = FAVICONS_DIR . $id . '.ico';
  20. $ico_mtime = @filemtime($ico) ?: 0;
  21. $txt_mtime = @filemtime($txt) ?: 0;
  22. if ($ico_mtime == false || $ico_mtime < $txt_mtime || ($ico_mtime < time() - (mt_rand(15, 20) * 86400))) {
  23. if ($txt_mtime == false) {
  24. show_default_favicon(1800);
  25. exit();
  26. }
  27. // no ico file or we should download a new one.
  28. $url = file_get_contents($txt);
  29. if ($url === false) {
  30. show_default_favicon(1800);
  31. exit();
  32. }
  33. if (!download_favicon($url, $ico)) {
  34. // Download failed
  35. if ($ico_mtime == false) {
  36. show_default_favicon(86400);
  37. exit();
  38. } else {
  39. touch($ico);
  40. }
  41. }
  42. }
  43. if (!httpConditional($ico_mtime, mt_rand(14, 21) * 86400, 2)) {
  44. $ico_content_type = 'image/x-icon';
  45. if (function_exists('mime_content_type')) {
  46. $ico_content_type = mime_content_type($ico);
  47. }
  48. switch ($ico_content_type) {
  49. case 'image/svg':
  50. $ico_content_type = 'image/svg+xml';
  51. break;
  52. }
  53. header('Content-Type: ' . $ico_content_type);
  54. header('Content-Disposition: inline; filename="' . $id . '.ico"');
  55. readfile($ico);
  56. }