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 300863c19cSAndreas Gohr $controller->register_hook( 310863c19cSAndreas Gohr 'IO_WIKIPAGE_WRITE', 3258511ae8SAndreas Gohr 'BEFORE', 3358511ae8SAndreas Gohr $this, 3458511ae8SAndreas Gohr 'logedits', 35a8acb244SAndreas Gohr [] 360863c19cSAndreas Gohr ); 370863c19cSAndreas Gohr $controller->register_hook( 380863c19cSAndreas Gohr 'SEARCH_QUERY_FULLPAGE', 395bccfe87SAndreas Gohr 'AFTER', 405bccfe87SAndreas Gohr $this, 415bccfe87SAndreas Gohr 'logsearch', 42a8acb244SAndreas Gohr [] 430863c19cSAndreas Gohr ); 440863c19cSAndreas Gohr $controller->register_hook( 450863c19cSAndreas Gohr 'ACTION_ACT_PREPROCESS', 46b5e880bdSAndreas Gohr 'BEFORE', 47b5e880bdSAndreas Gohr $this, 48b5e880bdSAndreas Gohr 'loglogins', 49a8acb244SAndreas Gohr [] 500863c19cSAndreas Gohr ); 510863c19cSAndreas Gohr $controller->register_hook( 520863c19cSAndreas Gohr 'AUTH_USER_CHANGE', 53535aeea1SAndreas Gohr 'AFTER', 54535aeea1SAndreas Gohr $this, 55535aeea1SAndreas Gohr 'logregistration', 56a8acb244SAndreas Gohr [] 570863c19cSAndreas Gohr ); 581664ba1dSAndreas Gohr $controller->register_hook( 591664ba1dSAndreas Gohr 'FETCH_MEDIA_STATUS', 601664ba1dSAndreas Gohr 'BEFORE', 611664ba1dSAndreas Gohr $this, 621664ba1dSAndreas Gohr 'logmedia', 63a8acb244SAndreas Gohr [] 641664ba1dSAndreas Gohr ); 65cae4a1c5SAndreas Gohr $controller->register_hook( 66cae4a1c5SAndreas Gohr 'INDEXER_TASKS_RUN', 67cae4a1c5SAndreas Gohr 'AFTER', 68cae4a1c5SAndreas Gohr $this, 69cae4a1c5SAndreas Gohr 'loghistory', 70a8acb244SAndreas Gohr [] 71cae4a1c5SAndreas Gohr ); 7214d99ec0SAndreas Gohr } 7314d99ec0SAndreas Gohr 7414d99ec0SAndreas Gohr /** 7514d99ec0SAndreas Gohr * @fixme call this in the webbug call 7614d99ec0SAndreas Gohr */ 77a8acb244SAndreas Gohr public function putpixel() 78a8acb244SAndreas Gohr { 7914d99ec0SAndreas Gohr global $ID; 8014d99ec0SAndreas Gohr $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) . 8114d99ec0SAndreas Gohr '&r=' . rawurlencode($_SERVER['HTTP_REFERER']) . '&rnd=' . time(); 8214d99ec0SAndreas Gohr 8314d99ec0SAndreas Gohr echo '<noscript><img src="' . $url . '" width="1" height="1" /></noscript>'; 8414d99ec0SAndreas Gohr } 8558511ae8SAndreas Gohr 8658511ae8SAndreas Gohr /** 875bccfe87SAndreas Gohr * Log page edits actions 8858511ae8SAndreas Gohr */ 89a8acb244SAndreas Gohr public function logedits(Event $event, $param) 90a8acb244SAndreas Gohr { 9158511ae8SAndreas Gohr if ($event->data[3]) return; // no revision 9258511ae8SAndreas Gohr 9358511ae8SAndreas Gohr if (file_exists($event->data[0][0])) { 9458511ae8SAndreas Gohr if ($event->data[0][1] == '') { 9558511ae8SAndreas Gohr $type = 'D'; 9658511ae8SAndreas Gohr } else { 9758511ae8SAndreas Gohr $type = 'E'; 9858511ae8SAndreas Gohr } 9958511ae8SAndreas Gohr } else { 10058511ae8SAndreas Gohr $type = 'C'; 10158511ae8SAndreas Gohr } 1021664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 10358511ae8SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 104*aab59130SAndreas Gohr $hlp->Logger()->logEdit(cleanID($event->data[1] . ':' . $event->data[2]), $type); 10558511ae8SAndreas Gohr } 1065bccfe87SAndreas Gohr 1075bccfe87SAndreas Gohr /** 1085bccfe87SAndreas Gohr * Log internal search 1095bccfe87SAndreas Gohr */ 110a8acb244SAndreas Gohr public function logsearch(Event $event, $param) 111a8acb244SAndreas Gohr { 1121664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 1135bccfe87SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 114*aab59130SAndreas Gohr $hlp->Logger()->logSearch('', $event->data['query'], $event->data['highlight'], 'dokuwiki'); 1155bccfe87SAndreas Gohr } 116b5e880bdSAndreas Gohr 117b5e880bdSAndreas Gohr /** 118b5e880bdSAndreas Gohr * Log login/logouts 119b5e880bdSAndreas Gohr */ 120a8acb244SAndreas Gohr public function loglogins(Event $event, $param) 121a8acb244SAndreas Gohr { 122b5e880bdSAndreas Gohr $type = ''; 123b5e880bdSAndreas Gohr $act = $this->_act_clean($event->data); 124b5e880bdSAndreas Gohr if ($act == 'logout') { 125b5e880bdSAndreas Gohr $type = 'o'; 126b5e880bdSAndreas Gohr } elseif ($_SERVER['REMOTE_USER'] && $act == 'login') { 127b5e880bdSAndreas Gohr if ($_REQUEST['r']) { 128b5e880bdSAndreas Gohr $type = 'p'; 129b5e880bdSAndreas Gohr } else { 130b5e880bdSAndreas Gohr $type = 'l'; 131b5e880bdSAndreas Gohr } 132b5e880bdSAndreas Gohr } elseif ($_REQUEST['u'] && !$_REQUEST['http_credentials'] && !$_SERVER['REMOTE_USER']) { 133b5e880bdSAndreas Gohr $type = 'f'; 134b5e880bdSAndreas Gohr } 135b5e880bdSAndreas Gohr if (!$type) return; 136b5e880bdSAndreas Gohr 1371664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 138b5e880bdSAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 139*aab59130SAndreas Gohr $hlp->Logger()->logLogin($type); 14014d99ec0SAndreas Gohr } 14114d99ec0SAndreas Gohr 142535aeea1SAndreas Gohr /** 143535aeea1SAndreas Gohr * Log user creations 144535aeea1SAndreas Gohr */ 145a8acb244SAndreas Gohr public function logregistration(Event $event, $param) 146a8acb244SAndreas Gohr { 147535aeea1SAndreas Gohr if ($event->data['type'] == 'create') { 1481664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 149535aeea1SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 150*aab59130SAndreas Gohr $hlp->Logger()->logLogin('C', $event->data['params'][0]); 151535aeea1SAndreas Gohr } 152535aeea1SAndreas Gohr } 153b5e880bdSAndreas Gohr 154b5e880bdSAndreas Gohr /** 1551664ba1dSAndreas Gohr * Log media access 1561664ba1dSAndreas Gohr */ 157a8acb244SAndreas Gohr public function logmedia(Event $event, $param) 158a8acb244SAndreas Gohr { 1591664ba1dSAndreas Gohr if ($event->data['status'] < 200) return; 1601664ba1dSAndreas Gohr if ($event->data['status'] >= 400) return; 1611664ba1dSAndreas Gohr if (preg_match('/^\w+:\/\//', $event->data['media'])) return; 1621664ba1dSAndreas Gohr 1631664ba1dSAndreas Gohr // no size for redirect/not modified 1641664ba1dSAndreas Gohr if ($event->data['status'] >= 300) { 1651664ba1dSAndreas Gohr $size = 0; 1661664ba1dSAndreas Gohr } else { 16717978b38SAndreas Gohr $size = @filesize($event->data['file']); 1681664ba1dSAndreas Gohr } 1691664ba1dSAndreas Gohr 1701664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 1711664ba1dSAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 172*aab59130SAndreas Gohr $hlp->Logger()->logMedia( 1731664ba1dSAndreas Gohr $event->data['media'], 1741664ba1dSAndreas Gohr $event->data['mime'], 1751664ba1dSAndreas Gohr !$event->data['download'], 1761664ba1dSAndreas Gohr $size 1771664ba1dSAndreas Gohr ); 1781664ba1dSAndreas Gohr } 1791664ba1dSAndreas Gohr 1801664ba1dSAndreas Gohr /** 181cae4a1c5SAndreas Gohr * Log the daily page and media counts for the history 182cae4a1c5SAndreas Gohr */ 183a8acb244SAndreas Gohr public function loghistory(Event $event, $param) 184a8acb244SAndreas Gohr { 185cae4a1c5SAndreas Gohr echo 'Plugin Statistics: started' . DOKU_LF; 186cae4a1c5SAndreas Gohr 187cae4a1c5SAndreas Gohr /** @var helper_plugin_statistics $hlp */ 188cae4a1c5SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 189cae4a1c5SAndreas Gohr 190cae4a1c5SAndreas Gohr // check if a history was gathered already today 191cae4a1c5SAndreas Gohr $sql = "SELECT `info` FROM " . $hlp->prefix . "history WHERE `dt` = DATE(NOW())"; 192cae4a1c5SAndreas Gohr $result = $hlp->runSQL($sql); 193cae4a1c5SAndreas Gohr if (is_null($result)) { 194cae4a1c5SAndreas Gohr global $MSG; 195cae4a1c5SAndreas Gohr print_r($MSG); 196cae4a1c5SAndreas Gohr } 197cae4a1c5SAndreas Gohr 198cae4a1c5SAndreas Gohr $page_ran = false; 199cae4a1c5SAndreas Gohr $media_ran = false; 200cae4a1c5SAndreas Gohr foreach ($result as $row) { 201cae4a1c5SAndreas Gohr if ($row['info'] == 'page_count') $page_ran = true; 202cae4a1c5SAndreas Gohr if ($row['info'] == 'media_count') $media_ran = true; 203cae4a1c5SAndreas Gohr } 204cae4a1c5SAndreas Gohr 205cae4a1c5SAndreas Gohr if ($page_ran && $media_ran) { 206cae4a1c5SAndreas Gohr echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF; 207cae4a1c5SAndreas Gohr return; 208cae4a1c5SAndreas Gohr } 209cae4a1c5SAndreas Gohr 210cae4a1c5SAndreas Gohr $event->stopPropagation(); 211cae4a1c5SAndreas Gohr $event->preventDefault(); 212cae4a1c5SAndreas Gohr 213cae4a1c5SAndreas Gohr if ($page_ran) { 214cae4a1c5SAndreas Gohr echo 'Plugin Statistics: logging media' . DOKU_LF; 215*aab59130SAndreas Gohr $hlp->Logger()->logHistoryMedia(); 216cae4a1c5SAndreas Gohr } else { 217cae4a1c5SAndreas Gohr echo 'Plugin Statistics: logging pages' . DOKU_LF; 218*aab59130SAndreas Gohr $hlp->Logger()->logHistoryPages(); 219cae4a1c5SAndreas Gohr } 220cae4a1c5SAndreas Gohr echo 'Plugin Statistics: finished' . DOKU_LF; 221cae4a1c5SAndreas Gohr } 222cae4a1c5SAndreas Gohr 223cae4a1c5SAndreas Gohr /** 224b5e880bdSAndreas Gohr * Pre-Sanitize the action command 225b5e880bdSAndreas Gohr * 226b5e880bdSAndreas Gohr * Similar to act_clean in action.php but simplified and without 227b5e880bdSAndreas Gohr * error messages 228b5e880bdSAndreas Gohr */ 229a8acb244SAndreas Gohr public function _act_clean($act) 230a8acb244SAndreas Gohr { 231b5e880bdSAndreas Gohr // check if the action was given as array key 232b5e880bdSAndreas Gohr if (is_array($act)) { 233a8acb244SAndreas Gohr [$act] = array_keys($act); 234b5e880bdSAndreas Gohr } 235b5e880bdSAndreas Gohr 236b5e880bdSAndreas Gohr //remove all bad chars 237b5e880bdSAndreas Gohr $act = strtolower($act); 238b5e880bdSAndreas Gohr $act = preg_replace('/[^a-z_]+/', '', $act); 239b5e880bdSAndreas Gohr 240b5e880bdSAndreas Gohr return $act; 241b5e880bdSAndreas Gohr } 242b5e880bdSAndreas Gohr} 243