114d99ec0SAndreas Gohr<?php 2a8acb244SAndreas Gohr 3a8acb244SAndreas Gohruse dokuwiki\Extension\ActionPlugin; 4a8acb244SAndreas Gohruse dokuwiki\Extension\EventHandler; 5a8acb244SAndreas Gohruse dokuwiki\Extension\Event; 6a8acb244SAndreas Gohr 714d99ec0SAndreas Gohr/** 814d99ec0SAndreas Gohr * 914d99ec0SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 1014d99ec0SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1114d99ec0SAndreas Gohr */ 1214d99ec0SAndreas Gohr 1314d99ec0SAndreas Gohr// must be run within Dokuwiki 1414d99ec0SAndreas Gohrif (!defined('DOKU_INC')) die(); 1514d99ec0SAndreas Gohr 1614d99ec0SAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 1714d99ec0SAndreas Gohrrequire_once(DOKU_PLUGIN . 'action.php'); 1814d99ec0SAndreas Gohr 19a8acb244SAndreas Gohrclass action_plugin_statistics extends ActionPlugin 20a8acb244SAndreas Gohr{ 2114d99ec0SAndreas Gohr /** 2214d99ec0SAndreas Gohr * register the eventhandlers and initialize some options 2314d99ec0SAndreas Gohr */ 24a8acb244SAndreas Gohr public function register(EventHandler $controller) 25a8acb244SAndreas Gohr { 26eabe0d07SAndreas Gohr global $JSINFO; 27eabe0d07SAndreas Gohr global $ACT; 28eabe0d07SAndreas Gohr $JSINFO['act'] = $ACT; 2914d99ec0SAndreas Gohr 30*2257e39bSAndreas Gohr $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []); 31*2257e39bSAndreas Gohr $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []); 32*2257e39bSAndreas Gohr $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []); 33*2257e39bSAndreas Gohr $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []); 34*2257e39bSAndreas Gohr $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []); 35*2257e39bSAndreas Gohr $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []); 3614d99ec0SAndreas Gohr } 3714d99ec0SAndreas Gohr 3814d99ec0SAndreas Gohr /** 3914d99ec0SAndreas Gohr * @fixme call this in the webbug call 4014d99ec0SAndreas Gohr */ 41a8acb244SAndreas Gohr public function putpixel() 42a8acb244SAndreas Gohr { 4314d99ec0SAndreas Gohr global $ID; 4414d99ec0SAndreas Gohr $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) . 4514d99ec0SAndreas Gohr '&r=' . rawurlencode($_SERVER['HTTP_REFERER']) . '&rnd=' . time(); 4614d99ec0SAndreas Gohr 47*2257e39bSAndreas Gohr echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>'; 4814d99ec0SAndreas Gohr } 4958511ae8SAndreas Gohr 5058511ae8SAndreas Gohr /** 515bccfe87SAndreas Gohr * Log page edits actions 5258511ae8SAndreas Gohr */ 53a8acb244SAndreas Gohr public function logedits(Event $event, $param) 54a8acb244SAndreas Gohr { 5558511ae8SAndreas Gohr if ($event->data[3]) return; // no revision 5658511ae8SAndreas Gohr 5758511ae8SAndreas Gohr if (file_exists($event->data[0][0])) { 5858511ae8SAndreas Gohr if ($event->data[0][1] == '') { 5958511ae8SAndreas Gohr $type = 'D'; 6058511ae8SAndreas Gohr } else { 6158511ae8SAndreas Gohr $type = 'E'; 6258511ae8SAndreas Gohr } 6358511ae8SAndreas Gohr } else { 6458511ae8SAndreas Gohr $type = 'C'; 6558511ae8SAndreas Gohr } 661664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 6758511ae8SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 68aab59130SAndreas Gohr $hlp->Logger()->logEdit(cleanID($event->data[1] . ':' . $event->data[2]), $type); 6958511ae8SAndreas Gohr } 705bccfe87SAndreas Gohr 715bccfe87SAndreas Gohr /** 725bccfe87SAndreas Gohr * Log internal search 735bccfe87SAndreas Gohr */ 74a8acb244SAndreas Gohr public function logsearch(Event $event, $param) 75a8acb244SAndreas Gohr { 761664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 775bccfe87SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 78aab59130SAndreas Gohr $hlp->Logger()->logSearch('', $event->data['query'], $event->data['highlight'], 'dokuwiki'); 795bccfe87SAndreas Gohr } 80b5e880bdSAndreas Gohr 81b5e880bdSAndreas Gohr /** 82b5e880bdSAndreas Gohr * Log login/logouts 83b5e880bdSAndreas Gohr */ 84a8acb244SAndreas Gohr public function loglogins(Event $event, $param) 85a8acb244SAndreas Gohr { 86b5e880bdSAndreas Gohr $type = ''; 87b5e880bdSAndreas Gohr $act = $this->_act_clean($event->data); 88b5e880bdSAndreas Gohr if ($act == 'logout') { 89b5e880bdSAndreas Gohr $type = 'o'; 90b5e880bdSAndreas Gohr } elseif ($_SERVER['REMOTE_USER'] && $act == 'login') { 91b5e880bdSAndreas Gohr if ($_REQUEST['r']) { 92b5e880bdSAndreas Gohr $type = 'p'; 93b5e880bdSAndreas Gohr } else { 94b5e880bdSAndreas Gohr $type = 'l'; 95b5e880bdSAndreas Gohr } 96b5e880bdSAndreas Gohr } elseif ($_REQUEST['u'] && !$_REQUEST['http_credentials'] && !$_SERVER['REMOTE_USER']) { 97b5e880bdSAndreas Gohr $type = 'f'; 98b5e880bdSAndreas Gohr } 99b5e880bdSAndreas Gohr if (!$type) return; 100b5e880bdSAndreas Gohr 1011664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 102b5e880bdSAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 103aab59130SAndreas Gohr $hlp->Logger()->logLogin($type); 10414d99ec0SAndreas Gohr } 10514d99ec0SAndreas Gohr 106535aeea1SAndreas Gohr /** 107535aeea1SAndreas Gohr * Log user creations 108535aeea1SAndreas Gohr */ 109a8acb244SAndreas Gohr public function logregistration(Event $event, $param) 110a8acb244SAndreas Gohr { 111535aeea1SAndreas Gohr if ($event->data['type'] == 'create') { 1121664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 113535aeea1SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 114aab59130SAndreas Gohr $hlp->Logger()->logLogin('C', $event->data['params'][0]); 115535aeea1SAndreas Gohr } 116535aeea1SAndreas Gohr } 117b5e880bdSAndreas Gohr 118b5e880bdSAndreas Gohr /** 1191664ba1dSAndreas Gohr * Log media access 1201664ba1dSAndreas Gohr */ 121a8acb244SAndreas Gohr public function logmedia(Event $event, $param) 122a8acb244SAndreas Gohr { 1231664ba1dSAndreas Gohr if ($event->data['status'] < 200) return; 1241664ba1dSAndreas Gohr if ($event->data['status'] >= 400) return; 1251664ba1dSAndreas Gohr if (preg_match('/^\w+:\/\//', $event->data['media'])) return; 1261664ba1dSAndreas Gohr 1271664ba1dSAndreas Gohr // no size for redirect/not modified 1281664ba1dSAndreas Gohr if ($event->data['status'] >= 300) { 1291664ba1dSAndreas Gohr $size = 0; 1301664ba1dSAndreas Gohr } else { 13117978b38SAndreas Gohr $size = @filesize($event->data['file']); 1321664ba1dSAndreas Gohr } 1331664ba1dSAndreas Gohr 1341664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 1351664ba1dSAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 136aab59130SAndreas Gohr $hlp->Logger()->logMedia( 1371664ba1dSAndreas Gohr $event->data['media'], 1381664ba1dSAndreas Gohr $event->data['mime'], 1391664ba1dSAndreas Gohr !$event->data['download'], 1401664ba1dSAndreas Gohr $size 1411664ba1dSAndreas Gohr ); 1421664ba1dSAndreas Gohr } 1431664ba1dSAndreas Gohr 1441664ba1dSAndreas Gohr /** 145cae4a1c5SAndreas Gohr * Log the daily page and media counts for the history 146cae4a1c5SAndreas Gohr */ 147a8acb244SAndreas Gohr public function loghistory(Event $event, $param) 148a8acb244SAndreas Gohr { 149cae4a1c5SAndreas Gohr echo 'Plugin Statistics: started' . DOKU_LF; 150cae4a1c5SAndreas Gohr 151cae4a1c5SAndreas Gohr /** @var helper_plugin_statistics $hlp */ 152cae4a1c5SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 153cae4a1c5SAndreas Gohr 154cae4a1c5SAndreas Gohr // check if a history was gathered already today 155cae4a1c5SAndreas Gohr $sql = "SELECT `info` FROM " . $hlp->prefix . "history WHERE `dt` = DATE(NOW())"; 156cae4a1c5SAndreas Gohr $result = $hlp->runSQL($sql); 157cae4a1c5SAndreas Gohr if (is_null($result)) { 158cae4a1c5SAndreas Gohr global $MSG; 159cae4a1c5SAndreas Gohr print_r($MSG); 160cae4a1c5SAndreas Gohr } 161cae4a1c5SAndreas Gohr 162cae4a1c5SAndreas Gohr $page_ran = false; 163cae4a1c5SAndreas Gohr $media_ran = false; 164cae4a1c5SAndreas Gohr foreach ($result as $row) { 165cae4a1c5SAndreas Gohr if ($row['info'] == 'page_count') $page_ran = true; 166cae4a1c5SAndreas Gohr if ($row['info'] == 'media_count') $media_ran = true; 167cae4a1c5SAndreas Gohr } 168cae4a1c5SAndreas Gohr 169cae4a1c5SAndreas Gohr if ($page_ran && $media_ran) { 170cae4a1c5SAndreas Gohr echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF; 171cae4a1c5SAndreas Gohr return; 172cae4a1c5SAndreas Gohr } 173cae4a1c5SAndreas Gohr 174cae4a1c5SAndreas Gohr $event->stopPropagation(); 175cae4a1c5SAndreas Gohr $event->preventDefault(); 176cae4a1c5SAndreas Gohr 177cae4a1c5SAndreas Gohr if ($page_ran) { 178cae4a1c5SAndreas Gohr echo 'Plugin Statistics: logging media' . DOKU_LF; 179aab59130SAndreas Gohr $hlp->Logger()->logHistoryMedia(); 180cae4a1c5SAndreas Gohr } else { 181cae4a1c5SAndreas Gohr echo 'Plugin Statistics: logging pages' . DOKU_LF; 182aab59130SAndreas Gohr $hlp->Logger()->logHistoryPages(); 183cae4a1c5SAndreas Gohr } 184cae4a1c5SAndreas Gohr echo 'Plugin Statistics: finished' . DOKU_LF; 185cae4a1c5SAndreas Gohr } 186cae4a1c5SAndreas Gohr 187cae4a1c5SAndreas Gohr /** 188b5e880bdSAndreas Gohr * Pre-Sanitize the action command 189b5e880bdSAndreas Gohr * 190b5e880bdSAndreas Gohr * Similar to act_clean in action.php but simplified and without 191b5e880bdSAndreas Gohr * error messages 192b5e880bdSAndreas Gohr */ 193a8acb244SAndreas Gohr public function _act_clean($act) 194a8acb244SAndreas Gohr { 195b5e880bdSAndreas Gohr // check if the action was given as array key 196b5e880bdSAndreas Gohr if (is_array($act)) { 197a8acb244SAndreas Gohr [$act] = array_keys($act); 198b5e880bdSAndreas Gohr } 199b5e880bdSAndreas Gohr 200b5e880bdSAndreas Gohr //remove all bad chars 201b5e880bdSAndreas Gohr $act = strtolower($act); 202b5e880bdSAndreas Gohr $act = preg_replace('/[^a-z_]+/', '', $act); 203b5e880bdSAndreas Gohr 204b5e880bdSAndreas Gohr return $act; 205b5e880bdSAndreas Gohr } 206b5e880bdSAndreas Gohr} 207