From 4e39f0ba11ff3cbb5ca51f5ccdcf5c46c9e600d9 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 21 Apr 2015 06:58:14 +0000 Subject: [PATCH] WPDB: When deciding if a query needs extra sanity checking based on collation, return early when we can. Merges [32232] and [32233] to the 3.9 branch. See #32029. Built from https://develop.svn.wordpress.org/branches/3.9@32239 git-svn-id: http://core.svn.wordpress.org/branches/3.9@32210 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/wp-db.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php index 4b86cb7381..7f5876e4ad 100644 --- a/wp-includes/wp-db.php +++ b/wp-includes/wp-db.php @@ -2335,20 +2335,38 @@ class wpdb { if ( $this->checking_collation ) { return true; } + + // We don't need to check the collation for queries that don't read data. + $query = ltrim( $query, "\r\n\t (" ); + if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN)\s/i', $query ) ) { + return true; + } + + // All-ASCII queries don't need extra checking. + if ( $this->check_ascii( $query ) ) { + return true; + } + $table = $this->get_table_from_query( $query ); if ( ! $table ) { return false; } $this->checking_collation = true; - $this->get_table_charset( $table ); + $collation = $this->get_table_charset( $table ); $this->checking_collation = false; + // Tables with no collation, or latin1 only, don't need extra checking. + if ( false === $collation || 'latin1' === $collation ) { + return true; + } + $table = strtolower( $table ); if ( empty( $this->col_meta[ $table ] ) ) { return false; } + // If any of the columns don't have one of these collations, it needs more sanity checking. foreach( $this->col_meta[ $table ] as $col ) { if ( empty( $col->Collation ) ) { continue; @@ -2376,6 +2394,7 @@ class wpdb { * such as 'field' are retained in each value array. If we cannot * remove invalid characters, a {@see WP_Error} object is returned. */ + // If any of the columns don't have one of these collations, it needs more sanity checking. protected function strip_invalid_text( $data ) { // Some multibyte character sets that we can check in PHP. $mb_charsets = array(