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