Improved session handling

This commit is contained in:
Sascha Leib
2025-09-04 09:01:37 +02:00
parent 43d9de6b9b
commit abfc901f99
5 changed files with 40 additions and 21 deletions

View File

@@ -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(

View File

@@ -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

View File

@@ -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 */

View File

@@ -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 <ad@hominem.info> */
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;

View File

@@ -1,10 +1,22 @@
<?php /* BOTMON PLUGIN HEARTBEAT TICKER SCRIPT */
// 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 */
$_GET['p'] ?? '', /* page ID */
$_COOKIE['DokuWiki'] ?? session_id() ?? '', /* DokuWiki session ID */
$sessionId, /* Session ID */
$_SERVER['HTTP_USER_AGENT'] ?? '' /* User agent */
);