Files
dokuwiki-plugin-botmon/action.php

152 lines
4.5 KiB
PHP
Raw Normal View History

<?php
use dokuwiki\Extension\EventHandler;
use dokuwiki\Extension\Event;
use dokuwiki\Logger;
/**
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');
}
/* session information */
private $sessionId = null;
private $sessionType = '';
2025-08-20 22:58:42 +03:00
/**
* Inserts tracking code to the page header
*
* @param Event $event event object by reference
* @return void
*/
public function insertHeader(Event $event, $param) {
global $INFO;
// populate the session id and type:
$this->getSessionInfo();
// 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:
$code = NL . DOKU_TAB . "document._botmon = {'t0': Date.now(), 'session': '" . json_encode($this->sessionId) . "'};" . 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-09-06 20:01:03 +02:00
$this->writeServerLog($username);
}
/**
* Writes data to the server log.
*
* @return void
*/
private function writeServerLog($username) {
global $conf;
global $INFO;
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'] ?? '');
// collect GeoIP information (if available):
$geoIp = ( $this->sessionId == 'localhost' ? 'local' : 'ZZ' ); /* User-defined code for unknown country */
try {
if (extension_loaded('geoip') && geoip_db_avail(GEOIP_COUNTRY_EDITION)) {
$geoIp = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
} else {
Logger::debug('BotMon Plugin: GeoIP module not available');
}
} catch (Exception $e) {
Logger::error('BotMon Plugin: GeoIP Error', $e->getMessage());
}
2025-09-06 20:01:03 +02:00
// create the log array:
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 */
$this->sessionId, /* Session ID */
$this->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 */
2025-09-06 20:01:03 +02:00
$_SERVER['HTTP_REFERER'] ?? '', /* HTTP Referrer */
substr($conf['lang'],0,2), /* page language */
implode(',', array_unique(array_map( function($it) { return substr($it,0,2); }, explode(',',trim($_SERVER['HTTP_ACCEPT_LANGUAGE'], " \t;,*"))))), /* accepted client languages */
$geoIp /* GeoIP country code */
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);
}
private function getSessionInfo() {
// what is the session identifier?
if (isset($_SESSION)) {
$sesKeys = array_keys($_SESSION); /* DokuWiki Session ID preferred */
foreach ($sesKeys as $key) {
if (substr($key, 0, 2) == 'DW') {
$this->sessionId = $key;
$this->sessionType = 'dw';
return;
}
}
}
if (!$this->sessionId) { /* no DokuWiki Session ID, try PHP session ID */
$this->sessionId = session_id();
$this->sessionType = 'php';
}
if (!$this->sessionId) { /* no PHP session ID, try IP address */
$this->sessionId = $_SERVER['REMOTE_ADDR'] ?? '';
$this->sessionType = 'ip';
}
if (!$this->sessionId) { /* if everything else fails, just us a random ID */
$this->sessionId = rand(1000000, 9999999);
$this->sessionType = 'rand';
}
}
}