forms.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * forms.php - html form functions
  4. *
  5. * Functions to build HTML forms in a safe and consistent manner.
  6. * All attribute values are sanitized with htmlspecialchars().
  7. *
  8. * Currently functions don't provide simple wrappers for file and
  9. * image input fields, support only submit and reset buttons and use
  10. * html input tags for buttons.
  11. *
  12. * Since 1.5.1:
  13. *
  14. * * all form functions should support id tags. Original
  15. * idea by dugan <at> passwall.com. Tags can be used for Section 508
  16. * or WAI compliance.
  17. *
  18. * * input tag functions accept extra html attributes that can be submitted
  19. * in $aAttribs array.
  20. *
  21. * * default css class attributes are added.
  22. *
  23. * @link http://www.section508.gov/ Section 508
  24. * @link http://www.w3.org/WAI/ Web Accessibility Initiative (WAI)
  25. * @link http://www.w3.org/TR/html4/ W3.org HTML 4.01 form specs
  26. * @copyright &copy; 2004-2006 The SquirrelMail Project Team
  27. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  28. * @version $Id$
  29. * @package squirrelmail
  30. * @subpackage forms
  31. * @since 1.4.3 and 1.5.1
  32. */
  33. /**
  34. * Helper function to create form fields, not to be called directly,
  35. * only by other functions below.
  36. *
  37. * Function used different syntax before 1.5.1
  38. * @param string $sType type of input field. Possible values (html 4.01
  39. * specs.): text, password, checkbox, radio, submit, reset, file,
  40. * hidden, image, button.
  41. * @param array $aAttribs (since 1.5.1) extra attributes. Array key is
  42. * attribute name, array value is attribute value. Array keys must use
  43. * lowercase.
  44. * @return string html formated input field
  45. * @deprecated use other functions that provide simple wrappers to this function
  46. */
  47. function addInputField($sType, $aAttribs=array()) {
  48. $sAttribs = '';
  49. // define unique identifier
  50. if (! isset($aAttribs['id']) && isset($aAttribs['name']) && ! is_null($aAttribs['name'])) {
  51. /**
  52. * if 'id' is not set, set it to 'name' and replace brackets
  53. * with underscores. 'name' might contain field name with squire
  54. * brackets (array). Brackets are not allowed in id (validator.w3.org
  55. * fails to validate document). According to html 4.01 manual cdata
  56. * type description, 'name' attribute uses same type, but validator.w3.org
  57. * does not barf on brackets in 'name' attributes.
  58. */
  59. $aAttribs['id'] = strtr($aAttribs['name'],'[]','__');
  60. }
  61. // create attribute string (do we have to sanitize keys?)
  62. foreach ($aAttribs as $key => $value) {
  63. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  64. }
  65. return '<input type="'.$sType.'"'.$sAttribs." />\n";
  66. }
  67. /**
  68. * Password input field
  69. * @param string $sName field name
  70. * @param string $sValue initial password value
  71. * @param array $aAttribs (since 1.5.1) extra attributes
  72. * @return string html formated password field
  73. */
  74. function addPwField($sName, $sValue = null, $aAttribs=array()) {
  75. $aAttribs['name'] = $sName;
  76. $aAttribs['value'] = (! is_null($sValue) ? $sValue : '');
  77. // add default css
  78. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmpwfield';
  79. return addInputField('password',$aAttribs);
  80. }
  81. /**
  82. * Form checkbox
  83. * @param string $sName field name
  84. * @param boolean $bChecked controls if field is checked
  85. * @param string $sValue
  86. * @param array $aAttribs (since 1.5.1) extra attributes
  87. * @return string html formated checkbox field
  88. */
  89. function addCheckBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  90. $aAttribs['name'] = $sName;
  91. if ($bChecked) $aAttribs['checked'] = 'checked';
  92. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  93. // add default css
  94. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmcheckbox';
  95. return addInputField('checkbox',$aAttribs);
  96. }
  97. /**
  98. * Form radio box
  99. * @param string $sName field name
  100. * @param boolean $bChecked controls if field is selected
  101. * @param string $sValue
  102. * @param array $aAttribs (since 1.5.1) extra attributes.
  103. * @return string html formated radio box
  104. */
  105. function addRadioBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  106. $aAttribs['name'] = $sName;
  107. if ($bChecked) $aAttribs['checked'] = 'checked';
  108. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  109. if (! isset($aAttribs['id'])) $aAttribs['id'] = $sName . $sValue;
  110. // add default css
  111. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmradiobox';
  112. return addInputField('radio', $aAttribs);
  113. }
  114. /**
  115. * A hidden form field.
  116. * @param string $sName field name
  117. * @param string $sValue field value
  118. * @param array $aAttribs (since 1.5.1) extra attributes
  119. * @return html formated hidden form field
  120. */
  121. function addHidden($sName, $sValue, $aAttribs=array()) {
  122. $aAttribs['name'] = $sName;
  123. $aAttribs['value'] = $sValue;
  124. // add default css
  125. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmhiddenfield';
  126. return addInputField('hidden', $aAttribs);
  127. }
  128. /**
  129. * An input textbox.
  130. * @param string $sName field name
  131. * @param string $sValue initial field value
  132. * @param integer $iSize field size (number of characters)
  133. * @param integer $iMaxlength maximum number of characters the user may enter
  134. * @param array $aAttribs (since 1.5.1) extra attributes - should be given
  135. * in the form array('attribute_name' => 'attribute_value', ...)
  136. * @return string html formated text input field
  137. */
  138. function addInput($sName, $sValue = '', $iSize = 0, $iMaxlength = 0, $aAttribs=array()) {
  139. $aAttribs['name'] = $sName;
  140. $aAttribs['value'] = $sValue;
  141. if ($iSize) $aAttribs['size'] = (int)$iSize;
  142. if ($iMaxlength) $aAttribs['maxlength'] = (int)$iMaxlength;
  143. // add default css
  144. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextfield';
  145. return addInputField('text', $aAttribs);
  146. }
  147. /**
  148. * Function to create a selectlist from an array.
  149. * @param string $sName field name
  150. * @param array $aValues field values array ( key => value ) -> <option value="key">value</option>
  151. * @param mixed $default the key that will be selected
  152. * @param boolean $bUsekeys use the keys of the array as option value or not
  153. * @param array $aAttribs (since 1.5.1) extra attributes
  154. * @return string html formated selection box
  155. * @todo add attributes argument for option tags and default css
  156. */
  157. function addSelect($sName, $aValues, $default = null, $bUsekeys = false, $aAttribs = array()) {
  158. // only one element
  159. if(count($aValues) == 1) {
  160. $k = key($aValues); $v = array_pop($aValues);
  161. return addHidden($sName, ($bUsekeys ? $k:$v), $aAttribs).
  162. htmlspecialchars($v) . "\n";
  163. }
  164. if (isset($aAttribs['id'])) {
  165. $label_open = '<label for="'.htmlspecialchars($aAttribs['id']).'">';
  166. $label_close = '</label>';
  167. } else {
  168. $label_open = '';
  169. $label_close = '';
  170. }
  171. // create attribute string for select tag
  172. $sAttribs = '';
  173. foreach ($aAttribs as $key => $value) {
  174. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  175. }
  176. $ret = '<select name="'.htmlspecialchars($sName) . '"' . $sAttribs . ">\n";
  177. foreach ($aValues as $k => $v) {
  178. if(!$bUsekeys) $k = $v;
  179. $ret .= '<option value="' .
  180. htmlspecialchars( $k ) . '"' .
  181. (($default == $k) ? ' selected="selected"' : '') .
  182. '>' . $label_open . htmlspecialchars($v) . $label_close ."</option>\n";
  183. }
  184. $ret .= "</select>\n";
  185. return $ret;
  186. }
  187. /**
  188. * Form submission button
  189. * Note the switched value/name parameters!
  190. * @param string $sValue button name
  191. * @param string $sName submitted key name
  192. * @param array $aAttribs (since 1.5.1) extra attributes
  193. * @return string html formated submit input field
  194. */
  195. function addSubmit($sValue, $sName = null, $aAttribs=array()) {
  196. $aAttribs['value'] = $sValue;
  197. if (! is_null($sName)) $aAttribs['name'] = $sName;
  198. // add default css
  199. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmsubmitfield';
  200. return addInputField('submit', $aAttribs);
  201. }
  202. /**
  203. * Form reset button
  204. * @param string $sValue button name
  205. * @param array $aAttribs (since 1.5.1) extra attributes
  206. * @return string html formated reset input field
  207. */
  208. function addReset($sValue, $aAttribs=array()) {
  209. $aAttribs['value'] = $sValue;
  210. // add default css
  211. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmresetfield';
  212. return addInputField('reset', $aAttribs);
  213. }
  214. /**
  215. * Textarea form element.
  216. * @param string $sName field name
  217. * @param string $sText initial field value
  218. * @param integer $iCols field width (number of chars)
  219. * @param integer $iRows field height (number of character rows)
  220. * @param array $aAttribs (since 1.5.1) extra attributes. function accepts string argument
  221. * for backward compatibility.
  222. * @return string html formated text area field
  223. */
  224. function addTextArea($sName, $sText = '', $iCols = 40, $iRows = 10, $aAttribs = array()) {
  225. $label_open = '';
  226. $label_close = '';
  227. if (is_array($aAttribs)) {
  228. // maybe id can default to name?
  229. if (isset($aAttribs['id'])) {
  230. $label_open = '<label for="'.htmlspecialchars($aAttribs['id']).'">';
  231. $label_close = '</label>';
  232. }
  233. // add default css
  234. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextarea';
  235. // create attribute string (do we have to sanitize keys?)
  236. $sAttribs = '';
  237. foreach ($aAttribs as $key => $value) {
  238. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  239. }
  240. } elseif (is_string($aAttribs)) {
  241. // backward compatibility mode. deprecated.
  242. $sAttribs = ' ' . $aAttribs;
  243. } else {
  244. $sAttribs = '';
  245. }
  246. return '<textarea name="'.htmlspecialchars($sName).'" '.
  247. 'rows="'.(int)$iRows .'" cols="'.(int)$iCols.'"'.
  248. $sAttribs . '>'. $label_open . htmlspecialchars($sText) . $label_close ."</textarea>\n";
  249. }
  250. /**
  251. * Make a <form> start-tag.
  252. * @param string $sAction form handler URL
  253. * @param string $sMethod http method used to submit form data. 'get' or 'post'
  254. * @param string $sName form name used for identification (used for backward
  255. * compatibility). Use of id is recommended.
  256. * @param string $sEnctype content type that is used to submit data. html 4.01
  257. * defaults to 'application/x-www-form-urlencoded'. Form with file field needs
  258. * 'multipart/form-data' encoding type.
  259. * @param string $sCharset charset that is used for submitted data
  260. * @param array $aAttribs (since 1.5.1) extra attributes
  261. * @return string html formated form start string
  262. */
  263. function addForm($sAction, $sMethod = 'post', $sName = '', $sEnctype = '', $sCharset = '', $aAttribs = array()) {
  264. // id tags
  265. if (! isset($aAttribs['id']) && ! empty($sName))
  266. $aAttribs['id'] = $sName;
  267. if($sName) {
  268. $sName = ' name="'.$sName.'"';
  269. }
  270. if($sEnctype) {
  271. $sEnctype = ' enctype="'.$sEnctype.'"';
  272. }
  273. if($sCharset) {
  274. $sCharset = ' accept-charset="'.htmlspecialchars($sCharset).'"';
  275. }
  276. // create attribute string (do we have to sanitize keys?)
  277. $sAttribs = '';
  278. foreach ($aAttribs as $key => $value) {
  279. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  280. }
  281. return '<form action="'. $sAction .'" method="'. $sMethod .'"'.
  282. $sEnctype . $sName . $sCharset . $sAttribs . ">\n";
  283. }