From d55c605242f5f291e73f3a16b57921b614189422 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 17 Nov 2015 06:12:08 +0000 Subject: [PATCH] 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 --- src/wp-includes/wp-db.php | 14 ++++++++++++-- tests/phpunit/tests/db/charset.php | 22 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 22fa6aa475..caae412afa 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -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'] ); diff --git a/tests/phpunit/tests/db/charset.php b/tests/phpunit/tests/db/charset.php index cb605d697c..cfd025e003 100644 --- a/tests/phpunit/tests/db/charset.php +++ b/tests/phpunit/tests/db/charset.php @@ -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 ); + } }