xref: /plugin/botmon/tick.php (revision f4417fde3b994fe0c6f5285c818940fa0214754b)
1<?php /* BOTMON PLUGIN HEARTBEAT TICKER SCRIPT */
2
3
4// what is the session identifier?
5$sessionId = $_GET['u'] ?? '';
6if ($sessionId == '') {
7	$sessionId = $_COOKIE['DokuWiki'] ?? '';
8}
9if ($sessionId == '') {
10	$sessionId = $_SERVER['REMOTE_ADDR'] ?? '';
11	if ($sessionId == '127.0.0.1' || $sessionId == '::1') {
12		$sessionId = 'localhost';
13	}
14}
15
16/* build the resulting log line (ensure fixed column positions!) */
17$logArr = Array(
18	$_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */
19	$_GET['p'] ?? '', /* page ID */
20	$sessionId, /* Session ID */
21	$_SERVER['HTTP_USER_AGENT'] ?? '' /* User agent */
22);
23
24/* create the log line */
25$filename = 'logs/' . gmdate('Y-m-d') . '.tck.txt'; /* use GMT date for filename */
26$line = gmdate('Y-m-d H:i:s'); /* use GMT time for log entries */
27foreach ($logArr as $val) {
28	$line .= "\t" . $val;
29};
30
31/* write the log line to the file */
32$tickfile = fopen($filename, 'a');
33if (!$tickfile) {
34	http_response_code(500);
35	die("Error: Unable to open log file. Please check file permissions.");
36}
37if (fwrite($tickfile, $line . "\n") === false) {
38	http_response_code(500);
39	fclose($tickfile);
40	die("Error: Could not write to log file.");
41}
42fclose($tickfile);
43
44/* Send "Accepted" header */
45http_response_code(202);
46echo "OK";