xref: /plugin/statistics/helper.php (revision 8dfec278503075336af660ef1e5f3448ce514c13)
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($this->getConf('db_server'),
66                                          $this->getConf('db_user'),
67                                          $this->getConf('db_password'));
68            if(!$this->dblink){
69                msg('DB Error: connection failed',-1);
70                return null;
71            }
72            if(!mysql_select_db($this->getConf('db_database'))) {
73                msg('DB Error: failed to select database',-1);
74                return null;
75            }
76
77            // set utf-8
78            if(!mysql_query('set names utf8', $this->dblink)){
79                msg('DB Error: could not set UTF-8 ('.mysql_error($this->dblink).')',-1);
80                return null;
81            }
82        }
83        return $this->dblink;
84    }
85
86    /**
87     * Simple function to run a DB query
88     */
89    public function runSQL($sql_string) {
90        $link = $this->dbLink();
91
92        $result = mysql_query($sql_string,$link);
93        if(!$result){
94            dbglog('DB Error: '.mysql_error($link).' '.hsc($sql_string),-1);
95            msg('DB Error: '.mysql_error($link).' '.hsc($sql_string),-1);
96            return null;
97        }
98
99        $resultarray = array();
100
101        //mysql_db_query returns 1 on a insert statement -> no need to ask for results
102        if ($result != 1) {
103            for($i=0; $i< mysql_num_rows($result); $i++) {
104                $temparray = mysql_fetch_assoc($result);
105                $resultarray[]=$temparray;
106            }
107            mysql_free_result($result);
108        }
109
110        if (mysql_insert_id($link)) {
111            $resultarray = mysql_insert_id($link); //give back ID on insert
112        }
113
114        return $resultarray;
115    }
116
117
118    /**
119     * Just send a 1x1 pixel blank gif to the browser
120     *
121     * @called from log.php
122     *
123     * @author Andreas Gohr <andi@splitbrain.org>
124     * @author Harry Fuecks <fuecks@gmail.com>
125     */
126    function sendGIF($transparent = true){
127        if($transparent){
128            $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
129        }else{
130            $img = base64_decode('R0lGODdhAQABAIAAAP///////ywAAAAAAQABAAACAkQBADs=');
131        }
132        header('Content-Type: image/gif');
133        header('Content-Length: '.strlen($img));
134        header('Connection: Close');
135        print $img;
136        flush();
137        // Browser should drop connection after this
138        // Thinks it's got the whole image
139    }
140}
141