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'] ?? 'null', /* remote IP */ 13 $json['pg'] ?? 'null', /* DW page ID */ 14 $_COOKIE['DokuWiki'] ?? 'null', /* DokuWiki session ID */ 15 $json['u'] ?? 'null' /* DW User id (if logged in) */ 16 // $json['tz'] ?? 'null', /* timzone offset */ 17 // $json['lg'] ?? 'null', /* browser language */ 18 // $json['td'] ?? 'null', /* load time */ 19 // $json['scr'] ?? 'null', /* Screen dimensions */ 20 // $json['l'] ?? 'null', /* Accepted languages list */ 21 // $json['url'] ?? 'null', /* Full request URL */ 22 // $json['r'] ?? 'null', /* Referrer URL */ 23 // $_SERVER['HTTP_USER_AGENT'] ?? 'null', /* User agent */ 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";