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