Files
dokuwiki-plugin-botmon/pview.php

72 lines
2.1 KiB
PHP
Raw Normal View History

2025-09-01 16:25:25 +02:00
<?php /* BOT MONITOR PLUGIN PAGE VIEW SCRIPT */
2025-08-20 22:58:42 +03:00
/* parse the JSON payload */
$json = json_decode($_POST['pageview'], true);
if (!$json) {
http_response_code(400);
2025-09-05 09:15:08 +02:00
die("Invalid JSON Data.");
2025-08-20 22:58:42 +03:00
}
// what is the session identifier?
$sessionId = preg_replace('/[\x00-\x1F{};\"\']/', "\u{FFFD}", $json['id']) /* clean json parameter */
?? session_id()
?? $_SERVER['REMOTE_ADDR'];
2025-09-05 09:15:08 +02:00
// clean the page ID
$pageId = preg_replace('/[\x00-\x1F{};\"\']/', "\u{FFFD}", $json['pg'] ?? '');
2025-09-05 09:15:08 +02:00
// clean the user-name
$userName = preg_replace('/[\x00-\x1F\"]/', "\u{FFFD}", $json['u'] ?? '');
2025-09-05 09:15:08 +02:00
// check load time
$loadTime = $json['lt'] ?? '';
if ($loadTime !== '' ) $loadTime = intval($loadTime);
// clean the user agent string
$agent = preg_replace('/[\x00-\x1F]/', "\u{FFFD}", $_SERVER['HTTP_USER_AGENT'] ?? '');
// clean the referer
$referer = preg_replace('/[\x00-\x1F]/', "\u{FFFD}", $json['r'] ?? '');
2025-08-20 22:58:42 +03:00
/* build the resulting log line (ensure fixed column positions!) */
$logArr = Array(
2025-08-26 21:37:19 +03:00
$_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */
2025-09-05 09:15:08 +02:00
$pageId, /* DW Page ID */
2025-09-04 09:01:37 +02:00
$sessionId, /* Session ID */
2025-09-05 09:15:08 +02:00
$userName, /* DW User name (if logged in) */
$loadTime, /* load time */
$referer, /* Referrer URL */
$agent /* User agent */
2025-08-26 21:37:19 +03:00
// $json['lg'] ?? '', /* browser language */
// $json['scr'] ?? '', /* Screen dimensions */
2025-08-30 13:01:50 +03:00
// $json['tz'] ?? '', /* timzone offset */
2025-08-26 21:37:19 +03:00
// $json['l'] ?? '', /* Accepted languages list */
// $json['url'] ?? '', /* Full request URL */
2025-08-20 22:58:42 +03:00
// $json['t'] ?? '' /* Page title */
);
/* create the log line */
2025-09-01 10:15:36 +02:00
$filename = 'logs/' . gmdate('Y-m-d') . '.log.txt'; /* use server datetime */
2025-08-20 22:58:42 +03:00
$logline = gmdate('Y-m-d H:i:s');
foreach ($logArr as $val) {
$logline .= "\t" . $val;
};
/* write the log line to the file */
$logfile = fopen($filename, 'a');
if (!$logfile) {
2025-09-06 20:01:03 +02:00
http_response_code(507);
2025-08-20 22:58:42 +03:00
die("Error: Unable to open log file. Please check file permissions.");
}
if (fwrite($logfile, $logline . "\n") === false) {
http_response_code(500);
fclose($logfile);
die("Error: Could not write to log file.");
}
fclose($logfile);
/* send a 202 response */
http_response_code(202);
echo "OK";