| 1 | <?php |
||
| 3 | class Nip_Form_Element_Hash extends Nip_Form_Element_Hidden |
||
|
|
|||
| 4 | { |
||
| 5 | protected $_ID; |
||
| 6 | |||
| 7 | public function init() |
||
| 8 | { |
||
| 9 | parent::init(); |
||
| 10 | $this->initSession(); |
||
| 11 | } |
||
| 12 | |||
| 13 | public function initSession() |
||
| 14 | { |
||
| 15 | $name = $this->getSessionName(); |
||
| 16 | if (!$_SESSION[$name]) { |
||
| 17 | $this->reset(); |
||
| 18 | } |
||
| 19 | |||
| 20 | $this->setValue($this->getSessionValue()); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function reset() |
||
| 24 | { |
||
| 25 | $name = $this->getSessionName(); |
||
| 26 | $hash = $this->_generateHash(); |
||
| 27 | $_SESSION[$name] = $hash; |
||
| 28 | $this->setValue($hash); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function validate() |
||
| 32 | { |
||
| 33 | if (!$this->getValue()) { |
||
| 34 | $this->addError('Request received without security hash'); |
||
| 35 | } elseif ($this->getValue() != $this->getSessionValue()) { |
||
| 36 | $this->addError('Form security hash different from server'); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | public function getSessionName() |
||
| 41 | { |
||
| 42 | return $this->getForm()->getName().'_'.$this->getSalt(); |
||
| 43 | } |
||
| 44 | |||
| 45 | public function getSessionValue() |
||
| 46 | { |
||
| 47 | $name = $this->getSessionName(); |
||
| 48 | |||
| 49 | return $_SESSION[$name]; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function getSalt() |
||
| 55 | } |
||
| 56 | |||
| 57 | protected function _generateHash() |
||
| 58 | { |
||
| 59 | return md5( |
||
| 65 | ); |
||
| 66 | } |
||
| 67 | } |
||
| 68 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.