f.php 1.9 KB

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