Created
July 14, 2010 21:34
-
-
Save davidcoallier/476122 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Vendor_View_Helper_AutoEscape | |
{ | |
protected $_unescapedVars = null; | |
protected $_view; | |
public function setView(Zend_View_Interface $view) | |
{ | |
$this->_view = $view; | |
} | |
public function autoEscape() | |
{ | |
if ($this->_unescapedVars === null) { | |
$vars = $this->_view->getVars(); | |
$this->_unescapedVars = $vars; | |
foreach ($vars as $k => $v) { | |
$this->_view->$k = $this->escapeDeep($v); | |
} | |
} else { | |
$this->_view->assign($this->_unescapedVars); | |
$this->_unescapedVars = null; | |
} | |
} | |
/** | |
* Escapes strings and recursively iterates over arrays to escape all entries. | |
* | |
* @param mixed $original | |
* @return mixed | |
*/ | |
protected function escapeDeep(&$original) { | |
if (is_string($original)) | |
return $this->_view->escape($original); | |
elseif (is_array($original)) { | |
foreach ($original as $k => $v) { | |
$original[$k] = $this->escapeDeep($v); | |
} | |
} | |
return $original; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment