QuickForm.php 69 KB

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