From cf9f7fe823093a2f336627bc7e05ea0296d98bca Mon Sep 17 00:00:00 2001 From: Sascha Leib Date: Wed, 20 Aug 2025 22:58:42 +0300 Subject: [PATCH] General project setup --- .gitignore | 1 + action.php | 74 +++++++++++++++++++++++++++++++-------------- client.js | 13 ++++---- logs/2025-08-20.log | 20 ++++++++++++ logs/2025-08-20.srv | 19 ++++++++++++ logs/2025-08-20.tck | 6 ++++ pview.php | 50 ++++++++++++++++++++++++++++++ tick.php | 34 +++++++++++++++++++++ 8 files changed, 188 insertions(+), 29 deletions(-) create mode 100644 .gitignore create mode 100644 logs/2025-08-20.log create mode 100644 logs/2025-08-20.srv create mode 100644 logs/2025-08-20.tck create mode 100644 pview.php create mode 100644 tick.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/action.php b/action.php index d12e69b..17a2e1a 100644 --- a/action.php +++ b/action.php @@ -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); + + } + } \ No newline at end of file diff --git a/client.js b/client.js index 0cfd11b..b1baf5c 100644 --- a/client.js +++ b/client.js @@ -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); diff --git a/logs/2025-08-20.log b/logs/2025-08-20.log new file mode 100644 index 0000000..93997b9 --- /dev/null +++ b/logs/2025-08-20.log @@ -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 diff --git a/logs/2025-08-20.srv b/logs/2025-08-20.srv new file mode 100644 index 0000000..a12dbb4 --- /dev/null +++ b/logs/2025-08-20.srv @@ -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 diff --git a/logs/2025-08-20.tck b/logs/2025-08-20.tck new file mode 100644 index 0000000..acbe3f0 --- /dev/null +++ b/logs/2025-08-20.tck @@ -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 diff --git a/pview.php b/pview.php new file mode 100644 index 0000000..0c67131 --- /dev/null +++ b/pview.php @@ -0,0 +1,50 @@ +