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