Files
dokuwiki-plugin-botmon/action.php

101 lines
2.8 KiB
PHP
Raw Normal View History

<?php
use dokuwiki\Extension\EventHandler;
use dokuwiki\Extension\Event;
/**
2025-09-01 16:25:25 +02:00
* Action Component for the Bot Monitoring Plugin
*
* @license GPL 3 (http://www.gnu.org/licenses/gpl.html)
* @author Sascha Leib <sascha.leib(at)kolmio.com>
*/
2025-09-01 16:25:25 +02:00
class action_plugin_botmon extends DokuWiki_Action_Plugin {
/**
2025-08-20 22:58:42 +03:00
* Registers a callback functions
*
* @param EventHandler $controller DokuWiki's event controller object
* @return void
*/
public function register(EventHandler $controller) {
$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'insertHeader');
}
/**
* Inserts tracking code to the page header
*
* @param Event $event event object by reference
* @return void
*/
public function insertHeader(Event $event, $param) {
global $INFO;
// is there a user logged in?
2025-08-20 22:58:42 +03:00
$username = ( !empty($INFO['userinfo']) && !empty($INFO['userinfo']['name'])
2025-09-04 09:44:01 +02:00
? $INFO['userinfo']['name'] : '');
// build the tracker code:
2025-09-01 16:25:25 +02:00
$code = NL . DOKU_TAB . "document._botmon = {'t0': Date.now()};" . NL;
2025-08-20 22:58:42 +03:00
if ($username) {
2025-09-01 16:25:25 +02:00
$code .= DOKU_TAB . 'document._botmon.user = "' . $username . '";'. NL;
}
$code .= DOKU_TAB . "addEventListener('load',function(){" . NL;
$code .= DOKU_TAB . DOKU_TAB . "const e=document.createElement('script');" . NL;
2025-08-20 22:58:42 +03:00
$code .= DOKU_TAB . DOKU_TAB . "e.async=true;e.defer=true;" . NL;
2025-09-01 16:25:25 +02:00
$code .= DOKU_TAB . DOKU_TAB . "e.src='".DOKU_BASE."lib/plugins/botmon/client.js';" . NL;
2025-08-20 22:58:42 +03:00
$code .= DOKU_TAB . DOKU_TAB . "document.getElementsByTagName('head')[0].appendChild(e);" . NL;
2025-09-05 16:22:39 +02:00
$code .= DOKU_TAB . "});" . NL . DOKU_TAB;
2025-08-20 22:58:42 +03:00
$event->data['script'][] = [
'_data' => $code
];
2025-08-20 22:58:42 +03:00
2025-09-01 10:15:36 +02:00
/* Write out server-side info to a server log: */
2025-08-29 23:18:51 +03:00
// what is the session identifier?
2025-09-05 09:15:08 +02:00
$sessionId = $_COOKIE['DokuWiki'] ?? '';
$sessionType = 'dw';
2025-09-04 09:44:01 +02:00
if ($sessionId == '') {
2025-09-04 09:01:37 +02:00
$sessionId = $_SERVER['REMOTE_ADDR'] ?? '';
2025-09-04 09:04:29 +02:00
if ($sessionId == '127.0.0.1' || $sessionId == '::1') {
2025-09-04 09:01:37 +02:00
$sessionId = 'localhost';
2025-08-29 23:18:51 +03:00
}
2025-09-04 09:01:37 +02:00
$sessionType = 'ip';
2025-08-29 23:18:51 +03:00
}
2025-09-05 09:15:08 +02:00
// clean the page ID
$pageId = preg_replace('/[\x00-\x1F]/', "\u{FFFD}", $INFO['id'] ?? '');
2025-08-20 22:58:42 +03:00
$logArr = Array(
2025-08-26 21:37:19 +03:00
$_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */
2025-09-05 09:15:08 +02:00
$pageId, /* page ID */
2025-08-29 23:18:51 +03:00
$sessionId, /* Session ID */
2025-09-05 09:15:08 +02:00
$sessionType, /* session ID type */
2025-08-20 22:58:42 +03:00
$username,
2025-08-30 13:01:50 +03:00
$_SERVER['HTTP_USER_AGENT'] ?? '', /* User agent */
$_SERVER['HTTP_REFERER'] ?? '' /* HTTP Referrer */
2025-08-20 22:58:42 +03:00
);
//* create the log line */
2025-09-01 10:15:36 +02:00
$filename = __DIR__ .'/logs/' . gmdate('Y-m-d') . '.srv.txt'; /* use GMT date for filename */
2025-08-20 22:58:42 +03:00
$logline = gmdate('Y-m-d H:i:s'); /* use GMT time for log entries */
foreach ($logArr as $tab) {
$logline .= "\t" . $tab;
};
/* write the log line to the file */
$logfile = fopen($filename, 'a');
if (!$logfile) die();
if (fwrite($logfile, $logline . "\n") === false) {
fclose($logfile);
die();
}
/* Done */
fclose($logfile);
}
}