This repository has been archived by the owner on Aug 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
db.mysql.class.php
764 lines (651 loc) · 19.1 KB
/
db.mysql.class.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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
<?php
/*
* MySQL Database Class
* By Timothy 'TiM' Oliver
* http://www.timoliver.com.au
*
* Abstraction class that manages the establishment of a connection
* to a MySQL database, and streamlines queries made to it.
*
* ============================================================================
*
* Copyright (C) 2011 by Tim Oliver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
class MySQLDatabase
{
/**
* The target DB hostname
* @var string
*/
var $hostname = '';
/**
* The name of the database to connect to
* @var string
*/
var $database = '';
/**
* The database login name
* @var string
*/
var $username = '';
/**
* The database login password
* @var string
*/
var $password = '';
/**
* A single prefix or list of prefixes
* @var string or array
*/
var $prefix = NULL;
/**
* Link ID for MySQL connection
* @var int
*/
var $link = NULL;
/**
* Main results handle for all general queries
* @var int
*/
var $main_handle = NULL;
/**
* List of handles to manage concurrent queries
* @var array
*/
var $handles = NULL;
/*
* Class Constructor
*
* Init MySQL and connect to server based on supplied arguments
*
* @param string $hostname The hostname of the target database
* @param string $database The name of the target database
* @param string $username The login username for the database
* @param string $password The login password for the database
* @param str/array $prefix A single or list of table prefixes
* @param bool $connect Connect immediately on instantiation
*
*/
function __construct( $hostname, $database, $username, $password, $prefix=NULL, $connect = true )
{
//init default values across the board
$this->results = array();
//set up database properties
$this->database = $database;
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->prefix = $prefix;
//on by default, connect to the database straight away
if( $connect )
$this->connect();
}
/**
* Class destructor
*
* Closes the connection to the server and frees up resources as needed
*
*/
function __destruct()
{
mysql_close( $this->link );
}
/**
* Connect
*
* Connects to the MySQL database and throws an exception if it fails
*
*/
function connect()
{
$this->link = mysql_connect( $this->hostname, $this->username, $this->password );
if( $this->link === FALSE )
throw new Exception('MySQLDatabase: Failed to connect to database.');
$db_selected = mysql_select_db( $this->database, $this->link );
if( $db_selected === FALSE )
throw new Exception('MySQLDatabase: Failed to select database: '.$this->database );
}
/**
* Query
*
* Perform a query on the MySQL database.
*
* @param string $query The MySQL query to execute. (MUST be sanitized by calling function beforehand)
* @param string $handle - A unique ID that can be used to refer to this query later
*
* @return bool(varies) Depending on the type of query:
* - 'SELECT' int Number of rows that were selected
* - 'UPDATE|REPLACE|DELETE' int Number of rows that were affected
* - 'INSERT' int The unique ID of the newly inserted row
*
*/
function query($query, $handle='')
{
//Perform the MySQL query
$result = mysql_query($query, $this->link);
//if result was straight-up false, return
if( $result === FALSE )
{
$this->throw_mysql_error();
return FALSE;
}
//if query was one that returns a resultset, grab and store it
if( preg_match( '%^(select|show|describe|explain)%is', $query ) > 0 )
$this->set_handle( $result, $handle );
//if query was a 'SELECT', return the number of affected rows
if( preg_match( '%^(select)%is', $query ) > 0 )
{
return mysql_num_rows($result);
}
//if query was one that affected existing rows (ie UPDATE, DELETE etc), return the number of affected rows
elseif( preg_match( '%^(update|replace|delete)%is', $query ) > 0 )
{
return mysql_affected_rows($this->link);
}
//if query was an insert, return the new ID
elseif( preg_match( '%^(insert)%is', $query ) > 0 )
{
return mysql_insert_id( $this->link );
}
return TRUE;
}
/**
* Prepare
*
* Sanitizes a MySQL query and its arguments to reduce the
* potential of SQL injection attacks.
*
* @param string $query MySQL string to sanitize
*
* Takes the following wildcards for the substitution (a la sprintf style)
* %s - string value (No quotes required)
* %d/%i - integer value
* %[0-9]f - float value [Int indicates rounding]
* %[0-9]t - table name (auto-appends prefix) [Int indicates prefix array index]
*
* After $query, an arbitrary number of arguments of types int, float, or string can be added which
* will be substituted for each wildcard in that order.
* In addition, a single array() containing the values can be used instead.
*
* @return string The sanitized query string
*/
function prepare( $query = '' )
{
if( $query == '' )
return '';
//get the arguments from this method
$args = func_get_args();
array_shift($args); //remove $query
//If the first optional argument
//is an array, then assume that that
//is the supplied list of arguments
if( is_array($args[0]) )
$args = $args[0];
//loop through each argument
foreach( $args as $arg )
{
//escape any '%' in the args
$arg = str_replace( '%', '\\\\%', $arg );
//select each argument from left to right, one at a time, retrieving the symbol, and offset
if( preg_match( '/[^\\\\]%([0-9]*?)([dfst])/is', $query, $match, PREG_OFFSET_CAPTURE ) <= 0 )
break;
//grab the info from the match
$tag = strval($match[0][0]); //The full match (eg %3t )
$param = intval($match[1][0]); //The number (if any) after the % (ie 4)
$offset = intval($match[0][1]+1); //The location of these chars from the front of the string (ie 15) (+1 to offset escape slash)
$symbol = strval($match[2][0]); //The particular parameter defined in the query (ie t)
//prepare the argument for insertion
switch( strtolower( $symbol ) )
{
//parse as an int
case 'd':
case 'i':
$arg = intval( $arg );
break;
//parse as a float
case 'f':
//if an argument was given, round the float off to that set
if( $param > 0 )
$arg = round( $arg, $param );
$arg = floatval( $arg );
break;
case 't':
//if prefix is an array, then param becomes the index
if( is_array( $this->prefix ) )
$prefix = $this->prefix[$param];
else
$prefix = $this->prefix;
$arg = ($prefix).(strval( $arg ));
break;
//parse as a string
case 's':
default:
if( get_magic_quotes_gpc() )
$arg = stripslashes( $arg );
//sanitize with MySQL sanitation function
$arg = "'".mysql_real_escape_string($arg, $this->link)."'";
break;
}
//remove the '%%%' string from the query
$query = substr_replace( $query, '', $offset, strlen($tag)-1 );
//insert the sanitized value in its place
$query = substr_replace( $query, $arg, $offset, 0 );
}
//unescape the % symbols
$query = str_replace( '\\\\%', '%', $query );
return $query;
}
/**
* Fetch Row
*
* Iterates through all of the rows selected from a query and
* iteratively returns one on each call of this method.
*
* @param string $handle The unique handle of the SELECT query
* @param bool $return_as_array Return the row as an array, instead of an object
* @param bool $close_on_finish Automatically close the connection on completion
*
* @return array|object Depending on $return_as_array, an array or object containing all of the data from the current row
*/
function fetch_row( $handle='', $return_as_array = FALSE, $close_on_finish = TRUE, )
{
$result = $this->get_handle($handle);
if( !$result )
return FALSE;
//get the row
$row = mysql_fetch_assoc( $result );
//if loop has finished, close the result
if( $row === FALSE )
{
if( $close_on_finish )
mysql_free_result( $result );
return FALSE;
}
//return an object by default, but also allow for arrays
if( $return_as_array )
return $row;
return (object)$row;
}
/**
* Get Row
* Return a single row from a query, formatted as array or object
*
* @param string $query The MySQL query (MUST be properly sanitized)
* @param bool $return_as_array Return associative array instead of object
*
* @return array|object Depending on $return_as_array, an array or object containing the first row from the query
*
*/
function get_row( $query, $return_as_array = false)
{
if( $query == '' )
return NULL;
//perform the query
$num_rows = $this->query( $query );
if( $num_rows === FALSE || $num_rows <= 0 )
return FALSE;
//get the first row
$row = $this->fetch_row( '', $return_as_array, false );
//close the result
mysql_free_result( $this->get_handle() );
return $row;
}
/**
* Get Rows
*
* Return multiple rows from a query as an array of objects or arrays
*
* @param string $query The MySQL query (MUST be properly sanitized)
* @param bool $return_as_array Return associative array instead of object
*
* @return array|object Depending on $return_as_array, an index array containing either arrays or objects of each row
*
*/
function get_rows( $query, $return_as_array = false )
{
if( $query == '' )
return NULL;
//perform the query
$num_rows = $this->query( $query );
if( $num_rows === FALSE || $num_rows <= 0 )
return FALSE;
//final output
$rows = array();
//grab each row and add to the local array
while( ( $row = $this->fetch_row( '', $return_as_array ) ) !== FALSE )
$rows[] = $row;
return $rows;
}
/**
* Get Column
*
* Gets all of the entries from one column of a table (eg all of the IDs of a certain condition)
*
* @param string $query The MySQL query (MUST be sanitized)
* @param int $col_offset Starting from 0, the offset ofthe column to get
*
* @return array An array containing all of the column items from each row
*
*/
function get_column( $query, $col_offset = 0 )
{
$num_rows = $this->query( $query );
if( $num_rows === FALSE )
return FALSE;
//grab handle of the call we just made
$handle = $this->get_handle();
//init final output array
$rows = array();
while( ($row = mysql_fetch_row($handle)) !== FALSE )
array_push( $rows, $row[$col_offset] );
//clean up the query in memory
mysql_free_result( $handle );
//return the result
return $rows;
}
/**
* Insert
*
* Inserts a new row into a table
*
* @param array|string $table Table to insert to (eg 'table' or array( 'table', '%2t' ) )
* @param array $data Data to insert into the table (eg array( 'col_name' => 'value' )
* @param array $format Array matching the order of $data, dictating each value data type (eg array('%s', '%d') )
*
* @return int The unique ID of the newly inserted row.
*
*/
function insert( $table = '', $data = NULL, $format = NULL )
{
//check all necessary arguments
if( !$table || !$data )
return FALSE;
//prepare an array to store the arg values
$arg_list = array();
//start building the query
$query = 'INSERT INTO';
//set the table name in the query
if( is_array( $table ) ) //allow for different prefix
{
$query .= $table[0];
$arg_list[] = $table[1];
}
else
{
$query .= ' %t ';
$arg_list[] = $table;
}
$query .= ' (';
//add the name of each column
$i=0;
foreach( $data as $name => $value )
{
$query .= ' '.$name;
//if not the final value, be sure to append a comma
if( $i < count( $data ) - 1)
$query .= ',';
$i++;
}
$query .= ' ) VALUES (';
//add the value from the data array
//if possible, use proper formatting
$i=0;
foreach( $data as $name => $value )
{
if( is_array( $format ) )
$query .= ' '.$format[$i];
else
$query .= ' %s';
$arg_list[] = $value;
//if not the final value, be sure to append a comma
if( $i < count( $data ) - 1)
$query .= ',';
$i++;
}
//cap off the end
$query .= ' );';
//prepare/sanitze the query
$query = $this->prepare( $query, $arg_list );
//execute the query and return the results
return $this->query( $query );
}
/**
* Update
*
* Construct a query and then execute to update one or more entries in a table.
*
* @param array|string $table The name of table, and/or formatting (eg 'table' or array( 'table', '%2t' ) )
* @param array $data The data to insert into table in name => value format (eg array( 'foo' => 'bar' ) )
* @param array $where An array stating 1 or more conditions of the update query (eg array('id' => 1) )
* @param array $format (optional) array dictating the data type of each data value (eg array( '%s', '%d' ) )
* @param array $where_format (optional) array dictating the data type of each where data value (eg array( '%s', '%d' ) )
*
* @return int The number of affected rows
*
*/
function update( $table = '', $data = NULL, $where = NULL, $format = NULL, $where_format = NULL )
{
//check all necessary arguments
if( !$table || !$data || !$where )
return FALSE;
//prepare a list to store the insert args as they come
$arg_list = array();
//begin building the query
$query = 'UPDATE';
//set the table name in the query
if( is_array( $table ) ) //allow for different prefix
{
$query .= $table[0];
$arg_list[] = $table[1];
}
else
{
$query .= ' %t';
$arg_list[] = $table;
}
$query .= ' SET';
//add each piece of data to the query
$i=0;
foreach( $data as $name => $value )
{
//if format is specified, use it, else default to string
if( is_array( $format ) )
$query .= ' '.$name.' = '.$format[$i];
else
$query .= ' '.$name.' = %s';
//append the value to the arglist
$arg_list[] = $value;
//if not the final value, be sure to append a comma
if( $i < count( $data ) - 1)
$query .= ',';
$i++;
}
$query .= ' WHERE';
//add each where condition to the query
$i=0;
foreach( $where as $name => $value )
{
//if format is specified, use it, else default to string
if( is_array( $where_format ) )
$query .= ' '.$name.' = '.$where_format[$i];
else
$query .= ' '.$name.' = %s';
//append the value to the arglist
$arg_list[] = $value;
//if not the final value, be sure to append a comma
if( $i < count( $where ) - 1)
$query .= ',';
$i++;
}
$query .= ';';
//prepare/sanitze the query
$query = $this->prepare( $query, $arg_list );
//execute the query and return the results
return $this->query( $query );
}
/**
* Delete Row
*
* Delete a single row from a table
*
* @param array|string $table The name of table, and/or formatting (eg 'table' or array( 'table', '%2t' ) )
* @param array $where An array stating 1 or more conditions of the update query (eg array('id' => 1) )
* @param array $format (optional) array dictating the data type of each data value (eg array( '%s', '%d' ) )
*
* @return int The number of affected rows
*
*/
function delete_row( $table='', $where = NULL, $format = NULL )
{
//check all necessary arguments
if( !$table || !$where )
return FALSE;
//prepare a list to store the insert args as they come
$arg_list = array();
//begin building the query
$query = 'DELETE FROM';
//set the table name in the query
if( is_array( $table ) ) //allow for different prefix
{
$query .= $table[0];
$arg_list[] = $table[1];
}
else
{
$query .= ' %t';
$arg_list[] = $table;
}
$query .= ' WHERE';
//add each where condition to the query
$i=0;
foreach( $where as $name => $value )
{
//if format is specified, use it, else default to string
if( is_array( $format ) )
$query .= ' '.$name.' = '.$format[$i];
else
$query .= ' '.$name.' = %s';
//append the value to the arglist
$arg_list[] = $value;
//if not the final value, be sure to append a comma
if( $i < count( $where ) - 1)
$query .= ',';
$i++;
}
//lock it off at limit 1
//(to prevent accidents)
$query .= ' LIMIT 1';
//prepare/sanitze the query
$query = $this->prepare( $query, $arg_list );
//execute the query and return the results
return $this->query( $query );
}
/**
* Num Rows
*
* Get the number of rows returned from the query
*
* @param string $handle (optional) The unique handle for the target
*
* @return int The number of rows the query returned
*
*/
function num_rows( $handle='' )
{
return mysql_num_rows($this->get_handle($handle));
}
/**
* Affected Rows
*
* Get the number of rows affected from the query
*
* @param string $handle (optional) The unique handle for the target
*
* @return int The number of rows the query affected
*
*/
function affected_rows( $handle='' )
{
return mysql_affected_rows($this->get_handle($handle));
}
/**
* Insert ID
*
* Get the insert ID from the last insertion query made
*
* @return int The unique ID of the newly inserted row
*
*/
function insert_id()
{
return mysql_insert_id($this->link);
}
/**
* Get Handle
*
* Returns the unique MySQL resource handle given the associated
* handle name.
*
* @param string $handle (Optional) Name of handle to retrieve
*
* @return int The unique MySQL resource handle for that name
*
*/
function get_handle( $handle='' )
{
//no handle specified, use the main hardcoded block
if( !$handle )
return $this->main_result;
else
return $this->results[strval($handle)];
}
/**
* Set Result Handle
*
* Assigns a unique MySQL resource to a local unique name
* so it can be retrieved later
*
* @param int $result The MySQL resource handle to set
* @param string $handle (Optional) Name of handle to retrieve
*/
function set_handle( $result, $handle='' )
{
if( !$result )
return false;
if( !$handle )
$this->main_result = $result;
else
$this->results[strval($handle)] = $result;
return true;
}
/**
* Throw MySQL Error
*
* Throws an exception and prints the last MySQL error generated.
*
*/
function throw_mysql_error()
{
throw new Exception('MySQLDatabase: A MySQL error occurred: '.mysql_error() );
}
}
/*EOF*/