forms.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * forms.php - html form functions
  4. *
  5. * Functions to build forms in a safe and consistent manner.
  6. * All attribute values are sanitized with sm_encode_html_special_chars().
  7. //FIXME: I think the Template class might be better place to sanitize inside assign() method
  8. *
  9. * Currently functions don't provide simple wrappers for file and
  10. * image input fields, support only submit and reset buttons and use
  11. * html input tags for buttons.
  12. *
  13. * Since 1.5.1:
  14. *
  15. * * all form functions should support id tags. Original
  16. * idea by dugan <at> passwall.com. Tags can be used for Section 508
  17. * or WAI compliance.
  18. *
  19. * * input tag functions accept extra html attributes that can be submitted
  20. * in $aAttribs array.
  21. *
  22. * * default css class attributes are added.
  23. *
  24. * @link http://www.section508.gov/ Section 508
  25. * @link http://www.w3.org/WAI/ Web Accessibility Initiative (WAI)
  26. * @link http://www.w3.org/TR/html4/ W3.org HTML 4.01 form specs
  27. * @copyright 2004-2017 The SquirrelMail Project Team
  28. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  29. * @version $Id$
  30. * @package squirrelmail
  31. * @subpackage forms
  32. * @since 1.4.3 and 1.5.1
  33. */
  34. /**
  35. * Helper function to create form fields, not to be called directly,
  36. * only by other functions below.
  37. *
  38. * Function used different syntax before 1.5.1
  39. * @param string $sType type of input field. Possible values (html 4.01
  40. * specs.): text, password, checkbox, radio, submit, reset, file,
  41. * hidden, image, button.
  42. * @param array $aAttribs (since 1.5.1) extra attributes. Array key is
  43. * attribute name, array value is attribute value. Array keys must use
  44. * lowercase.
  45. * @return string html formated input field
  46. * @deprecated use other functions that provide simple wrappers to this function
  47. */
  48. function addInputField($sType, $aAttribs=array()) {
  49. $sAttribs = '';
  50. // define unique identifier
  51. if (! isset($aAttribs['id']) && isset($aAttribs['name']) && ! is_null($aAttribs['name'])) {
  52. /**
  53. * if 'id' is not set, set it to 'name' and replace brackets
  54. * with underscores. 'name' might contain field name with squire
  55. * brackets (array). Brackets are not allowed in id (validator.w3.org
  56. * fails to validate document). According to html 4.01 manual cdata
  57. * type description, 'name' attribute uses same type, but validator.w3.org
  58. * does not barf on brackets in 'name' attributes.
  59. */
  60. $aAttribs['id'] = strtr($aAttribs['name'],'[]','__');
  61. }
  62. global $oTemplate;
  63. $oTemplate->assign('type', $sType);
  64. //FIXME: all the values in the $aAttribs list used to go thru sm_encode_html_special_chars()... I would propose that most everything that is assigned to the template should go thru that *in the template class* on its way between here and the actual template file. Otherwise we have to do something like: foreach ($aAttribs as $key => $value) $aAttribs[$key] = sm_encode_html_special_chars($value);
  65. $oTemplate->assign('aAttribs', $aAttribs);
  66. return $oTemplate->fetch('input.tpl');
  67. }
  68. /**
  69. * Password input field
  70. * @param string $sName field name
  71. * @param string $sValue initial password value
  72. * @param integer $iSize field size (number of characters)
  73. * @param integer $iMaxlength maximum number of characters the user may enter
  74. * @param array $aAttribs (since 1.5.1) extra attributes - should be given
  75. * in the form array('attribute_name' => 'attribute_value', ...)
  76. * @return string html formated password field
  77. */
  78. function addPwField($sName, $sValue = '', $iSize = 0, $iMaxlength = 0, $aAttribs=array()) {
  79. $aAttribs['name'] = $sName;
  80. $aAttribs['value'] = $sValue;
  81. if ($iSize) $aAttribs['size'] = (int)$iSize;
  82. if ($iMaxlength) $aAttribs['maxlength'] = (int)$iMaxlength;
  83. // add default css
  84. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmpwfield';
  85. return addInputField('password',$aAttribs);
  86. }
  87. /**
  88. * Form checkbox
  89. * @param string $sName field name
  90. * @param boolean $bChecked controls if field is checked
  91. * @param string $sValue
  92. * @param array $aAttribs (since 1.5.1) extra attributes
  93. * @return string html formated checkbox field
  94. */
  95. function addCheckBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  96. $aAttribs['name'] = $sName;
  97. if ($bChecked) $aAttribs['checked'] = 'checked';
  98. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  99. // add default css
  100. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmcheckbox';
  101. return addInputField('checkbox',$aAttribs);
  102. }
  103. /**
  104. * Form radio box
  105. * @param string $sName field name
  106. * @param boolean $bChecked controls if field is selected
  107. * @param string $sValue
  108. * @param array $aAttribs (since 1.5.1) extra attributes.
  109. * @return string html formated radio box
  110. */
  111. function addRadioBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  112. $aAttribs['name'] = $sName;
  113. if ($bChecked) $aAttribs['checked'] = 'checked';
  114. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  115. if (! isset($aAttribs['id'])) $aAttribs['id'] = $sName . $sValue;
  116. // add default css
  117. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmradiobox';
  118. return addInputField('radio', $aAttribs);
  119. }
  120. /**
  121. * A hidden form field.
  122. * @param string $sName field name
  123. * @param string $sValue field value
  124. * @param array $aAttribs (since 1.5.1) extra attributes
  125. * @return html formated hidden form field
  126. */
  127. function addHidden($sName, $sValue, $aAttribs=array()) {
  128. $aAttribs['name'] = $sName;
  129. $aAttribs['value'] = $sValue;
  130. // add default css
  131. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmhiddenfield';
  132. return addInputField('hidden', $aAttribs);
  133. }
  134. /**
  135. * An input textbox.
  136. * @param string $sName field name
  137. * @param string $sValue initial field value
  138. * @param integer $iSize field size (number of characters)
  139. * @param integer $iMaxlength maximum number of characters the user may enter
  140. * @param array $aAttribs (since 1.5.1) extra attributes - should be given
  141. * in the form array('attribute_name' => 'attribute_value', ...)
  142. * @return string html formated text input field
  143. */
  144. function addInput($sName, $sValue = '', $iSize = 0, $iMaxlength = 0, $aAttribs=array()) {
  145. $aAttribs['name'] = $sName;
  146. $aAttribs['value'] = $sValue;
  147. if ($iSize) $aAttribs['size'] = (int)$iSize;
  148. if ($iMaxlength) $aAttribs['maxlength'] = (int)$iMaxlength;
  149. // add default css
  150. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextfield';
  151. return addInputField('text', $aAttribs);
  152. }
  153. /**
  154. * Function to create a selectlist from an array.
  155. * @param string $sName Field name
  156. * @param array $aValues Field values array(key => value) results in:
  157. * <option value="key">value</option>,
  158. * although if $bUsekeys is FALSE, then it changes to:
  159. * <option value="value">value</option>
  160. * @param mixed $default The key(s) that will be selected (it is OK to pass
  161. * in an array here in the case of multiple select lists)
  162. * @param boolean $bUsekeys Use the keys of the array as option value or not
  163. * @param array $aAttribs (since 1.5.1) Extra attributes
  164. * @param boolean $bMultiple When TRUE, a multiple select list will be shown
  165. * (OPTIONAL; default is FALSE (single select list))
  166. * @param int $iSize Desired height of multiple select boxes
  167. * (OPTIONAL; default is SMOPT_SIZE_NORMAL)
  168. * (only applicable when $bMultiple is TRUE)
  169. *
  170. * @return string html formated selection box
  171. * @todo add attributes argument for option tags and default css
  172. */
  173. function addSelect($sName, $aValues, $default = null, $bUsekeys = false, $aAttribs = array(), $bMultiple = FALSE, $iSize = SMOPT_SIZE_NORMAL) {
  174. // only one element
  175. if (!$bMultiple && count($aValues) == 1) {
  176. $k = key($aValues); $v = array_pop($aValues);
  177. return addHidden($sName, ($bUsekeys ? $k : $v), $aAttribs)
  178. . sm_encode_html_special_chars($v);
  179. }
  180. if (! isset($aAttribs['id'])) $aAttribs['id'] = $sName;
  181. // make sure $default is an array, since multiple select lists
  182. // need the chance to have more than one default...
  183. //
  184. if (!is_array($default))
  185. $default = array($default);
  186. global $oTemplate;
  187. //FIXME: all the values in the $aAttribs list and $sName and both the keys and values in $aValues used to go thru sm_encode_html_special_chars()... I would propose that most everything that is assigned to the template should go thru that *in the template class* on its way between here and the actual template file. Otherwise we have to do something like: foreach ($aAttribs as $key => $value) $aAttribs[$key] = sm_encode_html_special_chars($value); $sName = sm_encode_html_special_chars($sName); $aNewValues = array(); foreach ($aValues as $key => $value) $aNewValues[sm_encode_html_special_chars($key)] = sm_encode_html_special_chars($value); $aValues = $aNewValues; And probably this too because it has to be matched to a value that has already been sanitized: $default = sm_encode_html_special_chars($default); (oops, watch out for when $default is an array! (multiple select lists))
  188. $oTemplate->assign('aAttribs', $aAttribs);
  189. $oTemplate->assign('aValues', $aValues);
  190. $oTemplate->assign('bUsekeys', $bUsekeys);
  191. $oTemplate->assign('default', $default);
  192. $oTemplate->assign('name', $sName);
  193. $oTemplate->assign('multiple', $bMultiple);
  194. $oTemplate->assign('size', $iSize);
  195. return $oTemplate->fetch('select.tpl');
  196. }
  197. /**
  198. * Normal button
  199. *
  200. * Note the switched value/name parameters!
  201. * Note also that regular buttons are not very useful unless
  202. * used with onclick handlers, thus are only really appropriate
  203. * if you use them after having checked if JavaScript is turned
  204. * on by doing this: if (checkForJavascript()) ...
  205. *
  206. * @param string $sValue button name
  207. * @param string $sName key name
  208. * @param array $aAttribs extra attributes
  209. *
  210. * @return string html formated submit input field
  211. *
  212. * @since 1.5.2
  213. */
  214. function addButton($sValue, $sName = null, $aAttribs=array()) {
  215. $aAttribs['value'] = $sValue;
  216. if (! is_null($sName)) $aAttribs['name'] = $sName;
  217. // add default css
  218. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmsubmitfield';
  219. return addInputField('button', $aAttribs);
  220. }
  221. /**
  222. * Form submission button
  223. * Note the switched value/name parameters!
  224. * @param string $sValue button name
  225. * @param string $sName submitted key name
  226. * @param array $aAttribs (since 1.5.1) extra attributes
  227. * @return string html formated submit input field
  228. */
  229. function addSubmit($sValue, $sName = null, $aAttribs=array()) {
  230. $aAttribs['value'] = $sValue;
  231. if (! is_null($sName)) $aAttribs['name'] = $sName;
  232. // add default css
  233. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmsubmitfield';
  234. return addInputField('submit', $aAttribs);
  235. }
  236. /**
  237. * Form reset button
  238. * @param string $sValue button name
  239. * @param array $aAttribs (since 1.5.1) extra attributes
  240. * @return string html formated reset input field
  241. */
  242. function addReset($sValue, $aAttribs=array()) {
  243. $aAttribs['value'] = $sValue;
  244. // add default css
  245. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmresetfield';
  246. return addInputField('reset', $aAttribs);
  247. }
  248. /**
  249. * Textarea form element.
  250. *
  251. * @param string $sName field name
  252. * @param string $sText initial field value (OPTIONAL; default empty)
  253. * @param integer $iCols field width (number of chars) (OPTIONAL; default 40)
  254. * @param integer $iRows field height (number of character rows) (OPTIONAL; default 10)
  255. * @param array $aAttribs (since 1.5.1) extra attributes (OPTIONAL; default empty)
  256. *
  257. * @return string html formated text area field
  258. *
  259. */
  260. function addTextArea($sName, $sText = '', $iCols = 40, $iRows = 10, $aAttribs = array()) {
  261. // no longer accept string arguments for attribs; print
  262. // backtrace to help people fix their code
  263. //FIXME: throw error instead?
  264. if (!is_array($aAttribs)) {
  265. echo '$aAttribs argument to addTextArea() must be an array<br /><pre>';
  266. debug_print_backtrace();
  267. echo '</pre><br />';
  268. exit;
  269. }
  270. // add default css
  271. else if (!isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextarea';
  272. if ( empty( $aAttribs['id'] ) ) {
  273. $aAttribs['id'] = strtr($sName,'[]','__');
  274. }
  275. global $oTemplate;
  276. //FIXME: all the values in the $aAttribs list as well as $sName and $sText used to go thru sm_encode_html_special_chars()... I would propose that most everything that is assigned to the template should go thru that *in the template class* on its way between here and the actual template file. Otherwise we have to do something like: foreach ($aAttribs as $key => $value) $aAttribs[$key] = sm_encode_html_special_chars($value); $sName = sm_encode_html_special_chars($sName); $sText = sm_encode_html_special_chars($sText);
  277. $oTemplate->assign('aAttribs', $aAttribs);
  278. $oTemplate->assign('name', $sName);
  279. $oTemplate->assign('text', $sText);
  280. $oTemplate->assign('cols', (int)$iCols);
  281. $oTemplate->assign('rows', (int)$iRows);
  282. return $oTemplate->fetch('textarea.tpl');
  283. }
  284. /**
  285. * Make a <form> start-tag.
  286. *
  287. * @param string $sAction form handler URL
  288. * @param string $sMethod http method used to submit form data. 'get' or 'post'
  289. * @param string $sName form name used for identification (used for backward
  290. * compatibility). Use of id is recommended instead.
  291. * @param string $sEnctype content type that is used to submit data. html 4.01
  292. * defaults to 'application/x-www-form-urlencoded'. Form
  293. * with file field needs 'multipart/form-data' encoding type.
  294. * @param string $sCharset charset that is used for submitted data
  295. * @param array $aAttribs (since 1.5.1) extra attributes
  296. * @param boolean $bAddToken (since 1.5.2) When given as a string or as boolean TRUE,
  297. * a hidden input is also added to the form containing a
  298. * security token. When given as TRUE, the input name is
  299. * "smtoken"; otherwise the name is the string that is
  300. * given for this parameter. When FALSE, no hidden token
  301. * input field is added. (OPTIONAL; default not used)
  302. *
  303. * @return string html formated form start string
  304. *
  305. */
  306. function addForm($sAction, $sMethod = 'post', $sName = '', $sEnctype = '', $sCharset = '', $aAttribs = array(), $bAddToken = FALSE) {
  307. global $oTemplate;
  308. //FIXME: all the values in the $aAttribs list as well as $charset used to go thru sm_encode_html_special_chars()... I would propose that most everything that is assigned to the template should go thru that *in the template class* on its way between here and the actual template file. Otherwise we have to do something like: foreach ($aAttribs as $key => $value) $aAttribs[$key] = sm_encode_html_special_chars($value); $sCharset = sm_encode_html_special_chars($sCharset);
  309. $oTemplate->assign('aAttribs', $aAttribs);
  310. $oTemplate->assign('name', $sName);
  311. $oTemplate->assign('method', $sMethod);
  312. $oTemplate->assign('action', $sAction);
  313. $oTemplate->assign('enctype', $sEnctype);
  314. $oTemplate->assign('charset', $sCharset);
  315. $sForm = $oTemplate->fetch('form.tpl');
  316. if ($bAddToken) {
  317. $sForm .= addHidden((is_string($bAddToken) ? $bAddToken : 'smtoken'),
  318. sm_generate_security_token());
  319. }
  320. return $sForm;
  321. }
  322. /**
  323. * Creates unique widget names
  324. *
  325. * Names are formatted as such: "send1", "send2", "send3", etc.,
  326. * where "send" in this example is what was given for $base_name
  327. *
  328. * @param string $base_name The name upon which to base the
  329. * returned widget name.
  330. * @param boolean $return_count When TRUE, this function will
  331. * return the last number used to
  332. * create a widget name for $base_name
  333. * (OPTIONAL; default = FALSE).
  334. *
  335. * @return mixed When $return_output is FALSE, a string containing
  336. * the unique widget name; otherwise an integer with
  337. * the last number used to create the last widget
  338. * name for the given $base_name (where 0 (zero) means
  339. * that no such widgets have been created yet).
  340. *
  341. * @since 1.5.2
  342. *
  343. */
  344. function unique_widget_name($base_name, $return_count=FALSE)
  345. {
  346. static $counts = array();
  347. if (!isset($counts[$base_name]))
  348. $counts[$base_name] = 0;
  349. if ($return_count)
  350. return $counts[$base_name];
  351. ++$counts[$base_name];
  352. return $base_name . $counts[$base_name];
  353. }