Make WordPress Core

Ticket #11608: 11608.3.patch

File 11608.3.patch, 4.3 KB (added by hakre, 15 years ago)
  • wp-includes/wp-db.php

     
    530530        }
    531531
    532532        /**
    533          * Prepares a SQL query for safe execution.  Uses sprintf()-like syntax.
     533         * Mimicks a prepare of a SQL query for execution. 
     534         *
     535         * The following placeholders can be used for values:
     536         *
     537         *   %d (decimal number)
     538         *   %s (string)
     539         *   %% (%)
     540         *   
     541         * Both %d and %s should be left unquoted in the query string.
    534542         *
     543         * <code>
     544         * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 )
     545         * </code>
     546         *
     547         * NOTE: This has nothing to do with prepared statements your database might support.
     548         * 
     549         * More technical information:
     550         *
     551         * Uses sprintf()-like syntax.
     552         *
    535553         * This function only supports a small subset of the sprintf syntax; it only supports %d (decimal number), %s (string).
    536554         * Does not support sign, padding, alignment, width or precision specifiers.
    537555         * Does not support argument numbering/swapping.
    538556         *
    539557         * May be called like {@link http://php.net/sprintf sprintf()} or like {@link http://php.net/vsprintf vsprintf()}.
    540558         *
    541          * Both %d and %s should be left unquoted in the query string.
    542          *
    543          * <code>
    544          * wpdb::prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d", "foo", 1337 )
    545          * </code>
    546          *
    547559         * @link http://php.net/sprintf Description of syntax.
    548560         * @since 2.3.0
    549561         *
    550          * @param string $query Query statement with sprintf()-like placeholders
    551          * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called like {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
    552          * @param mixed $args,... further variables to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
     562         * @param string $query Query statement with wpdb->prepare placeholders
     563         * @param array|mixed $args The array of variables to substitute into the query's placeholders if being called compareable to {@link http://php.net/vsprintf vsprintf()}, or the first variable to substitute into the query's placeholders if being called like {@link http://php.net/sprintf sprintf()}.
     564         * @param mixed $args,... further variables to substitute.
    553565         * @return null|string Sanitized query string
    554566         */
    555567        function prepare($query = null) { // ( $query, *$args )
     
    560572                // If args were passed as an array (as in vsprintf), move them up
    561573                if ( isset($args[0]) && is_array($args[0]) )
    562574                        $args = $args[0];
    563                 $query = str_replace("'%s'", '%s', $query); // in case someone mistakenly already singlequoted it
    564                 $query = str_replace('"%s"', '%s', $query); // doublequote unquoting
    565                 $query = str_replace('%s', "'%s'", $query); // quote the strings
     575
     576                $query = $this->_prepare_quote_lits($query); //quote %s in query-pattern                               
     577                $query = str_replace( array( "''%s''", "\"'%'\"" ), "'%s'", $query ); // in case someone mistakenly already single/double quoted it
     578
    566579                array_walk($args, array(&$this, 'escape_by_ref'));
    567580                return @vsprintf($query, $args);
    568581        }
    569582
    570583        /**
     584         * helper function for prepare()
     585         *
     586         * will quote %s tokens with single quotes
     587         * allowed tokens are: %%, %d and %s
     588         *
     589         * can do strict parsing and return false if the query is 
     590         * not valid.
     591         *
     592         * @access private
     593         * @param  string $query  wpdb prepare-pattern that needs to be single-quoted to %s
     594         * @param  bool   $strict (optional) wether or not do strict parsing of the query
     595         * @return string pattern with single quotes added around %s literals
     596         * @return bool   false on syntax error if $strict param is true
     597         */
     598        function _prepare_quote_lits( $query, $strict = false ) {
     599                $query = (string) $query;                               
     600                $m = strlen( $query );         
     601                for ( $i = -1; $c = $query[++$i], $i < $m; ) {
     602                        if ( '%' == $c ) {                             
     603                                switch ( $query[$i+1] ) {
     604                                        case 's':
     605                                                $query = substr( $query, 0, $i) . "'%s'" . substr( $query, $i+=2 );
     606                                        case '%':
     607                                        case 'd':
     608                                                $i++;
     609                                                break;
     610                                        default: # illegal pattern
     611                                                if ( $strict ) return false;
     612                                }
     613                        }
     614                }
     615                return $query;
     616        }
     617
     618        /**
    571619         * Print SQL/DB error.
    572620         *
    573621         * @since 0.71