xref: /plugin/statistics/action.php (revision 1664ba1d9990a1fe5de8e8510099d5a8448eadbe)
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     */
1914d99ec0SAndreas Gohr    function register(&$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        );
52*1664ba1dSAndreas Gohr        $controller->register_hook(
53*1664ba1dSAndreas Gohr            'FETCH_MEDIA_STATUS',
54*1664ba1dSAndreas Gohr            'BEFORE',
55*1664ba1dSAndreas Gohr            $this,
56*1664ba1dSAndreas Gohr            'logmedia',
57*1664ba1dSAndreas Gohr            array()
58*1664ba1dSAndreas Gohr        );
5914d99ec0SAndreas Gohr    }
6014d99ec0SAndreas Gohr
6114d99ec0SAndreas Gohr    /**
6214d99ec0SAndreas Gohr     * @fixme call this in the webbug call
6314d99ec0SAndreas Gohr     */
6414d99ec0SAndreas Gohr    function putpixel() {
6514d99ec0SAndreas Gohr        global $ID;
6614d99ec0SAndreas Gohr        $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) .
6714d99ec0SAndreas Gohr            '&amp;r=' . rawurlencode($_SERVER['HTTP_REFERER']) . '&rnd=' . time();
6814d99ec0SAndreas Gohr
6914d99ec0SAndreas Gohr        echo '<noscript><img src="' . $url . '" width="1" height="1" /></noscript>';
7014d99ec0SAndreas Gohr    }
7158511ae8SAndreas Gohr
7258511ae8SAndreas Gohr    /**
735bccfe87SAndreas Gohr     * Log page edits actions
7458511ae8SAndreas Gohr     */
7558511ae8SAndreas Gohr    function logedits(&$event, $param) {
7658511ae8SAndreas Gohr        if($event->data[3]) return; // no revision
7758511ae8SAndreas Gohr
7858511ae8SAndreas Gohr        if(file_exists($event->data[0][0])) {
7958511ae8SAndreas Gohr            if($event->data[0][1] == '') {
8058511ae8SAndreas Gohr                $type = 'D';
8158511ae8SAndreas Gohr            } else {
8258511ae8SAndreas Gohr                $type = 'E';
8358511ae8SAndreas Gohr            }
8458511ae8SAndreas Gohr        } else {
8558511ae8SAndreas Gohr            $type = 'C';
8658511ae8SAndreas Gohr        }
87*1664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
8858511ae8SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
8958511ae8SAndreas Gohr        $hlp->Logger()->log_edit(cleanID($event->data[1] . ':' . $event->data[2]), $type);
9058511ae8SAndreas Gohr    }
915bccfe87SAndreas Gohr
925bccfe87SAndreas Gohr    /**
935bccfe87SAndreas Gohr     * Log internal search
945bccfe87SAndreas Gohr     */
955bccfe87SAndreas Gohr    function logsearch(&$event, $param) {
96*1664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
975bccfe87SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
985bccfe87SAndreas Gohr        $hlp->Logger()->log_search('', $event->data['query'], $event->data['highlight'], 'dokuwiki');
995bccfe87SAndreas Gohr    }
100b5e880bdSAndreas Gohr
101b5e880bdSAndreas Gohr    /**
102b5e880bdSAndreas Gohr     * Log login/logouts
103b5e880bdSAndreas Gohr     */
104b5e880bdSAndreas Gohr    function loglogins(&$event, $param) {
105b5e880bdSAndreas Gohr        $type = '';
106b5e880bdSAndreas Gohr        $act  = $this->_act_clean($event->data);
107b5e880bdSAndreas Gohr        if($act == 'logout') {
108b5e880bdSAndreas Gohr            $type = 'o';
109b5e880bdSAndreas Gohr        } elseif($_SERVER['REMOTE_USER'] && $act == 'login') {
110b5e880bdSAndreas Gohr            if($_REQUEST['r']) {
111b5e880bdSAndreas Gohr                $type = 'p';
112b5e880bdSAndreas Gohr            } else {
113b5e880bdSAndreas Gohr                $type = 'l';
114b5e880bdSAndreas Gohr            }
115b5e880bdSAndreas Gohr        } elseif($_REQUEST['u'] && !$_REQUEST['http_credentials'] && !$_SERVER['REMOTE_USER']) {
116b5e880bdSAndreas Gohr            $type = 'f';
117b5e880bdSAndreas Gohr        }
118b5e880bdSAndreas Gohr        if(!$type) return;
119b5e880bdSAndreas Gohr
120*1664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
121b5e880bdSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
122b5e880bdSAndreas Gohr        $hlp->Logger()->log_login($type);
12314d99ec0SAndreas Gohr    }
12414d99ec0SAndreas Gohr
125535aeea1SAndreas Gohr    /**
126535aeea1SAndreas Gohr     * Log user creations
127535aeea1SAndreas Gohr     */
128535aeea1SAndreas Gohr    function logregistration(&$event, $param) {
129535aeea1SAndreas Gohr        if($event->data['type'] == 'create') {
130*1664ba1dSAndreas Gohr            /** @var helper_plugin_statistics $hlp */
131535aeea1SAndreas Gohr            $hlp = plugin_load('helper', 'statistics');
132535aeea1SAndreas Gohr            $hlp->Logger()->log_login('C', $event->data['params'][0]);
133535aeea1SAndreas Gohr        }
134535aeea1SAndreas Gohr    }
135b5e880bdSAndreas Gohr
136b5e880bdSAndreas Gohr    /**
137*1664ba1dSAndreas Gohr     * Log media access
138*1664ba1dSAndreas Gohr     */
139*1664ba1dSAndreas Gohr    function logmedia(&$event, $param) {
140*1664ba1dSAndreas Gohr        if($event->data['status'] < 200) return;
141*1664ba1dSAndreas Gohr        if($event->data['status'] >= 400) return;
142*1664ba1dSAndreas Gohr        if(preg_match('/^\w+:\/\//', $event->data['media'])) return;
143*1664ba1dSAndreas Gohr
144*1664ba1dSAndreas Gohr        // no size for redirect/not modified
145*1664ba1dSAndreas Gohr        if($event->data['status'] >= 300) {
146*1664ba1dSAndreas Gohr            $size = 0;
147*1664ba1dSAndreas Gohr        } else {
148*1664ba1dSAndreas Gohr            $size = filesize($event->data['file']);
149*1664ba1dSAndreas Gohr        }
150*1664ba1dSAndreas Gohr
151*1664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
152*1664ba1dSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
153*1664ba1dSAndreas Gohr        $hlp->Logger()->log_media(
154*1664ba1dSAndreas Gohr            $event->data['media'],
155*1664ba1dSAndreas Gohr            $event->data['mime'],
156*1664ba1dSAndreas Gohr            !$event->data['download'],
157*1664ba1dSAndreas Gohr            $size
158*1664ba1dSAndreas Gohr        );
159*1664ba1dSAndreas Gohr    }
160*1664ba1dSAndreas Gohr
161*1664ba1dSAndreas Gohr    /**
162b5e880bdSAndreas Gohr     * Pre-Sanitize the action command
163b5e880bdSAndreas Gohr     *
164b5e880bdSAndreas Gohr     * Similar to act_clean in action.php but simplified and without
165b5e880bdSAndreas Gohr     * error messages
166b5e880bdSAndreas Gohr     */
167b5e880bdSAndreas Gohr    function _act_clean($act) {
168b5e880bdSAndreas Gohr        // check if the action was given as array key
169b5e880bdSAndreas Gohr        if(is_array($act)) {
170b5e880bdSAndreas Gohr            list($act) = array_keys($act);
171b5e880bdSAndreas Gohr        }
172b5e880bdSAndreas Gohr
173b5e880bdSAndreas Gohr        //remove all bad chars
174b5e880bdSAndreas Gohr        $act = strtolower($act);
175b5e880bdSAndreas Gohr        $act = preg_replace('/[^a-z_]+/', '', $act);
176b5e880bdSAndreas Gohr
177b5e880bdSAndreas Gohr        return $act;
178b5e880bdSAndreas Gohr    }
179b5e880bdSAndreas Gohr}
180