aboutsummaryrefslogtreecommitdiff
path: root/p/f.php
blob: 5b7b7474b3fdee09bc83cb91d40543e526e7791a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/constants.php';
require LIB_PATH . '/lib_rss.php';	//Includes class autoloader
require LIB_PATH . '/favicons.php';
require LIB_PATH . '/http-conditional.php';

header("Content-Security-Policy: default-src 'none'; frame-ancestors 'none'; sandbox");
header('X-Content-Type-Options: nosniff');

$no_cache = file_exists(DATA_PATH . '/no-cache.txt');

function show_default_favicon(int $cacheSeconds = 3600): void {
	global $no_cache;
	$default_mtime = @filemtime(DEFAULT_FAVICON) ?: 0;
	if ($no_cache || !httpConditional($default_mtime, $cacheSeconds, 2)) {
		header('Content-Type: image/x-icon');
		header('Content-Disposition: inline; filename="default_favicon.ico"');
		readfile(DEFAULT_FAVICON);
	}
}

$id = $_GET['h'] ?? '0';
if (!is_string($id) || !ctype_xdigit($id)) {
	$id = '0';
}

$txt = FAVICONS_DIR . $id . '.txt';
$ico = FAVICONS_DIR . $id . '.ico';

$ico_mtime = @filemtime($ico) ?: 0;
$txt_mtime = @filemtime($txt) ?: 0;

$is_custom_favicon = $ico_mtime != false && $txt_mtime == false;

if (($ico_mtime == false || $ico_mtime < $txt_mtime || ($ico_mtime < time() - (mt_rand(15, 20) * 86400))) && !$is_custom_favicon) {
	if ($txt_mtime == false) {
		show_default_favicon(1800);
		exit();
	}

	// no ico file or we should download a new one.
	$url = file_get_contents($txt);
	if ($url === false) {
		show_default_favicon(1800);
		exit();
	}

	FreshRSS_Context::initSystem();
	if (!FreshRSS_Context::hasSystemConf()) {
		header('HTTP/1.1 500 Internal Server Error');
		die('Invalid system init!');
	}
	if (!download_favicon($url, $ico)) {
		// Download failed
		if ($ico_mtime == false) {
			show_default_favicon(86400);
			exit();
		}

		touch($ico);
	}
}

if ($no_cache || !httpConditional($ico_mtime, mt_rand(14, 21) * 86400, 2)) {
	$ico_content_type = contentType($ico);
	header('Content-Type: ' . $ico_content_type);
	header('Content-Disposition: inline; filename="' . $id . '.ico"');
	if (!$no_cache && isset($_GET['t'])) {
		header('Cache-Control: immutable');
	}
	readfile($ico);
}