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