-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDOEngine.php
More file actions
718 lines (652 loc) · 25.2 KB
/
Copy pathPDOEngine.php
File metadata and controls
718 lines (652 loc) · 25.2 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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
/**
* @package PDO_For_Wordpress
* @version $Id$
* @author Justin Adie, rathercurious.net
*/
/**
* provides a 'driver' for wordpress to use PDO databases
*
* The core behaviours of this class is <ul>
* <li> to create an appropriate factory item for query rewriting
* <li> to extract variables from the query to use in a prepared statements
* <li> to handback to the standard wpdb object the kind of outputs it wants
* </ul>
*/
class PDO_Engine{
//public properties
public $isError = false;
public $foundRowsResult;
//private properties
private $initialQuery;
private $rewrittenQuery;
private $queryType;
private $rewriteEngine;
private $needsPostProcessing;
private $results=array();
private $pdo;
private $preparedQuery;
private $extractedVariables = array();
private $errorMessages = array();
private $errors;
public $queries = array();
private $dbType;
private $lastInsertID;
private $affectedRows;
private $columnNames;
private $numRows;
private $returnValue;
private $startTime;
private $stopTime;
/**
* object instantiation.
*
* At this point it's just a connection object...
*
* @param array $connectionParams a simple array of connection parameters (see list command for order
*/
public function __construct($connectionParams){
$this->connect($connectionParams);
}
/**
* method to construct the dsn dependent on the database type
*
* @param array $connectionParams a simple array of connection parameters (see list command for order
* @return string a constructed dsn
*/
private function connect($connectionParams){
set_time_limit(30);
global $wpdb;
list ($this->dbType, $dbUser, $dbPassword, $dbName, $dbHost, $dbCharset) = $connectionParams;
switch ($this->dbType){
case 'sqlite':
//as sqlite is file system database, we need to make sure that permissions
//don't bite us in the arse
$u = umask(0000);
// determines whether we are in the installation process. If so, we
// create the holding directory, protect it from direct access and create the database
if (!is_dir(FQDBDIR)){
if (!@mkdir(FQDBDIR, 0777, true)){
umask($u);
$wpdb->bail("<h1>Cannot create folder</h1><p>The installation routine cannot create the folder in which the sqlite database will be stored. This will usually be because of permissions errors.</p>");
}
}
if (!is_writable (FQDBDIR)){
umask($u);
$wpdb->bail('<h1>Permissions Problem</h1><p>PDO For WordPress needs to be able to write to the folder ' .FQDBDIR ."</p>");
}
if (!is_file(FQDBDIR.'/.htaccess')){
$fh = fopen(FQDBDIR.'/.htaccess', "w");
if (!$fh) {
umask($u);
$wpdb->bail("<h1>Cannot create htaccess file</h1><p>The installation routine cannot create the htaccess file needed to protect your database</p>");
}
fwrite ($fh, "DENY FROM ALL");
fclose ($fh);
}
//reset the umask to what it was.
umask($u);
$dsn = "sqlite:".FQDB;
if (is_file(FQDB)){
$this->pdo = new PDO($dsn);
$s = $this->pdo->query('select count(*) from SQLite_Master where type="table"');
$count = $s->fetchColumn(0);
$s = null; //release the recordset
if ($count < 2){
//echo 'installing database';
$this->installDB();
}
//install the UDFs
require_once PDODIR . '/driver_sqlite/pdo_sqlite_udfs.php';
new PDO_SQLITE_UDFS($this->pdo);
} else {
$this->pdo = new PDO($dsn);
//if the database did not exist, we need to step in
//quickly and create the tables to stop wordpress from trying to do it
$this->installDB();
}
break;
case 'mysql':
if(strstr($dbHost, ':')) {
$bits = explode(':', $dbHost);
$dsn = "mysql:dbname={$dbName};host=" . $bits[0] . ';port=' . $bits[1];
} else {
$dsn = "mysql:dbname={$dbName};host={$dbHost}";
}
$params = array();
if($dbCharset == 'utf8')
{
$dsn = "{$dsn};charset={$dbCharset}";
$params = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
}
$this->pdo = new PDO ($dsn, $dbUser, $dbPassword, $params);
break;
default:
$message = <<<HTML
<h1>You have requested an unsupported database type</h1>
<p>Through this plugin we currently support</p>
<ul>
<li>mysql</li>
<li>sqlite</li>
</ul>
<p>If you're unsure what these terms mean you should probably contact your host. If you still need help you can always visit the <a href='http://wordpress.org/support/'>WordPress Support Forums</a>.</p>
HTML;
$this->setError(__LINE__, __FUNCTION__ , $message);
}//end switch
if ($this->pdo === false){
$message = "<h1>Database Error</h1><p>We have been unable to connect to the specified database.<br/>The error message received was " . print_r($this->pdo->errorInfo(), true) .'</p>';
$this->setError(__LINE__, __FUNCTION__, $message);
}
}
/**
* interrupts the usual wordpress table creation regime
*
* it does not feel right to have this in this class. Consider moving it out
* note that we cannot use neat ->bail() error function as at this point $wpdb is not fully instantiated.
*/
private function installDB(){
switch ($this->dbType){
case "sqlite":
require_once PDODIR.'/wp_install.php';
break;
}
}
public function getPdo()
{
return $this->pdo;
}
/**
* handles a query request from db.php
*
* determines whether the query needs rewriting
* parses out the variables from the query
* escapes them and enquotes them as necessary
* this function is really the big kahuna of this class.
*
* @param $query string the query that is to be processed.
*/
public function query($query){
//design decision to keep this as one object serving muliple queries rather than a new object per query.
$this->flush();
//I store details of the query at each stage of use for ease of debugging
$this->queries[] = "Raw query:\t$query";
//currently I don't use initialquery again, but keeping just in case.
$this->initialQuery = $query;
$this->determineQueryType($query);
switch (strtolower($this->queryType)){
case 'multiinsert':
switch ($this->dbType){
case "sqlite":
list($insertSQLPrefix, $values, $count) = $this->multiInsertMatches;
$this->multiInsertMatches = array();
if ($count > 1){
$cnt = 1;
$first = true;
$this->reriteEngine = new pdo_sqlite_driver();
foreach ($values as $value){
if (substr($value, -1, 1) === ')'){
$suffix = '';
} else {
$suffix = ')';
}
$query = $insertSQLPrefix .' ' . $value . $suffix;
$this->rewrittenQuery = $this->rewriteEngine->rewriteQuery($query, 'insert');
//logically they should all be the same shape so we can prepare the first one.
//and execute against subsequents
$this->queries[] = $this->rewrittenQuery;
//get variables
$this->extractedVariables = array();
$this->extractVariables();
if ($first){
$this->prepareQuery();
$first = false;
/*if ($this->isError){
global $wpdb;
$wpdb->bail($this->getErrorMessage());
}
*/
} else {
$this->executeQuery($this->statement);
/*
if ($this->isError){
global $wpdb;
$wpdb->bail($this->getErrorMessage());
}
*/
}
}
}
break;
case "mysql":
$this->rewrittenQuery = $this->initialQuery;
$this->needsPostProcessing = false;
$this->queries[]="Rewritten: $this->rewrittenQuery";
// prepare the query to use placeholders (avoids sql injection attacks)
$this->extractVariables();
//prepare the query
//and execute it (called from prepare())
$this->prepareQuery();
if (!$this->isError){
//postprocess, for IF statements.
$this->processResults();
//$wpdb expects an array of objects as a result whereas ironically we are using arrays
$this->convertToObject();
} else {
/*
*
global $wpdb;
$wpdb->bail($this->getErrorMessage());
*/
}
break;
}
break;
default:
switch ($this->dbType){
case "sqlite":
require_once PDODIR."/driver_sqlite/pdo_sqlite_driver.php";
$this->rewriteEngine = new pdo_sqlite_driver();
$this->rewrittenQuery = $this->rewriteEngine->rewriteQuery($this->initialQuery, $this->queryType);
$this->needsPostProcessing = true;
break;
case "mysql":
//no rewrite engine needed
$this->rewrittenQuery = $this->initialQuery;
$this->needsPostProcessing = false;
break;
}
$this->queries[]="Rewritten: $this->rewrittenQuery";
// prepare the query to use placeholders (avoids sql injection attacks)
$this->extractVariables();
//prepare the query
//and execute it (called from prepare())
$this->prepareQuery();
if (!$this->isError){
//postprocess, for IF statements.
$this->processResults();
//$wpdb expects an array of objects as a result whereas ironically we are using arrays
$this->convertToObject();
} else {
/*
*
global $wpdb;
$wpdb->bail($this->getErrorMessage());
*/
}
break;
}
//dump some queries to a text file for testing
if (defined('PDO_DEBUG') && PDO_DEBUG === true){
error_log($this->getDebugInfo());
}
}
/**
* returns the actual result set, as cleansed by processResults
*
*/
public function getInsertID(){
return $this->lastInsertID;
}
/**
* returns the number of rows affected by the last query
*
*/
public function getAffectedRows(){
return $this->affectedRows;
}
/**
* returns the actual result set, as cleansed by processResults
*
*/
public function getColumns(){
return $this->columnNames;
}
/**
* returns the actual result set, as cleansed
*
*/
public function getQueryResults(){
return $this->results;
}
/**
* returns the number of rows returned by the previous query
*
*/
public function getNumRows(){
return $this->numRows;
}
/**
* returns a value to be used by WordPress as a result
*
*/
public function getReturnValue(){
return $this->returnValue;
}
/**
* method to return the current array of error messages in a string form
*
* @return string a string representing the error messages recorded by this class
*/
public function getErrorMessage(){
if (count($this->errorMessages) === 0){
$this->isError = false;
$this->errorMessages = array();
return '';
}
$output = '<div style="clear:both"> </div>';
if ($this->isError === false){
return $output;
}
$output .= "<div class=\"queries\" style=\"clear:both; margin_bottom:2px; border: red dotted thin;\">Queries made or created this session were<br/>\r\n\t<ol>\r\n";
foreach ($this->queries as $q){
$output .= "\t\t<li>".$q."</li>\r\n";
}
$output .= "\t</ol>\r\n</div>";
foreach ($this->errorMessages as $num=>$m){
$output .= "<div style=\"clear:both; margin_bottom:2px; border: red dotted thin;\" class=\"errorMessage\" style=\"border-bottom:dotted blue thin;\">Error occurred at line {$this->errors[$num]['line']} in Function {$this->errors[$num]['function']}. <br/> Error message was: $m </div>";
}
ob_start();
debug_print_backtrace();
$output .= "<pre>" . ob_get_contents() . "</pre>";
ob_end_clean();
return $output;
}
/**
* method to return a plain text dump of queries for output to a text file
*
* @return string: plain text set of queries
*/
private function getDebugInfo(){
$output = '';
foreach ($this->queries as $q){
$output .= $q ."\r\n";
}
return $output;
}
/**
* function to reset the object for a new query
*
*/
private function flush(){
$this->initialQuery = '';
$this->rewrittenQuery = '';
$this->queryType = '';
$this->needsPostProcessing = false;
$this->results = array();
$this->_results = array();
$this->lastInsertID = NULL;
$this->affectedRows = NULL;
$this->columnNames = array();
$this->numRows = NULL;
$this->returnValue = NULL;
$this->extractedVariables = array();
$this->errorMessages=array();
$this->isError = false;
$this->queries = array();
}
/**
* interacts with the rewrite engine to cleanse the result set from a query
*
*/
private function processResults(){
if(in_array($this->queryType, array("select", "describe","show")) && $this->needsPostProcessing){
$this->results = $this->rewriteEngine->processResults($this->_results);
}else{
$this->results = $this->_results;
}
}
/**
* prepares a query (to execute a query)
*
*/
private function prepareQuery(){
$this->queries[] = "Prepare:\t". $this->preparedQuery;
do {
$this->statement = $this->pdo->prepare($this->preparedQuery);
if ($this->statement === false){
$reasons = $this->pdo->errorinfo();
$reason = $reasons[1];
$this->pdo->exec('vacuum');
} else {
$reason = 0;
}
} while ($reason == 17);
if ($reason > 0){
$message = "Problem preparing the PDO SQL Statement. Error was ".$reasons[2];
$this->setError(__LINE__, __FUNCTION__, $message);
return false;
}
return $this->executeQuery($this->statement);
}
/**
* executes a query (prepared by prepareQuery)
*
* query is executed and various variables are set
* for use later
*/
private function executeQuery( $statement ){
global $wpdb;
if (!is_object($statement)) return;
if (count($this->extractedVariables) > 0){
$this->queries[] = "Executing:\t ".serialize($this->extractedVariables);
do {
$result = $statement->execute($this->extractedVariables);
if (!$result){
$reasons = $statement->errorinfo();
$reason = $reasons[1];
} else {
$reason = 0;
}
} while ($reason == 17);
} else {
$this->queries[] = "Executing: (no parameters)\t ";
do{
$result = $statement->execute();
if (!$result){
$reasons = $statement->errorinfo();
$reason = $reasons[1];
} else {
$reason = 0;
}
} while ($reason == 17);
}
if ($result === false){
$message = "Error executing query. Error was was ". $reasons[2];
$this->setError(__LINE__, __FUNCTION__, $message);
return FALSE;
}else {
//grab the results in an associative array
//CONSIDER CHANGING TO FETCH OBJECTS AND THEN REWRITE THE POST PROCESSING FUNCTION
$this->_results = $statement->fetchAll(PDO::FETCH_ASSOC);
}
//generate the results that $wpdb will want to see
switch ($this->queryType){
case "insert":
case "update":
case "replace":
$this->lastInsertID = $this->pdo->lastInsertId();
$this->affectedRows = $statement->rowCount();
$this->returnValue = $this->affectedRows;
break;
case "select":
case "show":
case "describe":
case "foundrows":
$this->numRows = count($this->_results);
$this->returnValue = $this->numRows;
break;
case "delete":
$this->affectedRows = $statement->rowCount();
$this->returnValue = $this->affectedRows;
break;
case "alter":
case "drop":
case "create":
if ($this->isError){
$this->returnValue = true;
}else {
$this->returnValue = false;
}
break;
}
}
/**
* releases all string variables and replaces with placeholders
*
* note that this only handles mysql variables that are enquoted.
* so variables passed as integers are not given this protection.
* this _should_ always be OK ...
* interacts with replaceVariablesWithPlaceHolders
*/
private function extractVariables(){
if ($this->queryType == 'create'){
$this->preparedQuery = $this->rewrittenQuery;
return;
}
//long queries can really kill this
$pattern = '/(?<!\\\\)([\'"])(.*?)(?<!\\\\)\\1/imsx';
$_limit = $limit = ini_get('pcre.backtrack_limit');
do{
if ($limit > 10000000){
$message = "The query is too big to parse properly";
$this->setError(__LINE__, __FUNCTION__, $message);
break; //no point in continuing execution, would get into a loop
} else {
ini_set('pcre.backtrack_limit', $limit);
$query = preg_replace_callback($pattern, array($this,'replaceVariablesWithPlaceHolders'), $this->rewrittenQuery);
}
$limit = $limit * 10;
} while (empty($query));
//reset the pcre.backtrack_limit
ini_set('pcre.backtrack_limit', $_limit);
$this->queries[]= "With Placeholders: $query ";
$this->preparedQuery = $query;
}
/**
* substitutes enquoted variables (i.e. strings and dates and blobs etc) with placeholders
*
* this callback function assumes that variables are escaped for use in
* mysql, i.e. using an inline method (plugin) or a call to $wpdb.
* it unescapes them, does a bit of cleansing and then spits
* back out a clean variable into a holding array and returns
* a SQL placeholder for use in the prepared query.
*
* @param $matches array [an array provided by the calling function]
* @return string '?' a place holder for use in SQL queries
*/
private function replaceVariablesWithPlaceHolders($matches){
//remove the wordpress escaping mechanism
$param = stripslashes($matches[0]);
//remove trailing spaces
$param = trim($param);
//remove the quotes at the end and the beginning
if (in_array($param{strlen($param)-1}, array("'",'"'))){
$param = substr($param,0,-1) ;//end
}
if (in_array($param{0}, array("'",'"'))){
$param = substr($param, 1); //start
}
$this->extractedVariables[] = $param;
//return the placeholder
return ' ? ';
}
/**
* takes the query and determines the query type
*
* we divide up the types of query as follows:
* -select
* -select found_rows
* -insert
* -update/replace
* -delete
* -alter
* -create
* -drop
* the result is stored in the queryType property
*
* @param string $query - the query being analysed
*/
public function determineQueryType($query){
$result = preg_match('/^\\s*(select\\s*found_rows|select|insert|update|replace|delete|alter|create|drop|show|describe|truncate|optimize)/i', $query, $match);
if (!$result){
$bailoutString = <<<HTML
<h1>Unknown query type</h1>
<p>Sorry, we cannot determine the type of query that is being requested.</p>
<p>The query is {$query}</p>
HTML;
$this->setError(__LINE__, __FUNCTION__, $bailoutString);
} else {
$this->queryType = strtolower($match[1]);
if(stristr($this->queryType, 'found') !== FALSE){
$this->queryType = "foundrows";
}else {
if (stristr($this->queryType, 'insert')){
$pattern = '/(INSERT.*VALUES\s*)(\(.*\))/imsx';
preg_match($pattern, $query, $match);
$explodedParts = explode ('),', $match[2]);
$count = count($explodedParts);
if ($count > 1){
$this->queryType = 'multiInsert';
$this->multiInsertMatches = array($match[1], $explodedParts, $count);
}
}
}
}
}
/**
* method for adding data to the error array
*
* @param string $line the line of the script that throws the error
* @param string $function the name of the function in which the error was thrown
*/
private function setError ($line, $function, $message){
$this->errors[] = array("line"=>$line, "function"=>$function);
$this->errorMessages[] = $message;
$this->isError = true;
error_log("Line $line, Function: $function, Message: $message \n");
}
/**
* method that takes the associative array of query results and creates a numeric array of anonymous objects
*/
private function convertToObject(){
$_results = array();
if (count ($this->results) === 0){
echo $this->getErrorMessage();
} else {
foreach($this->results as $row){
$_results[] = new objArray($row);
}
}
$this->results = $_results;
}
}
/**
* class for creating an anonymous object
*/
class objArray
{
function __construct($data = null,&$node= null)
{
foreach ($data as $key => $value)
{
if ( is_array($value) )
{
if (!$node)
{
$node =& $this;
}
$node->$key = new stdClass();
self::__construct($value,$node->$key);
}
else
{
if (!$node)
{
$node =& $this;
}
$node->$key = $value;
}
}
}
}
?>