Zend Framework 的自动变量转义器

发布于 2024-11-17 17:16:50 字数 538 浏览 3 评论 0原文

您能为 Zend Framework 1.x 推荐任何好的自动视图变量转义解决方案吗?

到目前为止我已经尝试过:

  • ZF2 实现;看起来它没有转义变量语法,如下所示: $this->var->object()->string
  • gnix-view,非常好,但是有一个令人讨厌的递归错误
  • 基于视图流的自定义解决方案,类似于Rob Allen 的转义符,但是用正则表达式解析语法总是失败
  • Twig(没有对视图助手和布局的良好支持)

Can you recommend any good solution for automatic view variable escaping for Zend Framework 1.x?

I have tried so far:

  • ZF2 implementation; looks like it does not escape variables syntax like this: $this->var->object()->string
  • gnix-view, very nice, but has a nasty recursion bug
  • custom solutions based on view streams, similar to Rob Allen's escaper, but parsing syntax with regex always fails
  • Twig (no good support for view helpers and layout)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘑菇王子 2024-11-24 17:16:50

这是我的解决方案

/**
 * Purifies all data passed to view
 *
 * @author miholeus
 */
class HTMLPurifier_View extends Zend_View {
    protected $_vars = array();

    public function __set($key, $val)
    {

        if(is_string($val)) {
            $purified = $this->escape($val);
        } elseif(is_array($val)) {
            $purified = array_map(array($this, 'traverseSingle'), $val);
        } else { // other types: integers, bools, objects
            $purified = $this->traverseSingle($val);
        }

        $this->_vars[$key] = array(
            'raw' => $val,
            'purified' => $purified
        );

        return $this;
    }

    public function getRaw($key)
    {
        if(isset($this->_vars[$key])) {
            return $this->_vars[$key]['raw'];
        }
        return null;
    }

    public function __get($key)
    {
        if(isset($this->_vars[$key])) {
            return $this->_vars[$key]['purified'];
        }
        return null;
    }

    private function traverseSingle($element)
    {
        if(is_object($element)) {
            $reflect = new ReflectionObject($element);
            foreach ($reflect->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
              $element->{$prop->getName()} = $this->escape($element->{$prop->getName()});
            }
            return $element;
        } else {
            return $this->escape($element);
        }
    }
}

您所需要做的就是将其设置为引导程序中的视图。

Here is my solution

/**
 * Purifies all data passed to view
 *
 * @author miholeus
 */
class HTMLPurifier_View extends Zend_View {
    protected $_vars = array();

    public function __set($key, $val)
    {

        if(is_string($val)) {
            $purified = $this->escape($val);
        } elseif(is_array($val)) {
            $purified = array_map(array($this, 'traverseSingle'), $val);
        } else { // other types: integers, bools, objects
            $purified = $this->traverseSingle($val);
        }

        $this->_vars[$key] = array(
            'raw' => $val,
            'purified' => $purified
        );

        return $this;
    }

    public function getRaw($key)
    {
        if(isset($this->_vars[$key])) {
            return $this->_vars[$key]['raw'];
        }
        return null;
    }

    public function __get($key)
    {
        if(isset($this->_vars[$key])) {
            return $this->_vars[$key]['purified'];
        }
        return null;
    }

    private function traverseSingle($element)
    {
        if(is_object($element)) {
            $reflect = new ReflectionObject($element);
            foreach ($reflect->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) {
              $element->{$prop->getName()} = $this->escape($element->{$prop->getName()});
            }
            return $element;
        } else {
            return $this->escape($element);
        }
    }
}

All you need to do is to set it as your view in bootstrap.

小帐篷 2024-11-24 17:16:50

如果我想制作一个自动转义器,我会创建一个在 postDispatch 中运行的 ZF 插件:

postDispatch() 在调度程序调度操作后调用。此回调允许代理或过滤器行为。通过更改请求并重置其分派标志(通过 Zend_Controller_Request_Abstract::setDispatched(false)),可以为分派指定新的操作。 source

mybe 使用 htmlprifier 将是一项明智的工作:)

class Automatic_Escaper extends Zend_Controller_Plugin_Abstract{
   public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $response = $this->getResponse();
        $htmlpurifier = Zend_Registry::get('purifier');
        $safe = $htmlpurifier->purify($response);
        return $this->setResponse($safe);
    }
}

我希望我解释了我的想法,无论状态上述示例。

if i would think to make an automatic escaper i would create a ZF plugin that run in postDispatch :

postDispatch() is called after an action is dispatched by the dispatcher. This callback allows for proxy or filter behavior. By altering the request and resetting its dispatched flag (via Zend_Controller_Request_Abstract::setDispatched(false)), a new action may be specified for dispatching. source

mybe some use of htmlprifier would be a smart job :)

class Automatic_Escaper extends Zend_Controller_Plugin_Abstract{
   public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        $response = $this->getResponse();
        $htmlpurifier = Zend_Registry::get('purifier');
        $safe = $htmlpurifier->purify($response);
        return $this->setResponse($safe);
    }
}

I hope I explained my idea regardless of the status the sample above .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文