Csrf.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ fnPGDs0Z/qAwNaUwjcoVbFZRVI6QHZ42WtBl3ze5+HQ=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2015 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.4
  11. */
  12. namespace Cube\Form\Element;
  13. use Cube\Form\Element,
  14. Cube\Controller\Front,
  15. Cube\Session;
  16. /**
  17. * csrf (cross site request forgery) form element generator class
  18. *
  19. * Class Csrf
  20. *
  21. * @package Cube\Form\Element
  22. */
  23. class Csrf extends Element
  24. {
  25. const SESSION_NAMESPACE = 'Csrf';
  26. /**
  27. *
  28. * type of element - override the variable from the parent class
  29. *
  30. * @var string
  31. */
  32. protected $_element = 'csrf';
  33. /**
  34. *
  35. * session object
  36. *
  37. * @var \Cube\Session
  38. */
  39. protected $_session;
  40. /**
  41. *
  42. * class constructor
  43. *
  44. * @param string $name
  45. */
  46. public function __construct($name = 'csrf')
  47. {
  48. parent::__construct($this->_element, $name);
  49. $this->addValidator('Csrf')
  50. ->setSession()
  51. ->setHidden(true);
  52. }
  53. /**
  54. *
  55. * get session object
  56. *
  57. * @return \Cube\Session
  58. */
  59. public function getSession()
  60. {
  61. if (!($this->_session instanceof Session)) {
  62. $this->setSession();
  63. }
  64. return $this->_session;
  65. }
  66. /**
  67. *
  68. * set session object
  69. *
  70. * @param \Cube\Session $session
  71. *
  72. * @return \Cube\Validate\Csrf
  73. */
  74. public function setSession(Session $session = null)
  75. {
  76. if ($session === null) {
  77. $session = Front::getInstance()->getBootstrap()->getResource('session');
  78. }
  79. if (!$session instanceof Session) {
  80. $session = new Session();
  81. $session->setNamespace(self::SESSION_NAMESPACE);
  82. }
  83. $this->_session = $session;
  84. return $this;
  85. }
  86. /**
  87. *
  88. * create a csrf token for the csrf form element
  89. *
  90. * @return string
  91. */
  92. public function getToken()
  93. {
  94. return sha1(uniqid(rand(), true));
  95. }
  96. /**
  97. *
  98. * render element
  99. *
  100. * @return string
  101. */
  102. public function render()
  103. {
  104. $value = $this->getToken();
  105. $variable = array_filter((array)$this->_session->get($this->_name));
  106. array_push($variable, $value);
  107. $this->_session->set($this->_name, $variable);
  108. return '<input type="hidden" name="' . $this->_name . '" '
  109. . ((!empty($value)) ? 'value="' . $value . '" ' : '')
  110. . $this->_endTag;
  111. }
  112. }