QuickForm.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931
  1. <?php
  2. /**
  3. * Create, validate and process HTML forms
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @author Alexey Borzov <avb@php.net>
  18. * @copyright 2001-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version CVS: $Id: QuickForm.php,v 1.166 2009/04/04 21:34:02 avb Exp $
  21. * @link http://pear.php.net/package/HTML_QuickForm
  22. */
  23. /**
  24. * Validation rules known to HTML_QuickForm
  25. * @see HTML_QuickForm::registerRule(), HTML_QuickForm::getRegisteredRules(),
  26. * HTML_QuickForm::isRuleRegistered()
  27. * @global array $GLOBALS['_HTML_QuickForm_registered_rules']
  28. */
  29. /**
  30. * Error codes for HTML_QuickForm
  31. *
  32. * Codes are mapped to textual messages by errorMessage() method, if you add a
  33. * new code be sure to add a new message for it to errorMessage()
  34. *
  35. * @see HTML_QuickForm::errorMessage()
  36. */
  37. define('QUICKFORM_OK', 1);
  38. define('QUICKFORM_ERROR', -1);
  39. define('QUICKFORM_INVALID_RULE', -2);
  40. define('QUICKFORM_NONEXIST_ELEMENT', -3);
  41. define('QUICKFORM_INVALID_FILTER', -4);
  42. define('QUICKFORM_UNREGISTERED_ELEMENT', -5);
  43. define('QUICKFORM_INVALID_ELEMENT_NAME', -6);
  44. define('QUICKFORM_INVALID_PROCESS', -7);
  45. define('QUICKFORM_DEPRECATED', -8);
  46. define('QUICKFORM_INVALID_DATASOURCE', -9);
  47. /**
  48. * Class HTML_QuickForm
  49. * Create, validate and process HTML forms
  50. *
  51. * @category HTML
  52. * @package HTML_QuickForm
  53. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  54. * @author Bertrand Mansion <bmansion@mamasam.com>
  55. * @author Alexey Borzov <avb@php.net>
  56. * @version Release: 3.2.11
  57. */
  58. class HTML_QuickForm extends HTML_Common
  59. {
  60. const MAX_ELEMENT_ARGUMENT = 10;
  61. /**
  62. * Array containing the form fields
  63. * @since 1.0
  64. * @var array
  65. * @access private
  66. */
  67. public $_elements = array();
  68. /**
  69. * Array containing element name to index map
  70. * @since 1.1
  71. * @var array
  72. * @access private
  73. */
  74. public $_elementIndex = array();
  75. /**
  76. * Array containing indexes of duplicate elements
  77. * @since 2.10
  78. * @var array
  79. * @access private
  80. */
  81. public $_duplicateIndex = array();
  82. /**
  83. * Array containing required field IDs
  84. * @since 1.0
  85. * @var array
  86. * @access private
  87. */
  88. public $_required = array();
  89. /**
  90. * Prefix message in javascript alert if error
  91. * @since 1.0
  92. * @var string
  93. * @access public
  94. */
  95. public $_jsPrefix = 'Invalid information entered.';
  96. /**
  97. * Postfix message in javascript alert if error
  98. * @since 1.0
  99. * @var string
  100. * @access public
  101. */
  102. public $_jsPostfix = 'Please correct these fields.';
  103. /**
  104. * Datasource object implementing the informal
  105. * datasource protocol
  106. * @since 3.3
  107. * @var object
  108. * @access private
  109. */
  110. public $_datasource;
  111. /**
  112. * Array of default form values
  113. * @since 2.0
  114. * @var array
  115. * @access private
  116. */
  117. public $_defaultValues = array();
  118. /**
  119. * Array of constant form values
  120. * @since 2.0
  121. * @var array
  122. * @access private
  123. */
  124. public $_constantValues = array();
  125. /**
  126. * Array of submitted form values
  127. * @since 1.0
  128. * @var array
  129. * @access private
  130. */
  131. public $_submitValues = array();
  132. /**
  133. * Array of submitted form files
  134. * @since 1.0
  135. * @var integer
  136. * @access public
  137. */
  138. public $_submitFiles = array();
  139. /**
  140. * Value for maxfilesize hidden element if form contains file input
  141. * @since 1.0
  142. * @var integer
  143. * @access public
  144. */
  145. public $_maxFileSize = 1048576; // 1 Mb = 1048576
  146. /**
  147. * Flag to know if all fields are frozen
  148. * @since 1.0
  149. * @var boolean
  150. * @access private
  151. */
  152. public $_freezeAll = false;
  153. /**
  154. * Array containing the form rules
  155. * @since 1.0
  156. * @var array
  157. * @access private
  158. */
  159. public $_rules = array();
  160. /**
  161. * Form rules, global variety
  162. * @var array
  163. * @access private
  164. */
  165. public $_formRules = array();
  166. /**
  167. * Array containing the validation errors
  168. * @since 1.0
  169. * @var array
  170. * @access private
  171. */
  172. public $_errors = array();
  173. /**
  174. * Note for required fields in the form
  175. * @var string
  176. * @since 1.0
  177. * @access private
  178. */
  179. public $_requiredNote = '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>';
  180. /**
  181. * Whether the form was submitted
  182. * @var boolean
  183. * @access private
  184. */
  185. public $_flagSubmitted = false;
  186. /**
  187. * Class constructor
  188. * @param string $formName Form's name.
  189. * @param string $method (optional)Form's method defaults to 'POST'
  190. * @param string $action (optional)Form's action
  191. * @param string $target (optional)Form's target defaults to '_self'
  192. * @param mixed $attributes (optional)Extra attributes for <form> tag
  193. * @param bool $trackSubmit (optional)Whether to track if the form was submitted by adding a special hidden field
  194. * @access public
  195. */
  196. public function __construct(
  197. $formName = '',
  198. $method = 'post',
  199. $action = '',
  200. $target = '',
  201. $attributes = null,
  202. $trackSubmit = false
  203. ) {
  204. parent::__construct($attributes);
  205. $method = (strtoupper($method) == 'GET') ? 'get' : 'post';
  206. $action = ($action == '') ? api_get_self() : $action;
  207. $target = empty($target) ? array() : array('target' => $target);
  208. $form_id = $formName;
  209. if (isset($attributes['id']) && !empty($attributes['id'])) {
  210. $form_id = Security::remove_XSS($attributes['id']);
  211. }
  212. $attributes = array(
  213. 'action' => $action,
  214. 'method' => $method,
  215. 'name' => $formName,
  216. 'id' => $form_id
  217. ) + $target;
  218. $this->updateAttributes($attributes);
  219. if (!$trackSubmit || isset($_REQUEST['_qf__' . $formName])) {
  220. $this->_submitValues = 'get' == $method ? $_GET : $_POST;
  221. $this->_submitFiles = $_FILES;
  222. $this->_flagSubmitted = count($this->_submitValues) > 0 || count($this->_submitFiles) > 0;
  223. }
  224. if ($trackSubmit) {
  225. unset($this->_submitValues['_qf__' . $formName]);
  226. $this->addElement('hidden', '_qf__' . $formName, null);
  227. }
  228. if (preg_match('/^([0-9]+)([a-zA-Z]*)$/', ini_get('upload_max_filesize'), $matches)) {
  229. // see http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
  230. switch (strtoupper($matches['2'])) {
  231. case 'G':
  232. $this->_maxFileSize = $matches['1'] * 1073741824;
  233. break;
  234. case 'M':
  235. $this->_maxFileSize = $matches['1'] * 1048576;
  236. break;
  237. case 'K':
  238. $this->_maxFileSize = $matches['1'] * 1024;
  239. break;
  240. default:
  241. $this->_maxFileSize = $matches['1'];
  242. }
  243. }
  244. }
  245. /**
  246. * Returns the current API version
  247. *
  248. * @since 1.0
  249. * @access public
  250. * @return float
  251. */
  252. function apiVersion()
  253. {
  254. return 3.2;
  255. }
  256. /**
  257. * Registers a new element type
  258. *
  259. * @param string $typeName Name of element type
  260. * @param string $include Include path for element type
  261. * @param string $className Element class name
  262. * @since 1.0
  263. * @access public
  264. * @return void
  265. */
  266. public function registerElementType($typeName, $include, $className)
  267. {
  268. $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($typeName)] = array($include, $className);
  269. }
  270. /**
  271. * Registers a new validation rule
  272. *
  273. * @param string $ruleName Name of validation rule
  274. * @param string $type Either: 'regex', 'function' or 'rule' for an HTML_QuickForm_Rule object
  275. * @param string $data1 Name of function, regular expression or HTML_QuickForm_Rule classname
  276. * @param string $data2 Object parent of above function or HTML_QuickForm_Rule file path
  277. * @since 1.0
  278. * @access public
  279. * @return void
  280. */
  281. public static function registerRule($ruleName, $type, $data1, $data2 = null)
  282. {
  283. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  284. $registry->registerRule($ruleName, $type, $data1, $data2);
  285. }
  286. /**
  287. * Returns true if element is in the form
  288. *
  289. * @param string $element form name of element to check
  290. * @since 1.0
  291. * @access public
  292. * @return boolean
  293. */
  294. public function elementExists($element = null)
  295. {
  296. return isset($this->_elementIndex[$element]);
  297. }
  298. /**
  299. * Initializes default form values
  300. *
  301. * @param array $defaultValues values used to fill the form
  302. * @param mixed $filter (optional) filter(s) to apply to all default values
  303. * @since 1.0
  304. * @access public
  305. */
  306. public function setDefaults($defaultValues = null, $filter = null)
  307. {
  308. if (is_array($defaultValues)) {
  309. if (isset($filter)) {
  310. if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
  311. foreach ($filter as $val) {
  312. if (!is_callable($val)) {
  313. throw new \Exception('Callback function does not exist in QuickForm::setDefaults()');
  314. } else {
  315. $defaultValues = $this->_recursiveFilter($val, $defaultValues);
  316. }
  317. }
  318. } elseif (!is_callable($filter)) {
  319. throw new \Exception('Callback function does not exist in QuickForm::setDefaults()');
  320. } else {
  321. $defaultValues = $this->_recursiveFilter($filter, $defaultValues);
  322. }
  323. }
  324. $this->_defaultValues = self::arrayMerge($this->_defaultValues, $defaultValues);
  325. $this->_constantValues = $this->_defaultValues;
  326. foreach (array_keys($this->_elements) as $key) {
  327. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  328. }
  329. }
  330. }
  331. /**
  332. * Initializes constant form values.
  333. * These values won't get overridden by POST or GET vars
  334. *
  335. * @param array $constantValues values used to fill the form
  336. * @param mixed $filter (optional) filter(s) to apply to all default values
  337. *
  338. * @since 2.0
  339. * @access public
  340. * @return void
  341. */
  342. public function setConstants($constantValues = null, $filter = null)
  343. {
  344. if (is_array($constantValues)) {
  345. if (isset($filter)) {
  346. if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
  347. foreach ($filter as $val) {
  348. if (!is_callable($val)) {
  349. throw new \Exception("Callback function does not exist in QuickForm::setConstants()");
  350. } else {
  351. $constantValues = $this->_recursiveFilter($val, $constantValues);
  352. }
  353. }
  354. } elseif (!is_callable($filter)) {
  355. throw new \Exception("Callback function does not exist in QuickForm::setConstants()");
  356. } else {
  357. $constantValues = $this->_recursiveFilter($filter, $constantValues);
  358. }
  359. }
  360. $this->_constantValues = HTML_QuickForm::arrayMerge($this->_constantValues, $constantValues);
  361. foreach (array_keys($this->_elements) as $key) {
  362. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  363. }
  364. }
  365. }
  366. /**
  367. * Sets the value of MAX_FILE_SIZE hidden element
  368. *
  369. * @param int $bytes Size in bytes
  370. * @since 3.0
  371. * @access public
  372. * @return void
  373. */
  374. public function setMaxFileSize($bytes = 0)
  375. {
  376. if ($bytes > 0) {
  377. $this->_maxFileSize = $bytes;
  378. }
  379. if (!$this->elementExists('MAX_FILE_SIZE')) {
  380. $this->addElement('hidden', 'MAX_FILE_SIZE', $this->_maxFileSize);
  381. } else {
  382. $el =& $this->getElement('MAX_FILE_SIZE');
  383. $el->updateAttributes(array('value' => $this->_maxFileSize));
  384. }
  385. }
  386. /**
  387. * Returns the value of MAX_FILE_SIZE hidden element
  388. *
  389. * @since 3.0
  390. * @access public
  391. * @return int max file size in bytes
  392. */
  393. public function getMaxFileSize()
  394. {
  395. return $this->_maxFileSize;
  396. }
  397. /**
  398. * Creates a new form element of the given type.
  399. *
  400. * This method accepts variable number of parameters, their
  401. * meaning and count depending on $elementType
  402. *
  403. * @param string $elementType type of element to add (text, textarea, file...)
  404. * @since 1.0
  405. * @access public
  406. * @return HTML_QuickForm_Element
  407. * @throws HTML_QuickForm_Error
  408. */
  409. public function &createElement($elementType)
  410. {
  411. $args = func_get_args();
  412. $element = $this->_loadElement(
  413. 'createElement',
  414. $elementType,
  415. array_slice($args, 1)
  416. );
  417. return $element;
  418. }
  419. /**
  420. * Returns a form element of the given type
  421. *
  422. * @param string $event event to send to newly created element ('createElement' or 'addElement')
  423. * @param string $type element type
  424. * @param array $args arguments for event
  425. * @since 2.0
  426. * @access private
  427. * @return HTML_QuickForm_Element
  428. * @throws HTML_QuickForm_Error
  429. */
  430. public function &_loadElement($event, $type, $args)
  431. {
  432. $className = null;
  433. // Try if class exists
  434. if (!class_exists($type)) {
  435. // Try classic class name HTML_QuickForm_
  436. $className = 'HTML_QuickForm_'.$type;
  437. if (!class_exists($className)) {
  438. // Try classic class name HTML_QuickForm_ with strtolower
  439. $lowerType = strtolower($type);
  440. $className = 'HTML_QuickForm_'.$lowerType;
  441. if (!class_exists($className)) {
  442. // Try new class name CamelCase
  443. $className = underScoreToCamelCase($type);
  444. if (!class_exists($className)) {
  445. throw new \Exception("Class '$className' does not exist. ");
  446. }
  447. }
  448. }
  449. } else {
  450. $className = $type;
  451. }
  452. if (empty($className)) {
  453. throw new \Exception("Class '$className' does not exist. ");
  454. }
  455. for ($i = 0; $i < self::MAX_ELEMENT_ARGUMENT; $i++) {
  456. if (!isset($args[$i])) {
  457. $args[$i] = null;
  458. }
  459. }
  460. /** @var HTML_QuickForm_element $element */
  461. $element = new $className(
  462. $args[0],
  463. $args[1],
  464. $args[2],
  465. $args[3],
  466. $args[4],
  467. $args[5],
  468. $args[6],
  469. $args[7],
  470. $args[8],
  471. $args[9]
  472. );
  473. if ($event != 'createElement') {
  474. $err = $element->onQuickFormEvent($event, $args, $this);
  475. if ($err !== true) {
  476. return $err;
  477. }
  478. }
  479. return $element;
  480. }
  481. /**
  482. * Adds an element into the form
  483. *
  484. * If $element is a string representing element type, then this
  485. * method accepts variable number of parameters, their meaning
  486. * and count depending on $element
  487. *
  488. * @param mixed $element element object or type of element to add (text, textarea, file...)
  489. * @since 1.0
  490. * @return HTML_QuickForm_element a reference to newly added element
  491. * @access public
  492. * @throws HTML_QuickForm_Error
  493. */
  494. public function &addElement($element)
  495. {
  496. if (is_object($element) && is_subclass_of($element, 'html_quickform_element')) {
  497. $elementObject = &$element;
  498. $elementObject->onQuickFormEvent('updateValue', null, $this);
  499. } else {
  500. $args = func_get_args();
  501. $elementObject =& $this->_loadElement('addElement', $element, array_slice($args, 1));
  502. if (PEAR::isError($elementObject)) {
  503. return $elementObject;
  504. }
  505. }
  506. $elementName = $elementObject->getName();
  507. // Add the element if it is not an incompatible duplicate
  508. if (!empty($elementName) &&
  509. isset($this->_elementIndex[$elementName])
  510. ) {
  511. if ($this->_elements[$this->_elementIndex[$elementName]]->getType() == $elementObject->getType()) {
  512. $this->_elements[] =& $elementObject;
  513. $elKeys = array_keys($this->_elements);
  514. $this->_duplicateIndex[$elementName][] = end($elKeys);
  515. } else {
  516. throw new \Exception("Element '$elementName' already exists in HTML_QuickForm::addElement()");
  517. }
  518. } else {
  519. $this->_elements[] =& $elementObject;
  520. $elKeys = array_keys($this->_elements);
  521. $this->_elementIndex[$elementName] = end($elKeys);
  522. }
  523. $elId = $elementObject->getAttribute('id');
  524. if (empty($elId)) {
  525. $elementObject->setAttribute('id', "{$this->getAttribute('name')}_$elementName");
  526. }
  527. if ($this->_freezeAll) {
  528. $elementObject->freeze();
  529. }
  530. return $elementObject;
  531. }
  532. /**
  533. * @return array
  534. */
  535. public function getElements()
  536. {
  537. return $this->_elements;
  538. }
  539. /**
  540. * Inserts a new element right before the other element
  541. *
  542. * Warning: it is not possible to check whether the $element is already
  543. * added to the form, therefore if you want to move the existing form
  544. * element to a new position, you'll have to use removeElement():
  545. * $form->insertElementBefore($form->removeElement('foo', false), 'bar');
  546. *
  547. * @access public
  548. * @since 3.2.4
  549. * @param HTML_QuickForm_element Element to insert
  550. * @param string Name of the element before which the new
  551. * one is inserted
  552. * @return HTML_QuickForm_element reference to inserted element
  553. */
  554. public function &insertElementBefore(&$element, $nameAfter)
  555. {
  556. if (!empty($this->_duplicateIndex[$nameAfter])) {
  557. throw new \Exception('Several elements named "' . $nameAfter . '" exist in HTML_QuickForm::insertElementBefore().');
  558. } elseif (!$this->elementExists($nameAfter)) {
  559. throw new \Exception("Element '$nameAfter' does not exist in HTML_QuickForm::insertElementBefore()");
  560. }
  561. $elementName = $element->getName();
  562. $targetIdx = $this->_elementIndex[$nameAfter];
  563. $duplicate = false;
  564. // Like in addElement(), check that it's not an incompatible duplicate
  565. if (!empty($elementName) && isset($this->_elementIndex[$elementName])) {
  566. if ($this->_elements[$this->_elementIndex[$elementName]]->getType() != $element->getType()) {
  567. throw new \Exception("Element '$elementName' already exists in HTML_QuickForm::insertElementBefore()");
  568. }
  569. $duplicate = true;
  570. }
  571. // Move all the elements after added back one place, reindex _elementIndex and/or _duplicateIndex
  572. $elKeys = array_keys($this->_elements);
  573. for ($i = end($elKeys); $i >= $targetIdx; $i--) {
  574. if (isset($this->_elements[$i])) {
  575. $currentName = $this->_elements[$i]->getName();
  576. $this->_elements[$i + 1] =& $this->_elements[$i];
  577. if ($this->_elementIndex[$currentName] == $i) {
  578. $this->_elementIndex[$currentName] = $i + 1;
  579. } else {
  580. $dupIdx = array_search($i, $this->_duplicateIndex[$currentName]);
  581. $this->_duplicateIndex[$currentName][$dupIdx] = $i + 1;
  582. }
  583. unset($this->_elements[$i]);
  584. }
  585. }
  586. // Put the element in place finally
  587. $this->_elements[$targetIdx] =& $element;
  588. if (!$duplicate) {
  589. $this->_elementIndex[$elementName] = $targetIdx;
  590. } else {
  591. $this->_duplicateIndex[$elementName][] = $targetIdx;
  592. }
  593. $element->onQuickFormEvent('updateValue', null, $this);
  594. if ($this->_freezeAll) {
  595. $element->freeze();
  596. }
  597. // If not done, the elements will appear in reverse order
  598. ksort($this->_elements);
  599. return $element;
  600. }
  601. /**
  602. * Adds an element group
  603. * @param array $elements array of elements composing the group
  604. * @param string $name (optional)group name
  605. * @param string $groupLabel (optional)group label
  606. * @param string $separator (optional)string to separate elements
  607. * @param bool $appendName (optional)specify whether the group name should be
  608. * used in the form element name ex: group[element]
  609. * @return HTML_QuickForm_group reference to a newly added group
  610. * @since 2.8
  611. * @access public
  612. * @throws HTML_QuickForm_Error
  613. */
  614. public function &addGroup(
  615. $elements,
  616. $name = null,
  617. $groupLabel = '',
  618. $separator = null,
  619. $appendName = true,
  620. $createElement = false
  621. ) {
  622. static $anonGroups = 1;
  623. if (0 == strlen($name)) {
  624. $name = 'qf_group_'.$anonGroups++;
  625. $appendName = false;
  626. }
  627. if ($createElement) {
  628. return $this->createElement('group', $name, $groupLabel, $elements, $separator, $appendName);
  629. }
  630. $group = & $this->addElement('group', $name, $groupLabel, $elements, $separator, $appendName);
  631. return $group;
  632. }
  633. /**
  634. * Returns a reference to the element
  635. *
  636. * @param string $element Element name
  637. * @since 2.0
  638. * @access public
  639. * @return HTML_QuickForm_element reference to element
  640. * @throws HTML_QuickForm_Error
  641. */
  642. public function &getElement($element)
  643. {
  644. if (isset($this->_elementIndex[$element])) {
  645. return $this->_elements[$this->_elementIndex[$element]];
  646. } else {
  647. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::getElement()");
  648. }
  649. }
  650. /**
  651. * @param string $name
  652. * @return mixed
  653. */
  654. public function getElementByName($name)
  655. {
  656. foreach ($this->_elements as &$element) {
  657. $elementName = $element->getName();
  658. if ($elementName == $name) {
  659. return $element;
  660. }
  661. }
  662. }
  663. /**
  664. * @param string $element
  665. * @return bool
  666. */
  667. public function hasElement($element)
  668. {
  669. return isset($this->_elementIndex[$element]);
  670. }
  671. /**
  672. * Returns the element's raw value
  673. *
  674. * This returns the value as submitted by the form (not filtered)
  675. * or set via setDefaults() or setConstants()
  676. *
  677. * @param string $element Element name
  678. * @since 2.0
  679. * @access public
  680. * @return mixed element value
  681. * @throws HTML_QuickForm_Error
  682. */
  683. public function &getElementValue($element)
  684. {
  685. if (!isset($this->_elementIndex[$element])) {
  686. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::getElementValue()");
  687. }
  688. $value = $this->_elements[$this->_elementIndex[$element]]->getValue();
  689. if (isset($this->_duplicateIndex[$element])) {
  690. foreach ($this->_duplicateIndex[$element] as $index) {
  691. if (null !== ($v = $this->_elements[$index]->getValue())) {
  692. if (is_array($value)) {
  693. $value[] = $v;
  694. } else {
  695. $value = (null === $value)? $v: array($value, $v);
  696. }
  697. }
  698. }
  699. }
  700. return $value;
  701. }
  702. /**
  703. * Returns the elements value after submit and filter
  704. *
  705. * @param string Element name
  706. * @since 2.0
  707. * @access public
  708. * @return mixed submitted element value or null if not set
  709. */
  710. public function getSubmitValue($elementName)
  711. {
  712. $value = null;
  713. if (isset($this->_submitValues[$elementName]) || isset($this->_submitFiles[$elementName])) {
  714. $value = isset($this->_submitValues[$elementName])? $this->_submitValues[$elementName]: array();
  715. if (is_array($value) && isset($this->_submitFiles[$elementName])) {
  716. foreach ($this->_submitFiles[$elementName] as $k => $v) {
  717. $value = HTML_QuickForm::arrayMerge(
  718. $value,
  719. $this->_reindexFiles($this->_submitFiles[$elementName][$k], $k)
  720. );
  721. }
  722. }
  723. } elseif ('file' == $this->getElementType($elementName)) {
  724. return $this->getElementValue($elementName);
  725. } elseif (false !== ($pos = strpos($elementName, '['))) {
  726. $base = str_replace(
  727. array('\\', '\''),
  728. array('\\\\', '\\\''),
  729. substr($elementName, 0, $pos)
  730. );
  731. $idx = "['".str_replace(
  732. array('\\', '\'', ']', '['),
  733. array('\\\\', '\\\'', '', "']['"),
  734. substr($elementName, $pos + 1, -1)
  735. )."']";
  736. if (isset($this->_submitValues[$base])) {
  737. $value = eval("return (isset(\$this->_submitValues['{$base}']{$idx})) ? \$this->_submitValues['{$base}']{$idx} : null;");
  738. }
  739. if ((is_array($value) || null === $value) && isset($this->_submitFiles[$base])) {
  740. $props = array('name', 'type', 'size', 'tmp_name', 'error');
  741. $code = "if (!isset(\$this->_submitFiles['{$base}']['name']{$idx})) {\n" .
  742. " return null;\n" .
  743. "} else {\n" .
  744. " \$v = array();\n";
  745. foreach ($props as $prop) {
  746. $code .= " \$v = HTML_QuickForm::arrayMerge(\$v, \$this->_reindexFiles(\$this->_submitFiles['{$base}']['{$prop}']{$idx}, '{$prop}'));\n";
  747. }
  748. $fileValue = eval($code . " return \$v;\n}\n");
  749. if (null !== $fileValue) {
  750. $value = null === $value? $fileValue: HTML_QuickForm::arrayMerge($value, $fileValue);
  751. }
  752. }
  753. }
  754. // This is only supposed to work for groups with appendName = false
  755. if (null === $value && 'group' == $this->getElementType($elementName)) {
  756. $group =& $this->getElement($elementName);
  757. $elements =& $group->getElements();
  758. foreach (array_keys($elements) as $key) {
  759. $name = $group->getElementName($key);
  760. // prevent endless recursion in case of radios and such
  761. if ($name != $elementName) {
  762. if (null !== ($v = $this->getSubmitValue($name))) {
  763. $value[$name] = $v;
  764. }
  765. }
  766. }
  767. }
  768. if ($this->hasElement($elementName)) {
  769. $element = $this->getElement($elementName);
  770. if (method_exists($element, 'getSubmitValue')) {
  771. $value = $element->getSubmitValue(
  772. $value,
  773. $this->_submitValues,
  774. $this->_errors
  775. );
  776. }
  777. }
  778. return $value;
  779. }
  780. /**
  781. * A helper function to change the indexes in $_FILES array
  782. *
  783. * @param mixed Some value from the $_FILES array
  784. * @param string The key from the $_FILES array that should be appended
  785. * @return array
  786. */
  787. public function _reindexFiles($value, $key)
  788. {
  789. if (!is_array($value)) {
  790. return array($key => $value);
  791. } else {
  792. $ret = array();
  793. foreach ($value as $k => $v) {
  794. $ret[$k] = $this->_reindexFiles($v, $key);
  795. }
  796. return $ret;
  797. }
  798. }
  799. /**
  800. * Returns error corresponding to validated element
  801. *
  802. * @param string $element Name of form element to check
  803. * @since 1.0
  804. * @access public
  805. * @return string error message corresponding to checked element
  806. */
  807. public function getElementError($element)
  808. {
  809. if (isset($this->_errors[$element])) {
  810. return $this->_errors[$element];
  811. }
  812. }
  813. /**
  814. * Set error message for a form element
  815. *
  816. * @param string $element Name of form element to set error for
  817. * @param string $message Error message, if empty then removes the current error message
  818. * @since 1.0
  819. * @access public
  820. * @return void
  821. */
  822. public function setElementError($element, $message = null)
  823. {
  824. if (!empty($message)) {
  825. $this->_errors[$element] = $message;
  826. } else {
  827. unset($this->_errors[$element]);
  828. }
  829. }
  830. /**
  831. * Returns the type of the given element
  832. *
  833. * @param string $element Name of form element
  834. * @since 1.1
  835. * @access public
  836. * @return string Type of the element, false if the element is not found
  837. */
  838. public function getElementType($element)
  839. {
  840. if (isset($this->_elementIndex[$element])) {
  841. return $this->_elements[$this->_elementIndex[$element]]->getType();
  842. }
  843. return false;
  844. }
  845. /**
  846. * Updates Attributes for one or more elements
  847. *
  848. * @param mixed $elements Array of element names/objects or string of elements to be updated
  849. * @param mixed $attrs Array or sting of html attributes
  850. * @since 2.10
  851. * @access public
  852. * @return void
  853. */
  854. public function updateElementAttr($elements, $attrs)
  855. {
  856. if (is_string($elements)) {
  857. $elements = split('[ ]?,[ ]?', $elements);
  858. }
  859. foreach (array_keys($elements) as $key) {
  860. if (is_object($elements[$key]) && is_a($elements[$key], 'HTML_QuickForm_element')) {
  861. $elements[$key]->updateAttributes($attrs);
  862. } elseif (isset($this->_elementIndex[$elements[$key]])) {
  863. $this->_elements[$this->_elementIndex[$elements[$key]]]->updateAttributes($attrs);
  864. if (isset($this->_duplicateIndex[$elements[$key]])) {
  865. foreach ($this->_duplicateIndex[$elements[$key]] as $index) {
  866. $this->_elements[$index]->updateAttributes($attrs);
  867. }
  868. }
  869. }
  870. }
  871. }
  872. /**
  873. * Removes an element
  874. *
  875. * The method "unlinks" an element from the form, returning the reference
  876. * to the element object. If several elements named $elementName exist,
  877. * it removes the first one, leaving the others intact.
  878. *
  879. * @param string $elementName The element name
  880. * @param boolean $removeRules True if rules for this element are to be removed too
  881. * @access public
  882. * @since 2.0
  883. * @return HTML_QuickForm_element a reference to the removed element
  884. * @throws HTML_QuickForm_Error
  885. */
  886. public function &removeElement($elementName, $removeRules = true)
  887. {
  888. if (!isset($this->_elementIndex[$elementName])) {
  889. throw new \Exception("Element '$elementName' does not exist in HTML_QuickForm::removeElement()");
  890. }
  891. $el =& $this->_elements[$this->_elementIndex[$elementName]];
  892. unset($this->_elements[$this->_elementIndex[$elementName]]);
  893. if (empty($this->_duplicateIndex[$elementName])) {
  894. unset($this->_elementIndex[$elementName]);
  895. } else {
  896. $this->_elementIndex[$elementName] = array_shift($this->_duplicateIndex[$elementName]);
  897. }
  898. if ($removeRules) {
  899. $this->_required = array_diff($this->_required, array($elementName));
  900. unset($this->_rules[$elementName], $this->_errors[$elementName]);
  901. if ('group' == $el->getType()) {
  902. foreach (array_keys($el->getElements()) as $key) {
  903. unset($this->_rules[$el->getElementName($key)]);
  904. }
  905. }
  906. }
  907. return $el;
  908. }
  909. /**
  910. * Adds a validation rule for the given field
  911. *
  912. * If the element is in fact a group, it will be considered as a whole.
  913. * To validate grouped elements as separated entities,
  914. * use addGroupRule instead of addRule.
  915. *
  916. * @param string $element Form element name
  917. * @param string $message Message to display for invalid data
  918. * @param string $type Rule type, use getRegisteredRules() to get types
  919. * @param string $format (optional)Required for extra rule data
  920. * @param string $validation (optional)Where to perform validation: "server", "client"
  921. * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error?
  922. * @param boolean $force Force the rule to be applied, even if the target form element does not exist
  923. * @param array|string $dependent needed when comparing values
  924. * @since 1.0
  925. * @access public
  926. */
  927. public function addRule(
  928. $element,
  929. $message,
  930. $type,
  931. $format = null,
  932. $validation = 'server',
  933. $reset = false,
  934. $force = false,
  935. $dependent = null
  936. ) {
  937. if (!$force) {
  938. if (!is_array($element) && !$this->elementExists($element)) {
  939. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::addRule()");
  940. } elseif (is_array($element)) {
  941. foreach ($element as $el) {
  942. if (!$this->elementExists($el)) {
  943. throw new \Exception("Element '$el' does not exist in HTML_QuickForm::addRule()");
  944. }
  945. }
  946. }
  947. }
  948. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  949. throw new \Exception("Rule '$type' is not registered in HTML_QuickForm::addRule()");
  950. } elseif (is_string($newName)) {
  951. $type = $newName;
  952. }
  953. if (is_array($element)) {
  954. $dependent = $element;
  955. $element = array_shift($dependent);
  956. }
  957. if ($type == 'required' || $type == 'uploadedfile') {
  958. $this->_required[] = $element;
  959. }
  960. if (!isset($this->_rules[$element])) {
  961. $this->_rules[$element] = array();
  962. }
  963. if ($validation == 'client') {
  964. $this->updateAttributes(
  965. array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);')
  966. );
  967. }
  968. $this->_rules[$element][] = array(
  969. 'type' => $type,
  970. 'format' => $format,
  971. 'message' => $message,
  972. 'validation' => $validation,
  973. 'reset' => $reset,
  974. 'dependent' => $dependent,
  975. );
  976. }
  977. /**
  978. * Adds a validation rule for the given group of elements
  979. *
  980. * Only groups with a name can be assigned a validation rule
  981. * Use addGroupRule when you need to validate elements inside the group.
  982. * Use addRule if you need to validate the group as a whole. In this case,
  983. * the same rule will be applied to all elements in the group.
  984. * Use addRule if you need to validate the group against a function.
  985. *
  986. * @param string $group Form group name
  987. * @param mixed $arg1 Array for multiple elements or error message string for one element
  988. * @param string $type (optional)Rule type use getRegisteredRules() to get types
  989. * @param string $format (optional)Required for extra rule data
  990. * @param int $howmany (optional)How many valid elements should be in the group
  991. * @param string $validation (optional)Where to perform validation: "server", "client"
  992. * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed.
  993. * @since 2.5
  994. * @access public
  995. * @throws HTML_QuickForm_Error
  996. */
  997. public function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false)
  998. {
  999. if (!$this->elementExists($group)) {
  1000. throw new \Exception("Group '$group' does not exist in HTML_QuickForm::addGroupRule()");
  1001. }
  1002. $groupObj =& $this->getElement($group);
  1003. if (is_array($arg1)) {
  1004. $required = 0;
  1005. foreach ($arg1 as $elementIndex => $rules) {
  1006. $elementName = $groupObj->getElementName($elementIndex);
  1007. foreach ($rules as $rule) {
  1008. $format = (isset($rule[2])) ? $rule[2] : null;
  1009. $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server';
  1010. $reset = isset($rule[4]) && $rule[4];
  1011. $type = $rule[1];
  1012. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  1013. throw new \Exception("Rule '$type' is not registered in HTML_QuickForm::addGroupRule()");
  1014. } elseif (is_string($newName)) {
  1015. $type = $newName;
  1016. }
  1017. $this->_rules[$elementName][] = array(
  1018. 'type' => $type,
  1019. 'format' => $format,
  1020. 'message' => $rule[0],
  1021. 'validation' => $validation,
  1022. 'reset' => $reset,
  1023. 'group' => $group,
  1024. );
  1025. if ('required' == $type || 'uploadedfile' == $type) {
  1026. $groupObj->_required[] = $elementName;
  1027. $this->_required[] = $elementName;
  1028. $required++;
  1029. }
  1030. if ('client' == $validation) {
  1031. $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);'));
  1032. }
  1033. }
  1034. }
  1035. if ($required > 0 && count($groupObj->getElements()) == $required) {
  1036. $this->_required[] = $group;
  1037. }
  1038. } elseif (is_string($arg1)) {
  1039. if (false === ($newName = $this->isRuleRegistered($type, true))) {
  1040. throw new \Exception("Rule '$type' is not registered in HTML_QuickForm::addGroupRule()");
  1041. } elseif (is_string($newName)) {
  1042. $type = $newName;
  1043. }
  1044. // addGroupRule() should also handle <select multiple>
  1045. if (is_a($groupObj, 'html_quickform_group')) {
  1046. // Radios need to be handled differently when required
  1047. if ($type == 'required' && $groupObj->getGroupType() == 'radio') {
  1048. $howmany = ($howmany == 0) ? 1 : $howmany;
  1049. } else {
  1050. $howmany = ($howmany == 0) ? count($groupObj->getElements()) : $howmany;
  1051. }
  1052. }
  1053. $this->_rules[$group][] = array(
  1054. 'type' => $type,
  1055. 'format' => $format,
  1056. 'message' => $arg1,
  1057. 'validation' => $validation,
  1058. 'howmany' => $howmany,
  1059. 'reset' => $reset,
  1060. );
  1061. if ($type == 'required') {
  1062. $this->_required[] = $group;
  1063. }
  1064. if ($validation == 'client') {
  1065. $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);'));
  1066. }
  1067. }
  1068. }
  1069. /**
  1070. * Adds a global validation rule
  1071. *
  1072. * This should be used when for a rule involving several fields or if
  1073. * you want to use some completely custom validation for your form.
  1074. * The rule function/method should return true in case of successful
  1075. * validation and array('element name' => 'error') when there were errors.
  1076. *
  1077. * @access public
  1078. * @param mixed Callback, either function name or array(&$object, 'method')
  1079. * @throws HTML_QuickForm_Error
  1080. */
  1081. public function addFormRule($rule)
  1082. {
  1083. if (!is_callable($rule)) {
  1084. throw new \Exception('Callback function does not exist in HTML_QuickForm::addFormRule()');
  1085. }
  1086. $this->_formRules[] = $rule;
  1087. }
  1088. /**
  1089. * Applies a data filter for the given field(s)
  1090. *
  1091. * @param mixed $element Form element name or array of such names
  1092. * @param mixed $filter Callback, either function name or array(&$object, 'method')
  1093. * @since 2.0
  1094. * @access public
  1095. * @throws HTML_QuickForm_Error
  1096. */
  1097. public function applyFilter($element, $filter)
  1098. {
  1099. if (!is_callable($filter)) {
  1100. throw new \Exception("Callback function does not exist in QuickForm::applyFilter()");
  1101. }
  1102. if ($element == '__ALL__') {
  1103. $this->_submitValues = $this->_recursiveFilter($filter, $this->_submitValues);
  1104. } else {
  1105. if (!is_array($element)) {
  1106. $element = array($element);
  1107. }
  1108. foreach ($element as $elName) {
  1109. $value = $this->getSubmitValue($elName);
  1110. if (null !== $value) {
  1111. if (false === strpos($elName, '[')) {
  1112. $this->_submitValues[$elName] = $this->_recursiveFilter($filter, $value);
  1113. } else {
  1114. $idx = "['" . str_replace(
  1115. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  1116. $elName
  1117. ) . "']";
  1118. eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);");
  1119. }
  1120. }
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. * Recursively apply a filter function
  1126. *
  1127. * @param string $filter filter to apply
  1128. * @param mixed $value submitted values
  1129. * @since 2.0
  1130. * @access private
  1131. * @return cleaned values
  1132. */
  1133. public function _recursiveFilter($filter, $value)
  1134. {
  1135. if (is_array($value)) {
  1136. $cleanValues = array();
  1137. foreach ($value as $k => $v) {
  1138. $cleanValues[$k] = $this->_recursiveFilter($filter, $v);
  1139. }
  1140. return $cleanValues;
  1141. } else {
  1142. return call_user_func($filter, $value);
  1143. }
  1144. }
  1145. /**
  1146. * Merges two arrays
  1147. *
  1148. * Merges two array like the PHP function array_merge but recursively.
  1149. * The main difference is that existing keys will not be renumbered
  1150. * if they are integers.
  1151. *
  1152. * @access public
  1153. * @param array $a original array
  1154. * @param array $b array which will be merged into first one
  1155. * @return array merged array
  1156. */
  1157. static function arrayMerge($a, $b)
  1158. {
  1159. foreach ($b as $k => $v) {
  1160. if (is_array($v)) {
  1161. if (isset($a[$k]) && !is_array($a[$k])) {
  1162. $a[$k] = $v;
  1163. } else {
  1164. if (!isset($a[$k])) {
  1165. $a[$k] = array();
  1166. }
  1167. $a[$k] = HTML_QuickForm::arrayMerge($a[$k], $v);
  1168. }
  1169. } else {
  1170. $a[$k] = $v;
  1171. }
  1172. }
  1173. return $a;
  1174. }
  1175. /**
  1176. * Returns whether or not the form element type is supported
  1177. *
  1178. * @param string $type Form element type
  1179. * @since 1.0
  1180. * @access public
  1181. * @return boolean
  1182. */
  1183. static function isTypeRegistered($type)
  1184. {
  1185. return isset($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($type)]);
  1186. }
  1187. /**
  1188. * Returns an array of registered element types
  1189. *
  1190. * @since 1.0
  1191. * @access public
  1192. * @return array
  1193. */
  1194. function getRegisteredTypes()
  1195. {
  1196. return array_keys($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']);
  1197. }
  1198. /**
  1199. * Returns whether or not the given rule is supported
  1200. *
  1201. * @param string $name Validation rule name
  1202. * @param bool Whether to automatically register subclasses of HTML_QuickForm_Rule
  1203. * @since 1.0
  1204. * @access public
  1205. * @return mixed true if previously registered, false if not, new rule name if auto-registering worked
  1206. */
  1207. function isRuleRegistered($name, $autoRegister = false)
  1208. {
  1209. return true;
  1210. }
  1211. /**
  1212. * Returns an array of registered validation rules
  1213. *
  1214. * @since 1.0
  1215. * @access public
  1216. * @return array
  1217. */
  1218. function getRegisteredRules()
  1219. {
  1220. return array_keys($GLOBALS['_HTML_QuickForm_registered_rules']);
  1221. }
  1222. /**
  1223. * Returns whether or not the form element is required
  1224. *
  1225. * @param string $element Form element name
  1226. * @since 1.0
  1227. * @access public
  1228. * @return boolean
  1229. */
  1230. function isElementRequired($element)
  1231. {
  1232. return in_array($element, $this->_required, true);
  1233. }
  1234. /**
  1235. * Returns whether or not the form element is frozen
  1236. *
  1237. * @param string $element Form element name
  1238. * @since 1.0
  1239. * @access public
  1240. * @return boolean
  1241. */
  1242. function isElementFrozen($element)
  1243. {
  1244. if (isset($this->_elementIndex[$element])) {
  1245. return $this->_elements[$this->_elementIndex[$element]]->isFrozen();
  1246. }
  1247. return false;
  1248. }
  1249. /**
  1250. * Sets JavaScript warning messages
  1251. *
  1252. * @param string $pref Prefix warning
  1253. * @param string $post Postfix warning
  1254. * @since 1.1
  1255. * @access public
  1256. * @return void
  1257. */
  1258. function setJsWarnings($pref, $post)
  1259. {
  1260. $this->_jsPrefix = $pref;
  1261. $this->_jsPostfix = $post;
  1262. }
  1263. /**
  1264. * Sets required-note
  1265. *
  1266. * @param string $note Message indicating some elements are required
  1267. * @since 1.1
  1268. * @access public
  1269. * @return void
  1270. */
  1271. function setRequiredNote($note)
  1272. {
  1273. $this->_requiredNote = $note;
  1274. }
  1275. /**
  1276. * Returns the required note
  1277. *
  1278. * @since 2.0
  1279. * @access public
  1280. * @return string
  1281. */
  1282. function getRequiredNote()
  1283. {
  1284. return $this->_requiredNote;
  1285. }
  1286. /**
  1287. * Performs the server side validation
  1288. * @access public
  1289. * @since 1.0
  1290. * @return boolean true if no error found
  1291. * @throws HTML_QuickForm_Error
  1292. */
  1293. public function validate()
  1294. {
  1295. if (count($this->_rules) == 0 && count($this->_formRules) == 0 && $this->isSubmitted()) {
  1296. return (0 == count($this->_errors));
  1297. } elseif (!$this->isSubmitted()) {
  1298. return false;
  1299. }
  1300. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1301. foreach ($this->_rules as $target => $rules) {
  1302. $submitValue = $this->getSubmitValue($target);
  1303. foreach ($rules as $rule) {
  1304. if ((isset($rule['group']) && isset($this->_errors[$rule['group']])) ||
  1305. isset($this->_errors[$target])) {
  1306. continue 2;
  1307. }
  1308. // If element is not required and is empty, we shouldn't validate it
  1309. if (!$this->isElementRequired($target)) {
  1310. if (!isset($submitValue) || '' == $submitValue) {
  1311. continue 2;
  1312. // Fix for bug #3501: we shouldn't validate not uploaded files, either.
  1313. // Unfortunately, we can't just use $element->isUploadedFile() since
  1314. // the element in question can be buried in group. Thus this hack.
  1315. // See also bug #12014, we should only consider a file that has
  1316. // status UPLOAD_ERR_NO_FILE as not uploaded, in all other cases
  1317. // validation should be performed, so that e.g. 'maxfilesize' rule
  1318. // will display an error if status is UPLOAD_ERR_INI_SIZE
  1319. // or UPLOAD_ERR_FORM_SIZE
  1320. } elseif (is_array($submitValue)) {
  1321. if (false === ($pos = strpos($target, '['))) {
  1322. $isUpload = !empty($this->_submitFiles[$target]);
  1323. } else {
  1324. $base = str_replace(
  1325. array('\\', '\''), array('\\\\', '\\\''),
  1326. substr($target, 0, $pos)
  1327. );
  1328. $idx = "['" . str_replace(
  1329. array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"),
  1330. substr($target, $pos + 1, -1)
  1331. ) . "']";
  1332. eval("\$isUpload = isset(\$this->_submitFiles['{$base}']['name']{$idx});");
  1333. }
  1334. if ($isUpload && (!isset($submitValue['error']) || UPLOAD_ERR_NO_FILE == $submitValue['error'])) {
  1335. continue 2;
  1336. }
  1337. }
  1338. }
  1339. if (isset($rule['dependent'])) {
  1340. $values = array($submitValue);
  1341. if (is_array($rule['dependent'])) {
  1342. foreach ($rule['dependent'] as $elName) {
  1343. $values[] = $this->getSubmitValue($elName);
  1344. }
  1345. } else {
  1346. $values[] = $rule['dependent'];
  1347. }
  1348. $result = $registry->validate(
  1349. $rule['type'],
  1350. $values,
  1351. $rule['format'],
  1352. true
  1353. );
  1354. } elseif (is_array($submitValue) && !isset($rule['howmany'])) {
  1355. $result = $registry->validate($rule['type'], $submitValue, $rule['format'], true);
  1356. } else {
  1357. $result = $registry->validate(
  1358. $rule['type'],
  1359. $submitValue,
  1360. $rule['format'],
  1361. false
  1362. );
  1363. }
  1364. if (!$result || (!empty($rule['howmany']) && $rule['howmany'] > (int)$result)) {
  1365. if (isset($rule['group'])) {
  1366. $this->_errors[$rule['group']] = $rule['message'];
  1367. } else {
  1368. $this->_errors[$target] = $rule['message'];
  1369. }
  1370. }
  1371. }
  1372. }
  1373. // process the global rules now
  1374. foreach ($this->_formRules as $rule) {
  1375. if (true !== ($res = call_user_func($rule, $this->_submitValues, $this->_submitFiles))) {
  1376. if (is_array($res)) {
  1377. $this->_errors += $res;
  1378. } else {
  1379. throw new \Exception('Form rule callback returned invalid value in HTML_QuickForm::validate()');
  1380. }
  1381. }
  1382. }
  1383. return (0 == count($this->_errors));
  1384. }
  1385. /**
  1386. * Displays elements without HTML input tags
  1387. *
  1388. * @param mixed $elementList array or string of element(s) to be frozen
  1389. * @since 1.0
  1390. * @access public
  1391. * @throws HTML_QuickForm_Error
  1392. */
  1393. public function freeze($elementList = null, $setTemplateFrozen = '')
  1394. {
  1395. if (!isset($elementList)) {
  1396. $this->_freezeAll = true;
  1397. $elementList = array();
  1398. } else {
  1399. if (!is_array($elementList)) {
  1400. $elementList = preg_split('/[ ]*,[ ]*/', $elementList);
  1401. }
  1402. $elementList = array_flip($elementList);
  1403. }
  1404. foreach (array_keys($this->_elements) as $key) {
  1405. $name = $this->_elements[$key]->getName();
  1406. if ($this->_freezeAll || isset($elementList[$name])) {
  1407. if (!empty($setTemplateFrozen)) {
  1408. $this->_elements[$key]->setCustomFrozenTemplate($setTemplateFrozen);
  1409. }
  1410. $this->_elements[$key]->freeze();
  1411. unset($elementList[$name]);
  1412. }
  1413. }
  1414. if (!empty($elementList)) {
  1415. throw new \Exception("Nonexistant element(s): '" . implode("', '", array_keys($elementList)) . "' in HTML_QuickForm::freeze()");
  1416. }
  1417. return true;
  1418. }
  1419. /**
  1420. * Returns whether or not the whole form is frozen
  1421. *
  1422. * @since 3.0
  1423. * @access public
  1424. * @return boolean
  1425. */
  1426. public function isFrozen()
  1427. {
  1428. return $this->_freezeAll;
  1429. }
  1430. /**
  1431. * Performs the form data processing
  1432. *
  1433. * @param mixed $callback Callback, either function name or array(&$object, 'method')
  1434. * @param bool $mergeFiles Whether uploaded files should be processed too
  1435. * @since 1.0
  1436. * @access public
  1437. * @throws HTML_QuickForm_Error
  1438. * @return mixed Whatever value the $callback function returns
  1439. */
  1440. public function process($callback, $mergeFiles = true)
  1441. {
  1442. if (!is_callable($callback)) {
  1443. throw new \Exception("Callback function does not exist in QuickForm::process()");
  1444. }
  1445. $values = ($mergeFiles === true) ? HTML_QuickForm::arrayMerge($this->_submitValues, $this->_submitFiles) : $this->_submitValues;
  1446. return call_user_func($callback, $values);
  1447. }
  1448. /**
  1449. * Accepts a renderer
  1450. *
  1451. * @param object An HTML_QuickForm_Renderer object
  1452. * @since 3.0
  1453. * @access public
  1454. * @return void
  1455. */
  1456. public function accept(&$renderer)
  1457. {
  1458. $renderer->startForm($this);
  1459. foreach (array_keys($this->_elements) as $key) {
  1460. $element =& $this->_elements[$key];
  1461. $elementName = $element->getName();
  1462. $required = ($this->isElementRequired($elementName) && !$element->isFrozen());
  1463. $error = $this->getElementError($elementName);
  1464. $element->accept($renderer, $required, $error);
  1465. }
  1466. $renderer->finishForm($this);
  1467. }
  1468. /**
  1469. * Returns a reference to default renderer object
  1470. *
  1471. * @access public
  1472. * @since 3.0
  1473. * @return HTML_QuickForm_Renderer_Default
  1474. */
  1475. public function &defaultRenderer()
  1476. {
  1477. if (!isset($GLOBALS['_HTML_QuickForm_default_renderer'])) {
  1478. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing a deprecation warning on PHP 5.3
  1479. //$GLOBALS['_HTML_QuickForm_default_renderer'] =& new HTML_QuickForm_Renderer_Default();
  1480. $GLOBALS['_HTML_QuickForm_default_renderer'] = new HTML_QuickForm_Renderer_Default();
  1481. }
  1482. return $GLOBALS['_HTML_QuickForm_default_renderer'];
  1483. }
  1484. /**
  1485. * Returns an HTML version of the form
  1486. *
  1487. * @param string $in_data (optional) Any extra data to insert right
  1488. * before form is rendered. Useful when using templates.
  1489. *
  1490. * @return string Html version of the form
  1491. * @since 1.0
  1492. * @access public
  1493. */
  1494. public function toHtml($in_data = null)
  1495. {
  1496. if (!is_null($in_data)) {
  1497. $this->addElement('html', $in_data);
  1498. }
  1499. $renderer =& $this->defaultRenderer();
  1500. $this->accept($renderer);
  1501. return $renderer->toHtml();
  1502. }
  1503. /**
  1504. * Returns the client side validation script
  1505. *
  1506. * @since 2.0
  1507. * @access public
  1508. * @return string Javascript to perform validation, empty string if no 'client' rules were added
  1509. */
  1510. public function getValidationScript()
  1511. {
  1512. if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) {
  1513. return '';
  1514. }
  1515. $registry =& HTML_QuickForm_RuleRegistry::singleton();
  1516. $test = array();
  1517. $js_escape = array(
  1518. "\r" => '\r',
  1519. "\n" => '\n',
  1520. "\t" => '\t',
  1521. "'" => "\\'",
  1522. '"' => '\"',
  1523. '\\' => '\\\\'
  1524. );
  1525. foreach ($this->_rules as $elementName => $rules) {
  1526. foreach ($rules as $rule) {
  1527. if ('client' == $rule['validation']) {
  1528. unset($element);
  1529. $dependent = isset($rule['dependent']) && is_array($rule['dependent']);
  1530. $rule['message'] = strtr($rule['message'], $js_escape);
  1531. if (isset($rule['group'])) {
  1532. $group =& $this->getElement($rule['group']);
  1533. // No JavaScript validation for frozen elements
  1534. if ($group->isFrozen()) {
  1535. continue 2;
  1536. }
  1537. $elements =& $group->getElements();
  1538. foreach (array_keys($elements) as $key) {
  1539. if ($elementName == $group->getElementName($key)) {
  1540. $element =& $elements[$key];
  1541. break;
  1542. }
  1543. }
  1544. } elseif ($dependent) {
  1545. $element = array();
  1546. $element[] =& $this->getElement($elementName);
  1547. foreach ($rule['dependent'] as $elName) {
  1548. $element[] =& $this->getElement($elName);
  1549. }
  1550. } else {
  1551. $element =& $this->getElement($elementName);
  1552. }
  1553. // No JavaScript validation for frozen elements
  1554. if (is_object($element) && $element->isFrozen()) {
  1555. continue 2;
  1556. } elseif (is_array($element)) {
  1557. foreach (array_keys($element) as $key) {
  1558. if ($element[$key]->isFrozen()) {
  1559. continue 3;
  1560. }
  1561. }
  1562. }
  1563. $test[] = $registry->getValidationScript($element, $elementName, $rule);
  1564. }
  1565. }
  1566. }
  1567. if (count($test) > 0) {
  1568. return
  1569. "<script>" .
  1570. "//<![CDATA[\n" .
  1571. "function validate_" . $this->_attributes['id'] . "(frm) {\n" .
  1572. " var value = '';\n" .
  1573. " var errFlag = new Array();\n" .
  1574. " var _qfGroups = {};\n" .
  1575. " _qfMsg = '';\n\n" .
  1576. join("\n", $test) .
  1577. "\n if (_qfMsg != '') {\n" .
  1578. " _qfMsg = '" . strtr($this->_jsPrefix, $js_escape) . "' + _qfMsg;\n" .
  1579. " _qfMsg = _qfMsg + '\\n" . strtr($this->_jsPostfix, $js_escape) . "';\n" .
  1580. " alert(_qfMsg);\n" .
  1581. " return false;\n" .
  1582. " }\n" .
  1583. " return true;\n" .
  1584. "}\n" .
  1585. "//]]>\n" .
  1586. "</script>";
  1587. }
  1588. return '';
  1589. }
  1590. /**
  1591. * Returns the values submitted by the form
  1592. *
  1593. * @since 2.0
  1594. * @access public
  1595. * @param bool Whether uploaded files should be returned too
  1596. * @return array
  1597. */
  1598. public function getSubmitValues($mergeFiles = false)
  1599. {
  1600. return $mergeFiles ? HTML_QuickForm::arrayMerge($this->_submitValues, $this->_submitFiles): $this->_submitValues;
  1601. }
  1602. /**
  1603. * Returns the form's contents in an array.
  1604. *
  1605. * The description of the array structure is in HTML_QuickForm_Renderer_Array docs
  1606. *
  1607. * @since 2.0
  1608. * @access public
  1609. * @param bool Whether to collect hidden elements (passed to the Renderer's constructor)
  1610. * @return array of form contents
  1611. */
  1612. public function toArray($collectHidden = false)
  1613. {
  1614. include_once 'HTML/QuickForm/Renderer/Array.php';
  1615. // Modified by Ivan Tcholakov, 16-MAR-2010. Suppressing a deprecation warning on PHP 5.3
  1616. //$renderer =& new HTML_QuickForm_Renderer_Array($collectHidden);
  1617. $renderer = new HTML_QuickForm_Renderer_Array($collectHidden);
  1618. //
  1619. $this->accept($renderer);
  1620. return $renderer->toArray();
  1621. }
  1622. /**
  1623. * Returns a 'safe' element's value
  1624. *
  1625. * This method first tries to find a cleaned-up submitted value,
  1626. * it will return a value set by setValue()/setDefaults()/setConstants()
  1627. * if submitted value does not exist for the given element.
  1628. *
  1629. * @param string Name of an element
  1630. * @access public
  1631. * @return mixed
  1632. * @throws HTML_QuickForm_Error
  1633. */
  1634. public function exportValue($element)
  1635. {
  1636. if (!isset($this->_elementIndex[$element])) {
  1637. throw new \Exception("Element '$element' does not exist in HTML_QuickForm::getElementValue()");
  1638. }
  1639. $value = $this->_elements[$this->_elementIndex[$element]]->exportValue($this->_submitValues, false);
  1640. if (isset($this->_duplicateIndex[$element])) {
  1641. foreach ($this->_duplicateIndex[$element] as $index) {
  1642. if (null !== ($v = $this->_elements[$index]->exportValue($this->_submitValues, false))) {
  1643. if (is_array($value)) {
  1644. $value[] = $v;
  1645. } else {
  1646. $value = (null === $value)? $v: array($value, $v);
  1647. }
  1648. }
  1649. }
  1650. }
  1651. return $value;
  1652. }
  1653. /**
  1654. * Returns 'safe' elements' values
  1655. *
  1656. * Unlike getSubmitValues(), this will return only the values
  1657. * corresponding to the elements present in the form.
  1658. *
  1659. * @param mixed Array/string of element names, whose values we want. If not set then return all elements.
  1660. * @access public
  1661. * @return array An assoc array of elements' values
  1662. * @throws HTML_QuickForm_Error
  1663. */
  1664. public function exportValues($elementList = null)
  1665. {
  1666. $values = array();
  1667. if (null === $elementList) {
  1668. // iterate over all elements, calling their exportValue() methods
  1669. foreach (array_keys($this->_elements) as $key) {
  1670. $value = $this->_elements[$key]->exportValue($this->_submitValues, true);
  1671. if (is_array($value)) {
  1672. // This shit throws a bogus warning in PHP 4.3.x
  1673. $values = HTML_QuickForm::arrayMerge($values, $value);
  1674. }
  1675. }
  1676. } else {
  1677. if (!is_array($elementList)) {
  1678. $elementList = array_map('trim', explode(',', $elementList));
  1679. }
  1680. foreach ($elementList as $elementName) {
  1681. $value = $this->exportValue($elementName);
  1682. if (PEAR::isError($value)) {
  1683. return $value;
  1684. }
  1685. $values[$elementName] = $value;
  1686. }
  1687. }
  1688. return $values;
  1689. }
  1690. /**
  1691. * Tells whether the form was already submitted
  1692. *
  1693. * This is useful since the _submitFiles and _submitValues arrays
  1694. * may be completely empty after the trackSubmit value is removed.
  1695. *
  1696. * @access public
  1697. * @return bool
  1698. */
  1699. public function isSubmitted()
  1700. {
  1701. return $this->_flagSubmitted;
  1702. }
  1703. /**
  1704. * Tell whether a result from a QuickForm method is an error (an instance of HTML_QuickForm_Error)
  1705. *
  1706. * @access public
  1707. * @param mixed result code
  1708. * @return bool whether $value is an error
  1709. * @static
  1710. */
  1711. public function isError($value)
  1712. {
  1713. return (is_object($value) && is_a($value, 'html_quickform_error'));
  1714. }
  1715. /**
  1716. * Return a textual error message for an QuickForm error code
  1717. *
  1718. * @access public
  1719. * @param int error code
  1720. * @return string error message
  1721. * @static
  1722. */
  1723. public static function errorMessage($value)
  1724. {
  1725. // make the variable static so that it only has to do the defining on the first call
  1726. static $errorMessages;
  1727. // define the varies error messages
  1728. if (!isset($errorMessages)) {
  1729. $errorMessages = array(
  1730. QUICKFORM_OK => 'no error',
  1731. QUICKFORM_ERROR => 'unknown error',
  1732. QUICKFORM_INVALID_RULE => 'the rule does not exist as a registered rule',
  1733. QUICKFORM_NONEXIST_ELEMENT => 'nonexistent html element',
  1734. QUICKFORM_INVALID_FILTER => 'invalid filter',
  1735. QUICKFORM_UNREGISTERED_ELEMENT => 'unregistered element',
  1736. QUICKFORM_INVALID_ELEMENT_NAME => 'element already exists',
  1737. QUICKFORM_INVALID_PROCESS => 'process callback does not exist',
  1738. QUICKFORM_DEPRECATED => 'method is deprecated',
  1739. QUICKFORM_INVALID_DATASOURCE => 'datasource is not an object',
  1740. );
  1741. }
  1742. // If this is an error object, then grab the corresponding error code
  1743. if (HTML_QuickForm::isError($value)) {
  1744. $value = $value->getCode();
  1745. }
  1746. // return the textual error message corresponding to the code
  1747. return isset($errorMessages[$value]) ? $errorMessages[$value] : $errorMessages[QUICKFORM_ERROR];
  1748. }
  1749. /**
  1750. * @param HTML_QuickForm_element $element
  1751. */
  1752. public function setRequired(HTML_QuickForm_element $element)
  1753. {
  1754. $this->addRule(
  1755. $element->getName(),
  1756. get_lang('ThisFieldIsRequired'),
  1757. 'required'
  1758. );
  1759. }
  1760. }
  1761. /**
  1762. * Class for errors thrown by HTML_QuickForm package
  1763. *
  1764. * @category HTML
  1765. * @package HTML_QuickForm
  1766. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  1767. * @author Bertrand Mansion <bmansion@mamasam.com>
  1768. * @version Release: 3.2.11
  1769. */
  1770. class HTML_QuickForm_Error extends PEAR_Error
  1771. {
  1772. /**
  1773. * Prefix for all error messages
  1774. * @var string
  1775. */
  1776. public $error_message_prefix = 'QuickForm Error: ';
  1777. /**
  1778. * Creates a quickform error object, extending the PEAR_Error class
  1779. *
  1780. * @param int $code the error code
  1781. * @param int $mode the reaction to the error, either return, die or trigger/callback
  1782. * @param int $level intensity of the error (PHP error code)
  1783. * @param mixed $debuginfo any information that can inform user as to nature of the error
  1784. */
  1785. public function __construct(
  1786. $code = QUICKFORM_ERROR,
  1787. $mode = PEAR_ERROR_RETURN,
  1788. $level = E_USER_NOTICE,
  1789. $debuginfo = null
  1790. ) {
  1791. if (is_int($code)) {
  1792. parent::__construct(HTML_QuickForm::errorMessage($code), $code, $mode, $level, $debuginfo);
  1793. } else {
  1794. parent::__construct("Invalid error code: $code", QUICKFORM_ERROR, $mode, $level, $debuginfo);
  1795. }
  1796. }
  1797. }