1<?php
2
3/**
4 * DokuWiki Plugin logger (Action Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author Jens Brokfeld <geofreak.de@gmail.com>
8 */
9// must be run within Dokuwiki
10if (!defined('DOKU_INC'))
11    die();
12
13class action_plugin_logger_logging extends DokuWiki_Action_Plugin {
14
15    /**
16     * Registers a callback function for a given event
17     *
18     * @param Doku_Event_Handler $controller DokuWiki's event controller object
19     * @return void
20     */
21    public function register(Doku_Event_Handler $controller) {
22        #Log dokuwiki page requests
23        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_before', array());
24
25        #Log dokuwiki media files requests
26        $controller->register_hook('FETCH_MEDIA_STATUS', 'BEFORE', $this, 'handle_before', array());
27    }
28
29    /**
30     *
31     */
32    function handle_before(&$event, $param) {
33        $this->_log();
34    }
35
36    /**
37     * This function logs the current request to dokuwiki.
38     * @global type $conf
39     */
40    function _log() {
41        global $conf;
42
43        #Logger is enabled
44        if ($this->getConf('enabled') === 1) {
45            #-----------------------------------------------------------------------
46            #
47            #log dataset variable (empty)
48            $log_dataset = "";
49
50            #current timestamp
51            $timestamp = time();
52
53            #-----------------------------------------------------------------------
54            #
55            #Check date option
56            if ($this->getConf('date') === 1) {
57                $log_dataset .= ";" . '"' . date("Y-m-d", $timestamp) . '"';
58            }
59
60            #Check time option
61            if ($this->getConf('time') === 1) {
62                $log_dataset .= ";" . '"' . date("H:i:s", $timestamp) . '"';
63            }
64
65            #Check ip option
66            if ($this->getConf('ip') === 1) {
67                $log_dataset .= ";" . '"' . $_SERVER['REMOTE_ADDR'] . '"';
68            }
69
70            #Check user option
71            if ($this->getConf('user') === 1) {
72                if (isset($_SERVER['REMOTE_USER'])) {
73                    $log_dataset .= ";" . '"' . $_SERVER['REMOTE_USER'] . '"';
74                } else {
75                    $log_dataset .= ";\"\"";
76                }
77            }
78
79            #Check pc name option gethostbyaddr
80            if ($this->getConf('host_name') === 1) {
81                $log_dataset .= ";" . '"' . gethostbyaddr($_SERVER['REMOTE_ADDR']) . '"';
82            }
83
84
85            #Check query string option
86            if ($this->getConf('query_string') === 1) {
87                $log_dataset .= ";" . '"' . $_SERVER['QUERY_STRING'] . '"';
88            }
89
90            #Check user agent option
91            if ($this->getConf('user_agent') === 1) {
92                $log_dataset .= ";" . '"' . $_SERVER['HTTP_USER_AGENT'] . '"';
93            }
94
95
96
97            #-----------------------------------------------------------------------
98            #
99            #Remove the first delimiter (;)
100            $log_dataset = substr($log_dataset, 1);
101
102
103            #Check monthly option
104            if ($this->getConf('monthly') === 1) {
105                $log_filename = date("Y-m", $timestamp) . '.csv';
106            } else {
107                $log_filename = date("Y", $timestamp) . '.csv';
108            }
109
110            $ns_exclude = $this->getConf('ns_exclude');
111            if ($ns_exclude == '') {
112              #-----------------------------------------------------------------------
113              #
114              #Save log dataset
115              io_saveFile($conf['mediadir'] . '/admin/logs/' . $log_filename, "$log_dataset\n", true);
116              #-----------------------------------------------------------------------
117            }else{
118              $nsArray = explode(';',$ns_exclude);
119
120              #Check, if logging is desired
121              $doLogging = true;
122              for ($i=0; $i < sizeof($nsArray); $i++) {
123                $ns = 'id=' . $nsArray[$i];
124                if(strpos($_SERVER['QUERY_STRING'],$ns)===0){
125                  $doLogging = false;
126                  break;
127                }
128              }
129
130              #Save log dataset
131              if($doLogging){
132                io_saveFile($conf['mediadir'] . '/admin/logs/' . $log_filename, "$log_dataset\n", true);
133              }
134
135            }
136
137
138        }
139    }
140
141}
142