xref: /plugin/statistics/action.php (revision d550a4ad772c00d30c3bead8fc22362f3d1bec7a)
114d99ec0SAndreas Gohr<?php
2a8acb244SAndreas Gohr
3a8acb244SAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4a8acb244SAndreas Gohruse dokuwiki\Extension\Event;
504928db4SAndreas Gohruse dokuwiki\Extension\EventHandler;
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 */
12a8acb244SAndreas Gohrclass action_plugin_statistics extends ActionPlugin
13a8acb244SAndreas Gohr{
1414d99ec0SAndreas Gohr    /**
1514d99ec0SAndreas Gohr     * register the eventhandlers and initialize some options
1614d99ec0SAndreas Gohr     */
17a8acb244SAndreas Gohr    public function register(EventHandler $controller)
18a8acb244SAndreas Gohr    {
19eabe0d07SAndreas Gohr        global $JSINFO;
20eabe0d07SAndreas Gohr        global $ACT;
21eabe0d07SAndreas Gohr        $JSINFO['act'] = $ACT;
2214d99ec0SAndreas Gohr
2304928db4SAndreas Gohr        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'initSession', []);
2402aa9b73SAndreas Gohr        // FIXME new save event might be better:
252257e39bSAndreas Gohr        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []);
262257e39bSAndreas Gohr        $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []);
272257e39bSAndreas Gohr        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []);
282257e39bSAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []);
29*d550a4adSAndreas Gohr
30*d550a4adSAndreas Gohr        // log registration and login/logout actionsonly when user tracking is enabled
31*d550a4adSAndreas Gohr        if(!$this->getConf('nousers')) {
32*d550a4adSAndreas Gohr            $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []);
33*d550a4adSAndreas Gohr            $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []);
34*d550a4adSAndreas Gohr        }
3514d99ec0SAndreas Gohr    }
3614d99ec0SAndreas Gohr
3714d99ec0SAndreas Gohr    /**
3804928db4SAndreas Gohr     * This ensures we have a session for the statistics plugin
3904928db4SAndreas Gohr     *
4004928db4SAndreas Gohr     * We reset this when the user agent changes or the session is too old
4104928db4SAndreas Gohr     * (15 minutes).
4204928db4SAndreas Gohr     */
4304928db4SAndreas Gohr    public function initSession()
4404928db4SAndreas Gohr    {
4504928db4SAndreas Gohr        global $INPUT;
4604928db4SAndreas Gohr
4704928db4SAndreas Gohr        // load session data
4804928db4SAndreas Gohr        if (isset($_SESSION[DOKU_COOKIE]['statistics'])) {
4904928db4SAndreas Gohr            $session = $_SESSION[DOKU_COOKIE]['statistics'];
5004928db4SAndreas Gohr        } else {
5104928db4SAndreas Gohr            $session = [];
5204928db4SAndreas Gohr        }
5304928db4SAndreas Gohr        // reset if session is too old
5404928db4SAndreas Gohr        if (time() - ($session['time'] ?? 0) > 60 * 15) {
5504928db4SAndreas Gohr            $session = [];
5604928db4SAndreas Gohr        }
5704928db4SAndreas Gohr        // reset if user agent changed
5804928db4SAndreas Gohr        if ($INPUT->server->str('HTTP_USER_AGENT') != ($session['user_agent'] ?? '')) {
5904928db4SAndreas Gohr            $session = [];
6004928db4SAndreas Gohr        }
6104928db4SAndreas Gohr
6204928db4SAndreas Gohr        // update session data
6304928db4SAndreas Gohr        $session['time'] = time();
6404928db4SAndreas Gohr        $session['user_agent'] = $INPUT->server->str('HTTP_USER_AGENT');
6504928db4SAndreas Gohr        $session['uid'] = get_doku_pref('plgstats', bin2hex(random_bytes(16)));
6604928db4SAndreas Gohr        if (!isset($session['id'])) {
6704928db4SAndreas Gohr            // generate a new session id if not set
6804928db4SAndreas Gohr            $session['id'] = bin2hex(random_bytes(16));
6904928db4SAndreas Gohr        }
7004928db4SAndreas Gohr
7104928db4SAndreas Gohr        // store session and cookie data
7204928db4SAndreas Gohr        $_SESSION[DOKU_COOKIE]['statistics'] = $session;
7304928db4SAndreas Gohr        set_doku_pref('plgstats', $session['uid']);
7404928db4SAndreas Gohr    }
7504928db4SAndreas Gohr
7604928db4SAndreas Gohr    /**
7714d99ec0SAndreas Gohr     * @fixme call this in the webbug call
7814d99ec0SAndreas Gohr     */
79a8acb244SAndreas Gohr    public function putpixel()
80a8acb244SAndreas Gohr    {
81ed6e7cc1SAndreas Gohr (aider)        global $ID, $INPUT;
8214d99ec0SAndreas Gohr        $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) .
83ed6e7cc1SAndreas Gohr (aider)            '&amp;r=' . rawurlencode($INPUT->server->str('HTTP_REFERER')) . '&rnd=' . time();
8414d99ec0SAndreas Gohr
852257e39bSAndreas Gohr        echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>';
8614d99ec0SAndreas Gohr    }
8758511ae8SAndreas Gohr
8858511ae8SAndreas Gohr    /**
895bccfe87SAndreas Gohr     * Log page edits actions
9058511ae8SAndreas Gohr     */
91a8acb244SAndreas Gohr    public function logedits(Event $event, $param)
92a8acb244SAndreas Gohr    {
9358511ae8SAndreas Gohr        if ($event->data[3]) return; // no revision
9458511ae8SAndreas Gohr
9558511ae8SAndreas Gohr        if (file_exists($event->data[0][0])) {
9658511ae8SAndreas Gohr            if ($event->data[0][1] == '') {
9758511ae8SAndreas Gohr                $type = 'D';
9858511ae8SAndreas Gohr            } else {
9958511ae8SAndreas Gohr                $type = 'E';
10058511ae8SAndreas Gohr            }
10158511ae8SAndreas Gohr        } else {
10258511ae8SAndreas Gohr            $type = 'C';
10358511ae8SAndreas Gohr        }
1041664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
10558511ae8SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
10602aa9b73SAndreas Gohr        $hlp->getLogger()->logEdit($event->data[1] . ':' . $event->data[2], $type);
10758511ae8SAndreas Gohr    }
1085bccfe87SAndreas Gohr
1095bccfe87SAndreas Gohr    /**
1105bccfe87SAndreas Gohr     * Log internal search
1115bccfe87SAndreas Gohr     */
112a8acb244SAndreas Gohr    public function logsearch(Event $event, $param)
113a8acb244SAndreas Gohr    {
1141664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
1155bccfe87SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
11602aa9b73SAndreas Gohr        $hlp->getLogger()->logSearch($event->data['query'], $event->data['highlight']);
1175bccfe87SAndreas Gohr    }
118b5e880bdSAndreas Gohr
119b5e880bdSAndreas Gohr    /**
120b5e880bdSAndreas Gohr     * Log login/logouts
121b5e880bdSAndreas Gohr     */
122a8acb244SAndreas Gohr    public function loglogins(Event $event, $param)
123a8acb244SAndreas Gohr    {
124ed6e7cc1SAndreas Gohr (aider)        global $INPUT;
125ed6e7cc1SAndreas Gohr (aider)
126b5e880bdSAndreas Gohr        $type = '';
127a5dadbc1SAndreas Gohr        $act = $this->actClean($event->data);
128af93d154SAndreas Gohr        $user = $INPUT->server->str('REMOTE_USER');
129b5e880bdSAndreas Gohr        if ($act == 'logout') {
130af93d154SAndreas Gohr            // logout
131b5e880bdSAndreas Gohr            $type = 'o';
132ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->server->str('REMOTE_USER') && $act == 'login') {
133ed6e7cc1SAndreas Gohr (aider)            if ($INPUT->str('r')) {
134af93d154SAndreas Gohr                // permanent login
135b5e880bdSAndreas Gohr                $type = 'p';
136b5e880bdSAndreas Gohr            } else {
137af93d154SAndreas Gohr                // normal login
138b5e880bdSAndreas Gohr                $type = 'l';
139b5e880bdSAndreas Gohr            }
140ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->str('u') && !$INPUT->str('http_credentials') && !$INPUT->server->str('REMOTE_USER')) {
141af93d154SAndreas Gohr            // failed attempt
142af93d154SAndreas Gohr            $user = $INPUT->str('u');
143b5e880bdSAndreas Gohr            $type = 'f';
144b5e880bdSAndreas Gohr        }
145b5e880bdSAndreas Gohr        if (!$type) return;
146b5e880bdSAndreas Gohr
1471664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
148b5e880bdSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
149af93d154SAndreas Gohr        $hlp->getLogger()->logLogin($type, $user);
15014d99ec0SAndreas Gohr    }
15114d99ec0SAndreas Gohr
152535aeea1SAndreas Gohr    /**
153535aeea1SAndreas Gohr     * Log user creations
154535aeea1SAndreas Gohr     */
155a8acb244SAndreas Gohr    public function logregistration(Event $event, $param)
156a8acb244SAndreas Gohr    {
157535aeea1SAndreas Gohr        if ($event->data['type'] == 'create') {
1581664ba1dSAndreas Gohr            /** @var helper_plugin_statistics $hlp */
159535aeea1SAndreas Gohr            $hlp = plugin_load('helper', 'statistics');
160211caa5dSAndreas Gohr            $hlp->getLogger()->logLogin('C', $event->data['params'][0]);
161535aeea1SAndreas Gohr        }
162535aeea1SAndreas Gohr    }
163b5e880bdSAndreas Gohr
164b5e880bdSAndreas Gohr    /**
1651664ba1dSAndreas Gohr     * Log media access
1661664ba1dSAndreas Gohr     */
167a8acb244SAndreas Gohr    public function logmedia(Event $event, $param)
168a8acb244SAndreas Gohr    {
1691664ba1dSAndreas Gohr        if ($event->data['status'] < 200) return;
1701664ba1dSAndreas Gohr        if ($event->data['status'] >= 400) return;
1711664ba1dSAndreas Gohr        if (preg_match('/^\w+:\/\//', $event->data['media'])) return;
1721664ba1dSAndreas Gohr
1731664ba1dSAndreas Gohr        // no size for redirect/not modified
1741664ba1dSAndreas Gohr        if ($event->data['status'] >= 300) {
1751664ba1dSAndreas Gohr            $size = 0;
1761664ba1dSAndreas Gohr        } else {
17717978b38SAndreas Gohr            $size = @filesize($event->data['file']);
1781664ba1dSAndreas Gohr        }
1791664ba1dSAndreas Gohr
1801664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
1811664ba1dSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
182211caa5dSAndreas Gohr        $hlp->getLogger()->logMedia(
1831664ba1dSAndreas Gohr            $event->data['media'],
1841664ba1dSAndreas Gohr            $event->data['mime'],
1851664ba1dSAndreas Gohr            !$event->data['download'],
1861664ba1dSAndreas Gohr            $size
1871664ba1dSAndreas Gohr        );
1881664ba1dSAndreas Gohr    }
1891664ba1dSAndreas Gohr
1901664ba1dSAndreas Gohr    /**
191cae4a1c5SAndreas Gohr     * Log the daily page and media counts for the history
192cae4a1c5SAndreas Gohr     */
193a8acb244SAndreas Gohr    public function loghistory(Event $event, $param)
194a8acb244SAndreas Gohr    {
195cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: started' . DOKU_LF;
196cae4a1c5SAndreas Gohr
197cae4a1c5SAndreas Gohr        /** @var helper_plugin_statistics $hlp */
198cae4a1c5SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
199a9509e05SAndreas Gohr (aider)        $db = $hlp->getDB();
200cae4a1c5SAndreas Gohr
201cae4a1c5SAndreas Gohr        // check if a history was gathered already today
202a9509e05SAndreas Gohr (aider)        $result = $db->queryAll(
20302aa9b73SAndreas Gohr            "SELECT info FROM history WHERE date(dt) = date('now')"
204a9509e05SAndreas Gohr (aider)        );
205cae4a1c5SAndreas Gohr
206cae4a1c5SAndreas Gohr        $page_ran = false;
207cae4a1c5SAndreas Gohr        $media_ran = false;
208cae4a1c5SAndreas Gohr        foreach ($result as $row) {
209cae4a1c5SAndreas Gohr            if ($row['info'] == 'page_count') $page_ran = true;
210cae4a1c5SAndreas Gohr            if ($row['info'] == 'media_count') $media_ran = true;
211cae4a1c5SAndreas Gohr        }
212cae4a1c5SAndreas Gohr
213cae4a1c5SAndreas Gohr        if ($page_ran && $media_ran) {
214cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF;
215cae4a1c5SAndreas Gohr            return;
216cae4a1c5SAndreas Gohr        }
217cae4a1c5SAndreas Gohr
218cae4a1c5SAndreas Gohr        $event->stopPropagation();
219cae4a1c5SAndreas Gohr        $event->preventDefault();
220cae4a1c5SAndreas Gohr
221cae4a1c5SAndreas Gohr        if ($page_ran) {
222cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging media' . DOKU_LF;
223211caa5dSAndreas Gohr            $hlp->getLogger()->logHistoryMedia();
224cae4a1c5SAndreas Gohr        } else {
225cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging pages' . DOKU_LF;
226211caa5dSAndreas Gohr            $hlp->getLogger()->logHistoryPages();
227cae4a1c5SAndreas Gohr        }
228cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: finished' . DOKU_LF;
229cae4a1c5SAndreas Gohr    }
230cae4a1c5SAndreas Gohr
231cae4a1c5SAndreas Gohr    /**
232b5e880bdSAndreas Gohr     * Pre-Sanitize the action command
233b5e880bdSAndreas Gohr     *
234b5e880bdSAndreas Gohr     * Similar to act_clean in action.php but simplified and without
235b5e880bdSAndreas Gohr     * error messages
236b5e880bdSAndreas Gohr     */
237a5dadbc1SAndreas Gohr    protected function actClean($act)
238a8acb244SAndreas Gohr    {
239b5e880bdSAndreas Gohr        // check if the action was given as array key
240b5e880bdSAndreas Gohr        if (is_array($act)) {
241a8acb244SAndreas Gohr            [$act] = array_keys($act);
242b5e880bdSAndreas Gohr        }
243b5e880bdSAndreas Gohr
244b5e880bdSAndreas Gohr        //remove all bad chars
245b5e880bdSAndreas Gohr        $act = strtolower($act);
246b5e880bdSAndreas Gohr        $act = preg_replace('/[^a-z_]+/', '', $act);
247b5e880bdSAndreas Gohr
248b5e880bdSAndreas Gohr        return $act;
249b5e880bdSAndreas Gohr    }
250b5e880bdSAndreas Gohr}
251