Skip to content

Commit 99fa2ca

Browse files
committed
调整正则的 e 修饰符,以便支持php5.5
1 parent 087ece4 commit 99fa2ca

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

ThinkPHP/Common/common.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ function N($key, $step=0,$save=false) {
228228
*/
229229
function parse_name($name, $type=0) {
230230
if ($type) {
231-
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
231+
$callback = create_function('$match', 'return strtoupper($match[1]);');
232+
return ucfirst(preg_replace_callback('/_([a-zA-Z])/', $callback, $name));
232233
} else {
233234
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
234235
}

ThinkPHP/Lib/Behavior/CheckRouteBehavior.class.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
* @author liu21st <liu21st@gmail.com>
1919
*/
2020
class CheckRouteBehavior extends Behavior {
21+
22+
private $matcheValues = array();
23+
2124
// 行为参数定义(默认值) 可在项目配置中覆盖
2225
protected $options = array(
2326
'URL_ROUTER_ON' => false, // 是否开启URL路由
@@ -145,10 +148,11 @@ private function parseRule($rule,$route,$regx) {
145148
array_shift($paths);
146149
}
147150
}
151+
148152
if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转
149153
if(strpos($url,':')) { // 传递动态参数
150-
$values = array_values($matches);
151-
$url = preg_replace('/:(\d+)/e','$values[\\1-1]',$url);
154+
$this->matcheValues = array_values($matches);
155+
$url = preg_replace_callback('/:(\d+)/', array($this, 'parseRuleValue'), $url);
152156
}
153157
header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
154158
exit;
@@ -164,8 +168,12 @@ private function parseRule($rule,$route,$regx) {
164168
}
165169
$var = array_merge($matches,$var);
166170
// 解析剩余的URL参数
167-
if($paths) {
168-
preg_replace('@(\w+)\/([^\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', implode('/',$paths));
171+
if(!empty($paths)) {
172+
for ($i=0, $count = count($paths); $i < $count; $i += 2) {
173+
if(preg_match(/^\w+$/, $paths[$i]) && isset($paths[$i + 1])){
174+
$var[strtolower($paths[$i])] = strip_tags($paths[$i + 1]);
175+
}
176+
}
169177
}
170178
// 解析路由自动传入参数
171179
if(is_array($route) && isset($route[1])) {
@@ -177,6 +185,15 @@ private function parseRule($rule,$route,$regx) {
177185
return true;
178186
}
179187

188+
/**
189+
* 返回参数,用于preg_replace_callback
190+
* @param array $match preg_replace_callback 匹配结果
191+
* @return string
192+
*/
193+
private function parseRuleValue($match){
194+
return $this->matcheValues[$match[1] - 1];
195+
}
196+
180197
// 解析正则路由
181198
// '路由正则'=>'[分组/模块/操作]?参数1=值1&参数2=值2...'
182199
// '路由正则'=>array('[分组/模块/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...')
@@ -187,8 +204,9 @@ private function parseRule($rule,$route,$regx) {
187204
// '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向
188205
private function parseRegex($matches,$route,$regx) {
189206
// 获取路由地址规则
207+
$this->matcheValues = $matches;
190208
$url = is_array($route)?$route[0]:$route;
191-
$url = preg_replace('/:(\d+)/e','$matches[\\1]',$url);
209+
$url = preg_replace_callback('/:(\d+)/', array($this, 'parseRegexValue'), $url);
192210
if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转
193211
header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
194212
exit;
@@ -198,7 +216,12 @@ private function parseRegex($matches,$route,$regx) {
198216
// 解析剩余的URL参数
199217
$regx = substr_replace($regx,'',0,strlen($matches[0]));
200218
if($regx) {
201-
preg_replace('@(\w+)\/([^,\/]+)@e', '$var[strtolower(\'\\1\')]=strip_tags(\'\\2\');', $regx);
219+
$regx = explode('/', $regx);
220+
for ($i=0, $count = count($regx); $i < $count; $i += 2) {
221+
if(preg_match(/^\w+$/, $regx[$i]) && isset($regx[$i + 1])){
222+
$var[strtolower($regx[$i])] = strip_tags($regx[$i + 1]);
223+
}
224+
}
202225
}
203226
// 解析路由自动传入参数
204227
if(is_array($route) && isset($route[1])) {
@@ -209,4 +232,13 @@ private function parseRegex($matches,$route,$regx) {
209232
}
210233
return true;
211234
}
212-
}
235+
236+
/**
237+
* 返回参数,用于preg_replace_callback
238+
* @param array $match preg_replace_callback 匹配结果
239+
* @return string
240+
*/
241+
private function parseRegexValue($match){
242+
return $this->matcheValues[$match[1]];
243+
}
244+
}

ThinkPHP/Lib/Behavior/ReadHtmlCacheBehavior.class.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,30 @@ static private function requireHtmlCache() {
6666
// 解读静态规则
6767
$rule = $html[0];
6868
// 以$_开头的系统变量
69-
$rule = preg_replace('/{\$(_\w+)\.(\w+)\|(\w+)}/e',"\\3(\$\\1['\\2'])",$rule);
70-
$rule = preg_replace('/{\$(_\w+)\.(\w+)}/e',"\$\\1['\\2']",$rule);
69+
$callback = create_function('$match',
70+
'switch($match[1]){
71+
case "_GET": $var = $_GET[$match[2]]; break;
72+
case "_POST": $var = $_POST[$match[2]]; break;
73+
case "_REQUEST": $var = $_REQUEST[$match[2]]; break;
74+
case "_SERVER": $var = $_SERVER[$match[2]]; break;
75+
case "_SESSION": $var = $_SESSION[$match[2]]; break;
76+
case "_COOKIE": $var = $_COOKIE[$match[2]]; break;
77+
}
78+
return (count($match) == 4) ? $match[3]($var) : $var;'
79+
);
80+
$rule = preg_replace_callback('/{\$(_\w+)\.(\w+)(?:\|(\w+))?}/', $callback, $rule);
7181
// {ID|FUN} GET变量的简写
72-
$rule = preg_replace('/{(\w+)\|(\w+)}/e',"\\2(\$_GET['\\1'])",$rule);
73-
$rule = preg_replace('/{(\w+)}/e',"\$_GET['\\1']",$rule);
82+
$callback = create_function('$match', 'return $match[2]($_GET[$match[1]]);');
83+
$rule = preg_replace_callback('/{(\w+)\|(\w+)}/', $callback, $rule);
84+
$callback = create_function('$match', 'return $_GET[$match[1]];');
85+
$rule = preg_replace_callback('/{(\w+)}/', $callback, $rule);
7486
// 特殊系统变量
7587
$rule = str_ireplace(
7688
array('{:app}','{:controller}','{:action}','{:module}'),
7789
array(APP_NAME,CONTROLLER_NAME,ACTION_NAME,MODULE_NAME),
7890
$rule);
7991
// {|FUN} 单独使用函数
80-
$rule = preg_replace('/{|(\w+)}/e',"\\1()",$rule);
92+
$rule = preg_replace_callback('/{|(\w+)}/', create_function('$match', 'return $match[1]();'),$rule);
8193
if(!empty($html[2])) $rule = $html[2]($rule); // 应用附加函数
8294
$cacheTime = isset($html[1])?$html[1]:C('HTML_CACHE_TIME'); // 缓存有效期
8395
// 当前缓存文件

0 commit comments

Comments
 (0)