1<?php 2 3use dokuwiki\Extension\ActionPlugin; 4use dokuwiki\Extension\EventHandler; 5use dokuwiki\Extension\Event; 6 7/** 8 * 9 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 10 * @author Andreas Gohr <gohr@cosmocode.de> 11 */ 12 13// must be run within Dokuwiki 14if (!defined('DOKU_INC')) die(); 15 16if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 17require_once(DOKU_PLUGIN . 'action.php'); 18 19class action_plugin_statistics extends ActionPlugin 20{ 21 /** 22 * register the eventhandlers and initialize some options 23 */ 24 public function register(EventHandler $controller) 25 { 26 global $JSINFO; 27 global $ACT; 28 $JSINFO['act'] = $ACT; 29 30 $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []); 31 $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []); 32 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []); 33 $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []); 34 $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []); 35 $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []); 36 } 37 38 /** 39 * @fixme call this in the webbug call 40 */ 41 public function putpixel() 42 { 43 global $ID; 44 $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) . 45 '&r=' . rawurlencode($_SERVER['HTTP_REFERER']) . '&rnd=' . time(); 46 47 echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>'; 48 } 49 50 /** 51 * Log page edits actions 52 */ 53 public function logedits(Event $event, $param) 54 { 55 if ($event->data[3]) return; // no revision 56 57 if (file_exists($event->data[0][0])) { 58 if ($event->data[0][1] == '') { 59 $type = 'D'; 60 } else { 61 $type = 'E'; 62 } 63 } else { 64 $type = 'C'; 65 } 66 /** @var helper_plugin_statistics $hlp */ 67 $hlp = plugin_load('helper', 'statistics'); 68 $hlp->Logger()->logEdit(cleanID($event->data[1] . ':' . $event->data[2]), $type); 69 } 70 71 /** 72 * Log internal search 73 */ 74 public function logsearch(Event $event, $param) 75 { 76 /** @var helper_plugin_statistics $hlp */ 77 $hlp = plugin_load('helper', 'statistics'); 78 $hlp->Logger()->logSearch('', $event->data['query'], $event->data['highlight'], 'dokuwiki'); 79 } 80 81 /** 82 * Log login/logouts 83 */ 84 public function loglogins(Event $event, $param) 85 { 86 $type = ''; 87 $act = $this->_act_clean($event->data); 88 if ($act == 'logout') { 89 $type = 'o'; 90 } elseif ($_SERVER['REMOTE_USER'] && $act == 'login') { 91 if ($_REQUEST['r']) { 92 $type = 'p'; 93 } else { 94 $type = 'l'; 95 } 96 } elseif ($_REQUEST['u'] && !$_REQUEST['http_credentials'] && !$_SERVER['REMOTE_USER']) { 97 $type = 'f'; 98 } 99 if (!$type) return; 100 101 /** @var helper_plugin_statistics $hlp */ 102 $hlp = plugin_load('helper', 'statistics'); 103 $hlp->Logger()->logLogin($type); 104 } 105 106 /** 107 * Log user creations 108 */ 109 public function logregistration(Event $event, $param) 110 { 111 if ($event->data['type'] == 'create') { 112 /** @var helper_plugin_statistics $hlp */ 113 $hlp = plugin_load('helper', 'statistics'); 114 $hlp->Logger()->logLogin('C', $event->data['params'][0]); 115 } 116 } 117 118 /** 119 * Log media access 120 */ 121 public function logmedia(Event $event, $param) 122 { 123 if ($event->data['status'] < 200) return; 124 if ($event->data['status'] >= 400) return; 125 if (preg_match('/^\w+:\/\//', $event->data['media'])) return; 126 127 // no size for redirect/not modified 128 if ($event->data['status'] >= 300) { 129 $size = 0; 130 } else { 131 $size = @filesize($event->data['file']); 132 } 133 134 /** @var helper_plugin_statistics $hlp */ 135 $hlp = plugin_load('helper', 'statistics'); 136 $hlp->Logger()->logMedia( 137 $event->data['media'], 138 $event->data['mime'], 139 !$event->data['download'], 140 $size 141 ); 142 } 143 144 /** 145 * Log the daily page and media counts for the history 146 */ 147 public function loghistory(Event $event, $param) 148 { 149 echo 'Plugin Statistics: started' . DOKU_LF; 150 151 /** @var helper_plugin_statistics $hlp */ 152 $hlp = plugin_load('helper', 'statistics'); 153 154 // check if a history was gathered already today 155 $sql = "SELECT `info` FROM " . $hlp->prefix . "history WHERE `dt` = DATE(NOW())"; 156 $result = $hlp->runSQL($sql); 157 if (is_null($result)) { 158 global $MSG; 159 print_r($MSG); 160 } 161 162 $page_ran = false; 163 $media_ran = false; 164 foreach ($result as $row) { 165 if ($row['info'] == 'page_count') $page_ran = true; 166 if ($row['info'] == 'media_count') $media_ran = true; 167 } 168 169 if ($page_ran && $media_ran) { 170 echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF; 171 return; 172 } 173 174 $event->stopPropagation(); 175 $event->preventDefault(); 176 177 if ($page_ran) { 178 echo 'Plugin Statistics: logging media' . DOKU_LF; 179 $hlp->Logger()->logHistoryMedia(); 180 } else { 181 echo 'Plugin Statistics: logging pages' . DOKU_LF; 182 $hlp->Logger()->logHistoryPages(); 183 } 184 echo 'Plugin Statistics: finished' . DOKU_LF; 185 } 186 187 /** 188 * Pre-Sanitize the action command 189 * 190 * Similar to act_clean in action.php but simplified and without 191 * error messages 192 */ 193 public function _act_clean($act) 194 { 195 // check if the action was given as array key 196 if (is_array($act)) { 197 [$act] = array_keys($act); 198 } 199 200 //remove all bad chars 201 $act = strtolower($act); 202 $act = preg_replace('/[^a-z_]+/', '', $act); 203 204 return $act; 205 } 206} 207