forms.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. $aAttribs['id'] = $aAttribs['name'];
  52. }
  53. // create attribute string (do we have to sanitize keys?)
  54. foreach ($aAttribs as $key => $value) {
  55. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  56. }
  57. return '<input type="'.$sType.'"'.$sAttribs." />\n";
  58. }
  59. /**
  60. * Password input field
  61. * @param string $sName field name
  62. * @param string $sValue initial password value
  63. * @param array $aAttribs (since 1.5.1) extra attributes
  64. * @return string html formated password field
  65. */
  66. function addPwField($sName, $sValue = null, $aAttribs=array()) {
  67. $aAttribs['name'] = $sName;
  68. $aAttribs['value'] = (! is_null($sValue) ? $sValue : '');
  69. // add default css
  70. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmpwfield';
  71. return addInputField('password',$aAttribs);
  72. }
  73. /**
  74. * Form checkbox
  75. * @param string $sName field name
  76. * @param boolean $bChecked controls if field is checked
  77. * @param string $sValue
  78. * @param array $aAttribs (since 1.5.1) extra attributes
  79. * @return string html formated checkbox field
  80. */
  81. function addCheckBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  82. $aAttribs['name'] = $sName;
  83. if ($bChecked) $aAttribs['checked'] = 'checked';
  84. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  85. // add default css
  86. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmcheckbox';
  87. return addInputField('checkbox',$aAttribs);
  88. }
  89. /**
  90. * Form radio box
  91. * @param string $sName field name
  92. * @param boolean $bChecked controls if field is selected
  93. * @param string $sValue
  94. * @param array $aAttribs (since 1.5.1) extra attributes.
  95. * @return string html formated radio box
  96. */
  97. function addRadioBox($sName, $bChecked = false, $sValue = null, $aAttribs=array()) {
  98. $aAttribs['name'] = $sName;
  99. if ($bChecked) $aAttribs['checked'] = 'checked';
  100. if (! is_null($sValue)) $aAttribs['value'] = $sValue;
  101. if (! isset($aAttribs['id'])) $aAttribs['id'] = $sName . $sValue;
  102. // add default css
  103. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmradiobox';
  104. return addInputField('radio', $aAttribs);
  105. }
  106. /**
  107. * A hidden form field.
  108. * @param string $sName field name
  109. * @param string $sValue field value
  110. * @param array $aAttribs (since 1.5.1) extra attributes
  111. * @return html formated hidden form field
  112. */
  113. function addHidden($sName, $sValue, $aAttribs=array()) {
  114. $aAttribs['name'] = $sName;
  115. $aAttribs['value'] = $sValue;
  116. // add default css
  117. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmhiddenfield';
  118. return addInputField('hidden', $aAttribs);
  119. }
  120. /**
  121. * An input textbox.
  122. * @param string $sName field name
  123. * @param string $sValue initial field value
  124. * @param integer $iSize field size (number of characters)
  125. * @param integer $iMaxlength maximum number of characters the user may enter
  126. * @param array $aAttribs (since 1.5.1) extra attributes - should be given
  127. * in the form array('attribute_name' => 'attribute_value', ...)
  128. * @return string html formated text input field
  129. */
  130. function addInput($sName, $sValue = '', $iSize = 0, $iMaxlength = 0, $aAttribs=array()) {
  131. $aAttribs['name'] = $sName;
  132. $aAttribs['value'] = $sValue;
  133. if ($iSize) $aAttribs['size'] = (int)$iSize;
  134. if ($iMaxlength) $aAttribs['maxlength'] = (int)$iMaxlength;
  135. // add default css
  136. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextfield';
  137. return addInputField('text', $aAttribs);
  138. }
  139. /**
  140. * Function to create a selectlist from an array.
  141. * @param string $sName field name
  142. * @param array $aValues field values array ( key => value ) -> <option value="key">value</option>
  143. * @param mixed $default the key that will be selected
  144. * @param boolean $bUsekeys use the keys of the array as option value or not
  145. * @param array $aAttribs (since 1.5.1) extra attributes
  146. * @return string html formated selection box
  147. * @todo add attributes argument for option tags and default css
  148. */
  149. function addSelect($sName, $aValues, $default = null, $bUsekeys = false, $aAttribs = array()) {
  150. // only one element
  151. if(count($aValues) == 1) {
  152. $k = key($aValues); $v = array_pop($aValues);
  153. return addHidden($sName, ($bUsekeys ? $k:$v), $aAttribs).
  154. htmlspecialchars($v) . "\n";
  155. }
  156. if (isset($aAttribs['id'])) {
  157. $label_open = '<label for="'.htmlspecialchars($aAttribs['id']).'">';
  158. $label_close = '</label>';
  159. } else {
  160. $label_open = '';
  161. $label_close = '';
  162. }
  163. // create attribute string for select tag
  164. $sAttribs = '';
  165. foreach ($aAttribs as $key => $value) {
  166. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  167. }
  168. $ret = '<select name="'.htmlspecialchars($sName) . '"' . $sAttribs . ">\n";
  169. foreach ($aValues as $k => $v) {
  170. if(!$bUsekeys) $k = $v;
  171. $ret .= '<option value="' .
  172. htmlspecialchars( $k ) . '"' .
  173. (($default == $k) ? ' selected="selected"' : '') .
  174. '>' . $label_open . htmlspecialchars($v) . $label_close ."</option>\n";
  175. }
  176. $ret .= "</select>\n";
  177. return $ret;
  178. }
  179. /**
  180. * Form submission button
  181. * Note the switched value/name parameters!
  182. * @param string $sValue button name
  183. * @param string $sName submitted key name
  184. * @param array $aAttribs (since 1.5.1) extra attributes
  185. * @return string html formated submit input field
  186. */
  187. function addSubmit($sValue, $sName = null, $aAttribs=array()) {
  188. $aAttribs['value'] = $sValue;
  189. if (! is_null($sName)) $aAttribs['name'] = $sName;
  190. // add default css
  191. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmsubmitfield';
  192. return addInputField('submit', $aAttribs);
  193. }
  194. /**
  195. * Form reset button
  196. * @param string $sValue button name
  197. * @param array $aAttribs (since 1.5.1) extra attributes
  198. * @return string html formated reset input field
  199. */
  200. function addReset($sValue, $aAttribs=array()) {
  201. $aAttribs['value'] = $sValue;
  202. // add default css
  203. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmresetfield';
  204. return addInputField('reset', $aAttribs);
  205. }
  206. /**
  207. * Textarea form element.
  208. * @param string $sName field name
  209. * @param string $sText initial field value
  210. * @param integer $iCols field width (number of chars)
  211. * @param integer $iRows field height (number of character rows)
  212. * @param array $aAttribs (since 1.5.1) extra attributes. function accepts string argument
  213. * for backward compatibility.
  214. * @return string html formated text area field
  215. */
  216. function addTextArea($sName, $sText = '', $iCols = 40, $iRows = 10, $aAttribs = array()) {
  217. $label_open = '';
  218. $label_close = '';
  219. if (is_array($aAttribs)) {
  220. // maybe id can default to name?
  221. if (isset($aAttribs['id'])) {
  222. $label_open = '<label for="'.htmlspecialchars($aAttribs['id']).'">';
  223. $label_close = '</label>';
  224. }
  225. // add default css
  226. if (! isset($aAttribs['class'])) $aAttribs['class'] = 'sqmtextarea';
  227. // create attribute string (do we have to sanitize keys?)
  228. $sAttribs = '';
  229. foreach ($aAttribs as $key => $value) {
  230. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  231. }
  232. } elseif (is_string($aAttribs)) {
  233. // backward compatibility mode. deprecated.
  234. $sAttribs = ' ' . $aAttribs;
  235. } else {
  236. $sAttribs = '';
  237. }
  238. return '<textarea name="'.htmlspecialchars($sName).'" '.
  239. 'rows="'.(int)$iRows .'" cols="'.(int)$iCols.'"'.
  240. $sAttribs . '>'. $label_open . htmlspecialchars($sText) . $label_close ."</textarea>\n";
  241. }
  242. /**
  243. * Make a <form> start-tag.
  244. * @param string $sAction form handler URL
  245. * @param string $sMethod http method used to submit form data. 'get' or 'post'
  246. * @param string $sName form name used for identification (used for backward
  247. * compatibility). Use of id is recommended.
  248. * @param string $sEnctype content type that is used to submit data. html 4.01
  249. * defaults to 'application/x-www-form-urlencoded'. Form with file field needs
  250. * 'multipart/form-data' encoding type.
  251. * @param string $sCharset charset that is used for submitted data
  252. * @param array $aAttribs (since 1.5.1) extra attributes
  253. * @return string html formated form start string
  254. */
  255. function addForm($sAction, $sMethod = 'post', $sName = '', $sEnctype = '', $sCharset = '', $aAttribs = array()) {
  256. // id tags
  257. if (! isset($aAttribs['id']) && ! empty($sName))
  258. $aAttribs['id'] = $sName;
  259. if($sName) {
  260. $sName = ' name="'.$sName.'"';
  261. }
  262. if($sEnctype) {
  263. $sEnctype = ' enctype="'.$sEnctype.'"';
  264. }
  265. if($sCharset) {
  266. $sCharset = ' accept-charset="'.htmlspecialchars($sCharset).'"';
  267. }
  268. // create attribute string (do we have to sanitize keys?)
  269. $sAttribs = '';
  270. foreach ($aAttribs as $key => $value) {
  271. $sAttribs.= ' ' . $key . (! is_null($value) ? '="'.htmlspecialchars($value).'"':'');
  272. }
  273. return '<form action="'. $sAction .'" method="'. $sMethod .'"'.
  274. $sEnctype . $sName . $sCharset . $sAttribs . ">\n";
  275. }
  276. ?>