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