Make WordPress Core

Changeset 35655


Ignore:
Timestamp:
11/17/2015 06:12:08 AM (10 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r35544 r35655  
    28122812                    }
    28132813
     2814                    if ( $this->charset ) {
     2815                        $connection_charset = $this->charset;
     2816                    } else {
     2817                        if ( $this->use_mysqli ) {
     2818                            $connection_charset = mysqli_character_set_name( $this->dbh );
     2819                        } else {
     2820                            $connection_charset = mysql_client_encoding();
     2821                        }
     2822                    }
     2823
    28142824                    if ( is_array( $value['length'] ) ) {
    2815                         $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING {$this->charset} )", $value['value'], $value['length']['length'] );
     2825                        $queries[ $col ] = $this->prepare( "CONVERT( LEFT( CONVERT( %s USING $charset ), %.0f ) USING $connection_charset )", $value['value'], $value['length']['length'] );
    28162826                    } else if ( 'binary' !== $charset ) {
    28172827                        // If we don't have a length, there's no need to convert binary - it will always return the same result.
    2818                         $queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING {$this->charset} )", $value['value'] );
     2828                        $queries[ $col ] = $this->prepare( "CONVERT( CONVERT( %s USING $charset ) USING $connection_charset )", $value['value'] );
    28192829                    }
    28202830
  • trunk/tests/phpunit/tests/db/charset.php

    r35186 r35655  
    866866        $this->assertEquals( $safe_query, $stripped_query );
    867867    }
     868
     869    /**
     870     * @ticket 34708
     871     */
     872    function test_no_db_charset_defined() {
     873        $tablename = 'test_cp1251_query_' . rand_str( 5 );
     874        if ( ! self::$_wpdb->query( "CREATE TABLE $tablename ( a VARCHAR(50) ) DEFAULT CHARSET 'cp1251'" ) ) {
     875            $this->markTestSkipped( "Test requires the 'cp1251' charset" );
     876        }
     877
     878        $charset = self::$_wpdb->charset;
     879        self::$_wpdb->charset = '';
     880
     881        $safe_query = "INSERT INTO $tablename( `a` ) VALUES( 'safe data' )";
     882        $stripped_query = self::$_wpdb->strip_invalid_text_from_query( $safe_query );
     883
     884        self::$_wpdb->query( "DROP TABLE $tablename" );
     885
     886        self::$_wpdb->charset = $charset;
     887
     888        $this->assertEquals( $safe_query, $stripped_query );
     889    }
    868890}
Note: See TracChangeset for help on using the changeset viewer.