xref: /plugin/botmon/tick.php (revision f40be0ac64886d480c33cd63aa2483508f26606e)
17bd08c30SSascha Leib<?php /* BOTMON PLUGIN HEARTBEAT TICKER SCRIPT */
2cf9f7fe8SSascha Leib
3b148c85eSSascha Leib	// Note: this script is normally called in HEAD mode, therefore it can not return any payload.
413c9d29dSSascha Leib
5*f40be0acSSascha Leib	// quit out if it is called without athe right parameters:
6*f40be0acSSascha Leib	if (!isset($_GET['id']) || !isset($_GET['p'])) {
7*f40be0acSSascha Leib		http_response_code(400);
8*f40be0acSSascha Leib		die("Parameter error.");
9*f40be0acSSascha Leib	}
10*f40be0acSSascha Leib
11b148c85eSSascha Leib	// what is the session identifier?
12b148c85eSSascha Leib	$sessionId = preg_replace('/[\x00-\x1F{};\"\']/', "\u{FFFD}", $_GET['id']) /* clean json parameter */
13b148c85eSSascha Leib		?? session_id()
14b148c85eSSascha Leib		?? $_SERVER['REMOTE_ADDR'];
15abfc901fSSascha Leib
16b2e3bd8bSSascha Leib
17b2e3bd8bSSascha Leib	// clean the page ID
18b2e3bd8bSSascha Leib	$pageId = preg_replace('/[\x00-\x1F]/', "\u{FFFD}", $_GET['p'] ?? '');
19b2e3bd8bSSascha Leib
20b2e3bd8bSSascha Leib	// clean the user agent string
21b2e3bd8bSSascha Leib	$agent = preg_replace('/[\x00-\x1F]/', "\u{FFFD}", $_SERVER['HTTP_USER_AGENT'] ?? '');
22b2e3bd8bSSascha Leib
23b2e3bd8bSSascha Leib	/* build the resulting log line */
24cf9f7fe8SSascha Leib	$logArr = Array(
25b2e3bd8bSSascha Leib		$_SERVER['REMOTE_ADDR'] ?? '', /* Remote IP */
26b2e3bd8bSSascha Leib		$pageId, /* Page ID */
27abfc901fSSascha Leib		$sessionId, /* Session ID */
28b2e3bd8bSSascha Leib		$agent /* User agent */
29cf9f7fe8SSascha Leib	);
30cf9f7fe8SSascha Leib
31cf9f7fe8SSascha Leib	/* create the log line */
324cddc661SSascha Leib	$filename = 'logs/' . gmdate('Y-m-d') . '.tck.txt'; /* use GMT date for filename */
33cf9f7fe8SSascha Leib	$line = gmdate('Y-m-d H:i:s'); /* use GMT time for log entries */
34cf9f7fe8SSascha Leib	foreach ($logArr as $val) {
35cf9f7fe8SSascha Leib		$line .= "\t" . $val;
36cf9f7fe8SSascha Leib	};
37cf9f7fe8SSascha Leib
38cf9f7fe8SSascha Leib	/* write the log line to the file */
3987d76396SSascha Leib	$tickfile = fopen($filename, 'a');
40cf9f7fe8SSascha Leib	if (!$tickfile) {
41cf9f7fe8SSascha Leib		http_response_code(500);
42cf9f7fe8SSascha Leib		die("Error: Unable to open log file. Please check file permissions.");
43cf9f7fe8SSascha Leib	}
44cf9f7fe8SSascha Leib	if (fwrite($tickfile, $line . "\n") === false) {
45b2e3bd8bSSascha Leib		http_response_code(507);
46cf9f7fe8SSascha Leib		fclose($tickfile);
47cf9f7fe8SSascha Leib		die("Error: Could not write to log file.");
48cf9f7fe8SSascha Leib	}
49cf9f7fe8SSascha Leib	fclose($tickfile);
50cf9f7fe8SSascha Leib
51cf9f7fe8SSascha Leib	/* Send "Accepted" header */
52cf9f7fe8SSascha Leib	http_response_code(202);
53cf9f7fe8SSascha Leib	echo "OK";