-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysquirrel.php
515 lines (409 loc) · 15.5 KB
/
mysquirrel.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
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
<?php
/**
* -----------------------------------------------------------------------------
* M Y S Q U I R R E L : Protect PHP and MySQL against injection attacks!
* -----------------------------------------------------------------------------
*
* @package MySquirrel
* @author Kijin Sung <[email protected]>
* @copyright (c) 2010-2013, Kijin Sung <[email protected]>
* @license LGPL v3 <http://www.gnu.org/copyleft/lesser.html>
* @link http://github.com/kijin/mysquirrel
* @version 0.3.8
*
* -----------------------------------------------------------------------------
*
* Copyright (c) 2010-2013, Kijin Sung <[email protected]>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ----------------------------------------------------------------------------
*/
/**
* MySquirrel main class.
*
* Instantiate this class to obtain a MySQL connection. This should be your main
* gateway to all communications with the server.
*/
class MySquirrel
{
// Some protected properties.
protected $host;
protected $user;
protected $pass;
protected $database;
protected $charset;
protected $connection = false;
protected $paranoid = false;
// Constructor.
public function __construct($host, $user, $pass, $database, $charset = false)
{
if (!function_exists('mysql_connect'))
{
throw new MySquirrelException('Your installation of PHP does not support MySQL connectivity.');
}
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->database = $database;
$this->charset = $charset;
}
// Static connect method for 0.2 compatibility.
public static function connect($host, $user, $pass, $database, $charset = false)
{
return new MySquirrel($host, $user, $pass, $database, $charset);
}
// Lazy connect method.
protected function lazyConnect()
{
$this->connection = mysql_connect($this->host, $this->user, $this->pass);
if (!$this->connection) throw new MySquirrelException_ConnectionError('Could not connect to ' . $this->host . '.');
$select_db = mysql_select_db($this->database, $this->connection);
if (!$select_db) throw new MySquirrelException_ConnectionError('Could not select database ' . $this->database . '.');
// Attempt to change the character set if necessary.
if ($this->charset !== false)
{
if (function_exists('mysql_set_charset'))
{
$select_charset = mysql_set_charset($this->charset, $this->connection);
}
elseif (mysql_client_encoding($this->connection) !== $this->charset)
{
$msg = 'Changing the character set requires MySQL 5.0.7 or higher, and PHP 5.2.3 or higher. ';
$msg .= 'Your database\'s default character set is ' . mysql_client_encoding($this->connection) . '.';
throw new MySquirrelException_CharacterSetError($msg);
}
}
}
// Paranoid method. (Raw queries are disabled, and quotes and comments are not permitted.)
public function paranoid()
{
$this->paranoid = true;
}
// Prepare method.
public function prepare($querystring)
{
if ($this->connection === false) $this->lazyConnect();
// Perform some basic checks.
$querystring = trim($querystring);
if (strpos($querystring, ';') !== false)
{
throw new MySquirrelException_MultipleStatementsError('You cannot prepare multiple statements at once.');
}
if ($this->paranoid && (strpos($querystring, '\'') !== false || strpos($querystring, '"') !== false || strpos($querystring, '--') !== false))
{
throw new MySquirrelException_ParanoidModeError('While in paranoid mode, you cannot use queries with quotes or comments in them.');
}
// Instantiate and return a new prepared statement object.
return new MySquirrelPreparedStmt($this->connection, $querystring);
}
// Query method.
public function query($querystring /* and parameters */ )
{
if ($this->connection === false) $this->lazyConnect();
// Perform some basic checks.
$querystring = trim($querystring);
if (strpos($querystring, ';') !== false)
{
throw new MySquirrelException_MultipleStatementsError('You cannot use query() to send multiple statements at once. Please use rawQuery() instead.');
}
if ($this->paranoid && (strpos($querystring, '\'') !== false || strpos($querystring, '"') !== false || strpos($querystring, '--') !== false))
{
throw new MySquirrelException_ParanoidModeError('While in paranoid mode, you cannot use queries with quotes or comments in them.');
}
// Get all parameters.
$params = func_get_args();
array_shift($params);
if (count($params) === 1 && is_array($params[0])) $params = $params[0];
$count = substr_count($querystring, '?');
if ($count !== count($params))
{
throw new MySquirrelException_ParameterMismatchError('Query has ' . $count . ' placeholders, but ' . count($params) . ' parameters given.');
}
// Replace all placeholders with properly escaped parameter values.
$queryparts = explode('?', $querystring);
for ($i = 0; $i < $count; $i++)
{
$param = $params[$i];
if (!is_int($param) && !is_float($param))
{
$queryparts[$i] .= "'" . mysql_real_escape_string($param, $this->connection) . "'";
}
else
{
$queryparts[$i] .= $param;
}
}
$querystring = implode('', $queryparts);
// Run the reconstructed query.
return $this->commonQuery($querystring);
}
// Raw query method.
public function rawQuery($querystring)
{
if ($this->connection === false) $this->lazyConnect();
if ($this->paranoid) throw new MySquirrelException_ParanoidModeError('rawQuery() is disabled in paranoid mode.');
return $this->commonQuery($querystring);
}
// Common query method.
protected function commonQuery($querystring)
{
$result = mysql_query($querystring, $this->connection);
if ($error = mysql_errno($this->connection))
{
throw new MySquirrelException('Error ' . $error . ': ' . mysql_error($this->connection));
}
return (is_bool($result)) ? $result : new MySquirrelResult($result);
}
// Number of affected rows.
public function affectedRows()
{
if ($this->connection === false) return false;
return mysql_affected_rows($this->connection);
}
// Last insert ID.
public function lastInsertID()
{
if ($this->connection === false) return false;
return mysql_insert_id($this->connection);
}
// Begin transaction.
public function beginTransaction()
{
if ($this->connection === false) $this->lazyConnect();
try
{
return $this->commonQuery('BEGIN');
}
catch (MySquirrelException $e)
{
throw new MySquirrelException_TransactionError('Can\'t begin: ' . $e->getMessage());
}
}
// Commit transaction.
public function commit()
{
if ($this->connection === false) throw new MySquirrelException_TransactionError('Can\'t commit: No transaction is currently in progress.');
try
{
return $this->commonQuery('COMMIT');
}
catch (MySquirrelException $e)
{
throw new MySquirrelException_TransactionError('Can\'t commit: ' . $e->getMessage());
}
}
// Roll back transaction.
public function rollback()
{
if ($this->connection === false) throw new MySquirrelException_TransactionError('Can\'t rollback: No transaction is currently in progress.');
try
{
return $this->commonQuery('ROLLBACK');
}
catch (MySquirrelException $e)
{
throw new MySquirrelException_TransactionError('Can\'t rollback: ' . $e->getMessage());
}
}
// Generate a unique name to be used as a prepared statement name.
public static function genStatementName()
{
return 'ps_' . substr(microtime(), 2, 6) . '_' . mt_rand(10000, 99999);
}
}
/**
* Prepared statement class.
*
* This class is instantiated and returned when MySquirrel->prepare() is called.
*/
class MySquirrelPreparedStmt
{
// Information about the current statement.
protected $connection;
protected $querystring;
protected $statement;
protected $numargs;
// Constructor.
public function __construct($connection, $querystring)
{
$this->connection = $connection;
$this->querystring = $querystring;
$this->numargs = substr_count($querystring, '?');
$this->statement = MySquirrel::genStatementName();
$this->realQuery('PREPARE ' . $this->statement . ' FROM \'' . mysql_real_escape_string($this->querystring, $this->connection) . '\'');
}
// Execute method.
public function execute( /* parameters */ )
{
// Get all parameters.
$params = func_get_args();
if (count($params) === 1 && is_array($params[0])) $params = $params[0];
$count = count($params);
if ($count !== $this->numargs)
{
throw new MySquirrelException_ParameterMismatchError('Prepared statement has ' . $this->numargs . ' placeholders, but ' . $count . ' parameters given.');
}
// Initialize the execute querystring.
$querystring = 'EXECUTE ' . $this->statement;
// Set server-side variables with properly escaped parameter values.
for ($i = 0; $i < $count; $i++)
{
$param = $params[$i];
if (!is_int($param) && !is_float($param))
{
$param = "'" . mysql_real_escape_string($param, $this->connection) . "'";
}
$varname = '@' . $this->statement . '_v' . $i;
$this->realQuery('SET ' . $varname . ' = ' . $param);
if ($i == 0)
{
$querystring .= ' USING ' . $varname;
}
else
{
$querystring .= ', ' . $varname;
}
}
// Execute the query.
return $this->realQuery($querystring);
}
// Real query method.
protected function realQuery($querystring)
{
$result = mysql_query($querystring, $this->connection);
if ($error = mysql_errno($this->connection))
{
throw new MySquirrelException('Error ' . $error . ': ' . mysql_error($this->connection));
}
return (is_bool($result)) ? $result : new MySquirrelResult($result);
}
// Destructor.
public function __destruct()
{
$this->realQuery('DEALLOCATE PREPARE ' . $this->statement);
}
}
/**
* Result class.
*
* This class is instantiated and returned when a query has a result set.
* This is usually the case with SELECT queries.
*/
class MySquirrelResult implements Iterator
{
// Constructor.
public function __construct($result)
{
$this->result = $result;
}
// Some protected properties.
protected $result = null;
protected $iter_count = false;
protected $iter_index = false;
// Methods to implement the iterator interface.
public function rewind()
{
$this->iter_count = $this->numRows();
$this->iter_index = 0;
if (mysql_num_rows($this->result) > 0) mysql_data_seek($this->result, 0);
}
public function valid()
{
return ($this->iter_index < $this->iter_count) ? true : false;
}
public function current()
{
return $this->fetchAssoc();
}
public function key()
{
return $this->iter_index - 1;
}
public function next()
{
// iter_index is already incremented by fetchAssoc().
}
// Fetch method (generic).
public function fetch()
{
$this->iter_index++;
return mysql_fetch_array($this->result, MYSQL_BOTH);
}
// Fetch method (returns associated array).
public function fetchAssoc()
{
$this->iter_index++;
return mysql_fetch_assoc($this->result);
}
// Fetch method (returns object).
public function fetchObject($class_name = false, $params = array())
{
$this->iter_index++;
return $class_name ? mysql_fetch_object($this->result, $class_name, $params) : mysql_fetch_object($this->result);
}
// Fetch method (returns enumerated array).
public function fetchRow()
{
$this->iter_index++;
return mysql_fetch_row($this->result);
}
// Fetch-all method.
public function fetchAll()
{
$return = array();
$this->rewind();
while ($row = mysql_fetch_array($this->result, MYSQL_BOTH)) $return[] = $row;
return $return;
}
// Get field info.
public function fieldInfo($offset)
{
return array(
'name' => mysql_field_name($this->result, $offset),
'type' => mysql_field_type($this->result, $offset),
'length' => mysql_field_len($this->result, $offset),
'table' => mysql_field_table($this->result, $offset),
'flags' => mysql_field_flags($this->result, $offset),
);
}
// Number of fields.
public function numFields()
{
return mysql_num_fields($this->result);
}
// Number of rows.
public function numRows()
{
return mysql_num_rows($this->result);
}
// Destructor.
public function __destruct()
{
mysql_free_result($this->result);
}
}
/**
* MySquirrel exceptions.
*
* No error passes silently. If your code is lousy, you'll see a lot of these.
* It is your responsibility to handle these exceptions in an appropriate way.
*/
class MySquirrelException extends Exception { }
class MySquirrelException_ConnectionError extends MySquirrelException { }
class MySquirrelException_CharacterSetError extends MySquirrelException { }
class MySquirrelException_MultipleStatementsError extends MySquirrelException { }
class MySquirrelException_ParanoidModeError extends MySquirrelException { }
class MySquirrelException_ParameterMismatchError extends MySquirrelException { }
class MySquirrelException_TransactionError extends MySquirrelException { }