Make WordPress Core


Ignore:
Timestamp:
04/27/2015 06:33:43 PM (10 years ago)
Author:
mdawaffe
Message:

3.8:

  • WPDB: Sanity check that any strings being stored in the DB are not too long to store correctly.
  • When upgrading, remove any suspicious comments.
File:
1 edited

Legend:

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

    r32274 r32317  
    15221522    protected function process_fields( $table, $data, $format ) {
    15231523        $data = $this->process_field_formats( $data, $format );
     1524        if ( false === $data ) {
     1525            return false;
     1526        }
     1527
    15241528        $data = $this->process_field_charsets( $data, $table );
     1529        if ( false === $data ) {
     1530            return false;
     1531        }
     1532
     1533        $data = $this->process_field_lengths( $data, $table );
    15251534        if ( false === $data ) {
    15261535            return false;
     
    15991608                // This isn't ASCII. Don't have strip_invalid_text() re-check.
    16001609                $value['ascii'] = false;
     1610            }
     1611
     1612            $data[ $field ] = $value;
     1613        }
     1614
     1615        return $data;
     1616    }
     1617
     1618    /**
     1619     * For string fields, record the maximum string length that field can safely save.
     1620     *
     1621     * @since 4.2.1
     1622     * @access protected
     1623     *
     1624     * @param array  $data  As it comes from the wpdb::process_field_charsets() method.
     1625     * @param string $table Table name.
     1626     * @return array|False The same array as $data with additional 'length' keys, or false if
     1627     *                     any of the values were too long for their corresponding field.
     1628     */
     1629    protected function process_field_lengths( $data, $table ) {
     1630        foreach ( $data as $field => $value ) {
     1631            if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
     1632                // We can skip this field if we know it isn't a string.
     1633                // This checks %d/%f versus ! %s because it's sprintf() could take more.
     1634                $value['length'] = false;
     1635            } else {
     1636                $value['length'] = $this->get_col_length( $table, $field );
     1637                if ( is_wp_error( $value['length'] ) ) {
     1638                    return false;
     1639                }
     1640            }
     1641
     1642            if ( false !== $value['length'] && strlen( $value['value'] ) > $value['length'] ) {
     1643                return false;
    16011644            }
    16021645
     
    19231966
    19241967    /**
     1968     * Retrieve the maximum string length allowed in a given column.
     1969     *
     1970     * @since 4.2.1
     1971     * @access public
     1972     *
     1973     * @param string $table  Table name.
     1974     * @param string $column Column name.
     1975     * @return mixed Max column length as an int. False if the column has no
     1976     *               length. WP_Error object if there was an error.
     1977     */
     1978    public function get_col_length( $table, $column ) {
     1979        $tablekey = strtolower( $table );
     1980        $columnkey = strtolower( $column );
     1981
     1982        // Skip this entirely if this isn't a MySQL database.
     1983        if ( false === $this->is_mysql ) {
     1984            return false;
     1985        }
     1986
     1987        if ( empty( $this->col_meta[ $tablekey ] ) ) {
     1988            // This primes column information for us.
     1989            $table_charset = $this->get_table_charset( $table );
     1990            if ( is_wp_error( $table_charset ) ) {
     1991                return $table_charset;
     1992            }
     1993        }
     1994
     1995        if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
     1996            return false;
     1997        }
     1998
     1999        $typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type );
     2000
     2001        $type = strtolower( $typeinfo[0] );
     2002        if ( ! empty( $typeinfo[1] ) ) {
     2003            $length = trim( $typeinfo[1], ')' );
     2004        } else {
     2005            $length = false;
     2006        }
     2007
     2008        switch( $type ) {
     2009            case 'binary':
     2010            case 'char':
     2011            case 'varbinary':
     2012            case 'varchar':
     2013                return $length;
     2014                break;
     2015            case 'tinyblob':
     2016            case 'tinytext':
     2017                return 255; // 2^8 - 1
     2018                break;
     2019            case 'blob':
     2020            case 'text':
     2021                return 65535; // 2^16 - 1
     2022                break;
     2023            case 'mediumblob':
     2024            case 'mediumtext':
     2025                return 16777215; // 2^24 - 1
     2026                break;
     2027            case 'longblob':
     2028            case 'longtext':
     2029                return 4294967295; // 2^32 - 1
     2030                break;
     2031            default:
     2032                return false;
     2033        }
     2034
     2035        return false;
     2036    }
     2037
     2038    /**
    19252039     * Check if a string is ASCII.
    19262040     *
Note: See TracChangeset for help on using the changeset viewer.