-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDb.class.php
More file actions
478 lines (430 loc) · 12 KB
/
Db.class.php
File metadata and controls
478 lines (430 loc) · 12 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<?php
/**
* Superclass for all database classes, provides common functionality
*
* @package dblib
* @author Jamie Hurst
* @version 1.3
*/
require_once 'iDb.interface.php';
/**
* Common DB class
*/
abstract class Db implements iDb {
// Config file definition, normally this shouldn't need to be changed
const CONFIG_FILE = 'config.ini';
// Version constants
const VERSION_MAJOR = 1;
const VERSION_MINOR = 2;
const VERSION_REVISION = 3;
// Default configuration options that can be overwritten in config.ini (NOT HERE!)
private $_config = array(
'stripEnabled' => true,
'debug' => false,
'autoClose' => false,
'caching' => true,
'exitOnError' => true,
'getQueries' => false,
'adminEmail' => false,
'tableSeparator' => '|'
);
// Define a quick path variable that will be resolved later
private $_path = '.';
// New singleton instance for getInstance() calls
protected static $_instance = null;
/**
* Constructor
*/
protected function __construct() {
// Resolve the path of this DBlib installation
$includes = get_included_files();
$basepath = realpath('./');
foreach ($includes as $include) {
if (strpos($include, 'Db.class.php')) {
$basepath = realpath(dirname($include));
}
}
$this->_path = $basepath;
unset($includes, $include, $basepath);
// Check the configuration file exists
if (!file_exists($this->_path . '/' . self::CONFIG_FILE)) {
// Throw a warning
trigger_error('Could not open configuration file for DBlib, using default options.', E_USER_WARNING);
} else {
// Parse config file
$config = parse_ini_file($this->_path . '/' . self::CONFIG_FILE);
// Merge the custom configuration with the default
$this->_config = array_merge($this->_config, $config);
}
// Check for magic quotes (This needs to be removed at some point, probably causes more problems than it solves!)
if (get_magic_quotes_gpc()) {
$this->_stripEnabled = false;
}
}
/**
* Set a configuration option
*
* @param string $key Configuration option
* @param mixed $value Option value
* @return Db Object for chaining
* @since 1.3
*/
public function setConfig($key, $value) {
$this->_config[$key] = $value;
return $this;
}
/**
* Get configuration option
*
* @param string $key Config option key
* @return mixed Configuration setting
* @since 1.3
*/
public function getConfig($key) {
return $this->_config[$key];
}
/**
* Set the value of strip enabled
*
* @param mixed $value Value to set
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setStripEnabled($value) {
return $this->setConfig('stripEnabled', $value);
}
/**
* Get strip enabled value
*
* @return mixed strip enabled value
* @deprecated Use getConfig() instead
*/
public function getStripEnabled() {
return $this->getConfig('stripEnabled');
}
/**
* Set the value of debug
*
* @param boolean $value Value to set
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setDebug($value) {
return $this->setConfig('debug', $value);
}
/**
* Get debug value
*
* @return boolean Debug value
* @deprecated Use getConfig() instead
*/
public function getDebug() {
return $this->getConfig('debug');
}
/**
* Set the value of auto close
*
* @param boolean $value Value to set
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setAutoClose($value) {
return $this->setConfig('autoClose', $value);
}
/**
* Get auto close value
*
* @return boolean Auto close value
* @deprecated Use getConfig() instead
*/
public function getAutoClose() {
return $this->getConfig('autoClose');
}
/**
* Set the value of caching
*
* @param boolean $value Value to set
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setCaching($value) {
return $this->setConfig('caching', $value);
}
/**
* Get caching value
*
* @return boolean Caching value
* @deprecated Use getConfig() instead
*/
public function getCaching() {
return $this->getConfig('caching');
}
/**
* Set the value of exit on error
*
* @param boolean $value Value to set
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setExitOnError($value) {
return $this->setConfig('exitOnError', $value);
}
/**
* Get exit on error value
*
* @return boolean Exit on error value
* @deprecated Use getConfig() instead
*/
public function getExitOnError() {
return $this->getConfig('exitOnError');
}
/**
* Set the value of get queries
*
* @param boolean $value Value to set
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setGetQueries($value) {
return $this->setConfig('getQueries', $value);
}
/**
* Get get queries value
*
* @return boolean Get queries value
* @deprecated Use getConfig() instead
*/
public function getGetQueries() {
return $this->getConfig('getQueries');
}
/**
* Set the value of admin email
*
* @param mixed $value Value to set
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setAdminEmail($value) {
return $this->setConfig('adminEmail', $value);
}
/**
* Get admin email value
*
* @return mixed Admin email value
* @deprecated Use getConfig() instead
*/
public function getAdminEmail() {
return $this->getConfig('adminEmail');
}
/**
* Set the table separator
*
* @param string $sep Table separator
* @return Db For chaining
* @deprecated Use setConfig() instead
*/
public function setTableSeparator($sep) {
return $this->setConfig('tableSeparator', $value);
}
/**
* Get the table separator
*
* @return string Table separator
* @since 1.2
* @deprecated Use getConfig() instead
*/
public function getTableSeparator() {
return $this->getConfig('tableSeparator');
}
/**
* Handle any database errors
*
* @param string $error Error context
* @param string $dbError Error given from DB
* @param string $query [Optional] Query from where the error happened
*/
protected function errorDb($error, $dbError = '', $query = '') {
// Only provide detailed output in debug mode
echo '<p class="db_error">';
if ($this->getConfig('debug')) {
echo 'Query error in context "' . $error . '":<br /><strong>'
. $dbError .
'</strong><br /><br />';
if(!empty($query)) {
echo '<em>' . $query . '</em>';
}
} else {
echo 'A database error occurred when performing the last operation. The system administrator has been informed.';
if($this->getConfig('adminEmail')) {
mail($this->_adminEmail, 'DBlib -> DB Error (' . $error . ')', 'A database error occurred in context "' . $error . '".' . "\n\n" . $dbError . "\n\n" . 'Query: ' . $query);
}
}
echo '</p>';
if ($this->getConfig('exitOnError')) {
exit;
}
}
/**
* Prepare a variable to be used in a database query
*
* @param mixed $var Any variable
* @return mixed Variable post-processing
*/
protected function preDb($var) {
// Make sure any null variables are returned as passed
if (is_null($var) || $var === 'NULL') {
return 'NULL';
}
// Use a recursive call if the variable is an array, to make sure it
// is penetrated to the correct depth
if (is_array($var)) {
$newArray = array();
foreach ($var as $key => $value) {
if ($this->getConfig('stripEnabled') && strpos($value, "\'") === false) {
$newArray[$this->escape(html_entity_decode($key))] = $this->preDb($value);
} else {
$newArray[html_entity_decode($key)] = $this->preDb($value);
}
}
return $newArray;
} else {
if($this->getConfig('stripEnabled')) {
return "'" . $this->escape(html_entity_decode($var)) . "'";
}
return "'" . html_entity_decode($var) . "'";
}
}
/**
* Prepare a variable result from a database to be used
*
* @param mixed $var Any variable
* @return mixed Variable post-processing
*/
protected function postDb($var) {
// Make sure any false and null variables are returned as passed
if ($var === false) {
return false;
}
// Use a recursive call if the variable is an array, to make sure it
// is penetrated to the correct depth
if (is_array($var)) {
$newArray = array();
foreach ($var as $key => $value) {
$newArray[htmlentities(stripslashes($key))] = $this->postDb($value);
}
return $newArray;
}
return htmlentities(stripslashes($var));
}
/**
* Prepare a set of tables or fields by escaping them
*
* @param string $stmt Statement to prepare
* @return string Escaped statement
* @since 1.2
*/
protected function prepareFields($stmt) {
// Escape everything
$stmt = preg_replace('/(`+)/', '`', preg_replace('/(\w+)/i', '`$1`', $stmt));
// Sort out the 'AS' keywords
$stmt = str_ireplace('`AS`', 'AS', $stmt);
// Replace any literals
$stmt = preg_replace('/(\'`|`\')/', "'", $stmt);
// Return the finished statement
return $stmt;
}
/**
* Prepare a statement to be used in a database, replacing ? with variables
*
* @param string $stmt Statement to prepare
* @param string|array $vars [Optional] Variables to insert
* @return string Prepared statement
* @since 1.2
*/
protected function prepareStatement($stmt, $vars) {
// Go through each match and replace the ? if a value exists
if (is_array($vars)) {
// Get matches
$numMatches = preg_match_all('/((?<!\\\)\?)/', $stmt, $matches);
// Throw an error if the matches aren't the same
if ($numMatches > count($vars)) {
$this->errorDb('escape_count');
}
// Replace all matches found
for ($i = 0; $i < $numMatches; $i++) {
if (isset($vars[$i])) {
$stmt = preg_replace('/((?<!\\\)\?)/', $this->preDb($vars[$i]), $stmt, 1);
}
}
} else {
$stmt = preg_replace('/((?<!\\\)\?)/', $this->preDb($vars), $stmt, 1);
}
return $stmt;
}
/**
* Build a SELECT string for a query using one or more fields
*
* @param string|array $fields Fields to use
* @return string MySQL formatted string
*/
protected function buildSelect($fields) {
// Every field needs to be enclosed in ` characters
if (is_array($fields)) {
foreach ($fields as $key => $field) {
$fields[$key] = $this->prepareFields($field);
}
return join(', ', $fields);
}
return $this->prepareFields($fields);
}
/**
* Build a FROM string for a query using one or more tables
*
* @param string|array $tables Tables to use
* @return string MySQL formatted string
*/
protected function buildFrom($tables) {
// Every table name, with alias, needs to be enclosed in ` characters
if (is_array($tables)) {
foreach($tables as $key => $table) {
$tables[$key] = $this->prepareFields($table);
}
return '(' . join(', ', $tables) . ')';
}
return $this->prepareFields($tables);
}
/**
* Build a JOIN string for a query using one or more joins
*
* @param array $joins Joins to perform (as passed from other functions)
* @return string MySQL formatted string
*/
protected function buildJoin($joins) {
$string = '';
foreach ($joins as $join) {
// Build the join string each time by enclosing fields and tables in ` characters
$string .= "\n"
. strtoupper($join['type'])
. ' JOIN '
. $this->prepareFields($join['table'])
. ' ON '
. $this->prepareFields($join['local'])
. ' ' . (isset($join['condition']) ? $join['condition'] : '=') . ' '
. $this->prepareFields($join['foreign']);
}
// Return the formatted string
return $string;
}
/**
* Build a WHERE/GROUP/ORDER/LIMIT string using the given MySQL command string
* and a set of values that are to be used in replacements with ?
*
* @param string $opt Raw MySQL string
* @param string|array $optValues [Optional] Array of values or string to use in replacements
* @return string Correctly formatted and escaped string
*/
protected function buildOpt($opt, $optValues = null) {
// Return the finished string
return $this->prepareStatement($opt, $optValues);
}
}