Make WordPress Core


Ignore:
Timestamp:
04/27/2015 02:47:22 PM (11 years ago)
Author:
pento
Message:

WPDB: Sanity check that any strings being stored in the DB are not too long to store correctly.

Merge [32299] to the 4.1 and 4.2 branches.

File:
1 edited

Legend:

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

    r32262 r32307  
    19271927        protected function process_fields( $table, $data, $format ) {
    19281928                $data = $this->process_field_formats( $data, $format );
     1929                if ( false === $data ) {
     1930                        return false;
     1931                }
     1932
    19291933                $data = $this->process_field_charsets( $data, $table );
     1934                if ( false === $data ) {
     1935                        return false;
     1936                }
     1937
     1938                $data = $this->process_field_lengths( $data, $table );
    19301939                if ( false === $data ) {
    19311940                        return false;
     
    20042013                                // This isn't ASCII. Don't have strip_invalid_text() re-check.
    20052014                                $value['ascii'] = false;
     2015                        }
     2016
     2017                        $data[ $field ] = $value;
     2018                }
     2019
     2020                return $data;
     2021        }
     2022
     2023        /**
     2024         * For string fields, record the maximum string length that field can safely save.
     2025         *
     2026         * @since 4.2.1
     2027         * @access protected
     2028         *
     2029         * @param array  $data  As it comes from the wpdb::process_field_charsets() method.
     2030         * @param string $table Table name.
     2031         * @return array|False The same array as $data with additional 'length' keys, or false if
     2032         *                     any of the values were too long for their corresponding field.
     2033         */
     2034        protected function process_field_lengths( $data, $table ) {
     2035                foreach ( $data as $field => $value ) {
     2036                        if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
     2037                                // We can skip this field if we know it isn't a string.
     2038                                // This checks %d/%f versus ! %s because it's sprintf() could take more.
     2039                                $value['length'] = false;
     2040                        } else {
     2041                                $value['length'] = $this->get_col_length( $table, $field );
     2042                                if ( is_wp_error( $value['length'] ) ) {
     2043                                        return false;
     2044                                }
     2045                        }
     2046
     2047                        if ( false !== $value['length'] && strlen( $value['value'] ) > $value['length'] ) {
     2048                                return false;
    20062049                        }
    20072050
     
    23372380
    23382381        /**
     2382         * Retrieve the maximum string length allowed in a given column.
     2383         *
     2384         * @since 4.2.1
     2385         * @access public
     2386         *
     2387         * @param string $table  Table name.
     2388         * @param string $column Column name.
     2389         * @return mixed Max column length as an int. False if the column has no
     2390         *               length. WP_Error object if there was an error.
     2391         */
     2392        public function get_col_length( $table, $column ) {
     2393                $tablekey = strtolower( $table );
     2394                $columnkey = strtolower( $column );
     2395
     2396                // Skip this entirely if this isn't a MySQL database.
     2397                if ( false === $this->is_mysql ) {
     2398                        return false;
     2399                }
     2400
     2401                if ( empty( $this->col_meta[ $tablekey ] ) ) {
     2402                        // This primes column information for us.
     2403                        $table_charset = $this->get_table_charset( $table );
     2404                        if ( is_wp_error( $table_charset ) ) {
     2405                                return $table_charset;
     2406                        }
     2407                }
     2408
     2409                if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
     2410                        return false;
     2411                }
     2412
     2413                $typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type );
     2414
     2415                $type = strtolower( $typeinfo[0] );
     2416                if ( ! empty( $typeinfo[1] ) ) {
     2417                        $length = trim( $typeinfo[1], ')' );
     2418                } else {
     2419                        $length = false;
     2420                }
     2421
     2422                switch( $type ) {
     2423                        case 'binary':
     2424                        case 'char':
     2425                        case 'varbinary':
     2426                        case 'varchar':
     2427                                return $length;
     2428                                break;
     2429                        case 'tinyblob':
     2430                        case 'tinytext':
     2431                                return 255; // 2^8 - 1
     2432                                break;
     2433                        case 'blob':
     2434                        case 'text':
     2435                                return 65535; // 2^16 - 1
     2436                                break;
     2437                        case 'mediumblob':
     2438                        case 'mediumtext':
     2439                                return 16777215; // 2^24 - 1
     2440                                break;
     2441                        case 'longblob':
     2442                        case 'longtext':
     2443                                return 4294967295; // 2^32 - 1
     2444                                break;
     2445                        default:
     2446                                return false;
     2447                }
     2448
     2449                return false;
     2450        }
     2451
     2452        /**
    23392453         * Check if a string is ASCII.
    23402454         *
Note: See TracChangeset for help on using the changeset viewer.