7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Form_Element_Xhtml */
23 require_once 'Zend/Form/Element/Xhtml.php';
26 * CSRF form protection
31 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
33 * @version $Id: Hash.php 16218 2009-06-21 19:44:04Z thomas $
35 class Zend_Form_Element_Hash
extends Zend_Form_Element_Xhtml
38 * Use formHidden view helper by default
41 public $helper = 'formHidden';
54 protected $_salt = 'salt';
57 * @var Zend_Session_Namespace
65 protected $_timeout = 300;
70 * Creates session namespace for CSRF token, and adds validator for CSRF
73 * @param string|array|Zend_Config $spec
74 * @param array|Zend_Config $options
77 public function __construct($spec, $options = null)
79 parent
::__construct($spec, $options);
81 $this->setAllowEmpty(false)
83 ->initCsrfValidator();
89 * @param Zend_Session_Namespace $session
90 * @return Zend_Form_Element_Hash
92 public function setSession($session)
94 $this->_session
= $session;
101 * Instantiate session object if none currently exists
103 * @return Zend_Session_Namespace
105 public function getSession()
107 if (null === $this->_session
) {
108 require_once 'Zend/Session/Namespace.php';
109 $this->_session
= new Zend_Session_Namespace($this->getSessionName());
111 return $this->_session
;
115 * Initialize CSRF validator
117 * Creates Session namespace, and initializes CSRF token in session.
118 * Additionally, adds validator for validating CSRF token.
120 * @return Zend_Form_Element_Hash
122 public function initCsrfValidator()
124 $session = $this->getSession();
125 if (isset($session->hash
)) {
126 $rightHash = $session->hash
;
131 $this->addValidator('Identical', true, array($rightHash));
136 * Salt for CSRF token
138 * @param string $salt
139 * @return Zend_Form_Element_Hash
141 public function setSalt($salt)
143 $this->_salt
= (string) $salt;
148 * Retrieve salt for CSRF token
152 public function getSalt()
158 * Retrieve CSRF token
160 * If no CSRF token currently exists, generates one.
164 public function getHash()
166 if (null === $this->_hash
) {
167 $this->_generateHash();
173 * Get session namespace for CSRF token
175 * Generates a session namespace based on salt, element name, and class.
179 public function getSessionName()
181 return __CLASS__
. '_' . $this->getSalt() . '_' . $this->getName();
185 * Set timeout for CSRF session token
188 * @return Zend_Form_Element_Hash
190 public function setTimeout($ttl)
192 $this->_timeout
= (int) $ttl;
197 * Get CSRF session token timeout
201 public function getTimeout()
203 return $this->_timeout
;
207 * Override getLabel() to always be empty
211 public function getLabel()
217 * Initialize CSRF token in session
221 public function initCsrfToken()
223 $session = $this->getSession();
224 $session->setExpirationHops(1, null, true);
225 $session->setExpirationSeconds($this->getTimeout());
226 $session->hash
= $this->getHash();
230 * Render CSRF token in form
232 * @param Zend_View_Interface $view
235 public function render(Zend_View_Interface
$view = null)
237 $this->initCsrfToken();
238 return parent
::render($view);
242 * Generate CSRF token
244 * Generates CSRF token and stores both in {@link $_hash} and element
249 protected function _generateHash()
257 $this->setValue($this->_hash
);