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