xref: /plugin/statistics/action.php (revision 0863c19c7bf8b95c6f464efd1fa1fd2c4f9b4e94)
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    }
53
54    /**
55     * @fixme call this in the webbug call
56     */
57    function putpixel() {
58        global $ID;
59        $url = DOKU_BASE . 'lib/plugins/statistics/log.php?p=' . rawurlencode($ID) .
60            '&amp;r=' . rawurlencode($_SERVER['HTTP_REFERER']) . '&rnd=' . time();
61
62        echo '<noscript><img src="' . $url . '" width="1" height="1" /></noscript>';
63    }
64
65    /**
66     * Log page edits actions
67     */
68    function logedits(&$event, $param) {
69        if($event->data[3]) return; // no revision
70
71        if(file_exists($event->data[0][0])) {
72            if($event->data[0][1] == '') {
73                $type = 'D';
74            } else {
75                $type = 'E';
76            }
77        } else {
78            $type = 'C';
79        }
80        $hlp = plugin_load('helper', 'statistics');
81        $hlp->Logger()->log_edit(cleanID($event->data[1] . ':' . $event->data[2]), $type);
82    }
83
84    /**
85     * Log internal search
86     */
87    function logsearch(&$event, $param) {
88        $hlp = plugin_load('helper', 'statistics');
89        $hlp->Logger()->log_search('', $event->data['query'], $event->data['highlight'], 'dokuwiki');
90    }
91
92    /**
93     * Log login/logouts
94     */
95    function loglogins(&$event, $param) {
96        $type = '';
97        $act  = $this->_act_clean($event->data);
98        if($act == 'logout') {
99            $type = 'o';
100        } elseif($_SERVER['REMOTE_USER'] && $act == 'login') {
101            if($_REQUEST['r']) {
102                $type = 'p';
103            } else {
104                $type = 'l';
105            }
106        } elseif($_REQUEST['u'] && !$_REQUEST['http_credentials'] && !$_SERVER['REMOTE_USER']) {
107            $type = 'f';
108        }
109        if(!$type) return;
110
111        $hlp = plugin_load('helper', 'statistics');
112        $hlp->Logger()->log_login($type);
113    }
114
115    /**
116     * Log user creations
117     */
118    function logregistration(&$event, $param) {
119        if($event->data['type'] == 'create') {
120            $hlp = plugin_load('helper', 'statistics');
121            $hlp->Logger()->log_login('C', $event->data['params'][0]);
122        }
123    }
124
125    /**
126     * Pre-Sanitize the action command
127     *
128     * Similar to act_clean in action.php but simplified and without
129     * error messages
130     */
131    function _act_clean($act) {
132        // check if the action was given as array key
133        if(is_array($act)) {
134            list($act) = array_keys($act);
135        }
136
137        //remove all bad chars
138        $act = strtolower($act);
139        $act = preg_replace('/[^a-z_]+/', '', $act);
140
141        return $act;
142    }
143}
144