1<?php 2 3use dokuwiki\Extension\ActionPlugin; 4use dokuwiki\Extension\Event; 5use dokuwiki\Extension\EventHandler; 6 7/** 8 * 9 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 10 * @author Andreas Gohr <gohr@cosmocode.de> 11 */ 12class action_plugin_statistics extends ActionPlugin 13{ 14 /** 15 * register the eventhandlers and initialize some options 16 */ 17 public function register(EventHandler $controller) 18 { 19 global $JSINFO; 20 global $ACT; 21 $JSINFO['act'] = $ACT; 22 23 $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'initSession', []); 24 // FIXME new save event might be better: 25 $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []); 26 $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []); 27 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []); 28 $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []); 29 $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []); 30 $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []); 31 } 32 33 /** 34 * This ensures we have a session for the statistics plugin 35 * 36 * We reset this when the user agent changes or the session is too old 37 * (15 minutes). 38 */ 39 public function initSession() 40 { 41 global $INPUT; 42 43 // load session data 44 if (isset($_SESSION[DOKU_COOKIE]['statistics'])) { 45 $session = $_SESSION[DOKU_COOKIE]['statistics']; 46 } else { 47 $session = []; 48 } 49 // reset if session is too old 50 if (time() - ($session['time'] ?? 0) > 60 * 15) { 51 $session = []; 52 } 53 // reset if user agent changed 54 if ($INPUT->server->str('HTTP_USER_AGENT') != ($session['user_agent'] ?? '')) { 55 $session = []; 56 } 57 58 // update session data 59 $session['time'] = time(); 60 $session['user_agent'] = $INPUT->server->str('HTTP_USER_AGENT'); 61 $session['uid'] = get_doku_pref('plgstats', bin2hex(random_bytes(16))); 62 if (!isset($session['id'])) { 63 // generate a new session id if not set 64 $session['id'] = bin2hex(random_bytes(16)); 65 } 66 67 // store session and cookie data 68 $_SESSION[DOKU_COOKIE]['statistics'] = $session; 69 set_doku_pref('plgstats', $session['uid']); 70 } 71 72 /** 73 * @fixme call this in the webbug call 74 */ 75 public function putpixel() 76 { 77 global $ID, $INPUT; 78 $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) . 79 '&r=' . rawurlencode($INPUT->server->str('HTTP_REFERER')) . '&rnd=' . time(); 80 81 echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>'; 82 } 83 84 /** 85 * Log page edits actions 86 */ 87 public function logedits(Event $event, $param) 88 { 89 if ($event->data[3]) return; // no revision 90 91 if (file_exists($event->data[0][0])) { 92 if ($event->data[0][1] == '') { 93 $type = 'D'; 94 } else { 95 $type = 'E'; 96 } 97 } else { 98 $type = 'C'; 99 } 100 /** @var helper_plugin_statistics $hlp */ 101 $hlp = plugin_load('helper', 'statistics'); 102 $hlp->getLogger()->logEdit($event->data[1] . ':' . $event->data[2], $type); 103 } 104 105 /** 106 * Log internal search 107 */ 108 public function logsearch(Event $event, $param) 109 { 110 /** @var helper_plugin_statistics $hlp */ 111 $hlp = plugin_load('helper', 'statistics'); 112 $hlp->getLogger()->logSearch($event->data['query'], $event->data['highlight']); 113 } 114 115 /** 116 * Log login/logouts 117 */ 118 public function loglogins(Event $event, $param) 119 { 120 global $INPUT; 121 122 $type = ''; 123 $act = $this->actClean($event->data); 124 if ($act == 'logout') { 125 $type = 'o'; 126 } elseif ($INPUT->server->str('REMOTE_USER') && $act == 'login') { 127 if ($INPUT->str('r')) { 128 $type = 'p'; 129 } else { 130 $type = 'l'; 131 } 132 } elseif ($INPUT->str('u') && !$INPUT->str('http_credentials') && !$INPUT->server->str('REMOTE_USER')) { 133 $type = 'f'; 134 } 135 if (!$type) return; 136 137 /** @var helper_plugin_statistics $hlp */ 138 $hlp = plugin_load('helper', 'statistics'); 139 $hlp->getLogger()->logLogin($type); 140 } 141 142 /** 143 * Log user creations 144 */ 145 public function logregistration(Event $event, $param) 146 { 147 if ($event->data['type'] == 'create') { 148 /** @var helper_plugin_statistics $hlp */ 149 $hlp = plugin_load('helper', 'statistics'); 150 $hlp->getLogger()->logLogin('C', $event->data['params'][0]); 151 } 152 } 153 154 /** 155 * Log media access 156 */ 157 public function logmedia(Event $event, $param) 158 { 159 if ($event->data['status'] < 200) return; 160 if ($event->data['status'] >= 400) return; 161 if (preg_match('/^\w+:\/\//', $event->data['media'])) return; 162 163 // no size for redirect/not modified 164 if ($event->data['status'] >= 300) { 165 $size = 0; 166 } else { 167 $size = @filesize($event->data['file']); 168 } 169 170 /** @var helper_plugin_statistics $hlp */ 171 $hlp = plugin_load('helper', 'statistics'); 172 $hlp->getLogger()->logMedia( 173 $event->data['media'], 174 $event->data['mime'], 175 !$event->data['download'], 176 $size 177 ); 178 } 179 180 /** 181 * Log the daily page and media counts for the history 182 */ 183 public function loghistory(Event $event, $param) 184 { 185 echo 'Plugin Statistics: started' . DOKU_LF; 186 187 /** @var helper_plugin_statistics $hlp */ 188 $hlp = plugin_load('helper', 'statistics'); 189 $db = $hlp->getDB(); 190 191 // check if a history was gathered already today 192 $result = $db->queryAll( 193 "SELECT info FROM history WHERE date(dt) = date('now')" 194 ); 195 196 $page_ran = false; 197 $media_ran = false; 198 foreach ($result as $row) { 199 if ($row['info'] == 'page_count') $page_ran = true; 200 if ($row['info'] == 'media_count') $media_ran = true; 201 } 202 203 if ($page_ran && $media_ran) { 204 echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF; 205 return; 206 } 207 208 $event->stopPropagation(); 209 $event->preventDefault(); 210 211 if ($page_ran) { 212 echo 'Plugin Statistics: logging media' . DOKU_LF; 213 $hlp->getLogger()->logHistoryMedia(); 214 } else { 215 echo 'Plugin Statistics: logging pages' . DOKU_LF; 216 $hlp->getLogger()->logHistoryPages(); 217 } 218 echo 'Plugin Statistics: finished' . DOKU_LF; 219 } 220 221 /** 222 * Pre-Sanitize the action command 223 * 224 * Similar to act_clean in action.php but simplified and without 225 * error messages 226 */ 227 protected function actClean($act) 228 { 229 // check if the action was given as array key 230 if (is_array($act)) { 231 [$act] = array_keys($act); 232 } 233 234 //remove all bad chars 235 $act = strtolower($act); 236 $act = preg_replace('/[^a-z_]+/', '', $act); 237 238 return $act; 239 } 240} 241