1<?php 2 3use dokuwiki\Extension\EventHandler; 4use dokuwiki\Extension\Event; 5 6/** 7 * Action Component for the Bot Monitoring Plugin 8 * 9 * @license GPL 3 (http://www.gnu.org/licenses/gpl.html) 10 * @author Sascha Leib <sascha.leib(at)kolmio.com> 11 */ 12 13class action_plugin_botmon extends DokuWiki_Action_Plugin { 14 15 /** 16 * Registers a callback functions 17 * 18 * @param EventHandler $controller DokuWiki's event controller object 19 * @return void 20 */ 21 public function register(EventHandler $controller) { 22 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'insertHeader'); 23 } 24 25 /** 26 * Inserts tracking code to the page header 27 * 28 * @param Event $event event object by reference 29 * @return void 30 */ 31 public function insertHeader(Event $event, $param) { 32 33 global $INFO; 34 35 // is there a user logged in? 36 $username = ( !empty($INFO['userinfo']) && !empty($INFO['userinfo']['name']) 37 ? $INFO['userinfo']['name'] : ''); 38 39 // build the tracker code: 40 $code = NL . DOKU_TAB . "document._botmon = {'t0': Date.now()};" . NL; 41 if ($username) { 42 $code .= DOKU_TAB . 'document._botmon.user = "' . $username . '";'. NL; 43 } 44 $code .= DOKU_TAB . "addEventListener('load',function(){" . NL; 45 46 $code .= DOKU_TAB . DOKU_TAB . "const e=document.createElement('script');" . NL; 47 $code .= DOKU_TAB . DOKU_TAB . "e.async=true;e.defer=true;" . NL; 48 $code .= DOKU_TAB . DOKU_TAB . "e.src='".DOKU_BASE."lib/plugins/botmon/client.js';" . NL; 49 $code .= DOKU_TAB . DOKU_TAB . "document.getElementsByTagName('head')[0].appendChild(e);" . NL; 50 $code .= DOKU_TAB . "});" . NL . DOKU_TAB; 51 52 $event->data['script'][] = [ 53 '_data' => $code 54 ]; 55 56 /* Write out server-side info to a server log: */ 57 $this->writeServerLog($username); 58 } 59 60 /** 61 * Writes data to the server log. 62 * 63 * @return void 64 */ 65 private function writeServerLog($username) { 66 67 global $conf; 68 global $INFO; 69 70 // what is the session identifier? 71 $sessionId = $_COOKIE['DokuWiki'] ?? ''; 72 $sessionType = 'dw'; 73 if ($sessionId == '') { 74 $sessionId = $_SERVER['REMOTE_ADDR'] ?? ''; 75 if ($sessionId == '127.0.0.1' || $sessionId == '::1') { 76 $sessionId = 'localhost'; 77 } 78 $sessionType = 'ip'; 79 } 80 81 // clean the page ID 82 $pageId = preg_replace('/[\x00-\x1F]/', "\u{FFFD}", $INFO['id'] ?? ''); 83 84 // create the log array: 85 $logArr = Array( 86 $_SERVER['REMOTE_ADDR'] ?? '', /* remote IP */ 87 $pageId, /* page ID */ 88 $sessionId, /* Session ID */ 89 $sessionType, /* session ID type */ 90 $username, 91 $_SERVER['HTTP_USER_AGENT'] ?? '', /* User agent */ 92 $_SERVER['HTTP_REFERER'] ?? '', /* HTTP Referrer */ 93 substr($conf['lang'],0,2), /* page language */ 94 implode(',', array_unique(array_map( function($it) { return substr($it,0,2); }, explode(',',trim($_SERVER['HTTP_ACCEPT_LANGUAGE'], " \t;,*"))))) /* accepted client languages */ 95 ); 96 97 //* create the log line */ 98 $filename = __DIR__ .'/logs/' . gmdate('Y-m-d') . '.srv.txt'; /* use GMT date for filename */ 99 $logline = gmdate('Y-m-d H:i:s'); /* use GMT time for log entries */ 100 foreach ($logArr as $tab) { 101 $logline .= "\t" . $tab; 102 }; 103 104 /* write the log line to the file */ 105 $logfile = fopen($filename, 'a'); 106 if (!$logfile) die(); 107 if (fwrite($logfile, $logline . "\n") === false) { 108 fclose($logfile); 109 die(); 110 } 111 112 /* Done */ 113 fclose($logfile); 114 } 115}