xref: /plugin/botmon/pview.php (revision abfc901f99b90a4c2e4de802fce1a276b719177a)
1<?php /* BOT MONITOR PLUGIN PAGE VIEW SCRIPT */
2
3/* parse the JSON payload */
4$json = json_decode($_POST['pageview'], true);
5if (!$json) {
6	http_response_code(400);
7	die("Error: Invalid JSON data sent to server.");
8}
9
10// what is the session identifier?
11$sessionId = $_COOKIE['DokuWiki'] ?? null;
12$sessionType = 'dw';
13if (!$sessionId) {
14	$sessionId = $_SERVER['REMOTE_ADDR'] ?? '';
15	if ($sessionId == '127.0.0.1' || $sessionId = '::1') {
16		$sessionId = 'localhost';
17	}
18	$sessionType = 'ip';
19}
20
21/* build the resulting log line (ensure fixed column positions!) */
22$logArr = Array(
23	$_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */
24	$json['pg'] ?? '', /* DW page ID */
25	$sessionId, /* Session ID */
26	$json['u'] ?? '', /* DW User id (if logged in) */
27	$json['lt'] ?? '', /* load time */
28	$json['r'] ?? '', /* Referrer URL */
29	$_SERVER['HTTP_USER_AGENT'] ?? '' /* User agent */
30	// $json['lg'] ?? '', /* browser language */
31	// $json['scr'] ?? '', /* Screen dimensions */
32	// $json['tz'] ?? '', /* timzone offset */
33	// $json['l'] ?? '', /* Accepted languages list */
34	// $json['url'] ?? '', /* Full request URL */
35	// $json['t'] ?? '' /* Page title */
36);
37
38/* create the log line */
39$filename = 'logs/' . gmdate('Y-m-d') . '.log.txt'; /* use server datetime */
40$logline = gmdate('Y-m-d H:i:s');
41foreach ($logArr as $val) {
42	$logline .= "\t" . $val;
43};
44
45/* write the log line to the file */
46$logfile = fopen($filename, 'a');
47if (!$logfile) {
48	http_response_code(500);
49	die("Error: Unable to open log file. Please check file permissions.");
50}
51
52if (fwrite($logfile, $logline . "\n") === false) {
53	http_response_code(500);
54	fclose($logfile);
55	die("Error: Could not write to log file.");
56}
57fclose($logfile);
58
59/* send a 202 response */
60http_response_code(202);
61echo "OK";