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