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