Make WordPress Core

Ticket #11608: 11608.3.diff

File 11608.3.diff, 1.0 KB (added by Denis-de-Bernardy, 15 years ago)

double escape first approach (untested)

  • wp-includes/wp-db.php

     
    560560                // If args were passed as an array (as in vsprintf), move them up
    561561                if ( isset($args[0]) && is_array($args[0]) )
    562562                        $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
     563                // allow literal % to be entered as such
     564                $query = str_replace('%', '%%', $query);
     565                // leave things such as LIKE '%%stuff' or 'some %%stuff' untouched
     566                // but catch mistakingly quoted strings such as '%%s'
     567                $query = preg_replace("/(^|\s)(['\"]?)%%s\\2(\s|$)/", "$1'%d'$3", $query);
     568                $query = preg_replace("/(^|\s)(['\"]?)%%d\\2(\s|$)/", "$1%d$3", $query);
    566569                array_walk($args, array(&$this, 'escape_by_ref'));
    567570                return @vsprintf($query, $args);
    568571        }