WPDB: Fall back to the connection charset when sanity checking strings.

If `DB_CHARSET` isn't defined (or is empty), `wpdb::$charset` will be empty, too. `wpdb::strip_invalid_text()` assumes that it isn't empty, however, so we need to fall back to the connection character set when we're running our sanity checks.

Fixes #34708.



git-svn-id: https://develop.svn.wordpress.org/trunk@35655 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2015-11-17 06:12:08 +00:00
parent 6ad5ee9786
commit d55c605242
2 changed files with 34 additions and 2 deletions

View File

@ -2811,11 +2811,21 @@ class wpdb {
$charset = $value['charset'];
}
if ( $this->charset ) {
$connection_charset = $this->charset;
} else {
if ( $this->use_mysqli ) {
$connection_charset = mysqli_character_set_name( $this->dbh );
} else {
$connection_charset = mysql_client_encoding();
}
}
if ( is_array( $value['length'] ) ) {
$queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING {$this->charset} )", $value['value'], $value['length']['length'] );
$queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING $connection_charset )", $value['value'], $value['length']['length'] );
} else if ( 'binary' !== $charset ) {
// If we don't have a length, there's no need to convert binary - it will always return the same result.
$queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING {$this->charset} )", $value['value'] );
$queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING $connection_charset )", $value['value'] );
}
unset( $data[ $col ]['db'] );

View File

@ -865,4 +865,26 @@ class Tests_DB_Charset extends WP_UnitTestCase {
$this->assertEquals( $safe_query, $stripped_query );
}
/**
* @ticket 34708
*/
function test_no_db_charset_defined() {
$tablename = 'test_cp1251_query_' . rand_str( 5 );
if ( ! self::$_wpdb->query( "CREATE TABLE $tablename ( a VARCHAR(50) ) DEFAULT CHARSET 'cp1251'" ) ) {
$this->markTestSkipped( "Test requires the 'cp1251' charset" );
}
$charset = self::$_wpdb->charset;
self::$_wpdb->charset = '';
$safe_query = "INSERT INTO $tablename( `a` ) VALUES( 'safe data' )";
$stripped_query = self::$_wpdb->strip_invalid_text_from_query( $safe_query );
self::$_wpdb->query( "DROP TABLE $tablename" );
self::$_wpdb->charset = $charset;
$this->assertEquals( $safe_query, $stripped_query );
}
}