114d99ec0SAndreas Gohr<?php 214d99ec0SAndreas Gohr/** 314d99ec0SAndreas Gohr * 414d99ec0SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 514d99ec0SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 614d99ec0SAndreas Gohr */ 714d99ec0SAndreas Gohr 814d99ec0SAndreas Gohr// must be run within Dokuwiki 914d99ec0SAndreas Gohrif(!defined('DOKU_INC')) die(); 1014d99ec0SAndreas Gohr 1114d99ec0SAndreas Gohrif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/'); 1214d99ec0SAndreas Gohrrequire_once(DOKU_PLUGIN . 'action.php'); 1314d99ec0SAndreas Gohr 1414d99ec0SAndreas Gohrclass action_plugin_statistics extends DokuWiki_Action_Plugin { 1514d99ec0SAndreas Gohr 1614d99ec0SAndreas Gohr /** 1714d99ec0SAndreas Gohr * register the eventhandlers and initialize some options 1814d99ec0SAndreas Gohr */ 19fba9d47bSAndreas Gohr function register(Doku_Event_Handler $controller) { 20eabe0d07SAndreas Gohr global $JSINFO; 21eabe0d07SAndreas Gohr global $ACT; 22eabe0d07SAndreas Gohr $JSINFO['act'] = $ACT; 2314d99ec0SAndreas Gohr 240863c19cSAndreas Gohr $controller->register_hook( 250863c19cSAndreas Gohr 'IO_WIKIPAGE_WRITE', 2658511ae8SAndreas Gohr 'BEFORE', 2758511ae8SAndreas Gohr $this, 2858511ae8SAndreas Gohr 'logedits', 290863c19cSAndreas Gohr array() 300863c19cSAndreas Gohr ); 310863c19cSAndreas Gohr $controller->register_hook( 320863c19cSAndreas Gohr 'SEARCH_QUERY_FULLPAGE', 335bccfe87SAndreas Gohr 'AFTER', 345bccfe87SAndreas Gohr $this, 355bccfe87SAndreas Gohr 'logsearch', 360863c19cSAndreas Gohr array() 370863c19cSAndreas Gohr ); 380863c19cSAndreas Gohr $controller->register_hook( 390863c19cSAndreas Gohr 'ACTION_ACT_PREPROCESS', 40b5e880bdSAndreas Gohr 'BEFORE', 41b5e880bdSAndreas Gohr $this, 42b5e880bdSAndreas Gohr 'loglogins', 430863c19cSAndreas Gohr array() 440863c19cSAndreas Gohr ); 450863c19cSAndreas Gohr $controller->register_hook( 460863c19cSAndreas Gohr 'AUTH_USER_CHANGE', 47535aeea1SAndreas Gohr 'AFTER', 48535aeea1SAndreas Gohr $this, 49535aeea1SAndreas Gohr 'logregistration', 500863c19cSAndreas Gohr array() 510863c19cSAndreas Gohr ); 521664ba1dSAndreas Gohr $controller->register_hook( 531664ba1dSAndreas Gohr 'FETCH_MEDIA_STATUS', 541664ba1dSAndreas Gohr 'BEFORE', 551664ba1dSAndreas Gohr $this, 561664ba1dSAndreas Gohr 'logmedia', 571664ba1dSAndreas Gohr array() 581664ba1dSAndreas Gohr ); 59cae4a1c5SAndreas Gohr $controller->register_hook( 60cae4a1c5SAndreas Gohr 'INDEXER_TASKS_RUN', 61cae4a1c5SAndreas Gohr 'AFTER', 62cae4a1c5SAndreas Gohr $this, 63cae4a1c5SAndreas Gohr 'loghistory', 64cae4a1c5SAndreas Gohr array() 65cae4a1c5SAndreas Gohr ); 6614d99ec0SAndreas Gohr } 6714d99ec0SAndreas Gohr 6814d99ec0SAndreas Gohr /** 6914d99ec0SAndreas Gohr * @fixme call this in the webbug call 7014d99ec0SAndreas Gohr */ 7114d99ec0SAndreas Gohr function putpixel() { 7214d99ec0SAndreas Gohr global $ID; 7314d99ec0SAndreas Gohr $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) . 7414d99ec0SAndreas Gohr '&r=' . rawurlencode($_SERVER['HTTP_REFERER']) . '&rnd=' . time(); 7514d99ec0SAndreas Gohr 7614d99ec0SAndreas Gohr echo '<noscript><img src="' . $url . '" width="1" height="1" /></noscript>'; 7714d99ec0SAndreas Gohr } 7858511ae8SAndreas Gohr 7958511ae8SAndreas Gohr /** 805bccfe87SAndreas Gohr * Log page edits actions 8158511ae8SAndreas Gohr */ 82cae4a1c5SAndreas Gohr function logedits(Doku_Event $event, $param) { 8358511ae8SAndreas Gohr if($event->data[3]) return; // no revision 8458511ae8SAndreas Gohr 8558511ae8SAndreas Gohr if(file_exists($event->data[0][0])) { 8658511ae8SAndreas Gohr if($event->data[0][1] == '') { 8758511ae8SAndreas Gohr $type = 'D'; 8858511ae8SAndreas Gohr } else { 8958511ae8SAndreas Gohr $type = 'E'; 9058511ae8SAndreas Gohr } 9158511ae8SAndreas Gohr } else { 9258511ae8SAndreas Gohr $type = 'C'; 9358511ae8SAndreas Gohr } 941664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 9558511ae8SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 9658511ae8SAndreas Gohr $hlp->Logger()->log_edit(cleanID($event->data[1] . ':' . $event->data[2]), $type); 9758511ae8SAndreas Gohr } 985bccfe87SAndreas Gohr 995bccfe87SAndreas Gohr /** 1005bccfe87SAndreas Gohr * Log internal search 1015bccfe87SAndreas Gohr */ 102cae4a1c5SAndreas Gohr function logsearch(Doku_Event $event, $param) { 1031664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 1045bccfe87SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 1055bccfe87SAndreas Gohr $hlp->Logger()->log_search('', $event->data['query'], $event->data['highlight'], 'dokuwiki'); 1065bccfe87SAndreas Gohr } 107b5e880bdSAndreas Gohr 108b5e880bdSAndreas Gohr /** 109b5e880bdSAndreas Gohr * Log login/logouts 110b5e880bdSAndreas Gohr */ 111cae4a1c5SAndreas Gohr function loglogins(Doku_Event $event, $param) { 112b5e880bdSAndreas Gohr $type = ''; 113b5e880bdSAndreas Gohr $act = $this->_act_clean($event->data); 114b5e880bdSAndreas Gohr if($act == 'logout') { 115b5e880bdSAndreas Gohr $type = 'o'; 116b5e880bdSAndreas Gohr } elseif($_SERVER['REMOTE_USER'] && $act == 'login') { 117b5e880bdSAndreas Gohr if($_REQUEST['r']) { 118b5e880bdSAndreas Gohr $type = 'p'; 119b5e880bdSAndreas Gohr } else { 120b5e880bdSAndreas Gohr $type = 'l'; 121b5e880bdSAndreas Gohr } 122b5e880bdSAndreas Gohr } elseif($_REQUEST['u'] && !$_REQUEST['http_credentials'] && !$_SERVER['REMOTE_USER']) { 123b5e880bdSAndreas Gohr $type = 'f'; 124b5e880bdSAndreas Gohr } 125b5e880bdSAndreas Gohr if(!$type) return; 126b5e880bdSAndreas Gohr 1271664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 128b5e880bdSAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 129b5e880bdSAndreas Gohr $hlp->Logger()->log_login($type); 13014d99ec0SAndreas Gohr } 13114d99ec0SAndreas Gohr 132535aeea1SAndreas Gohr /** 133535aeea1SAndreas Gohr * Log user creations 134535aeea1SAndreas Gohr */ 135cae4a1c5SAndreas Gohr function logregistration(Doku_Event $event, $param) { 136535aeea1SAndreas Gohr if($event->data['type'] == 'create') { 1371664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 138535aeea1SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 139535aeea1SAndreas Gohr $hlp->Logger()->log_login('C', $event->data['params'][0]); 140535aeea1SAndreas Gohr } 141535aeea1SAndreas Gohr } 142b5e880bdSAndreas Gohr 143b5e880bdSAndreas Gohr /** 1441664ba1dSAndreas Gohr * Log media access 1451664ba1dSAndreas Gohr */ 146cae4a1c5SAndreas Gohr function logmedia(Doku_Event $event, $param) { 1471664ba1dSAndreas Gohr if($event->data['status'] < 200) return; 1481664ba1dSAndreas Gohr if($event->data['status'] >= 400) return; 1491664ba1dSAndreas Gohr if(preg_match('/^\w+:\/\//', $event->data['media'])) return; 1501664ba1dSAndreas Gohr 1511664ba1dSAndreas Gohr // no size for redirect/not modified 1521664ba1dSAndreas Gohr if($event->data['status'] >= 300) { 1531664ba1dSAndreas Gohr $size = 0; 1541664ba1dSAndreas Gohr } else { 155*17978b38SAndreas Gohr $size = @filesize($event->data['file']); 1561664ba1dSAndreas Gohr } 1571664ba1dSAndreas Gohr 1581664ba1dSAndreas Gohr /** @var helper_plugin_statistics $hlp */ 1591664ba1dSAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 1601664ba1dSAndreas Gohr $hlp->Logger()->log_media( 1611664ba1dSAndreas Gohr $event->data['media'], 1621664ba1dSAndreas Gohr $event->data['mime'], 1631664ba1dSAndreas Gohr !$event->data['download'], 1641664ba1dSAndreas Gohr $size 1651664ba1dSAndreas Gohr ); 1661664ba1dSAndreas Gohr } 1671664ba1dSAndreas Gohr 1681664ba1dSAndreas Gohr /** 169cae4a1c5SAndreas Gohr * Log the daily page and media counts for the history 170cae4a1c5SAndreas Gohr */ 171cae4a1c5SAndreas Gohr function loghistory(Doku_Event $event, $param) { 172cae4a1c5SAndreas Gohr echo 'Plugin Statistics: started'.DOKU_LF; 173cae4a1c5SAndreas Gohr 174cae4a1c5SAndreas Gohr /** @var helper_plugin_statistics $hlp */ 175cae4a1c5SAndreas Gohr $hlp = plugin_load('helper', 'statistics'); 176cae4a1c5SAndreas Gohr 177cae4a1c5SAndreas Gohr // check if a history was gathered already today 178cae4a1c5SAndreas Gohr $sql = "SELECT `info` FROM " . $hlp->prefix . "history WHERE `dt` = DATE(NOW())"; 179cae4a1c5SAndreas Gohr $result = $hlp->runSQL($sql); 180cae4a1c5SAndreas Gohr if(is_null($result)) { 181cae4a1c5SAndreas Gohr global $MSG; 182cae4a1c5SAndreas Gohr print_r($MSG); 183cae4a1c5SAndreas Gohr } 184cae4a1c5SAndreas Gohr 185cae4a1c5SAndreas Gohr $page_ran = false; 186cae4a1c5SAndreas Gohr $media_ran = false; 187cae4a1c5SAndreas Gohr foreach($result as $row) { 188cae4a1c5SAndreas Gohr if($row['info'] == 'page_count') $page_ran = true; 189cae4a1c5SAndreas Gohr if($row['info'] == 'media_count') $media_ran = true; 190cae4a1c5SAndreas Gohr } 191cae4a1c5SAndreas Gohr 192cae4a1c5SAndreas Gohr if($page_ran && $media_ran){ 193cae4a1c5SAndreas Gohr echo 'Plugin Statistics: nothing to do - finished'.DOKU_LF; 194cae4a1c5SAndreas Gohr return; 195cae4a1c5SAndreas Gohr } 196cae4a1c5SAndreas Gohr 197cae4a1c5SAndreas Gohr $event->stopPropagation(); 198cae4a1c5SAndreas Gohr $event->preventDefault(); 199cae4a1c5SAndreas Gohr 200cae4a1c5SAndreas Gohr if($page_ran) { 201cae4a1c5SAndreas Gohr echo 'Plugin Statistics: logging media'.DOKU_LF; 202cae4a1c5SAndreas Gohr $hlp->Logger()->log_history_media(); 203cae4a1c5SAndreas Gohr } else { 204cae4a1c5SAndreas Gohr echo 'Plugin Statistics: logging pages'.DOKU_LF; 205cae4a1c5SAndreas Gohr $hlp->Logger()->log_history_pages(); 206cae4a1c5SAndreas Gohr } 207cae4a1c5SAndreas Gohr echo 'Plugin Statistics: finished'.DOKU_LF; 208cae4a1c5SAndreas Gohr } 209cae4a1c5SAndreas Gohr 210cae4a1c5SAndreas Gohr /** 211b5e880bdSAndreas Gohr * Pre-Sanitize the action command 212b5e880bdSAndreas Gohr * 213b5e880bdSAndreas Gohr * Similar to act_clean in action.php but simplified and without 214b5e880bdSAndreas Gohr * error messages 215b5e880bdSAndreas Gohr */ 216b5e880bdSAndreas Gohr function _act_clean($act) { 217b5e880bdSAndreas Gohr // check if the action was given as array key 218b5e880bdSAndreas Gohr if(is_array($act)) { 219b5e880bdSAndreas Gohr list($act) = array_keys($act); 220b5e880bdSAndreas Gohr } 221b5e880bdSAndreas Gohr 222b5e880bdSAndreas Gohr //remove all bad chars 223b5e880bdSAndreas Gohr $act = strtolower($act); 224b5e880bdSAndreas Gohr $act = preg_replace('/[^a-z_]+/', '', $act); 225b5e880bdSAndreas Gohr 226b5e880bdSAndreas Gohr return $act; 227b5e880bdSAndreas Gohr } 228b5e880bdSAndreas Gohr} 229