1<?php
2
3use dokuwiki\Extension\ActionPlugin;
4use dokuwiki\Extension\Event;
5use dokuwiki\Extension\EventHandler;
6
7/**
8 *
9 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
10 * @author     Andreas Gohr <gohr@cosmocode.de>
11 */
12class action_plugin_statistics extends ActionPlugin
13{
14    /**
15     * register the eventhandlers and initialize some options
16     */
17    public function register(EventHandler $controller)
18    {
19        global $JSINFO;
20        global $ACT;
21        $JSINFO['act'] = $ACT;
22
23        $controller->register_hook('DOKUWIKI_INIT_DONE', 'AFTER', $this, 'initSession', []);
24        // FIXME new save event might be better:
25        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []);
26        $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []);
27        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []);
28        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []);
29        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'retention', []);
30
31        // log registration and login/logout actionsonly when user tracking is enabled
32        if (!$this->getConf('nousers')) {
33            $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []);
34            $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []);
35        }
36    }
37
38    /**
39     * This ensures we have a session for the statistics plugin
40     *
41     * We reset this when the user agent changes or the session is too old
42     * (15 minutes).
43     */
44    public function initSession()
45    {
46        global $INPUT;
47
48        // load session data
49        $session = $_SESSION[DOKU_COOKIE]['statistics'] ?? [];
50
51        // reset if session is too old
52        if (time() - ($session['time'] ?? 0) > 60 * 15) {
53            $session = [];
54        }
55        // reset if user agent changed
56        if ($INPUT->server->str('HTTP_USER_AGENT') != ($session['user_agent'] ?? '')) {
57            $session = [];
58        }
59
60        // update session data
61        $session['time'] = time();
62        $session['user_agent'] = $INPUT->server->str('HTTP_USER_AGENT');
63        $session['uid'] = get_doku_pref('plgstats', bin2hex(random_bytes(16)));
64        if (!isset($session['id'])) {
65            // generate a new session id if not set
66            $session['id'] = bin2hex(random_bytes(16));
67        }
68
69        // store session and cookie data
70        $_SESSION[DOKU_COOKIE]['statistics'] = $session;
71
72        // Workaround for dokuwiki/dokuwiki#4544
73        $old = get_doku_pref('plgstats', false);
74        if ($old !== $session['uid']) {
75            set_doku_pref('plgstats', $session['uid']);
76        }
77    }
78
79    /**
80     * @fixme call this in the webbug call
81     */
82    public function putpixel()
83    {
84        global $ID, $INPUT;
85        $url = DOKU_BASE . 'lib/plugins/statistics/dispatch.php?p=' . rawurlencode($ID) .
86            '&amp;r=' . rawurlencode($INPUT->server->str('HTTP_REFERER')) . '&rnd=' . time();
87
88        echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>';
89    }
90
91    /**
92     * Log page edits actions
93     */
94    public function logedits(Event $event, $param)
95    {
96        if ($event->data[3]) return; // no revision
97
98        if (file_exists($event->data[0][0])) {
99            if ($event->data[0][1] == '') {
100                $type = 'D';
101            } else {
102                $type = 'E';
103            }
104        } else {
105            $type = 'C';
106        }
107        /** @var helper_plugin_statistics $hlp */
108        $hlp = plugin_load('helper', 'statistics');
109        $hlp->getLogger()->logEdit($event->data[1] . ':' . $event->data[2], $type);
110    }
111
112    /**
113     * Log internal search
114     */
115    public function logsearch(Event $event, $param)
116    {
117        /** @var helper_plugin_statistics $hlp */
118        $hlp = plugin_load('helper', 'statistics');
119        $hlp->getLogger()->logSearch($event->data['query'], $event->data['highlight']);
120    }
121
122    /**
123     * Log login/logouts
124     */
125    public function loglogins(Event $event, $param)
126    {
127        global $INPUT;
128
129        $type = '';
130        $act = $this->actClean($event->data);
131        $user = $INPUT->server->str('REMOTE_USER');
132        if ($act == 'logout') {
133            // logout
134            $type = 'o';
135        } elseif ($INPUT->server->str('REMOTE_USER') && $act == 'login') {
136            if ($INPUT->str('r')) {
137                // permanent login
138                $type = 'p';
139            } else {
140                // normal login
141                $type = 'l';
142            }
143        } elseif ($INPUT->str('u') && !$INPUT->str('http_credentials') && !$INPUT->server->str('REMOTE_USER')) {
144            // failed attempt
145            $user = $INPUT->str('u');
146            $type = 'f';
147        }
148        if (!$type) return;
149
150        /** @var helper_plugin_statistics $hlp */
151        $hlp = plugin_load('helper', 'statistics');
152        $hlp->getLogger()->logLogin($type, $user);
153    }
154
155    /**
156     * Log user creations
157     */
158    public function logregistration(Event $event, $param)
159    {
160        if ($event->data['type'] == 'create') {
161            /** @var helper_plugin_statistics $hlp */
162            $hlp = plugin_load('helper', 'statistics');
163            $hlp->getLogger()->logLogin('C', $event->data['params'][0]);
164        }
165    }
166
167    /**
168     * Log media access
169     */
170    public function logmedia(Event $event, $param)
171    {
172        if ($event->data['status'] < 200) return;
173        if ($event->data['status'] >= 400) return;
174        if (preg_match('/^\w+:\/\//', $event->data['media'])) return;
175
176        // no size for redirect/not modified
177        if ($event->data['status'] >= 300) {
178            $size = 0;
179        } else {
180            $size = @filesize($event->data['file']);
181        }
182
183        /** @var helper_plugin_statistics $hlp */
184        $hlp = plugin_load('helper', 'statistics');
185        $hlp->getLogger()->logMedia(
186            $event->data['media'],
187            $event->data['mime'],
188            !$event->data['download'],
189            $size
190        );
191    }
192
193    /**
194     * Log the daily page and media counts for the history
195     */
196    public function loghistory(Event $event, $param)
197    {
198        echo 'Plugin Statistics: started' . DOKU_LF;
199
200        /** @var helper_plugin_statistics $hlp */
201        $hlp = plugin_load('helper', 'statistics');
202        $db = $hlp->getDB();
203
204        // check if a history was gathered already today
205        $result = $db->queryAll(
206            "SELECT info FROM history WHERE date(dt) = date('now')"
207        );
208
209        $page_ran = false;
210        $media_ran = false;
211        foreach ($result as $row) {
212            if ($row['info'] == 'page_count') $page_ran = true;
213            if ($row['info'] == 'media_count') $media_ran = true;
214        }
215
216        if ($page_ran && $media_ran) {
217            echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF;
218            return;
219        }
220
221        $event->stopPropagation();
222        $event->preventDefault();
223
224        if ($page_ran) {
225            echo 'Plugin Statistics: logging media' . DOKU_LF;
226            $hlp->getLogger()->logHistoryMedia();
227        } else {
228            echo 'Plugin Statistics: logging pages' . DOKU_LF;
229            $hlp->getLogger()->logHistoryPages();
230        }
231        echo 'Plugin Statistics: finished' . DOKU_LF;
232    }
233
234    /**
235     * Prune old data
236     *
237     * This is run once a day and removes all data older than the configured
238     * retention time.
239     */
240    public function retention(Event $event, $param)
241    {
242        $retention = (int)$this->getConf('retention');
243        if ($retention <= 0) return;
244        // pruning is only done once a day
245        $touch = getCacheName('statistics_retention', '.statistics-retention');
246        if (file_exists($touch) && time() - filemtime($touch) < 24 * 3600) {
247            return;
248        }
249
250        $event->stopPropagation();
251        $event->preventDefault();
252
253        // these are the tables to be pruned
254        $tables = [
255            'edits',
256            'history',
257            'iplocation',
258            'logins',
259            'media',
260            'outlinks',
261            'pageviews',
262            'referers',
263            'search',
264            'sessions',
265        ];
266
267        /** @var helper_plugin_statistics $hlp */
268        $hlp = plugin_load('helper', 'statistics');
269        $db = $hlp->getDB();
270
271        $db->getPdo()->beginTransaction();
272        foreach ($tables as $table) {
273            echo "Plugin Statistics: pruning $table" . DOKU_LF;
274            $db->exec(
275                "DELETE FROM $table WHERE dt < datetime('now', '-$retention days')"
276            );
277        }
278        $db->getPdo()->commit();
279
280        echo "Plugin Statistics: Optimizing" . DOKU_LF;
281        $db->exec('VACUUM');
282
283        // touch the retention file to prevent multiple runs
284        io_saveFile($touch, dformat());
285    }
286
287    /**
288     * Pre-Sanitize the action command
289     *
290     * Similar to act_clean in action.php but simplified and without
291     * error messages
292     */
293    protected function actClean($act)
294    {
295        // check if the action was given as array key
296        if (is_array($act)) {
297            [$act] = array_keys($act);
298        }
299
300        //remove all bad chars
301        $act = strtolower($act);
302        $act = preg_replace('/[^a-z_]+/', '', $act);
303
304        return $act;
305    }
306}
307