Make WordPress Core


Ignore:
Timestamp:
02/20/2009 07:05:12 PM (16 years ago)
Author:
ryan
Message:

Use mysql_real_escape_string() only in prepare(), insert(), and update(). escape() uses addslashes only. Add array support to escape(). see #9189

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r10597 r10604  
    428428    }
    429429
    430     /**
    431      * Escapes content for insertion into the database, for security
    432      *
    433      * @since 0.71
    434      *
    435      * @param string $string
    436      * @return string query safe string
    437      */
    438     function escape($string) {
     430    function _weak_escape($string) {
     431        return addslashes($string);
     432    }
     433
     434    function _real_escape($string) {
    439435        if ( $this->dbh && $this->real_escape )
    440436            return mysql_real_escape_string( $string, $this->dbh );
    441437        else
    442             return addslashes( $string );
     438            return addslashes( $string );   
     439    }
     440
     441    function _escape($data) {
     442        if ( is_array($data) ) {
     443            foreach ( (array) $data as $k => $v ) {
     444                if ( is_array($v) )
     445                    $data[$k] = $this->_escape( $v );
     446                else
     447                    $data[$k] = $this->_real_escape( $v );
     448            }
     449        } else {
     450            $data = $this->_real_escape( $data );
     451        }
     452
     453        return $data;
     454    }
     455
     456    /**
     457     * Escapes content for insertion into the database using addslashes(), for security
     458     *
     459     * @since 0.71
     460     *
     461     * @param string|array $data
     462     * @return string query safe string
     463     */
     464    function escape($data) {
     465        if ( is_array($data) ) {
     466            foreach ( (array) $data as $k => $v ) {
     467                if ( is_array($v) )
     468                    $data[$k] = $this->escape( $v );
     469                else
     470                    $data[$k] = $this->_weak_escape( $v );
     471            }
     472        } else {
     473            $data = $this->_weak_escape( $data );
     474        }
     475
     476        return $data;
    443477    }
    444478
     
    450484     * @param string $s
    451485     */
    452     function escape_by_ref(&$s) {
    453         $s = $this->escape($s);
     486    function escape_by_ref(&$string) {
     487        $string = $this->_real_escape( $string );
    454488    }
    455489
     
    666700     */
    667701    function insert($table, $data) {
    668         $data = add_magic_quotes($data);
     702        $data = $this->_escape($data);
    669703        $fields = array_keys($data);
    670704        return $this->query("INSERT INTO $table (`" . implode('`,`',$fields) . "`) VALUES ('".implode("','",$data)."')");
     
    682716     */
    683717    function update($table, $data, $where){
    684         $data = add_magic_quotes($data);
     718        $data = $this->_escape($data);
    685719        $bits = $wheres = array();
    686720        foreach ( (array) array_keys($data) as $k )
     
    689723        if ( is_array( $where ) )
    690724            foreach ( $where as $c => $v )
    691                 $wheres[] = "$c = '" . $this->escape( $v ) . "'";
     725                $wheres[] = "$c = '" . $this->_escape( $v ) . "'";
    692726        else
    693727            return false;
Note: See TracChangeset for help on using the changeset viewer.