xref: /plugin/botmon/pview.php (revision 9f1ee8c1f3ed9963111a116a92ad1baea9857250)
1<?php /* 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/* build the resulting log line (ensure fixed column positions!) */
11$logArr = Array(
12	$_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */
13	$json['pg'] ?? '', /* DW page ID */
14	$_COOKIE['DokuWiki'] ?? session_id() ?? '', /* DokuWiki session ID */
15	$json['u'] ?? '', /* DW User id (if logged in) */
16	$json['td'] ?? '', /* load time */
17	$json['r'] ?? '', /* Referrer URL */
18	$_SERVER['HTTP_USER_AGENT'] ?? '' /* User agent */
19	// $json['lg'] ?? '', /* browser language */
20	// $json['scr'] ?? '', /* Screen dimensions */
21	// $json['tz'] ?? '', /* timzone offset */
22	// $json['l'] ?? '', /* Accepted languages list */
23	// $json['url'] ?? '', /* Full request URL */
24	// $json['t'] ?? '' /* Page title */
25);
26
27/* create the log line */
28$filename = 'logs/' . gmdate('Y-m-d') . '.log'; /* use server datetime */
29$logline = gmdate('Y-m-d H:i:s');
30foreach ($logArr as $val) {
31	$logline .= "\t" . $val;
32};
33
34/* write the log line to the file */
35$logfile = fopen($filename, 'a');
36if (!$logfile) {
37	http_response_code(500);
38	die("Error: Unable to open log file. Please check file permissions.");
39}
40
41if (fwrite($logfile, $logline . "\n") === false) {
42	http_response_code(500);
43	fclose($logfile);
44	die("Error: Could not write to log file.");
45}
46fclose($logfile);
47
48/* send a 202 response */
49http_response_code(202);
50echo "OK";