-
Notifications
You must be signed in to change notification settings - Fork 26
/
jethrodb.php
262 lines (246 loc) · 7.68 KB
/
jethrodb.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/* Utility class for switching from MDB2 to PDO
* Some of the routines in this class are derived/copied(and adapted) from the MDB2 source.
* See https://pear.php.net/package/MDB2/ for the latest download of MDB2
* The following disclaimer applies to code from MDB2:
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class JethroDB extends PDO
{
/**
* Create a JethroDB object from the details in conf.php and make it a global variable.
* @param type $mode
*/
public static function init($mode = '')
{
$mode = strtoupper($mode);
if ($oldDsn = ifdef($mode . '_DSN')) {
// legacy config
preg_match('|([a-z]+)://([^:]*)(:(.*))?@([A-Za-z0-9\.-]*)(/([0-9a-zA-Z_/\.]*))|', $oldDsn, $matches);
if (!defined('DB_TYPE'))
define('DB_TYPE', $matches[1]);
if (!defined('DB_HOST'))
define('DB_HOST', $matches[5]);
if (!defined('DB_DATABASE'))
define('DB_DATABASE', $matches[7]);
$username = ifdef('DB_USERNAME', $matches[2]);
$password = ifdef('DB_PASSWORD', $matches[4]);
} else {
$username = DB_USERNAME;
$password = DB_PASSWORD;
if ($mode) {
if (strlen($x = ifdef('DB_'.$mode.'_USERNAME'))) {
$username = $x;
}
if (strlen($y = ifdef('DB_'.$mode.'_PASSWORD'))) {
$password = $y;
}
}
}
$port = ifdef('DB_PORT', '');
$type = ifdef('DB_TYPE', 'mysql');
$host = ifdef('DB_HOST', 'localhost');
$dsn = $type . ':host=' . $host . (strlen($port) ? (';port=' . $port) : '') . ';dbname=' . DB_DATABASE . ';charset=utf8';
$GLOBALS['db'] = new JethroDB($dsn, $username, $password);
}
/**
* Construct a JethroDB object
* @param string $dsn PDO dsn
* @param string $username Username
* @param string $password Password
* @param array $options PDO options
*/
public function __construct($dsn, $username, $password, $options = array())
{
if ($options === NULL) {
$options = array();
}
$options[PDO::ATTR_DEFAULT_FETCH_MODE] = PDO::FETCH_ASSOC;
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
try {
$result = parent::__construct($dsn, $username, $password, $options);
} catch (PDOException $e) {
error_log((string)$e);
trigger_error('Could not connect to database - please check for mistakes in your Database configuration in conf.php, and check in MySQL that the database exists and the specified user has been granted access.', E_USER_ERROR);
exit();
}
return $result;
}
/**
* Quote and escape a value ready for use in SQL
* @param string$string
* @param mixed $paramtype
* @return string
*/
public function quote($string, $paramtype = NULL)
{
if ($string === NULL) {
return 'NULL';
} else {
return parent::quote($string, $paramtype);
}
}
/**
* Quote an identifier (eg table or column name) ready for use in SQL
* @param string $field
* @return string
*/
public function quoteIdentifier($field)
{
$parts = explode('.', $field);
foreach (array_keys($parts) as $k) {
$parts[$k] = "`" . str_replace("`", "``", $parts[$k]) . "`";
}
return implode('.', $parts);
}
/**
* Execute the specified query and fetch the values from the first row
* of the result set into an array
*
* @param string $sql Query to execute
* @return array
*/
public function queryRow($sql)
{
$stmnt = self::prepare($sql);
$stmnt->execute();
$result = $stmnt->fetch();
$stmnt->closeCursor();
return $result;
}
/**
* Execute the specified query and fetch the value from a single column of
* each row of the result set into an array
*
* @param $sql Query to execute
* @param $colnum Column index OR column name to include in the result array
* @return array
*/
public function queryCol($sql, $colnum = 0)
{
$stmnt = self::prepare($sql);
$stmnt->execute();
$result = $stmnt->fetchAll(PDO::FETCH_COLUMN, $colnum);
return $result;
}
/**
* Execute the specified query and
* fetch all the rows of the result set into a two dimensional array
*
* @param string $sql Query to execute
* @param array $types Legacy parameter, not used.
* @param int $fetchmode Legacy parameter, not used.
* @param boolean $rekey Whether to use the first col of the query result as the keys of the result array
* @param boolean $force_array Used only when the query returns exactly two columns.
* If true, the values of the returned array will be one-element arrays instead of scalars.
* @param boolean $group If true, the values of the returned array is wrapped in another array.
* If the same key value (in the first column) repeats itself, the values will be appended to this array instead of overwriting the existing values.
* @return array
*/
public function queryAll($sql, $types = null, $fetchmode = null, $rekey = false, $force_array = false, $group = false)
{
$all = array();
$stmnt = self::prepare($sql);
$stmnt->execute();
if (!$rekey) {
$all = $stmnt->fetchAll();
} else {
$row = $stmnt->fetch();
if ($row === false) {
return $all;
} // return an empty array if there is nothing here
$shift_array = $rekey ? false : null;
if (null !== $shift_array) {
$colnum = count($row);
if ($colnum < 2) {
return new Exception('rekey feature requires at least 2 columns');
}
$shift_array = (!$force_array && $colnum == 2);
}
do {
$key = reset($row);
unset($row[key($row)]);
if ($shift_array) {
$row = array_shift($row);
}
if ($group) {
$all[$key][] = $row;
} else {
$all[$key] = $row;
}
} while (($row = $stmnt->fetch()));
}
return $all;
}
/**
* Execute the specified query,
* fetch the value from the first column of the first row of the result set
* and then frees the result set.
*
* @param string $query SQL query to run
* @param string $type
* @param mixed $colnum
* @return mixed field value
*/
public function queryOne($query, $type = null, $colnum = 0)
{
$result = false;
$stmnt = self::prepare($query);
$stmnt->execute();
$row = $stmnt->fetch(PDO::FETCH_NUM);
if ($row) {
$result = $row[$colnum];
}
$stmnt->closeCursor();
return $result;
}
/**
* Return true if the database has any tables in it
* @return boolean
*/
public function hasTables()
{
$stmnt = self::prepare('SHOW TABLES');
$stmnt->execute();
$result = $stmnt->fetchAll(PDO::FETCH_COLUMN, 0);
return !empty($result);
}
/**
* Return true if the database has the getCurrentUserID function in it
* @return boolean
*/
public function hasFunctions()
{
try {
$result = true;
$stmnt = self::prepare('SHOW CREATE FUNCTION getCurrentUserID');
$stmnt->execute();
$res = $stmnt->fetchAll(PDO::FETCH_COLUMN, 0);
$stmnt->closeCursor();
} catch (PDOException $e) {
$result = false;
}
return $result;
}
/**
* Set the current User ID variable in the DB
* @param int $userid
*/
public function setCurrentUserID($userid)
{
try {
$sql = 'SET @current_user_id = ' . $userid;
$result = parent::query($sql);
} catch (PDOException $e) {
trigger_error('Failed to set user id in database', E_USER_ERROR);
}
}
}