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