General project setup

This commit is contained in:
Sascha Leib
2025-08-20 22:58:42 +03:00
parent 6980370b98
commit cf9f7fe823
8 changed files with 188 additions and 29 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
.DS_Store

View File

@@ -13,44 +13,74 @@ use dokuwiki\Extension\Event;
class action_plugin_monitor extends DokuWiki_Action_Plugin {
/**
* 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');
}
* 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
*/
/**
* 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?
$username = ( !empty($INFO['userinfo']) && !empty($INFO['userinfo']['name'])
? $INFO['userinfo']['name'] : null);
$username = ( !empty($INFO['userinfo']) && !empty($INFO['userinfo']['name'])
? $INFO['userinfo']['name'] : null);
// build the tracker code:
$code = NL . DOKU_TAB . "document._monitor = {'t0': Date.now()};" . NL;
if ($username) {
if ($username) {
$code .= DOKU_TAB . 'document._monitor.user = "' . $username . '";'. NL;
}
$code .= DOKU_TAB . "addEventListener('load',function(){" . NL;
$code .= DOKU_TAB . DOKU_TAB . "const e=document.createElement('script');" . NL;
$code .= DOKU_TAB . DOKU_TAB . "e.async=true;e.defer=true;" . NL;
$code .= DOKU_TAB . DOKU_TAB . "e.src='".DOKU_BASE."lib/plugins/monitor/client.js';" . NL;
$code .= DOKU_TAB . DOKU_TAB . "document.getElementsByTagName('head')[0].appendChild(e);" . NL;
$code .= DOKU_TAB . DOKU_TAB . "e.async=true;e.defer=true;" . NL;
$code .= DOKU_TAB . DOKU_TAB . "e.src='".DOKU_BASE."lib/plugins/monitor/client.js';" . NL;
$code .= DOKU_TAB . DOKU_TAB . "document.getElementsByTagName('head')[0].appendChild(e);" . NL;
$code .= DOKU_TAB . "});" . NL;
$event->data['script'][] = [
$event->data['script'][] = [
'_data' => $code
];
}
/* Write out client info to a server log: */
$logArr = Array(
$_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */
$INFO['id'] ?? '', /* user agent */
$_COOKIE['DokuWiki'] ?? '', /* DokuWiki session ID */
$username,
$_SERVER['HTTP_USER_AGENT'] ?? '' /* User agent */
);
//* create the log line */
$filename = __DIR__ .'/logs/' . gmdate('Y-m-d') . '.srv'; /* use GMT date for filename */
$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);
}
}

View File

@@ -2,10 +2,10 @@ monitor_client = {
init: function() {
/* send the page view request: */
this._onPageView(this._src.replace( this._scriptName, '/view.php'));
this._onPageView(this._src.replace( this._scriptName, '/pview.php'));
/* send the first heartbeat signal after x seconds: */
setTimeout(this._onHeartbeat.bind(this, this._src.replace( this._scriptName, '/tock.php')),this._heartbeat * 1000);
setTimeout(this._onHeartbeat.bind(this, this._src.replace( this._scriptName, '/tick.php')),this._heartbeat * 1000);
},
/* keep a reference to the script URL: */
@@ -20,7 +20,6 @@ monitor_client = {
/* function to init page data on server: */
_onPageView: async function(url) {
try {
/* collect the data to send: */
const visit = {
'pg': JSINFO.id,
@@ -36,7 +35,7 @@ monitor_client = {
/* compile to a FormData object: */
const data = new FormData();
data.append( "visit", JSON.stringify( visit ) );
data.append( "pageview", JSON.stringify( visit ) );
/* send the request */
const response = await fetch(url + '?t=' + Date.now(), {
@@ -53,11 +52,11 @@ monitor_client = {
/* function to call regularly to show the user is still on the page: */
_onHeartbeat: async function(url) {
console.info('monitor_client._onHeartbeat', url);
console.log(this);
//console.info('monitor_client._onHeartbeat', url);
try {
const response = await fetch(url + '?p=' + encodeURIComponent(JSINFO.id) + '&t=' + Date.now(), {
method: 'HEAD',
//method: 'HEAD',
});
if (!response.ok) {
throw new Error(response.status + ' ' + response.statusText + ' - ' + url);

20
logs/2025-08-20.log Normal file
View File

@@ -0,0 +1,20 @@
2025-08-20 19:06:51 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:08:55 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:08:58 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:13:07 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:13:22 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:14:10 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:16:02 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:18:04 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:18:27 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:28:44 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:40:03 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:40:07 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:40:08 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:41:14 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:42:22 ::1 playground:index l95kv2efjemi3ccq0n27ovcupp null
2025-08-20 19:42:41 ::1 playground:index l95kv2efjemi3ccq0n27ovcupp null
2025-08-20 19:42:50 ::1 playground:index l95kv2efjemi3ccq0n27ovcupp null
2025-08-20 19:44:10 ::1 playground:index hh43pvar70lpbg6d3kol60kcth null
2025-08-20 19:44:31 ::1 playground:index hh43pvar70lpbg6d3kol60kcth null
2025-08-20 19:57:29 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator

19
logs/2025-08-20.srv Normal file
View File

@@ -0,0 +1,19 @@
2025-08-20 19:08:54 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:08:58 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:13:06 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:13:22 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:14:09 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:16:01 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:18:04 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:18:27 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:28:44 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:40:03 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:40:07 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:40:08 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:41:14 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator
2025-08-20 19:42:22 ::1 playground:index
2025-08-20 19:42:41 ::1 playground:index l95kv2efjemi3ccq0n27ovcupp
2025-08-20 19:42:50 ::1 playground:index l95kv2efjemi3ccq0n27ovcupp
2025-08-20 19:44:10 ::1 playground:index
2025-08-20 19:44:31 ::1 playground:index hh43pvar70lpbg6d3kol60kcth
2025-08-20 19:57:29 127.0.0.1 playground:index cn2d1mn0spa8rv861tbc25ui2g Administrator Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:141.0) Gecko/20100101 Firefox/141.0

6
logs/2025-08-20.tck Normal file
View File

@@ -0,0 +1,6 @@
2025-08-20 19:29:14 1755718154605 playground:index 127.0.0.1 cn2d1mn0spa8rv861tbc25ui2g 1755718154605
2025-08-20 19:40:38 1755718838992 playground:index 127.0.0.1 cn2d1mn0spa8rv861tbc25ui2g 1755718838992
2025-08-20 19:41:44 1755718904549 playground:index 127.0.0.1 cn2d1mn0spa8rv861tbc25ui2g 1755718904549
2025-08-20 19:43:20 1755719000646 playground:index ::1 l95kv2efjemi3ccq0n27ovcupp 1755719000646
2025-08-20 19:45:01 1755719101444 playground:index ::1 hh43pvar70lpbg6d3kol60kcth 1755719101444
2025-08-20 19:57:59 1755719879708 playground:index 127.0.0.1 cn2d1mn0spa8rv861tbc25ui2g 1755719879708

50
pview.php Normal file
View File

@@ -0,0 +1,50 @@
<?php /* MONITOR PLUGIN PAGE VIEW SCRIPT */
/* parse the JSON payload */
$json = json_decode($_POST['pageview'], true);
if (!$json) {
http_response_code(400);
die("Error: Invalid JSON data sent to server.");
}
/* build the resulting log line (ensure fixed column positions!) */
$logArr = Array(
$_SERVER['REMOTE_ADDR'] ?? 'null', /* remote IP */
$json['pg'] ?? 'null', /* DW page ID */
$_COOKIE['DokuWiki'] ?? 'null', /* DokuWiki session ID */
$json['u'] ?? 'null' /* DW User id (if logged in) */
// $json['tz'] ?? 'null', /* timzone offset */
// $json['lg'] ?? 'null', /* browser language */
// $json['td'] ?? 'null', /* load time */
// $json['scr'] ?? 'null', /* Screen dimensions */
// $json['l'] ?? 'null', /* Accepted languages list */
// $json['url'] ?? 'null', /* Full request URL */
// $json['r'] ?? 'null', /* Referrer URL */
// $_SERVER['HTTP_USER_AGENT'] ?? 'null', /* User agent */
// $json['t'] ?? '' /* Page title */
);
/* create the log line */
$filename = 'logs/' . gmdate('Y-m-d') . '.log'; /* use server datetime */
$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) {
http_response_code(500);
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";

34
tick.php Normal file
View File

@@ -0,0 +1,34 @@
<?php /* MONITOR PLUGIN HEARTBEAT TICKER SCRIPT */
/* build the resulting log line (ensure fixed column positions!) */
$logArr = Array(
intval($_GET['t']), /* timestamp */
$_GET['p'] ?? null, /* page ID */
$_SERVER['REMOTE_ADDR'] ?? 'null', /* remote IP */
$_COOKIE['DokuWiki'] ?? 'null', /* DokuWiki session ID */
$_GET['t'] ?? null /* client time */
);
/* create the log line */
$filename = 'logs/' . gmdate('Y-m-d') . ".tck"; /* use GMT date for filename */
$line = gmdate('Y-m-d H:i:s'); /* use GMT time for log entries */
foreach ($logArr as $val) {
$line .= "\t" . $val;
};
/* write the log line to the file */
$tickfile = fopen($filename, "a");
if (!$tickfile) {
http_response_code(500);
die("Error: Unable to open log file. Please check file permissions.");
}
if (fwrite($tickfile, $line . "\n") === false) {
http_response_code(500);
fclose($tickfile);
die("Error: Could not write to log file.");
}
fclose($tickfile);
/* Send "Accepted" header */
http_response_code(202);
echo "OK";