2009年4月23日星期四

Zend Framework Plugin:自适应 magic_quotes_gpc 环境

推荐关闭 php.ini 的 magic_quotes_gpc 选项,一来可以提升性能,二来可以保证得到的原始数据,Zend_Db 会自动转义数据入库。并且新版本的 php 推荐配置都是默认关闭 magic_quotes_gpc 选项。
当然某些系统打开了 magic_quotes_gpc 选项,并且你也没有足够权限去修改这个配置,这些反斜杠可能给你造成困扰,我们可以用代码抵消修改。
Zend Framework 的 plugin 提供了很好的方式来做这些事情,而不需要改动我们去参数的每段代码。

/**
* A Zend Controller Plugin dedicated to undoing the damage of magic_quotes_gpc
* in systems where it is on.
*
* @author Ken
* @version $Id:$
*/
require_once ('Zend/Controller/Plugin/Abstract.php');
class Mezi_Controller_Plugin_StripMagicQuotes extends Zend_Controller_Plugin_Abstract
{
/**
* strip all slashes off $request parameters
*
* @param Zend_Controller_Request_Abstract $request
*/
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$params = $request->getParams();
array_walk_recursive($params, array($this , '_stripSlashes'));
$request->setParams($params);

if ($request instanceof Zend_Controller_Request_Http) {
$this->_stripSlashesGPC();
}
}

/**
* strip all slashes from GPC
*/
protected function _stripSlashesGPC()
{
array_walk_recursive($_GET, array($this , '_stripSlashes'));
array_walk_recursive($_POST, array($this , '_stripSlashes'));
array_walk_recursive($_COOKIE, array($this , '_stripSlashes'));
}

/**
* callback strip the slashes off an item in the Params array
*
* @param string $value
* @param string $key
*/
protected function _stripSlashes (&$value, $key)
{
$value = stripslashes($value);
}
}


然后启动时安装 plugin, 仅在 magic_quotes_gpc 打开时需要处理:

$front = Zend_Controller_Front::getInstance();

if (get_magic_quotes_gpc()) {
require_once 'Mezi/Controller/Plugin/StripMagicQuotes.php';
$this->_front->registerPlugin(new Mezi_Controller_Plugin_StripMagicQuotes());
}


Plugin 非常漂亮的实现了我们的需求,以前写好的代码都无须改动。

没有评论: