PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Form/Element/Hash.php

https://bitbucket.org/Ebozavrik/test-application
PHP | 271 lines | 96 code | 29 blank | 146 comment | 4 complexity | 7fea5889ceb853ac661df0bb6766b9b9 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @subpackage Element
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Form_Element_Xhtml */
  22. require_once 'Zend/Form/Element/Xhtml.php';
  23. /**
  24. * CSRF form protection
  25. *
  26. * @category Zend
  27. * @package Zend_Form
  28. * @subpackage Element
  29. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. * @version $Id: Hash.php 24593 2012-01-05 20:35:02Z matthew $
  32. */
  33. class Zend_Form_Element_Hash extends Zend_Form_Element_Xhtml
  34. {
  35. /**
  36. * Use formHidden view helper by default
  37. * @var string
  38. */
  39. public $helper = 'formHidden';
  40. /**
  41. * Actual hash used.
  42. *
  43. * @var mixed
  44. */
  45. protected $_hash;
  46. /**
  47. * Salt for CSRF token
  48. * @var string
  49. */
  50. protected $_salt = 'salt';
  51. /**
  52. * @var Zend_Session_Namespace
  53. */
  54. protected $_session;
  55. /**
  56. * TTL for CSRF token
  57. * @var int
  58. */
  59. protected $_timeout = 300;
  60. /**
  61. * Constructor
  62. *
  63. * Creates session namespace for CSRF token, and adds validator for CSRF
  64. * token.
  65. *
  66. * @param string|array|Zend_Config $spec
  67. * @param array|Zend_Config $options
  68. *
  69. * @return void
  70. */
  71. public function __construct ($spec, $options = null)
  72. {
  73. parent::__construct($spec, $options);
  74. $this->setAllowEmpty(false)
  75. ->setRequired(true)
  76. ->initCsrfValidator();
  77. }
  78. /**
  79. * Set session object
  80. *
  81. * @param Zend_Session_Namespace $session
  82. *
  83. * @return Zend_Form_Element_Hash
  84. */
  85. public function setSession ($session)
  86. {
  87. $this->_session = $session;
  88. return $this;
  89. }
  90. /**
  91. * Get session object
  92. *
  93. * Instantiate session object if none currently exists
  94. *
  95. * @return Zend_Session_Namespace
  96. */
  97. public function getSession ()
  98. {
  99. if (null === $this->_session) {
  100. require_once 'Zend/Session/Namespace.php';
  101. $this->_session = new Zend_Session_Namespace( $this->getSessionName() );
  102. }
  103. return $this->_session;
  104. }
  105. /**
  106. * Initialize CSRF validator
  107. *
  108. * Creates Session namespace, and initializes CSRF token in session.
  109. * Additionally, adds validator for validating CSRF token.
  110. *
  111. * @return Zend_Form_Element_Hash
  112. */
  113. public function initCsrfValidator ()
  114. {
  115. $session = $this->getSession();
  116. if (isset( $session->hash )) {
  117. $rightHash = $session->hash;
  118. } else {
  119. $rightHash = null;
  120. }
  121. $this->addValidator('Identical', true, array( $rightHash ));
  122. return $this;
  123. }
  124. /**
  125. * Salt for CSRF token
  126. *
  127. * @param string $salt
  128. *
  129. * @return Zend_Form_Element_Hash
  130. */
  131. public function setSalt ($salt)
  132. {
  133. $this->_salt = (string)$salt;
  134. return $this;
  135. }
  136. /**
  137. * Retrieve salt for CSRF token
  138. *
  139. * @return string
  140. */
  141. public function getSalt ()
  142. {
  143. return $this->_salt;
  144. }
  145. /**
  146. * Retrieve CSRF token
  147. *
  148. * If no CSRF token currently exists, generates one.
  149. *
  150. * @return string
  151. */
  152. public function getHash ()
  153. {
  154. if (null === $this->_hash) {
  155. $this->_generateHash();
  156. }
  157. return $this->_hash;
  158. }
  159. /**
  160. * Get session namespace for CSRF token
  161. *
  162. * Generates a session namespace based on salt, element name, and class.
  163. *
  164. * @return string
  165. */
  166. public function getSessionName ()
  167. {
  168. return __CLASS__ . '_' . $this->getSalt() . '_' . $this->getName();
  169. }
  170. /**
  171. * Set timeout for CSRF session token
  172. *
  173. * @param int $ttl
  174. *
  175. * @return Zend_Form_Element_Hash
  176. */
  177. public function setTimeout ($ttl)
  178. {
  179. $this->_timeout = (int)$ttl;
  180. return $this;
  181. }
  182. /**
  183. * Get CSRF session token timeout
  184. *
  185. * @return int
  186. */
  187. public function getTimeout ()
  188. {
  189. return $this->_timeout;
  190. }
  191. /**
  192. * Override getLabel() to always be empty
  193. *
  194. * @return null
  195. */
  196. public function getLabel ()
  197. {
  198. return null;
  199. }
  200. /**
  201. * Initialize CSRF token in session
  202. *
  203. * @return void
  204. */
  205. public function initCsrfToken ()
  206. {
  207. $session = $this->getSession();
  208. $session->setExpirationHops(1, null, true);
  209. $session->setExpirationSeconds($this->getTimeout());
  210. $session->hash = $this->getHash();
  211. }
  212. /**
  213. * Render CSRF token in form
  214. *
  215. * @param Zend_View_Interface $view
  216. *
  217. * @return string
  218. */
  219. public function render (Zend_View_Interface $view = null)
  220. {
  221. $this->initCsrfToken();
  222. return parent::render($view);
  223. }
  224. /**
  225. * Generate CSRF token
  226. *
  227. * Generates CSRF token and stores both in {@link $_hash} and element
  228. * value.
  229. *
  230. * @return void
  231. */
  232. protected function _generateHash ()
  233. {
  234. $this->_hash = md5(
  235. mt_rand(1, 1000000)
  236. . $this->getSalt()
  237. . $this->getName()
  238. . mt_rand(1, 1000000)
  239. );
  240. $this->setValue($this->_hash);
  241. }
  242. }