Changeset 2
- Timestamp:
- 11/22/2007 01:37:21 AM (19 years ago)
- File:
-
- 1 edited
-
trunk/hyper-db.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/hyper-db.php
r1 r2 2 2 3 3 define('HYPERDB', true); 4 4 5 define('OBJECT', 'OBJECT', true); 5 6 define('ARRAY_A', 'ARRAY_A', false); … … 81 82 } 82 83 83 /**84 * Escapes content for insertion into the database, for security85 *86 * @param string $string87 * @return string query safe string88 */89 function escape($string) {90 return addslashes( $string ); // Disable rest for now, causing problems91 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' )92 return mysql_escape_string( $string );93 else94 return mysql_real_escape_string( $string, $this->dbh );95 }96 97 84 // ================================================================== 98 85 // Print SQL/DB error. … … 180 167 return ''; 181 168 } 182 169 183 170 function is_write_query( $q ) { 184 171 If( substr( $q, -1 ) == ';' ) 185 172 $q = substr( $q, 0, -1 ); 186 173 if ( preg_match('/^\s*SELECT.*?\s+FROM\s+`?(\w+)`?\s*/is', $q, $maybe) ) 174 return false; 175 if ( preg_match('/^\s*SHOW DATABASES\s*/is', $q, $maybe) ) 187 176 return false; 188 177 if ( preg_match('/^\s*UPDATE\s+`?(\w+)`?\s*/is', $q, $maybe) ) … … 399 388 400 389 /** 390 * Escapes content for insertion into the database, for security 391 * 392 * @param string $string 393 * @return string query safe string 394 */ 395 function escape($string) { 396 return addslashes( $string ); // Disable rest for now, causing problems 397 if( !$this->dbh || version_compare( phpversion(), '4.3.0' ) == '-1' ) 398 return mysql_escape_string( $string ); 399 else 400 return mysql_real_escape_string( $string, $this->dbh ); 401 } 402 403 /** 401 404 * Escapes content by reference for insertion into the database, for security 402 405 * @param string $s … … 404 407 function escape_by_ref( &$s ) { 405 408 $s = $this->escape( $s ); 409 } 410 411 function escape_deep( $array ) { 412 return is_array($array) ? array_map(array(&$this, 'escape_deep'), $array) : $this->escape( $array ); 406 413 } 407 414 … … 416 423 array_walk($args, array(&$this, 'escape_by_ref')); 417 424 return @call_user_func_array('sprintf', array_merge(array($query), $args)); 425 } 426 427 /** 428 * Insert an array of data into a table 429 * @param string $table WARNING: not sanitized! 430 * @param array $data should not already be SQL-escaped 431 * @return mixed results of $this->query() 432 */ 433 function insert($table, $data) { 434 $data = $this->escape_deep($data); 435 $fields = array_keys($data); 436 return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')"); 437 } 438 439 /** 440 * Update a row in the table with an array of data 441 * @param string $table WARNING: not sanitized! 442 * @param array $data should not already be SQL-escaped 443 * @param array $where a named array of WHERE column => value relationships. Multiple member pairs will be joined with ANDs. WARNING: the column names are not currently sanitized! 444 * @return mixed results of $this->query() 445 */ 446 function update($table, $data, $where){ 447 $data = $this->escape_deep($data); 448 $bits = $wheres = array(); 449 foreach ( array_keys($data) as $k ) 450 $bits[] = "`$k` = '$data[$k]'"; 451 452 if ( is_array( $where ) ) 453 foreach ( $where as $c => $v ) 454 $wheres[] = "$c = '" . $this->escape( $v ) . "'"; 455 else 456 return false; 457 return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) . ' LIMIT 1' ); 418 458 } 419 459 … … 695 735 } 696 736 737 /** 738 * Checks wether of not the database version is high enough to support the features backPress 739 * @global $wp_version 740 */ 741 function check_database_version() { 742 // Make sure the server has MySQL 4.0 743 $mysql_version = $this->db_version(); 744 if ( version_compare($mysql_version, '4.0.0', '<') ) 745 return new WP_Error('database_version', __('<strong>ERROR</strong>: MySQL 4.0.0 is required') ); 746 } 747 748 // Store just a few DB caps here - nothing fancy 749 function has_cap( $db_cap, $dbh_or_table = false ) { 750 $version = $this->db_version( $dbh_or_table ); 751 752 $db_cap = strtolower( $db_cap ); 753 754 switch ( $db_cap ) : 755 case 'group_concat' : 756 case 'collation' : 757 return version_compare($version, '4.1', '>='); 758 break; 759 endswitch; 760 761 return false; 762 } 763 764 // table name or mysql resource 765 function db_version( $dbh_or_table = false ) { 766 if ( !is_resource( $dbh_or_table ) ) 767 $dbh_or_table = $this->db_connect( "DESCRIBE $dbh_or_table" ); 768 // $dbh_or_table is now a $dbh 769 770 if ( $dbh_or_table ) 771 return mysql_get_server_info( $dbh_or_table ); 772 return false; 773 } 697 774 } 698 775
Note:
See TracChangeset
for help on using the changeset viewer.
