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

/system/library/PEAR/HTML/QuickForm2.php

https://bitbucket.org/spekkionu/passworddb
PHP | 257 lines | 115 code | 18 blank | 124 comment | 15 complexity | 0e40459b5714509e2f95b82570d54b11 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Class representing a HTML form
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE:
  8. *
  9. * Copyright (c) 2006-2012, Alexey Borzov <avb@php.net>,
  10. * Bertrand Mansion <golgote@mamasam.com>
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * The names of the authors may not be used to endorse or promote products
  23. * derived from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  33. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * @category HTML
  38. * @package HTML_QuickForm2
  39. * @author Alexey Borzov <avb@php.net>
  40. * @author Bertrand Mansion <golgote@mamasam.com>
  41. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  42. * @version SVN: $Id: QuickForm2.php 325701 2012-05-15 15:00:09Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Abstract base class for QuickForm2 containers
  47. */
  48. require_once 'HTML/QuickForm2/Container.php';
  49. /**
  50. * Data source for HTML_QuickForm2 objects based on superglobal arrays
  51. */
  52. require_once 'HTML/QuickForm2/DataSource/SuperGlobal.php';
  53. /**
  54. * Class representing a HTML form
  55. *
  56. * @category HTML
  57. * @package HTML_QuickForm2
  58. * @author Alexey Borzov <avb@php.net>
  59. * @author Bertrand Mansion <golgote@mamasam.com>
  60. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  61. * @version Release: 2.0.0
  62. * @link http://pear.php.net/package/HTML_QuickForm2
  63. */
  64. class HTML_QuickForm2 extends HTML_QuickForm2_Container
  65. {
  66. /**
  67. * Data sources providing values for form elements
  68. * @var array
  69. */
  70. protected $datasources = array();
  71. /**
  72. * We do not allow setting "method" and "id" other than through constructor
  73. * @var array
  74. */
  75. protected $watchedAttributes = array('id', 'method');
  76. /**
  77. * Class constructor, form's "id" and "method" attributes can only be set here
  78. *
  79. * @param string $id "id" attribute of <form> tag
  80. * @param string $method HTTP method used to submit the form
  81. * @param string|array $attributes Additional HTML attributes
  82. * (either a string or an array)
  83. * @param bool $trackSubmit Whether to track if the form was submitted
  84. * by adding a special hidden field
  85. */
  86. public function __construct(
  87. $id, $method = 'post', $attributes = null, $trackSubmit = true
  88. ) {
  89. $method = ('GET' == strtoupper($method))? 'get': 'post';
  90. $trackSubmit = empty($id) ? false : $trackSubmit;
  91. $this->attributes = array_merge(
  92. self::prepareAttributes($attributes),
  93. array('method' => $method)
  94. );
  95. parent::setId(empty($id) ? null : $id);
  96. if (!isset($this->attributes['action'])) {
  97. $this->attributes['action'] = $_SERVER['PHP_SELF'];
  98. }
  99. if ($trackSubmit && isset($_REQUEST['_qf__' . $id]) ||
  100. !$trackSubmit && ('get' == $method && !empty($_GET) ||
  101. 'post' == $method && (!empty($_POST) || !empty($_FILES)))
  102. ) {
  103. $this->addDataSource(new HTML_QuickForm2_DataSource_SuperGlobal(
  104. $method, get_magic_quotes_gpc()
  105. ));
  106. }
  107. if ($trackSubmit) {
  108. $this->appendChild(HTML_QuickForm2_Factory::createElement(
  109. 'hidden', '_qf__' . $id, array('id' => 'qf:' . $id)
  110. ));
  111. }
  112. $this->addFilter(array($this, 'skipInternalFields'));
  113. }
  114. protected function onAttributeChange($name, $value = null)
  115. {
  116. throw new HTML_QuickForm2_InvalidArgumentException(
  117. 'Attribute \'' . $name . '\' is read-only'
  118. );
  119. }
  120. protected function setContainer(HTML_QuickForm2_Container $container = null)
  121. {
  122. throw new HTML_QuickForm2_Exception('Form cannot be added to container');
  123. }
  124. public function setId($id = null)
  125. {
  126. throw new HTML_QuickForm2_InvalidArgumentException(
  127. "Attribute 'id' is read-only"
  128. );
  129. }
  130. /**
  131. * Adds a new data source to the form
  132. *
  133. * @param HTML_QuickForm2_DataSource $datasource Data source
  134. */
  135. public function addDataSource(HTML_QuickForm2_DataSource $datasource)
  136. {
  137. $this->datasources[] = $datasource;
  138. $this->updateValue();
  139. }
  140. /**
  141. * Replaces the list of form's data sources with a completely new one
  142. *
  143. * @param array $datasources A new data source list
  144. *
  145. * @throws HTML_QuickForm2_InvalidArgumentException if given array
  146. * contains something that is not a valid data source
  147. */
  148. public function setDataSources(array $datasources)
  149. {
  150. foreach ($datasources as $ds) {
  151. if (!$ds instanceof HTML_QuickForm2_DataSource) {
  152. throw new HTML_QuickForm2_InvalidArgumentException(
  153. 'Array should contain only DataSource instances'
  154. );
  155. }
  156. }
  157. $this->datasources = $datasources;
  158. $this->updateValue();
  159. }
  160. /**
  161. * Returns the list of data sources attached to the form
  162. *
  163. * @return array
  164. */
  165. public function getDataSources()
  166. {
  167. return $this->datasources;
  168. }
  169. public function getType()
  170. {
  171. return 'form';
  172. }
  173. public function setValue($value)
  174. {
  175. throw new HTML_QuickForm2_Exception('Not implemented');
  176. }
  177. /**
  178. * Tells whether the form was already submitted
  179. *
  180. * This is a shortcut for checking whether there is an instance of Submit
  181. * data source in the list of form data sources
  182. *
  183. * @return bool
  184. */
  185. public function isSubmitted()
  186. {
  187. foreach ($this->datasources as $ds) {
  188. if ($ds instanceof HTML_QuickForm2_DataSource_Submit) {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. /**
  195. * Performs the server-side validation
  196. *
  197. * @return boolean Whether all form's elements are valid
  198. */
  199. public function validate()
  200. {
  201. return $this->isSubmitted() && parent::validate();
  202. }
  203. /**
  204. * Renders the form using the given renderer
  205. *
  206. * @param HTML_QuickForm2_Renderer $renderer
  207. *
  208. * @return HTML_QuickForm2_Renderer
  209. */
  210. public function render(HTML_QuickForm2_Renderer $renderer)
  211. {
  212. $renderer->startForm($this);
  213. $renderer->getJavascriptBuilder()->setFormId($this->getId());
  214. foreach ($this as $element) {
  215. $element->render($renderer);
  216. }
  217. $this->renderClientRules($renderer->getJavascriptBuilder());
  218. $renderer->finishForm($this);
  219. return $renderer;
  220. }
  221. /**
  222. * Filter for form's getValue() removing internal fields' values from the array
  223. *
  224. * @param array $value
  225. *
  226. * @return array
  227. * @link http://pear.php.net/bugs/bug.php?id=19403
  228. */
  229. protected function skipInternalFields($value)
  230. {
  231. foreach (array_keys($value) as $key) {
  232. if ('_qf' === substr($key, 0, 3)) {
  233. unset($value[$key]);
  234. }
  235. }
  236. return $value;
  237. }
  238. }
  239. ?>