forms.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. * @return string html formated selection box
  156. * @todo add attributes argument for option tags and default css
  157. */
  158. function addSelect($sName, $aValues, $default = null, $bUsekeys = false, $aAttribs = array()) {
  159. // only one element
  160. if(count($aValues) == 1) {
  161. $k = key($aValues); $v = array_pop($aValues);
  162. return addHidden($sName, ($bUsekeys ? $k:$v), $aAttribs).
  163. htmlspecialchars($v) . "\n";
  164. }
  165. global $oTemplate;
  166. //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);
  167. $oTemplate->assign('aAttribs', $aAttribs);
  168. $oTemplate->assign('aValues', $aValues);
  169. $oTemplate->assign('bUsekeys', $bUsekeys);
  170. $oTemplate->assign('default', $default);
  171. $oTemplate->assign('name', $sName);
  172. return $oTemplate->fetch('select.tpl');
  173. }
  174. /**
  175. * Form submission button
  176. * Note the switched value/name parameters!
  177. * @param string $sValue button name
  178. * @param string $sName submitted key name
  179. * @param array $aAttribs (since 1.5.1) extra attributes
  180. * @return string html formated submit input field
  181. */
  182. function addSubmit($sValue, $sName = null, $aAttribs=array()) {
  183. $aAttribs['value'] = $sValue;
  184. if (! is_null($sName)) $aAttribs['name'] = $sName;
  185. // add default css
  186. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmsubmitfield';
  187. return addInputField('submit', $aAttribs);
  188. }
  189. /**
  190. * Form reset button
  191. * @param string $sValue button name
  192. * @param array $aAttribs (since 1.5.1) extra attributes
  193. * @return string html formated reset input field
  194. */
  195. function addReset($sValue, $aAttribs=array()) {
  196. $aAttribs['value'] = $sValue;
  197. // add default css
  198. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmresetfield';
  199. return addInputField('reset', $aAttribs);
  200. }
  201. /**
  202. * Textarea form element.
  203. *
  204. * @param string $sName field name
  205. * @param string $sText initial field value (OPTIONAL; default empty)
  206. * @param integer $iCols field width (number of chars) (OPTIONAL; default 40)
  207. * @param integer $iRows field height (number of character rows) (OPTIONAL; default 10)
  208. * @param array $aAttribs (since 1.5.1) extra attributes (OPTIONAL; default empty)
  209. *
  210. * @return string html formated text area field
  211. *
  212. */
  213. function addTextArea($sName, $sText = '', $iCols = 40, $iRows = 10, $aAttribs = array()) {
  214. // no longer accept string arguments for attribs; print
  215. // backtrace to help people fix their code
  216. if (!is_array($aAttribs)) {
  217. echo '$aAttribs argument to addTextArea() must be an array<br /><pre>';
  218. debug_print_backtrace();
  219. echo '</pre><br />';
  220. exit;
  221. }
  222. // FIXME: should the template do this instead????
  223. else if (!isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextarea';
  224. global $oTemplate;
  225. //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);
  226. $oTemplate->assign('aAttribs', $aAttribs);
  227. $oTemplate->assign('name', $sName);
  228. $oTemplate->assign('text', $sText);
  229. $oTemplate->assign('cols', (int)$iCols);
  230. $oTemplate->assign('rows', (int)$iRows);
  231. return $oTemplate->fetch('textarea.tpl');
  232. }
  233. /**
  234. * Make a <form> start-tag.
  235. *
  236. * @param string $sAction form handler URL
  237. * @param string $sMethod http method used to submit form data. 'get' or 'post'
  238. * @param string $sName form name used for identification (used for backward
  239. * compatibility). Use of id is recommended instead.
  240. * @param string $sEnctype content type that is used to submit data. html 4.01
  241. * defaults to 'application/x-www-form-urlencoded'. Form
  242. * with file field needs 'multipart/form-data' encoding type.
  243. * @param string $sCharset charset that is used for submitted data
  244. * @param array $aAttribs (since 1.5.1) extra attributes
  245. *
  246. * @return string html formated form start string
  247. *
  248. */
  249. function addForm($sAction, $sMethod = 'post', $sName = '', $sEnctype = '', $sCharset = '', $aAttribs = array()) {
  250. global $oTemplate;
  251. //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);
  252. $oTemplate->assign('aAttribs', $aAttribs);
  253. $oTemplate->assign('name', $sName);
  254. $oTemplate->assign('method', $sMethod);
  255. $oTemplate->assign('action', $sAction);
  256. $oTemplate->assign('enctype', $sEnctype);
  257. $oTemplate->assign('charset', $sCharset);
  258. return $oTemplate->fetch('form.tpl');
  259. }