PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/nooku/libraries/koowa/template/filter/form.php

https://github.com/bhar1red/anahita
PHP | 178 lines | 80 code | 19 blank | 79 comment | 4 complexity | b69ff4b3a9df2952600b963b28807051 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: form.php 4628 2012-05-06 19:56:43Z johanjanssens $
  4. * @package Koowa_Template
  5. * @subpackage Filter
  6. * @copyright Copyright (C) 2007 - 2012 Johan Janssens. All rights reserved.
  7. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  8. * @link http://www.nooku.org
  9. */
  10. /**
  11. * Template write filter to handle form html elements
  12. *
  13. * For forms that use a post method this filter adds a token to prevent CSRF. For forms
  14. * that use a get method this filter adds the action url query params as hidden fields
  15. * to comply with the html form standard.
  16. *
  17. * @author Johan Janssens <johan@nooku.org>
  18. * @package Koowa_Template
  19. * @subpackage Filter
  20. * @see http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3.4
  21. */
  22. class KTemplateFilterForm extends KTemplateFilterAbstract implements KTemplateFilterWrite
  23. {
  24. /**
  25. * The form token value
  26. *
  27. * @var string
  28. */
  29. protected $_token_value;
  30. /**
  31. * The form token name
  32. *
  33. * @var string
  34. */
  35. protected $_token_name;
  36. /**
  37. * Constructor.
  38. *
  39. * @param object An optional KConfig object with configuration options
  40. */
  41. public function __construct( KConfig $config = null)
  42. {
  43. parent::__construct($config);
  44. $this->_token_value = $config->token_value;
  45. $this->_token_name = $config->token_name;
  46. }
  47. /**
  48. * Initializes the options for the object
  49. *
  50. * Called from {@link __construct()} as a first step of object instantiation.
  51. *
  52. * @param object An optional KConfig object with configuration options
  53. * @return void
  54. */
  55. protected function _initialize(KConfig $config)
  56. {
  57. $config->append(array(
  58. 'token_value' => '',
  59. 'token_name' => '_token',
  60. ));
  61. parent::_initialize($config);
  62. }
  63. /**
  64. * Get the session token value.
  65. *
  66. * If a token isn't set yet one will be generated. Tokens are used to secure forms
  67. * from spamming attacks. Once a token has been generated the system will check the
  68. * post request to see if it is present, if not it will invalidate the session.
  69. *
  70. * @param boolean If true, force a new token to be created
  71. * @return string The session token
  72. */
  73. protected function _tokenValue($force = false)
  74. {
  75. return $this->_token_value;
  76. }
  77. /**
  78. * Get the session token name
  79. *
  80. * Tokens are used to secure forms from spamming attacks. Once a token
  81. * has been generated the system will check the post request to see if
  82. * it is present, if not it will invalidate the session.
  83. *
  84. * @return string The session token
  85. */
  86. protected function _tokenName()
  87. {
  88. return $this->_token_name;
  89. }
  90. /**
  91. * Add unique token field
  92. *
  93. * @param string
  94. * @return KTemplateFilterForm
  95. */
  96. public function write(&$text)
  97. {
  98. // All: Add the action if left empty
  99. if (preg_match_all('#<\s*form.*?action=""#im', $text, $matches, PREG_SET_ORDER))
  100. {
  101. $view = $this->getTemplate()->getView();
  102. $state = $view->getModel()->getState();
  103. $action = $view->getRoute(http_build_query($state->getData($state->isUnique())));
  104. foreach ($matches as $match)
  105. {
  106. $str = str_replace('action=""', 'action="'.$action.'"', $match[0]);
  107. $text = str_replace($match[0], $str, $text);
  108. }
  109. }
  110. // POST : Add token
  111. $matches = array();
  112. preg_match_all('/(<form.*method="post".*>)/i', $text, $matches, PREG_SET_ORDER);
  113. foreach($matches as $match)
  114. {
  115. $input = PHP_EOL.'<input type="hidden" name="'.$this->_tokenName().'" value="'.$this->_tokenValue().'" />';
  116. $text = str_replace($match[0], $match[0].$input, $text, $count);
  117. }
  118. // GET : Add token to .-koowa-grid forms
  119. $matches = array();
  120. preg_match_all('#(<\s*?form\s+?.*?class=(?:\'|")[^\'"]*?-koowa-grid.*?(?:\'|").*?)#im', $text, $matches, PREG_SET_ORDER);
  121. foreach($matches as $match)
  122. {
  123. $input = ' data-token-name="'.$this->_tokenName().'" data-token-value="'.$this->_tokenValue().'"';
  124. $text= str_replace($match[0], $match[0].$input, $text, $count);
  125. }
  126. // GET : Add query params
  127. $matches = array();
  128. if(preg_match_all('#<form.*action=".*\?(.*)".*method="get".*>#iU', $text, $matches))
  129. {
  130. foreach($matches[1] as $key => $query)
  131. {
  132. parse_str(str_replace('&amp;', '&', $query), $query);
  133. $input = $this->_renderQuery($query);
  134. $text = str_replace($matches[0][$key], $matches[0][$key].$input, $text);
  135. }
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Recursive function that transforms the query array into a string of input elements
  141. *
  142. * @param array Associative array of query information
  143. * @param string The name of the current input element
  144. * @return string String of the html input elements
  145. */
  146. protected function _renderQuery($query, $key = '')
  147. {
  148. $input = '';
  149. foreach($query as $name => $value)
  150. {
  151. $name = $key ? $key.'['.$name.']' : $name;
  152. if(is_array($value)) {
  153. $input .= $this->_renderQuery($value, $name);
  154. }
  155. else $input .= PHP_EOL.'<input type="hidden" name="'.$name.'" value="'.$value.'" />';
  156. }
  157. return $input;
  158. }
  159. }