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