xref: /plugin/statistics/helper.php (revision a8acb244f011a1b8bd9a22373e14f2819297c46f)
1<?php
2
3/**
4 * Statistics Plugin
5 *
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author  Andreas Gohr <andi@splitbrain.org>
8 */
9
10class helper_plugin_statistics extends Dokuwiki_Plugin
11{
12    private $dblink;
13    public $prefix;
14    private $oQuery;
15    private $oLogger;
16    private $oGraph;
17
18    /**
19     * Constructor
20     */
21    public function __construct()
22    {
23        $this->prefix = $this->getConf('db_prefix');
24    }
25
26    /**
27     * Return an instance of the query class
28     *
29     * @return StatisticsQuery
30     */
31    public function Query()
32    {
33        if (is_null($this->oQuery)) {
34            require __DIR__ . '/inc/StatisticsQuery.class.php';
35            $this->oQuery = new StatisticsQuery($this);
36        }
37        return $this->oQuery;
38    }
39
40    /**
41     * Return an instance of the logger class
42     *
43     * @return StatisticsLogger
44     */
45    public function Logger()
46    {
47        $this->prefix = $this->getConf('db_prefix');
48        if (is_null($this->oLogger)) {
49            require __DIR__ . '/inc/StatisticsLogger.class.php';
50            $this->oLogger = new StatisticsLogger($this);
51        }
52        return $this->oLogger;
53    }
54
55    /**
56     * Return an instance of the Graph class
57     *
58     * @return StatisticsGraph
59     */
60    public function Graph()
61    {
62        $this->prefix = $this->getConf('db_prefix');
63        if (is_null($this->oGraph)) {
64            require __DIR__ . '/inc/StatisticsGraph.class.php';
65            $this->oGraph = new StatisticsGraph($this);
66        }
67        return $this->oGraph;
68    }
69
70    /**
71     * Return a link to the DB, opening the connection if needed
72     */
73    protected function dbLink()
74    {
75        // connect to DB if needed
76        if (!$this->dblink) {
77            if (!$this->getConf('db_server')) return null;
78
79            $this->dblink = mysqli_connect(
80                $this->getConf('db_server'),
81                $this->getConf('db_user'),
82                $this->getConf('db_password')
83            );
84            if (!$this->dblink) {
85                msg('DB Error: connection failed', -1);
86                return null;
87            }
88            if (!mysqli_select_db($this->dblink, $this->getConf('db_database'))) {
89                msg('DB Error: failed to select database', -1);
90                return null;
91            }
92
93            // set utf-8
94            if (!mysqli_query($this->dblink, 'set names utf8')) {
95                msg('DB Error: could not set UTF-8 (' . mysqli_error($this->dblink) . ')', -1);
96                return null;
97            }
98        }
99        return $this->dblink;
100    }
101
102    /**
103     * Simple function to run a DB query
104     */
105    public function runSQL($sql_string)
106    {
107        $link = $this->dbLink();
108        if (!$link) return null;
109
110        $result = mysqli_query($link, $sql_string);
111        if ($result === false) {
112            dbglog('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
113            msg('DB Error: ' . mysqli_error($link) . ' ' . hsc($sql_string), -1);
114            return null;
115        }
116
117        $resultarray = [];
118
119        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
120        if ($result !== true) {
121            for ($i = 0; $i < mysqli_num_rows($result); $i++) {
122                $temparray     = mysqli_fetch_assoc($result);
123                $resultarray[] = $temparray;
124            }
125            mysqli_free_result($result);
126        }
127
128        if (mysqli_insert_id($link)) {
129            $resultarray = mysqli_insert_id($link); //give back ID on insert
130        }
131
132        return $resultarray;
133    }
134
135    /**
136     * Just send a 1x1 pixel blank gif to the browser
137     *
138     * @called from log.php
139     *
140     * @author Andreas Gohr <andi@splitbrain.org>
141     * @author Harry Fuecks <fuecks@gmail.com>
142     */
143    public function sendGIF($transparent = true)
144    {
145        if ($transparent) {
146            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
147        } else {
148            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
149        }
150        header('Content-Type: image/gif');
151        header('Content-Length: ' . strlen($img));
152        header('Connection: Close');
153        echo $img;
154        flush();
155        // Browser should drop connection after this
156        // Thinks it's got the whole image
157    }
158}
159