Make WordPress Core


Ignore:
Timestamp:
12/10/2014 10:56:25 PM (10 years ago)
Author:
pento
Message:

WPDB: In [30345,30346,30366,30375], we added sanity checks for saving posts containing emoji. We need to revert these checks for now, as they're not ready for release in 4.1.

See #21212.

File:
1 edited

Legend:

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

    r30768 r30807  
    145145
    146146    /**
    147      * Cached column info, for sanity checking data before inserting
    148      *
    149      * @since 4.1.0
    150      * @access protected
    151      * @var array
    152      */
    153     protected $col_meta = array();
    154 
    155     /**
    156      * Calculated character sets on tables
    157      *
    158      * @since 4.1.0
    159      * @access protected
    160      * @var array
    161      */
    162     protected $table_charset = array();
    163 
    164     /**
    165      * Whether text fields in the current query need to be sanity checked.
    166      *
    167      * @since 4.1.0
    168      * @access protected
    169      * @var bool
    170      */
    171     protected $check_current_query = true;
    172 
    173     /**
    174147     * Saved info on the table column
    175148     *
     
    675648     */
    676649    public function __set( $name, $value ) {
    677         $protected_members = array(
    678             'col_meta',
    679             'table_charset',
    680             'check_current_query',
    681         );
    682         if (  in_array( $name, $protected_members, true ) ) {
    683             return;
    684         }
    685650        $this->$name = $value;
    686651    }
     
    14791444            $this->has_connected = true;
    14801445            $this->set_charset( $this->dbh );
     1446            $this->set_sql_mode();
    14811447            $this->ready = true;
    1482             $this->set_sql_mode();
    14831448            $this->select( $this->dbname, $this->dbh );
    14841449
     
    15771542    public function query( $query ) {
    15781543        if ( ! $this->ready ) {
    1579             $this->check_current_query = true;
    15801544            return false;
    15811545        }
     
    15971561        // Log how the function was called
    15981562        $this->func_call = "\$db->query(\"$query\")";
    1599 
    1600         // If we're writing to the database, make sure the query will write safely.
    1601         if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
    1602             $stripped_query = $this->strip_invalid_text_from_query( $query );
    1603             if ( $stripped_query !== $query ) {
    1604                 $this->insert_id = 0;
    1605                 return false;
    1606             }
    1607         }
    1608 
    1609         $this->check_current_query = true;
    16101563
    16111564        // Keep track of the last query for debug..
     
    17781731     */
    17791732    function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) {
    1780         if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) ) {
     1733        if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ) ) )
    17811734            return false;
    1782         }
    1783 
    1784         $data = $this->process_fields( $table, $data, $format );
    1785         if ( false === $data ) {
    1786             return false;
    1787         }
    1788 
    1789         $formats = $values = array();
    1790         foreach ( $data as $value ) {
    1791             $formats[] = $value['format'];
    1792             $values[]  = $value['value'];
    1793         }
    1794 
    1795         $fields  = '`' . implode( '`, `', array_keys( $data ) ) . '`';
    1796         $formats = implode( ', ', $formats );
    1797 
    1798         $sql = "$type INTO `$table` ($fields) VALUES ($formats)";
    1799 
    18001735        $this->insert_id = 0;
    1801         $this->check_current_query = false;
    1802         return $this->query( $this->prepare( $sql, $values ) );
     1736        $formats = $format = (array) $format;
     1737        $fields = array_keys( $data );
     1738        $formatted_fields = array();
     1739        foreach ( $fields as $field ) {
     1740            if ( !empty( $format ) )
     1741                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
     1742            elseif ( isset( $this->field_types[$field] ) )
     1743                $form = $this->field_types[$field];
     1744            else
     1745                $form = '%s';
     1746            $formatted_fields[] = $form;
     1747        }
     1748        $sql = "{$type} INTO `$table` (`" . implode( '`,`', $fields ) . "`) VALUES (" . implode( ",", $formatted_fields ) . ")";
     1749        return $this->query( $this->prepare( $sql, $data ) );
    18031750    }
    18041751
     
    18231770     */
    18241771    public function update( $table, $data, $where, $format = null, $where_format = null ) {
    1825         if ( ! is_array( $data ) || ! is_array( $where ) ) {
     1772        if ( ! is_array( $data ) || ! is_array( $where ) )
    18261773            return false;
    1827         }
    1828 
    1829         $data = $this->process_fields( $table, $data, $format );
    1830         if ( false === $data ) {
    1831             return false;
    1832         }
    1833         $where = $this->process_fields( $table, $where, $where_format );
    1834         if ( false === $where ) {
    1835             return false;
    1836         }
    1837 
    1838         $fields = $conditions = $values = array();
    1839         foreach ( $data as $field => $value ) {
    1840             $fields[] = "`$field` = " . $value['format'];
    1841             $values[] = $value['value'];
    1842         }
    1843         foreach ( $where as $field => $value ) {
    1844             $conditions[] = "`$field` = " . $value['format'];
    1845             $values[] = $value['value'];
    1846         }
    1847 
    1848         $fields = implode( ', ', $fields );
    1849         $conditions = implode( ' AND ', $conditions );
    1850 
    1851         $sql = "UPDATE `$table` SET $fields WHERE $conditions";
    1852 
    1853         $this->check_current_query = false;
    1854         return $this->query( $this->prepare( $sql, $values ) );
     1774
     1775        $formats = $format = (array) $format;
     1776        $bits = $wheres = array();
     1777        foreach ( (array) array_keys( $data ) as $field ) {
     1778            if ( !empty( $format ) )
     1779                $form = ( $form = array_shift( $formats ) ) ? $form : $format[0];
     1780            elseif ( isset($this->field_types[$field]) )
     1781                $form = $this->field_types[$field];
     1782            else
     1783                $form = '%s';
     1784            $bits[] = "`$field` = {$form}";
     1785        }
     1786
     1787        $where_formats = $where_format = (array) $where_format;
     1788        foreach ( (array) array_keys( $where ) as $field ) {
     1789            if ( !empty( $where_format ) )
     1790                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
     1791            elseif ( isset( $this->field_types[$field] ) )
     1792                $form = $this->field_types[$field];
     1793            else
     1794                $form = '%s';
     1795            $wheres[] = "`$field` = {$form}";
     1796        }
     1797
     1798        $sql = "UPDATE `$table` SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres );
     1799        return $this->query( $this->prepare( $sql, array_merge( array_values( $data ), array_values( $where ) ) ) );
    18551800    }
    18561801
     
    18721817     */
    18731818    public function delete( $table, $where, $where_format = null ) {
    1874         if ( ! is_array( $where ) ) {
     1819        if ( ! is_array( $where ) )
    18751820            return false;
    1876         }
    1877 
    1878         $where = $this->process_fields( $table, $where, $where_format );
    1879         if ( false === $where ) {
    1880             return false;
    1881         }
    1882 
    1883         $conditions = $values = array();
    1884         foreach ( $where as $field => $value ) {
    1885             $conditions[] = "`$field` = " . $value['format'];
    1886             $values[] = $value['value'];
    1887         }
    1888 
    1889         $conditions = implode( ' AND ', $conditions );
    1890 
    1891         $sql = "DELETE FROM `$table` WHERE $conditions";
    1892 
    1893         $this->check_current_query = false;
    1894         return $this->query( $this->prepare( $sql, $values ) );
    1895     }
    1896 
    1897     /**
    1898      * Processes arrays of field/value pairs and field formats.
    1899      *
    1900      * This is a helper method for wpdb's CRUD methods, which take field/value
    1901      * pairs for inserts, updates, and where clauses. This method first pairs
    1902      * each value with a format. Then it determines the charset of that field,
    1903      * using that to determine if any invalid text would be stripped. If text is
    1904      * stripped, then field processing is rejected and the query fails.
    1905      *
    1906      * @since 4.1.0
    1907      * @access protected
    1908      *
    1909      * @param string $table  Table name.
    1910      * @param array  $data   Field/value pair.
    1911      * @param mixed  $format Format for each field.
    1912      * @return array|bool Returns an array of fields that contain paired values
    1913      *                    and formats. Returns false for invalid values.
    1914      */
    1915     protected function process_fields( $table, $data, $format ) {
    1916         $data = $this->process_field_formats( $data, $format );
    1917         $data = $this->process_field_charsets( $data, $table );
    1918         if ( false === $data ) {
    1919             return false;
    1920         }
    1921 
    1922         $converted_data = $this->strip_invalid_text( $data );
    1923 
    1924         if ( $data !== $converted_data ) {
    1925             return false;
    1926         }
    1927 
    1928         return $data;
    1929     }
    1930 
    1931     /**
    1932      * Prepares arrays of value/format pairs as passed to wpdb CRUD methods.
    1933      *
    1934      * @since 4.1.0
    1935      * @access protected
    1936      *
    1937      * @param array $data   Array of fields to values.
    1938      * @param mixed $format Formats to be mapped to the values in $data.
    1939      * @return array Array, keyed by field names with values being an array
    1940      *               of 'value' and 'format' keys.
    1941      */
    1942     protected function process_field_formats( $data, $format ) {
    1943         $formats = $original_formats = (array) $format;
    1944 
    1945         foreach ( $data as $field => $value ) {
    1946             $value = array(
    1947                 'value'  => $value,
    1948                 'format' => '%s',
    1949             );
    1950 
    1951             if ( ! empty( $format ) ) {
    1952                 $value['format'] = array_shift( $formats );
    1953                 if ( ! $value['format'] ) {
    1954                     $value['format'] = reset( $original_formats );
    1955                 }
     1821
     1822        $wheres = array();
     1823
     1824        $where_formats = $where_format = (array) $where_format;
     1825
     1826        foreach ( array_keys( $where ) as $field ) {
     1827            if ( !empty( $where_format ) ) {
     1828                $form = ( $form = array_shift( $where_formats ) ) ? $form : $where_format[0];
    19561829            } elseif ( isset( $this->field_types[ $field ] ) ) {
    1957                 $value['format'] = $this->field_types[ $field ];
    1958             }
    1959 
    1960             $data[ $field ] = $value;
    1961         }
    1962 
    1963         return $data;
    1964     }
    1965 
    1966     /**
    1967      * Adds field charsets to field/value/format arrays generated by
    1968      * the {@see wpdb::process_field_formats()} method.
    1969      *
    1970      * @since 4.1.0
    1971      * @access protected
    1972      *
    1973      * @param array  $data  As it comes from the {@see wpdb::process_field_formats()} method.
    1974      * @param string $table Table name.
    1975      * @return The same array as $data with additional 'charset' keys.
    1976      */
    1977     protected function process_field_charsets( $data, $table ) {
    1978         foreach ( $data as $field => $value ) {
    1979             if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
    1980                 // We can skip this field if we know it isn't a string.
    1981                 // This checks %d/%f versus ! %s because it's sprintf() could take more.
    1982                 $value['charset'] = false;
    1983             } elseif ( $this->check_ascii( $value['value'] ) ) {
    1984                 // If it's ASCII, then we don't need the charset. We can skip this field.
    1985                 $value['charset'] = false;
     1830                $form = $this->field_types[ $field ];
    19861831            } else {
    1987                 $value['charset'] = $this->get_col_charset( $table, $field );
    1988                 if ( is_wp_error( $value['charset'] ) ) {
    1989                     return false;
    1990                 }
    1991 
    1992                 // This isn't ASCII. Don't have strip_invalid_text() re-check.
    1993                 $value['ascii'] = false;
    1994             }
    1995 
    1996             $data[ $field ] = $value;
    1997         }
    1998 
    1999         return $data;
     1832                $form = '%s';
     1833            }
     1834
     1835            $wheres[] = "$field = $form";
     1836        }
     1837
     1838        $sql = "DELETE FROM $table WHERE " . implode( ' AND ', $wheres );
     1839        return $this->query( $this->prepare( $sql, $where ) );
    20001840    }
    20011841
     
    20181858
    20191859        if ( $query ) {
    2020             $this->check_current_query = false;
    20211860            $this->query( $query );
    20221861        }
     
    20471886        $this->func_call = "\$db->get_row(\"$query\",$output,$y)";
    20481887        if ( $query ) {
    2049             $this->check_current_query = false;
    20501888            $this->query( $query );
    20511889        } else {
     
    20851923    public function get_col( $query = null , $x = 0 ) {
    20861924        if ( $query ) {
    2087             $this->check_current_query = false;
    20881925            $this->query( $query );
    20891926        }
     
    21141951
    21151952        if ( $query ) {
    2116             $this->check_current_query = false;
    21171953            $this->query( $query );
    21181954        } else {
     
    21561992
    21571993    /**
    2158      * Retrieves the character set for the given table.
    2159      *
    2160      * @since 4.1.0
    2161      * @access protected
    2162      *
    2163      * @param string $table Table name.
    2164      * @return string|WP_Error Table character set, {@see WP_Error} object if it couldn't be found.
    2165      */
    2166     protected function get_table_charset( $table ) {
    2167         $tablekey = strtolower( $table );
    2168 
    2169         /**
    2170          * Filter the table charset value before the DB is checked.
    2171          *
    2172          * Passing a non-null value to the filter will effectively short-circuit
    2173          * checking the DB for the charset, returning that value instead.
    2174          *
    2175          * @since 4.1.0
    2176          *
    2177          * @param string $charset The character set to use. Default null.
    2178          * @param string $table   The name of the table being checked.
    2179          */
    2180         $charset = apply_filters( 'pre_get_table_charset', null, $table );
    2181         if ( null !== $charset ) {
    2182             return $charset;
    2183         }
    2184 
    2185         if ( isset( $this->table_charset[ $tablekey ] ) ) {
    2186             return $this->table_charset[ $tablekey ];
    2187         }
    2188 
    2189         $charsets = $columns = array();
    2190         $results = $this->get_results( "SHOW FULL COLUMNS FROM `$table`" );
    2191         if ( ! $results ) {
    2192             return new WP_Error( 'wpdb_get_table_charset_failure' );
    2193         }
    2194 
    2195         foreach ( $results as $column ) {
    2196             $columns[ strtolower( $column->Field ) ] = $column;
    2197         }
    2198 
    2199         $this->col_meta[ $tablekey ] = $columns;
    2200 
    2201         foreach ( $columns as $column ) {
    2202             if ( ! empty( $column->Collation ) ) {
    2203                 list( $charset ) = explode( '_', $column->Collation );
    2204                 $charsets[ strtolower( $charset ) ] = true;
    2205             }
    2206 
    2207             list( $type ) = explode( '(', $column->Type );
    2208 
    2209             // A binary/blob means the whole query gets treated like this.
    2210             if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ) ) ) {
    2211                 $this->table_charset[ $tablekey ] = 'binary';
    2212                 return 'binary';
    2213             }
    2214         }
    2215 
    2216         // utf8mb3 is an alias for utf8.
    2217         if ( isset( $charsets['utf8mb3'] ) ) {
    2218             $charsets['utf8'] = true;
    2219             unset( $charsets['utf8mb3'] );
    2220         }
    2221 
    2222         // Check if we have more than one charset in play.
    2223         $count = count( $charsets );
    2224         if ( 1 === $count ) {
    2225             $charset = key( $charsets );
    2226         } elseif ( 0 === $count ) {
    2227             // No charsets, assume this table can store whatever.
    2228             $charset = false;
    2229         } else {
    2230             // More than one charset. Remove latin1 if present and recalculate.
    2231             unset( $charsets['latin1'] );
    2232             $count = count( $charsets );
    2233             if ( 1 === $count ) {
    2234                 // Only one charset (besides latin1).
    2235                 $charset = key( $charsets );
    2236             } elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) {
    2237                 // Two charsets, but they're utf8 and utf8mb4, use utf8.
    2238                 $charset = 'utf8';
    2239             } else {
    2240                 // Two mixed character sets. ascii.
    2241                 $charset = 'ascii';
    2242             }
    2243         }
    2244 
    2245         $this->table_charset[ $tablekey ] = $charset;
    2246         return $charset;
    2247     }
    2248 
    2249     /**
    2250      * Retrieves the character set for the given column.
    2251      *
    2252      * @since 4.1.0
    2253      * @access protected
    2254      *
    2255      * @param string $table  Table name.
    2256      * @param string $column Column name.
    2257      * @return mixed Column character set as a string. False if the column has no
    2258      *               character set. {@see WP_Error} object if there was an error.
    2259      */
    2260     protected function get_col_charset( $table, $column ) {
    2261         $tablekey = strtolower( $table );
    2262         $columnkey = strtolower( $column );
    2263 
    2264         /**
    2265          * Filter the column charset value before the DB is checked.
    2266          *
    2267          * Passing a non-null value to the filter will short-circuit
    2268          * checking the DB for the charset, returning that value instead.
    2269          *
    2270          * @since 4.1.0
    2271          *
    2272          * @param string $charset The character set to use. Default null.
    2273          * @param string $table   The name of the table being checked.
    2274          * @param string $column  The name of the column being checked.
    2275          */
    2276         $charset = apply_filters( 'pre_get_col_charset', null, $table, $column );
    2277         if ( null !== $charset ) {
    2278             return $charset;
    2279         }
    2280 
    2281         // Skip this entirely if this isn't a MySQL database.
    2282         if ( false === $this->is_mysql ) {
    2283             return false;
    2284         }
    2285 
    2286         if ( empty( $this->table_charset[ $tablekey ] ) ) {
    2287             // This primes column information for us.
    2288             $table_charset = $this->get_table_charset( $table );
    2289             if ( is_wp_error( $table_charset ) ) {
    2290                 return $table_charset;
    2291             }
    2292         }
    2293 
    2294         // If still no column information, return the table charset.
    2295         if ( empty( $this->col_meta[ $tablekey ] ) ) {
    2296             return $this->table_charset[ $tablekey ];
    2297         }
    2298 
    2299         // If this column doesn't exist, return the table charset.
    2300         if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
    2301             return $this->table_charset[ $tablekey ];
    2302         }
    2303 
    2304         // Return false when it's not a string column.
    2305         if ( empty( $this->col_meta[ $tablekey ][ $columnkey ]->Collation ) ) {
    2306             return false;
    2307         }
    2308 
    2309         list( $charset ) = explode( '_', $this->col_meta[ $tablekey ][ $columnkey ]->Collation );
    2310         return $charset;
    2311     }
    2312 
    2313     /**
    2314      * Check if a string is ASCII.
    2315      *
    2316      * The negative regex is faster for non-ASCII strings, as it allows
    2317      * the search to finish as soon as it encounters a non-ASCII character.
    2318      *
    2319      * @since 4.1.0
    2320      * @access protected
    2321      *
    2322      * @param string $string String to check.
    2323      * @return bool True if ASCII, false if not.
    2324      */
    2325     protected function check_ascii( $string ) {
    2326         if ( function_exists( 'mb_check_encoding' ) ) {
    2327             if ( mb_check_encoding( $string, 'ASCII' ) ) {
    2328                 return true;
    2329             }
    2330         } elseif ( ! preg_match( '/[^\x00-\x7F]/', $string ) ) {
    2331             return true;
    2332         }
    2333 
    2334         return false;
    2335     }
    2336 
    2337     /**
    2338      * Strips any invalid characters based on value/charset pairs.
    2339      *
    2340      * @since 4.1.0
    2341      * @access protected
    2342      *
    2343      * @param array $data Array of value arrays. Each value array has the keys
    2344      *                    'value' and 'charset'. An optional 'ascii' key can be
    2345      *                    set to false to avoid redundant ASCII checks.
    2346      * @return array|WP_Error The $data parameter, with invalid characters removed from
    2347      *                        each value. This works as a passthrough: any additional keys
    2348      *                        such as 'field' are retained in each value array. If we cannot
    2349      *                        remove invalid characters, a {@see WP_Error} object is returned.
    2350      */
    2351     protected function strip_invalid_text( $data ) {
    2352         // Some multibyte character sets that we can check in PHP.
    2353         $mb_charsets = array(
    2354             'ascii'   => 'ASCII',
    2355             'big5'    => 'BIG-5',
    2356             'eucjpms' => 'eucJP-win',
    2357             'gb2312'  => 'EUC-CN',
    2358             'ujis'    => 'EUC-JP',
    2359             'utf32'   => 'UTF-32',
    2360             'utf8mb4' => 'UTF-8',
    2361         );
    2362 
    2363         $supported_charsets = array();
    2364         if ( function_exists( 'mb_list_encodings' ) ) {
    2365             $supported_charsets = mb_list_encodings();
    2366         }
    2367 
    2368         $db_check_string = false;
    2369 
    2370         foreach ( $data as &$value ) {
    2371             $charset = $value['charset'];
    2372 
    2373             // Column isn't a string, or is latin1, which will will happily store anything.
    2374             if ( false === $charset || 'latin1' === $charset ) {
    2375                 continue;
    2376             }
    2377 
    2378             if ( ! is_string( $value['value'] ) ) {
    2379                 continue;
    2380             }
    2381 
    2382             // ASCII is always OK.
    2383             if ( ! isset( $value['ascii'] ) && $this->check_ascii( $value['value'] ) ) {
    2384                 continue;
    2385             }
    2386 
    2387             // Convert the text locally.
    2388             if ( $supported_charsets ) {
    2389                 if ( isset( $mb_charsets[ $charset ] ) && in_array( $mb_charsets[ $charset ], $supported_charsets ) ) {
    2390                     $value['value'] = mb_convert_encoding( $value['value'], $mb_charsets[ $charset ], $mb_charsets[ $charset ] );
    2391                     continue;
    2392                 }
    2393             }
    2394 
    2395             // utf8(mb3) can be handled by regex, which is a bunch faster than a DB lookup.
    2396             if ( 'utf8' === $charset || 'utf8mb3' === $charset ) {
    2397                 $regex = '/
    2398                     (
    2399                         (?: [\x00-\x7F]                  # single-byte sequences   0xxxxxxx
    2400                         |   [\xC2-\xDF][\x80-\xBF]       # double-byte sequences   110xxxxx 10xxxxxx
    2401                         |   \xE0[\xA0-\xBF][\x80-\xBF]   # triple-byte sequences   1110xxxx 10xxxxxx * 2
    2402                         |   [\xE1-\xEC][\x80-\xBF]{2}
    2403                         |   \xED[\x80-\x9F][\x80-\xBF]
    2404                         |   [\xEE-\xEF][\x80-\xBF]{2}
    2405                         ){1,50}                          # ...one or more times
    2406                     )
    2407                     | .                                  # anything else
    2408                     /x';
    2409                 $value['value'] = preg_replace( $regex, '$1', $value['value'] );
    2410                 continue;
    2411             }
    2412 
    2413             // We couldn't use any local conversions, send it to the DB.
    2414             $value['db'] = $db_check_string = true;
    2415         }
    2416         unset( $value ); // Remove by reference.
    2417 
    2418         if ( $db_check_string ) {
    2419             $queries = array();
    2420             foreach ( $data as $col => $value ) {
    2421                 if ( ! empty( $value['db'] ) ) {
    2422                     if ( ! isset( $queries[ $value['charset'] ] ) ) {
    2423                         $queries[ $value['charset'] ] = array();
    2424                     }
    2425 
    2426                     // Split the CONVERT() calls by charset, so we can make sure the connection is right
    2427                     $queries[ $value['charset'] ][ $col ] = $this->prepare( "CONVERT( %s USING {$value['charset']} )", $value['value'] );
    2428                 }
    2429             }
    2430 
    2431             $connection_charset = $this->charset;
    2432             foreach ( $queries as $charset => $query ) {
    2433                 if ( ! $query ) {
    2434                     continue;
    2435                 }
    2436 
    2437                 // Change the charset to match the string(s) we're converting
    2438                 if ( $charset !== $this->charset ) {
    2439                     $this->set_charset( $this->dbh, $charset );
    2440                 }
    2441 
    2442                 $row = $this->get_row( "SELECT " . implode( ', ', $query ), ARRAY_N );
    2443                 if ( ! $row ) {
    2444                     $this->set_charset( $this->dbh, $connection_charset );
    2445                     return new WP_Error( 'wpdb_strip_invalid_text_failure' );
    2446                 }
    2447 
    2448                 $cols = array_keys( $query );
    2449                 $col_count = count( $cols );
    2450                 for ( $ii = 0; $ii < $col_count; $ii++ ) {
    2451                     $data[ $cols[ $ii ] ]['value'] = $row[ $ii ];
    2452                 }
    2453             }
    2454 
    2455             // Don't forget to change the charset back!
    2456             if ( $connection_charset !== $this->charset ) {
    2457                 $this->set_charset( $this->dbh, $connection_charset );
    2458             }
    2459         }
    2460 
    2461         return $data;
    2462     }
    2463 
    2464     /**
    2465      * Strips any invalid characters from the query.
    2466      *
    2467      * @since 4.1.0
    2468      * @access protected
    2469      *
    2470      * @param string $query Query to convert.
    2471      * @return string|WP_Error The converted query, or a {@see WP_Error} object if the conversion fails.
    2472      */
    2473     protected function strip_invalid_text_from_query( $query ) {
    2474         $table = $this->get_table_from_query( $query );
    2475         if ( $table ) {
    2476             $charset = $this->get_table_charset( $table );
    2477             if ( is_wp_error( $charset ) ) {
    2478                 return $charset;
    2479             }
    2480 
    2481             // We can't reliably strip text from tables containing binary/blob columns
    2482             if ( 'binary' === $charset ) {
    2483                 return $query;
    2484             }
    2485         } else {
    2486             $charset = $this->charset;
    2487         }
    2488 
    2489         $data = array(
    2490             'value'   => $query,
    2491             'charset' => $charset,
    2492             'ascii'   => false,
    2493         );
    2494 
    2495         $data = $this->strip_invalid_text( array( $data ) );
    2496         if ( is_wp_error( $data ) ) {
    2497             return $data;
    2498         }
    2499 
    2500         return $data[0]['value'];
    2501     }
    2502 
    2503     /**
    2504      * Strips any invalid characters from the string for a given table and column.
    2505      *
    2506      * @since 4.1.0
    2507      * @access public
    2508      *
    2509      * @param string $table  Table name.
    2510      * @param string $column Column name.
    2511      * @param string $value  The text to check.
    2512      * @return string|WP_Error The converted string, or a `WP_Error` object if the conversion fails.
    2513      */
    2514     public function strip_invalid_text_for_column( $table, $column, $value ) {
    2515         if ( $this->check_ascii( $value ) || ! is_string( $value ) ) {
    2516             return $value;
    2517         }
    2518 
    2519         $charset = $this->get_col_charset( $table, $column );
    2520         if ( ! $charset ) {
    2521             // Not a string column.
    2522             return $value;
    2523         } elseif ( is_wp_error( $charset ) ) {
    2524             // Bail on real errors.
    2525             return $charset;
    2526         }
    2527 
    2528         $data = array(
    2529             $column => array(
    2530                 'value'   => $value,
    2531                 'charset' => $charset,
    2532                 'ascii'   => false,
    2533             )
    2534         );
    2535 
    2536         $data = $this->strip_invalid_text( $data );
    2537         if ( is_wp_error( $data ) ) {
    2538             return $data;
    2539         }
    2540 
    2541         return $data[ $column ]['value'];
    2542     }
    2543 
    2544     /**
    2545      * Find the first table name referenced in a query.
    2546      *
    2547      * @since 4.1.0
    2548      * @access protected
    2549      *
    2550      * @param string $query The query to search.
    2551      * @return string|false $table The table name found, or false if a table couldn't be found.
    2552      */
    2553     protected function get_table_from_query( $query ) {
    2554         // Remove characters that can legally trail the table name.
    2555         $query = rtrim( $query, ';/-#' );
    2556 
    2557         // Allow (select...) union [...] style queries. Use the first query's table name.
    2558         $query = ltrim( $query, "\r\n\t (" );
    2559 
    2560         /*
    2561          * Strip everything between parentheses except nested selects and use only 1,000
    2562          * chars of the query.
    2563          */
    2564         $query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', substr( $query, 0, 1000 ) );
    2565 
    2566         // Quickly match most common queries.
    2567         if ( preg_match( '/^\s*(?:'
    2568                 . 'SELECT.*?\s+FROM'
    2569                 . '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?'
    2570                 . '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
    2571                 . '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
    2572                 . '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:\s+FROM)?'
    2573                 . ')\s+`?([\w-]+)`?/is', $query, $maybe ) ) {
    2574             return $maybe[1];
    2575         }
    2576 
    2577         // SHOW TABLE STATUS and SHOW TABLES
    2578         if ( preg_match( '/^\s*(?:'
    2579                 . 'SHOW\s+TABLE\s+STATUS.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    2580                 . '|SHOW\s+(?:FULL\s+)?TABLES.+(?:LIKE\s+|WHERE\s+Name\s*=\s*)'
    2581                 . ')\W([\w-]+)\W/is', $query, $maybe ) ) {
    2582             return $maybe[1];
    2583         }
    2584 
    2585         // Big pattern for the rest of the table-related queries.
    2586         if ( preg_match( '/^\s*(?:'
    2587                 . '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
    2588                 . '|DESCRIBE|DESC|EXPLAIN|HANDLER'
    2589                 . '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?'
    2590                 . '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|REPAIR).*\s+TABLE'
    2591                 . '|TRUNCATE(?:\s+TABLE)?'
    2592                 . '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?'
    2593                 . '|ALTER(?:\s+IGNORE)?\s+TABLE'
    2594                 . '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?'
    2595                 . '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON'
    2596                 . '|DROP\s+INDEX.*\s+ON'
    2597                 . '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
    2598                 . '|(?:GRANT|REVOKE).*ON\s+TABLE'
    2599                 . '|SHOW\s+(?:.*FROM|.*TABLE)'
    2600                 . ')\s+\(*\s*`?([\w-]+)`?\s*\)*/is', $query, $maybe ) ) {
    2601             return $maybe[1];
    2602         }
    2603 
    2604         return false;
    2605     }
    2606 
    2607     /**
    26081994     * Load the column metadata from the last query.
    26091995     *
Note: See TracChangeset for help on using the changeset viewer.