forms.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 htmlspecialchars().
  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 &copy; 2004-2007 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 htmlspecialchars()... 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] = htmlspecialchars($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 array $aAttribs (since 1.5.1) extra attributes
  73. * @return string html formated password field
  74. */
  75. function addPwField($sName, $sValue = null, $aAttribs=array()) {
  76. $aAttribs['name'] = $sName;
  77. $aAttribs['value'] = (! is_null($sValue) ? $sValue : '');
  78. // add default css
  79. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmpwfield';
  80. return addInputField('password',$aAttribs);
  81. }
  82. /**
  83. * Form checkbox
  84. * @param string $sName field name
  85. * @param boolean $bChecked controls if field is checked
  86. * @param string $sValue
  87. * @param array $aAttribs (since 1.5.1) extra attributes
  88. * @return string html formated checkbox field
  89. */
  90. function addCheckBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  91. $aAttribs['name'] = $sName;
  92. if ($bChecked) $aAttribs['checked'] = 'checked';
  93. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  94. // add default css
  95. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmcheckbox';
  96. return addInputField('checkbox',$aAttribs);
  97. }
  98. /**
  99. * Form radio box
  100. * @param string $sName field name
  101. * @param boolean $bChecked controls if field is selected
  102. * @param string $sValue
  103. * @param array $aAttribs (since 1.5.1) extra attributes.
  104. * @return string html formated radio box
  105. */
  106. function addRadioBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  107. $aAttribs['name'] = $sName;
  108. if ($bChecked) $aAttribs['checked'] = 'checked';
  109. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  110. if (! isset($aAttribs['id'])) $aAttribs['id'] = $sName . $sValue;
  111. // add default css
  112. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmradiobox';
  113. return addInputField('radio', $aAttribs);
  114. }
  115. /**
  116. * A hidden form field.
  117. * @param string $sName field name
  118. * @param string $sValue field value
  119. * @param array $aAttribs (since 1.5.1) extra attributes
  120. * @return html formated hidden form field
  121. */
  122. function addHidden($sName, $sValue, $aAttribs=array()) {
  123. $aAttribs['name'] = $sName;
  124. $aAttribs['value'] = $sValue;
  125. // add default css
  126. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmhiddenfield';
  127. return addInputField('hidden', $aAttribs);
  128. }
  129. /**
  130. * An input textbox.
  131. * @param string $sName field name
  132. * @param string $sValue initial field value
  133. * @param integer $iSize field size (number of characters)
  134. * @param integer $iMaxlength maximum number of characters the user may enter
  135. * @param array $aAttribs (since 1.5.1) extra attributes - should be given
  136. * in the form array('attribute_name' => 'attribute_value', ...)
  137. * @return string html formated text input field
  138. */
  139. function addInput($sName, $sValue = '', $iSize = 0, $iMaxlength = 0, $aAttribs=array()) {
  140. $aAttribs['name'] = $sName;
  141. $aAttribs['value'] = $sValue;
  142. if ($iSize) $aAttribs['size'] = (int)$iSize;
  143. if ($iMaxlength) $aAttribs['maxlength'] = (int)$iMaxlength;
  144. // add default css
  145. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextfield';
  146. return addInputField('text', $aAttribs);
  147. }
  148. /**
  149. * Function to create a selectlist from an array.
  150. * @param string $sName field name
  151. * @param array $aValues field values array(key => value) -> <option value="key">value</option>, although if $bUsekeys is FALSE, then <option value="value">value</option>
  152. * @param mixed $default the key that will be selected
  153. * @param boolean $bUsekeys use the keys of the array as option value or not
  154. * @param array $aAttribs (since 1.5.1) extra attributes
  155. *
  156. * @return string html formated selection box
  157. * @todo add attributes argument for option tags and default css
  158. */
  159. function addSelect($sName, $aValues, $default = null, $bUsekeys = false, $aAttribs = array()) {
  160. // only one element
  161. if(count($aValues) == 1) {
  162. $k = key($aValues); $v = array_pop($aValues);
  163. return addHidden($sName, ($bUsekeys ? $k:$v), $aAttribs).
  164. htmlspecialchars($v) . "\n";
  165. }
  166. global $oTemplate;
  167. //FIXME: all the values in the $aAttribs list and $sName and both the keys and values in $aValues used to go thru htmlspecialchars()... 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] = htmlspecialchars($value); $sName = htmlspecialchars($sName); $aNewValues = array(); foreach ($aValues as $key => $value) $aNewValues[htmlspecialchars($key)] = htmlspecialchars($value); $aValues = $aNewValues; And probably this too because it has to be matched to a value that has already been sanitized: $default = htmlspecialchars($default);
  168. $oTemplate->assign('aAttribs', $aAttribs);
  169. $oTemplate->assign('aValues', $aValues);
  170. $oTemplate->assign('bUsekeys', $bUsekeys);
  171. $oTemplate->assign('default', $default);
  172. $oTemplate->assign('name', $sName);
  173. return $oTemplate->fetch('select.tpl');
  174. }
  175. /**
  176. * Normal button
  177. *
  178. * Note the switched value/name parameters!
  179. * Note also that regular buttons are not very useful unless
  180. * used with onclick handlers, thus are only really appropriate
  181. * if you use them after having checked if JavaScript is turned
  182. * on by doing this: if (checkForJavascript()) ...
  183. *
  184. * @param string $sValue button name
  185. * @param string $sName key name
  186. * @param array $aAttribs extra attributes
  187. *
  188. * @return string html formated submit input field
  189. *
  190. * @since 1.5.2
  191. */
  192. function addButton($sValue, $sName = null, $aAttribs=array()) {
  193. $aAttribs['value'] = $sValue;
  194. if (! is_null($sName)) $aAttribs['name'] = $sName;
  195. // add default css
  196. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmsubmitfield';
  197. return addInputField('button', $aAttribs);
  198. }
  199. /**
  200. * Form submission button
  201. * Note the switched value/name parameters!
  202. * @param string $sValue button name
  203. * @param string $sName submitted key name
  204. * @param array $aAttribs (since 1.5.1) extra attributes
  205. * @return string html formated submit input field
  206. */
  207. function addSubmit($sValue, $sName = null, $aAttribs=array()) {
  208. $aAttribs['value'] = $sValue;
  209. if (! is_null($sName)) $aAttribs['name'] = $sName;
  210. // add default css
  211. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmsubmitfield';
  212. return addInputField('submit', $aAttribs);
  213. }
  214. /**
  215. * Form reset button
  216. * @param string $sValue button name
  217. * @param array $aAttribs (since 1.5.1) extra attributes
  218. * @return string html formated reset input field
  219. */
  220. function addReset($sValue, $aAttribs=array()) {
  221. $aAttribs['value'] = $sValue;
  222. // add default css
  223. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmresetfield';
  224. return addInputField('reset', $aAttribs);
  225. }
  226. /**
  227. * Textarea form element.
  228. *
  229. * @param string $sName field name
  230. * @param string $sText initial field value (OPTIONAL; default empty)
  231. * @param integer $iCols field width (number of chars) (OPTIONAL; default 40)
  232. * @param integer $iRows field height (number of character rows) (OPTIONAL; default 10)
  233. * @param array $aAttribs (since 1.5.1) extra attributes (OPTIONAL; default empty)
  234. *
  235. * @return string html formated text area field
  236. *
  237. */
  238. function addTextArea($sName, $sText = '', $iCols = 40, $iRows = 10, $aAttribs = array()) {
  239. // no longer accept string arguments for attribs; print
  240. // backtrace to help people fix their code
  241. //FIXME: throw error instead?
  242. if (!is_array($aAttribs)) {
  243. echo '$aAttribs argument to addTextArea() must be an array<br /><pre>';
  244. debug_print_backtrace();
  245. echo '</pre><br />';
  246. exit;
  247. }
  248. // add default css
  249. else if (!isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextarea';
  250. global $oTemplate;
  251. //FIXME: all the values in the $aAttribs list as well as $sName and $sText used to go thru htmlspecialchars()... 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] = htmlspecialchars($value); $sName = htmlspecialchars($sName); $sText = htmlspecialchars($sText);
  252. $oTemplate->assign('aAttribs', $aAttribs);
  253. $oTemplate->assign('name', $sName);
  254. $oTemplate->assign('text', $sText);
  255. $oTemplate->assign('cols', (int)$iCols);
  256. $oTemplate->assign('rows', (int)$iRows);
  257. return $oTemplate->fetch('textarea.tpl');
  258. }
  259. /**
  260. * Make a <form> start-tag.
  261. *
  262. * @param string $sAction form handler URL
  263. * @param string $sMethod http method used to submit form data. 'get' or 'post'
  264. * @param string $sName form name used for identification (used for backward
  265. * compatibility). Use of id is recommended instead.
  266. * @param string $sEnctype content type that is used to submit data. html 4.01
  267. * defaults to 'application/x-www-form-urlencoded'. Form
  268. * with file field needs 'multipart/form-data' encoding type.
  269. * @param string $sCharset charset that is used for submitted data
  270. * @param array $aAttribs (since 1.5.1) extra attributes
  271. *
  272. * @return string html formated form start string
  273. *
  274. */
  275. function addForm($sAction, $sMethod = 'post', $sName = '', $sEnctype = '', $sCharset = '', $aAttribs = array()) {
  276. global $oTemplate;
  277. //FIXME: all the values in the $aAttribs list as well as $charset used to go thru htmlspecialchars()... 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] = htmlspecialchars($value); $sCharset = htmlspecialchars($sCharset);
  278. $oTemplate->assign('aAttribs', $aAttribs);
  279. $oTemplate->assign('name', $sName);
  280. $oTemplate->assign('method', $sMethod);
  281. $oTemplate->assign('action', $sAction);
  282. $oTemplate->assign('enctype', $sEnctype);
  283. $oTemplate->assign('charset', $sCharset);
  284. return $oTemplate->fetch('form.tpl');
  285. }