f.php 1.5 KB

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