From abfc901f99b90a4c2e4de802fce1a276b719177a Mon Sep 17 00:00:00 2001 From: Sascha Leib Date: Thu, 4 Sep 2025 09:01:37 +0200 Subject: [PATCH] Improved session handling --- action.php | 12 ++++-------- plugin.info.txt | 2 +- pview.php | 13 ++++++++++++- script.js | 20 ++++++++++---------- tick.php | 14 +++++++++++++- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/action.php b/action.php index 2f735ff..22380cd 100644 --- a/action.php +++ b/action.php @@ -59,15 +59,11 @@ class action_plugin_botmon extends DokuWiki_Action_Plugin { $sessionId = $_COOKIE['DokuWiki'] ?? null; $sessionType = 'dw'; if (!$sessionId) { - if (session_id()) { - // if a session ID is set, use it - $sessionId = session_id(); - $sessionType = 'php'; - } else { - // if no session ID is set, use the ip address: - $sessionId = $_SERVER['REMOTE_ADDR'] ?? ''; - $sessionType = 'ip'; + $sessionId = $_SERVER['REMOTE_ADDR'] ?? ''; + if ($sessionId == '127.0.0.1' || $sessionId = '::1') { + $sessionId = 'localhost'; } + $sessionType = 'ip'; } $logArr = Array( diff --git a/plugin.info.txt b/plugin.info.txt index afc7202..7258326 100644 --- a/plugin.info.txt +++ b/plugin.info.txt @@ -1,7 +1,7 @@ base botmon author Sascha Leib email ad@hominem.com -date 2025-09-03 +date 2025-09-04 name Bot Monitoring desc Live monitoring of bot traffic on your DokuWiki instance (under development) url https://www.dokuwiki.org/plugin:botmon diff --git a/pview.php b/pview.php index bd9ae12..50f37e5 100644 --- a/pview.php +++ b/pview.php @@ -7,11 +7,22 @@ if (!$json) { die("Error: Invalid JSON data sent to server."); } +// what is the session identifier? +$sessionId = $_COOKIE['DokuWiki'] ?? null; +$sessionType = 'dw'; +if (!$sessionId) { + $sessionId = $_SERVER['REMOTE_ADDR'] ?? ''; + if ($sessionId == '127.0.0.1' || $sessionId = '::1') { + $sessionId = 'localhost'; + } + $sessionType = 'ip'; +} + /* build the resulting log line (ensure fixed column positions!) */ $logArr = Array( $_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */ $json['pg'] ?? '', /* DW page ID */ - $_COOKIE['DokuWiki'] ?? session_id() ?? '', /* DW session ID */ + $sessionId, /* Session ID */ $json['u'] ?? '', /* DW User id (if logged in) */ $json['lt'] ?? '', /* load time */ $json['r'] ?? '', /* Referrer URL */ diff --git a/script.js b/script.js index 4d1ab08..e3fb4ce 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,6 @@ "use strict"; /* DokuWiki BotMon Plugin Script file */ -/* 03.09.2025 - 0.1.7 - pre-release */ +/* 04.09.2025 - 0.1.8 - pre-release */ /* Authors: Sascha Leib */ const BotMon = { @@ -211,27 +211,27 @@ BotMon.live = { // register a new visitor (or update if already exists) registerVisit: function(dat, type) { - //console.info('registerVisit', dat); + console.info('registerVisit', dat); // shortcut to make code more readable: const model = BotMon.live.data.model; + // is it a known bot? + const bot = BotMon.live.data.bots.match(dat.agent); + + // which user id to use: + let visitorId = dat.id; // default is the session ID + if (bot) visitorId = bot.id; // use bot ID if known bot + if (dat.usr !== '') visitorId = 'usr'; // use user ID if known user + // check if it already exists: let visitor = model.findVisitor(dat.id); if (!visitor) { - // is it a known bot? - const bot = BotMon.live.data.bots.match(dat.agent); - // override the visitor type? let visitorType = dat.typ; if (bot) visitorType = 'bot'; - // which user id to use: - let visitorId = dat.id; // default is the session ID - if (bot) visitorId = bot.id; // use bot ID if known bot - if (dat.usr !== '') visitorId = 'usr'; // use user ID if known user - model._visitors.push(dat); visitor = dat; visitor.id = visitorId; diff --git a/tick.php b/tick.php index 7efd46c..c3fb063 100644 --- a/tick.php +++ b/tick.php @@ -1,10 +1,22 @@