xref: /plugin/statistics/action.php (revision ed6e7cc190e783aad31b8f8e2132fa503d26a99a)
114d99ec0SAndreas Gohr<?php
2a8acb244SAndreas Gohr
3a8acb244SAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4a8acb244SAndreas Gohruse dokuwiki\Extension\EventHandler;
5a8acb244SAndreas Gohruse dokuwiki\Extension\Event;
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 */
1214d99ec0SAndreas Gohr
1314d99ec0SAndreas Gohr// must be run within Dokuwiki
1414d99ec0SAndreas Gohrif (!defined('DOKU_INC')) die();
1514d99ec0SAndreas Gohr
1614d99ec0SAndreas Gohrif (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
1714d99ec0SAndreas Gohrrequire_once(DOKU_PLUGIN . 'action.php');
1814d99ec0SAndreas Gohr
19a8acb244SAndreas Gohrclass action_plugin_statistics extends ActionPlugin
20a8acb244SAndreas Gohr{
2114d99ec0SAndreas Gohr    /**
2214d99ec0SAndreas Gohr     * register the eventhandlers and initialize some options
2314d99ec0SAndreas Gohr     */
24a8acb244SAndreas Gohr    public function register(EventHandler $controller)
25a8acb244SAndreas Gohr    {
26eabe0d07SAndreas Gohr        global $JSINFO;
27eabe0d07SAndreas Gohr        global $ACT;
28eabe0d07SAndreas Gohr        $JSINFO['act'] = $ACT;
2914d99ec0SAndreas Gohr
302257e39bSAndreas Gohr        $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'logedits', []);
312257e39bSAndreas Gohr        $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'logsearch', []);
322257e39bSAndreas Gohr        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'loglogins', []);
332257e39bSAndreas Gohr        $controller->register_hook('AUTH_USER_CHANGE', 'AFTER', $this, 'logregistration', []);
342257e39bSAndreas Gohr        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'logmedia', []);
352257e39bSAndreas Gohr        $controller->register_hook('INDEXER_TASKS_RUN', 'AFTER', $this, 'loghistory', []);
3614d99ec0SAndreas Gohr    }
3714d99ec0SAndreas Gohr
3814d99ec0SAndreas Gohr    /**
3914d99ec0SAndreas Gohr     * @fixme call this in the webbug call
4014d99ec0SAndreas Gohr     */
41a8acb244SAndreas Gohr    public function putpixel()
42a8acb244SAndreas Gohr    {
43*ed6e7cc1SAndreas Gohr (aider)        global $ID, $INPUT;
4414d99ec0SAndreas Gohr        $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) .
45*ed6e7cc1SAndreas Gohr (aider)            '&amp;r=' . rawurlencode($INPUT->server->str('HTTP_REFERER')) . '&rnd=' . time();
4614d99ec0SAndreas Gohr
472257e39bSAndreas Gohr        echo '<noscript><img alt="" src="' . $url . '" width="1" height="1" /></noscript>';
4814d99ec0SAndreas Gohr    }
4958511ae8SAndreas Gohr
5058511ae8SAndreas Gohr    /**
515bccfe87SAndreas Gohr     * Log page edits actions
5258511ae8SAndreas Gohr     */
53a8acb244SAndreas Gohr    public function logedits(Event $event, $param)
54a8acb244SAndreas Gohr    {
5558511ae8SAndreas Gohr        if ($event->data[3]) return; // no revision
5658511ae8SAndreas Gohr
5758511ae8SAndreas Gohr        if (file_exists($event->data[0][0])) {
5858511ae8SAndreas Gohr            if ($event->data[0][1] == '') {
5958511ae8SAndreas Gohr                $type = 'D';
6058511ae8SAndreas Gohr            } else {
6158511ae8SAndreas Gohr                $type = 'E';
6258511ae8SAndreas Gohr            }
6358511ae8SAndreas Gohr        } else {
6458511ae8SAndreas Gohr            $type = 'C';
6558511ae8SAndreas Gohr        }
661664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
6758511ae8SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
68aab59130SAndreas Gohr        $hlp->Logger()->logEdit(cleanID($event->data[1] . ':' . $event->data[2]), $type);
6958511ae8SAndreas Gohr    }
705bccfe87SAndreas Gohr
715bccfe87SAndreas Gohr    /**
725bccfe87SAndreas Gohr     * Log internal search
735bccfe87SAndreas Gohr     */
74a8acb244SAndreas Gohr    public function logsearch(Event $event, $param)
75a8acb244SAndreas Gohr    {
761664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
775bccfe87SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
78aab59130SAndreas Gohr        $hlp->Logger()->logSearch('', $event->data['query'], $event->data['highlight'], 'dokuwiki');
795bccfe87SAndreas Gohr    }
80b5e880bdSAndreas Gohr
81b5e880bdSAndreas Gohr    /**
82b5e880bdSAndreas Gohr     * Log login/logouts
83b5e880bdSAndreas Gohr     */
84a8acb244SAndreas Gohr    public function loglogins(Event $event, $param)
85a8acb244SAndreas Gohr    {
86*ed6e7cc1SAndreas Gohr (aider)        global $INPUT;
87*ed6e7cc1SAndreas Gohr (aider)
88b5e880bdSAndreas Gohr        $type = '';
89b5e880bdSAndreas Gohr        $act  = $this->_act_clean($event->data);
90b5e880bdSAndreas Gohr        if ($act == 'logout') {
91b5e880bdSAndreas Gohr            $type = 'o';
92*ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->server->str('REMOTE_USER') && $act == 'login') {
93*ed6e7cc1SAndreas Gohr (aider)            if ($INPUT->str('r')) {
94b5e880bdSAndreas Gohr                $type = 'p';
95b5e880bdSAndreas Gohr            } else {
96b5e880bdSAndreas Gohr                $type = 'l';
97b5e880bdSAndreas Gohr            }
98*ed6e7cc1SAndreas Gohr (aider)        } elseif ($INPUT->str('u') && !$INPUT->str('http_credentials') && !$INPUT->server->str('REMOTE_USER')) {
99b5e880bdSAndreas Gohr            $type = 'f';
100b5e880bdSAndreas Gohr        }
101b5e880bdSAndreas Gohr        if (!$type) return;
102b5e880bdSAndreas Gohr
1031664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
104b5e880bdSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
105aab59130SAndreas Gohr        $hlp->Logger()->logLogin($type);
10614d99ec0SAndreas Gohr    }
10714d99ec0SAndreas Gohr
108535aeea1SAndreas Gohr    /**
109535aeea1SAndreas Gohr     * Log user creations
110535aeea1SAndreas Gohr     */
111a8acb244SAndreas Gohr    public function logregistration(Event $event, $param)
112a8acb244SAndreas Gohr    {
113535aeea1SAndreas Gohr        if ($event->data['type'] == 'create') {
1141664ba1dSAndreas Gohr            /** @var helper_plugin_statistics $hlp */
115535aeea1SAndreas Gohr            $hlp = plugin_load('helper', 'statistics');
116aab59130SAndreas Gohr            $hlp->Logger()->logLogin('C', $event->data['params'][0]);
117535aeea1SAndreas Gohr        }
118535aeea1SAndreas Gohr    }
119b5e880bdSAndreas Gohr
120b5e880bdSAndreas Gohr    /**
1211664ba1dSAndreas Gohr     * Log media access
1221664ba1dSAndreas Gohr     */
123a8acb244SAndreas Gohr    public function logmedia(Event $event, $param)
124a8acb244SAndreas Gohr    {
1251664ba1dSAndreas Gohr        if ($event->data['status'] < 200) return;
1261664ba1dSAndreas Gohr        if ($event->data['status'] >= 400) return;
1271664ba1dSAndreas Gohr        if (preg_match('/^\w+:\/\//', $event->data['media'])) return;
1281664ba1dSAndreas Gohr
1291664ba1dSAndreas Gohr        // no size for redirect/not modified
1301664ba1dSAndreas Gohr        if ($event->data['status'] >= 300) {
1311664ba1dSAndreas Gohr            $size = 0;
1321664ba1dSAndreas Gohr        } else {
13317978b38SAndreas Gohr            $size = @filesize($event->data['file']);
1341664ba1dSAndreas Gohr        }
1351664ba1dSAndreas Gohr
1361664ba1dSAndreas Gohr        /** @var helper_plugin_statistics $hlp */
1371664ba1dSAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
138aab59130SAndreas Gohr        $hlp->Logger()->logMedia(
1391664ba1dSAndreas Gohr            $event->data['media'],
1401664ba1dSAndreas Gohr            $event->data['mime'],
1411664ba1dSAndreas Gohr            !$event->data['download'],
1421664ba1dSAndreas Gohr            $size
1431664ba1dSAndreas Gohr        );
1441664ba1dSAndreas Gohr    }
1451664ba1dSAndreas Gohr
1461664ba1dSAndreas Gohr    /**
147cae4a1c5SAndreas Gohr     * Log the daily page and media counts for the history
148cae4a1c5SAndreas Gohr     */
149a8acb244SAndreas Gohr    public function loghistory(Event $event, $param)
150a8acb244SAndreas Gohr    {
151cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: started' . DOKU_LF;
152cae4a1c5SAndreas Gohr
153cae4a1c5SAndreas Gohr        /** @var helper_plugin_statistics $hlp */
154cae4a1c5SAndreas Gohr        $hlp = plugin_load('helper', 'statistics');
155cae4a1c5SAndreas Gohr
156cae4a1c5SAndreas Gohr        // check if a history was gathered already today
157cae4a1c5SAndreas Gohr        $sql = "SELECT `info` FROM " . $hlp->prefix . "history WHERE `dt` = DATE(NOW())";
158cae4a1c5SAndreas Gohr        $result = $hlp->runSQL($sql);
159cae4a1c5SAndreas Gohr        if (is_null($result)) {
160cae4a1c5SAndreas Gohr            global $MSG;
161cae4a1c5SAndreas Gohr            print_r($MSG);
162cae4a1c5SAndreas Gohr        }
163cae4a1c5SAndreas Gohr
164cae4a1c5SAndreas Gohr        $page_ran  = false;
165cae4a1c5SAndreas Gohr        $media_ran = false;
166cae4a1c5SAndreas Gohr        foreach ($result as $row) {
167cae4a1c5SAndreas Gohr            if ($row['info'] == 'page_count')  $page_ran  = true;
168cae4a1c5SAndreas Gohr            if ($row['info'] == 'media_count') $media_ran = true;
169cae4a1c5SAndreas Gohr        }
170cae4a1c5SAndreas Gohr
171cae4a1c5SAndreas Gohr        if ($page_ran && $media_ran) {
172cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: nothing to do - finished' . DOKU_LF;
173cae4a1c5SAndreas Gohr            return;
174cae4a1c5SAndreas Gohr        }
175cae4a1c5SAndreas Gohr
176cae4a1c5SAndreas Gohr        $event->stopPropagation();
177cae4a1c5SAndreas Gohr        $event->preventDefault();
178cae4a1c5SAndreas Gohr
179cae4a1c5SAndreas Gohr        if ($page_ran) {
180cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging media' . DOKU_LF;
181aab59130SAndreas Gohr            $hlp->Logger()->logHistoryMedia();
182cae4a1c5SAndreas Gohr        } else {
183cae4a1c5SAndreas Gohr            echo 'Plugin Statistics: logging pages' . DOKU_LF;
184aab59130SAndreas Gohr            $hlp->Logger()->logHistoryPages();
185cae4a1c5SAndreas Gohr        }
186cae4a1c5SAndreas Gohr        echo 'Plugin Statistics: finished' . DOKU_LF;
187cae4a1c5SAndreas Gohr    }
188cae4a1c5SAndreas Gohr
189cae4a1c5SAndreas Gohr    /**
190b5e880bdSAndreas Gohr     * Pre-Sanitize the action command
191b5e880bdSAndreas Gohr     *
192b5e880bdSAndreas Gohr     * Similar to act_clean in action.php but simplified and without
193b5e880bdSAndreas Gohr     * error messages
194b5e880bdSAndreas Gohr     */
195a8acb244SAndreas Gohr    public function _act_clean($act)
196a8acb244SAndreas Gohr    {
197b5e880bdSAndreas Gohr        // check if the action was given as array key
198b5e880bdSAndreas Gohr        if (is_array($act)) {
199a8acb244SAndreas Gohr            [$act] = array_keys($act);
200b5e880bdSAndreas Gohr        }
201b5e880bdSAndreas Gohr
202b5e880bdSAndreas Gohr        //remove all bad chars
203b5e880bdSAndreas Gohr        $act = strtolower($act);
204b5e880bdSAndreas Gohr        $act = preg_replace('/[^a-z_]+/', '', $act);
205b5e880bdSAndreas Gohr
206b5e880bdSAndreas Gohr        return $act;
207b5e880bdSAndreas Gohr    }
208b5e880bdSAndreas Gohr}
209