Skip to content

Commit 885fce4

Browse files
committed
Replace eval usage in request processing with new WP_MatchesMapRegex() class usage. See #9602 props hakre.
git-svn-id: https://develop.svn.wordpress.org/trunk@11853 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 50bb241 commit 885fce4

2 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎wp-includes/classes.php‎

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ function parse_request($extra_query_vars = '') {
214214

215215
// Trim the query of everything up to the '?'.
216216
$query = preg_replace("!^.+\?!", '', $query);
217-
217+
218218
// Substitute the substring matches into the query.
219-
eval("@\$query = \"" . addslashes($query) . "\";");
219+
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
220220

221221
$this->matched_query = $query;
222222

@@ -1592,4 +1592,94 @@ function send() {
15921592
}
15931593
}
15941594

1595+
/**
1596+
* Helper class to remove the need to use eval to replace $matches[] in query strings.
1597+
*
1598+
* @since 2.9.0
1599+
*/
1600+
class WP_MatchesMapRegex {
1601+
/**
1602+
* store for matches
1603+
*
1604+
* @access private
1605+
* @var array
1606+
*/
1607+
var $_matches;
1608+
1609+
/**
1610+
* store for mapping result
1611+
*
1612+
* @access public
1613+
* @var string
1614+
*/
1615+
var $output;
1616+
1617+
/**
1618+
* subject to perform mapping on (query string containing $matches[] references
1619+
*
1620+
* @access private
1621+
* @var string
1622+
*/
1623+
var $_subject;
1624+
1625+
/**
1626+
* regexp pattern to match $matches[] references
1627+
*
1628+
* @var string
1629+
*/
1630+
var $_pattern = '(\$matches\[[1-9]+[0-9]*\])'; // magic number
1631+
1632+
/**
1633+
* constructor
1634+
*
1635+
* @param string $subject subject if regex
1636+
* @param array $matches data to use in map
1637+
* @return self
1638+
*/
1639+
function WP_MatchesMapRegex($subject, $matches) {
1640+
$this->_subject = $subject;
1641+
$this->_matches = $matches;
1642+
$this->output = $this->_map();
1643+
}
1644+
1645+
/**
1646+
* Substitute substring matches in subject.
1647+
*
1648+
* static helper function to ease use
1649+
*
1650+
* @access public
1651+
* @param string $subject subject
1652+
* @param array $matches data used for subsitution
1653+
* @return string
1654+
*/
1655+
function apply($subject, $matches) {
1656+
$oSelf =& new WP_MatchesMapRegex($subject, $matches);
1657+
return $oSelf->output;
1658+
}
1659+
1660+
/**
1661+
* do the actual mapping
1662+
*
1663+
* @access private
1664+
* @return string
1665+
*/
1666+
function _map() {
1667+
$callback = array(&$this, 'callback');
1668+
return preg_replace_callback($this->_pattern, $callback, $this->_subject);
1669+
}
1670+
1671+
/**
1672+
* preg_replace_callback hook
1673+
*
1674+
* @access public
1675+
* @param array $matches preg_replace regexp matches
1676+
* @return string
1677+
*/
1678+
function callback($matches) {
1679+
$index = intval(substr($matches[0], 9, -1));
1680+
return ( isset( $this->_matches[$index] ) ? $this->_matches[$index] : '' );
1681+
}
1682+
1683+
}
1684+
15951685
?>

‎wp-includes/rewrite.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ function url_to_postid($url) {
299299
$query = preg_replace("!^.+\?!", '', $query);
300300

301301
// Substitute the substring matches into the query.
302-
eval("\$query = \"" . addslashes($query) . "\";");
302+
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
303303
// Filter out non-public query vars
304304
global $wp;
305305
parse_str($query, $query_vars);

0 commit comments

Comments
 (0)