现在PHP5.5版本出来了,我装了一下改动还真不小,咱们用的smarty居然不兼容了,一运行出现以下错误:
意思是说用preg_replace_callback来代替preg_replace里的/e参数,可能是因为安全性的考虑,在php5.5中把这个参数取消了,但是这个preg_replace_callback函数用起来不顺手,我就把那个Smarty_Compiler.class.php文件里的第270行里的preg_replace($search.'e'里的e去掉了,果然好了,可是问题又出来了,这个问题出来的几率不多,就是无法在smarty模板里写入php标签代码:{php}{/php}。
没办法还是要把这个preg_replace()用preg_replace_callback()函数来替换,具体替换方法如下:
在Smarty_Compiler这个类里加入一个方法:
function callback_source($matches){
return "".$this->_quote_replace($this->left_delimiter)."php".str_repeat("n",substr_count("","n"))."".$this->_quote_replace($this->right_delimiter)."";
}
然后在第270行左右找到:
$source_content = preg_replace($search.'e', "'"
. $this->_quote_replace($this->left_delimiter) . 'php'
. "' . str_repeat("n", substr_count('\0', "n")) .'"
. $this->_quote_replace($this->right_delimiter)
. "'"
, $source_content);
替换成:
$source_content = preg_replace_callback($search,
array("self","callback_source")
, $source_content);
即可,这样就可以完美兼容php5.5了。
=========================PHP7 literal================================
官网提示是这样的,对/e修饰符的支持已删除。请改用preg_replace_callback()
原因是/e 修正符使 preg_replace() 将 replacement 参数当作 PHP 代码(在适当的逆向引用替换完之后),会被一句话后门使用
看看smarty中是也是这样用的,也是存在问题
$source_content = preg_replace($search.'e', "'"
. $this->_quote_replace($this->left_delimiter) . 'php'
. "' . str_repeat(\"\n\", substr_count('\\0', \"\n\")) .'"
. $this->_quote_replace($this->right_delimiter)
. "'"
, $source_content);
可以把smarty模板修改成这个
$source_content = preg_replace_callback($search, function ($matches){
$str="";
$str.=$this->_quote_replace($this->left_delimiter) . 'php';
$str.=str_repeat("\\n\\", substr_count($matches[1], "\\n\\"));
$str.=$this->_quote_replace($this->right_delimiter);
return $str;
}, $source_content);