*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Form / Element / Hash.php
blobcd617c0100a4f8c92aac42d6131ba18e47aab7d4
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Form
17 * @subpackage Element
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';
25 /**
26 * CSRF form protection
28 * @category Zend
29 * @package Zend_Form
30 * @subpackage Element
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
37 /**
38 * Use formHidden view helper by default
39 * @var string
41 public $helper = 'formHidden';
43 /**
44 * Actual hash used.
46 * @var mixed
48 protected $_hash;
50 /**
51 * Salt for CSRF token
52 * @var string
54 protected $_salt = 'salt';
56 /**
57 * @var Zend_Session_Namespace
59 protected $_session;
61 /**
62 * TTL for CSRF token
63 * @var int
65 protected $_timeout = 300;
67 /**
68 * Constructor
70 * Creates session namespace for CSRF token, and adds validator for CSRF
71 * token.
73 * @param string|array|Zend_Config $spec
74 * @param array|Zend_Config $options
75 * @return void
77 public function __construct($spec, $options = null)
79 parent::__construct($spec, $options);
81 $this->setAllowEmpty(false)
82 ->setRequired(true)
83 ->initCsrfValidator();
86 /**
87 * Set session object
89 * @param Zend_Session_Namespace $session
90 * @return Zend_Form_Element_Hash
92 public function setSession($session)
94 $this->_session = $session;
95 return $this;
98 /**
99 * Get session object
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;
127 } else {
128 $rightHash = null;
131 $this->addValidator('Identical', true, array($rightHash));
132 return $this;
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;
144 return $this;
148 * Retrieve salt for CSRF token
150 * @return string
152 public function getSalt()
154 return $this->_salt;
158 * Retrieve CSRF token
160 * If no CSRF token currently exists, generates one.
162 * @return string
164 public function getHash()
166 if (null === $this->_hash) {
167 $this->_generateHash();
169 return $this->_hash;
173 * Get session namespace for CSRF token
175 * Generates a session namespace based on salt, element name, and class.
177 * @return string
179 public function getSessionName()
181 return __CLASS__ . '_' . $this->getSalt() . '_' . $this->getName();
185 * Set timeout for CSRF session token
187 * @param int $ttl
188 * @return Zend_Form_Element_Hash
190 public function setTimeout($ttl)
192 $this->_timeout = (int) $ttl;
193 return $this;
197 * Get CSRF session token timeout
199 * @return int
201 public function getTimeout()
203 return $this->_timeout;
207 * Override getLabel() to always be empty
209 * @return null
211 public function getLabel()
213 return null;
217 * Initialize CSRF token in session
219 * @return void
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
233 * @return string
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
245 * value.
247 * @return void
249 protected function _generateHash()
251 $this->_hash = md5(
252 mt_rand(1,1000000)
253 . $this->getSalt()
254 . $this->getName()
255 . mt_rand(1,1000000)
257 $this->setValue($this->_hash);