f.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. require dirname(__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. header("Content-Security-Policy: default-src 'none'; frame-ancestors 'none'; sandbox");
  8. header('X-Content-Type-Options: nosniff');
  9. $no_cache = file_exists(DATA_PATH . '/no-cache.txt');
  10. function show_default_favicon(int $cacheSeconds = 3600): void {
  11. global $no_cache;
  12. $default_mtime = @filemtime(DEFAULT_FAVICON) ?: 0;
  13. if ($no_cache || !httpConditional($default_mtime, $cacheSeconds, 2)) {
  14. header('Content-Type: image/x-icon');
  15. header('Content-Disposition: inline; filename="default_favicon.ico"');
  16. readfile(DEFAULT_FAVICON);
  17. }
  18. }
  19. $id = $_GET['h'] ?? '0';
  20. if (!is_string($id) || !ctype_xdigit($id)) {
  21. $id = '0';
  22. }
  23. $txt = FAVICONS_DIR . $id . '.txt';
  24. $ico = FAVICONS_DIR . $id . '.ico';
  25. $ico_mtime = @filemtime($ico) ?: 0;
  26. $txt_mtime = @filemtime($txt) ?: 0;
  27. $is_custom_favicon = $ico_mtime != false && $txt_mtime == false;
  28. if (($ico_mtime == false || $ico_mtime < $txt_mtime || ($ico_mtime < time() - (mt_rand(15, 20) * 86400))) && !$is_custom_favicon) {
  29. if ($txt_mtime == false) {
  30. show_default_favicon(1800);
  31. exit();
  32. }
  33. // no ico file or we should download a new one.
  34. $url = file_get_contents($txt);
  35. if ($url === false) {
  36. show_default_favicon(1800);
  37. exit();
  38. }
  39. FreshRSS_Context::initSystem();
  40. if (!FreshRSS_Context::hasSystemConf()) {
  41. header('HTTP/1.1 500 Internal Server Error');
  42. die('Invalid system init!');
  43. }
  44. if (!download_favicon($url, $ico)) {
  45. // Download failed
  46. if ($ico_mtime == false) {
  47. show_default_favicon(86400);
  48. exit();
  49. }
  50. touch($ico);
  51. }
  52. }
  53. if ($no_cache || !httpConditional($ico_mtime, mt_rand(14, 21) * 86400, 2)) {
  54. $ico_content_type = contentType($ico);
  55. header('Content-Type: ' . $ico_content_type);
  56. header('Content-Disposition: inline; filename="' . $id . '.ico"');
  57. if (!$no_cache && isset($_GET['t'])) {
  58. header('Cache-Control: immutable');
  59. }
  60. readfile($ico);
  61. }