Make WordPress Core


Ignore:
Timestamp:
04/27/2015 06:34:09 PM (11 years ago)
Author:
mdawaffe
Message:

3.7:

  • 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.7/src/wp-includes/wp-db.php

    r32275 r32318  
    15141514    protected function process_fields( $table, $data, $format ) {
    15151515        $data = $this->process_field_formats( $data, $format );
     1516        if ( false === $data ) {
     1517            return false;
     1518        }
     1519
    15161520        $data = $this->process_field_charsets( $data, $table );
     1521        if ( false === $data ) {
     1522            return false;
     1523        }
     1524
     1525        $data = $this->process_field_lengths( $data, $table );
    15171526        if ( false === $data ) {
    15181527            return false;
     
    15911600                // This isn't ASCII. Don't have strip_invalid_text() re-check.
    15921601                $value['ascii'] = false;
     1602            }
     1603
     1604            $data[ $field ] = $value;
     1605        }
     1606
     1607        return $data;
     1608    }
     1609
     1610    /**
     1611     * For string fields, record the maximum string length that field can safely save.
     1612     *
     1613     * @since 4.2.1
     1614     * @access protected
     1615     *
     1616     * @param array  $data  As it comes from the wpdb::process_field_charsets() method.
     1617     * @param string $table Table name.
     1618     * @return array|False The same array as $data with additional 'length' keys, or false if
     1619     *                     any of the values were too long for their corresponding field.
     1620     */
     1621    protected function process_field_lengths( $data, $table ) {
     1622        foreach ( $data as $field => $value ) {
     1623            if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
     1624                // We can skip this field if we know it isn't a string.
     1625                // This checks %d/%f versus ! %s because it's sprintf() could take more.
     1626                $value['length'] = false;
     1627            } else {
     1628                $value['length'] = $this->get_col_length( $table, $field );
     1629                if ( is_wp_error( $value['length'] ) ) {
     1630                    return false;
     1631                }
     1632            }
     1633
     1634            if ( false !== $value['length'] && strlen( $value['value'] ) > $value['length'] ) {
     1635                return false;
    15931636            }
    15941637
     
    19151958
    19161959    /**
     1960     * Retrieve the maximum string length allowed in a given column.
     1961     *
     1962     * @since 4.2.1
     1963     * @access public
     1964     *
     1965     * @param string $table  Table name.
     1966     * @param string $column Column name.
     1967     * @return mixed Max column length as an int. False if the column has no
     1968     *               length. WP_Error object if there was an error.
     1969     */
     1970    public function get_col_length( $table, $column ) {
     1971        $tablekey = strtolower( $table );
     1972        $columnkey = strtolower( $column );
     1973
     1974        // Skip this entirely if this isn't a MySQL database.
     1975        if ( false === $this->is_mysql ) {
     1976            return false;
     1977        }
     1978
     1979        if ( empty( $this->col_meta[ $tablekey ] ) ) {
     1980            // This primes column information for us.
     1981            $table_charset = $this->get_table_charset( $table );
     1982            if ( is_wp_error( $table_charset ) ) {
     1983                return $table_charset;
     1984            }
     1985        }
     1986
     1987        if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
     1988            return false;
     1989        }
     1990
     1991        $typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type );
     1992
     1993        $type = strtolower( $typeinfo[0] );
     1994        if ( ! empty( $typeinfo[1] ) ) {
     1995            $length = trim( $typeinfo[1], ')' );
     1996        } else {
     1997            $length = false;
     1998        }
     1999
     2000        switch( $type ) {
     2001            case 'binary':
     2002            case 'char':
     2003            case 'varbinary':
     2004            case 'varchar':
     2005                return $length;
     2006                break;
     2007            case 'tinyblob':
     2008            case 'tinytext':
     2009                return 255; // 2^8 - 1
     2010                break;
     2011            case 'blob':
     2012            case 'text':
     2013                return 65535; // 2^16 - 1
     2014                break;
     2015            case 'mediumblob':
     2016            case 'mediumtext':
     2017                return 16777215; // 2^24 - 1
     2018                break;
     2019            case 'longblob':
     2020            case 'longtext':
     2021                return 4294967295; // 2^32 - 1
     2022                break;
     2023            default:
     2024                return false;
     2025        }
     2026
     2027        return false;
     2028    }
     2029
     2030    /**
    19172031     * Check if a string is ASCII.
    19182032     *
Note: See TracChangeset for help on using the changeset viewer.