forms.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * forms.php
  4. *
  5. * Copyright (c) 2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * Functions to build HTML forms in a safe and consistent manner.
  9. * All name, value attributes are htmlentitied.
  10. *
  11. * $Id$
  12. * @package squirrelmail
  13. */
  14. /**
  15. * Helper function to create form fields, not to be called directly,
  16. * only by other functions below.
  17. */
  18. function addInputField($type, $name, $value, $attributes = '') {
  19. return '<input type="'.$type.'" name="'.htmlentities($name).'" '.
  20. ' value="'.htmlentities($value).'"'.
  21. $attributes . ">\n";
  22. }
  23. /**
  24. * Form checkbox
  25. */
  26. function addCheckBox($name, $checked = false, $value='') {
  27. return addInputField('checkbox', $name, $value,
  28. ($checked ? ' checked' : ''));
  29. }
  30. /**
  31. * Form radio box
  32. */
  33. function addRadioBox($name, $checked = false, $value='') {
  34. return addInputField('radio', $name, $value,
  35. ($checked ? ' checked' : ''));
  36. }
  37. /**
  38. * A hidden form field.
  39. */
  40. function addHidden($name, $value) {
  41. return addInputField('hidden', $name, $value);
  42. }
  43. /**
  44. * An input textbox.
  45. */
  46. function addInput($name, $value = '', $size = 0, $maxlength = 0) {
  47. $attr = '';
  48. if ($size) {
  49. $attr.= ' size="'.(int)$size.'"';
  50. }
  51. if ($maxlength) {
  52. $attr.= ' maxlength="'.(int)$maxlength .'"';
  53. }
  54. return addInputField('text', $name, $value, $attr);
  55. }
  56. /**
  57. * Function to create a selectlist from an array.
  58. * Usage:
  59. * name: html name attribute
  60. * values: array ( key => value ) -> <option value="key">value
  61. * default: the key that will be selected
  62. * usekeys: use the keys of the array as option value or not
  63. */
  64. function addSelect($name, $values, $default = null, $usekeys = false)
  65. {
  66. // only one element
  67. if(count($values) == 1) {
  68. $k = key($values); $v = array_pop($values);
  69. return addHidden($name, ($usekeys ? $k:$v)).
  70. htmlentities($v) . "\n";
  71. }
  72. $ret = '<select name="'.htmlentities($name) . "\">\n";
  73. foreach ($values as $k => $v) {
  74. if(!$usekeys) $k = $v;
  75. $ret .= '<option value="' .
  76. htmlentities( $k ) . '"' .
  77. (($default == $k) ? ' selected':'') .
  78. '>' . htmlentities($v) ."</option>\n";
  79. }
  80. $ret .= "</select>\n";
  81. return $ret;
  82. }
  83. /**
  84. * Textarea form element.
  85. */
  86. function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
  87. return '<textarea name="'.htmlentities($name).'" '.
  88. 'rows="'.(int)$rows .'" cols="'.(int)$cols.'"'.
  89. $attr . '">'.htmlentities($text) ."</textarea>\n";
  90. }
  91. /**
  92. * Make a <form> start-tag.
  93. */
  94. function addForm($action, $method = 'POST', $name = '', $enctype = '', $charset = '')
  95. {
  96. if($name) {
  97. $name = ' name="'.$name.'"';
  98. }
  99. if($enctype) {
  100. $enctype = ' enctype="'.$enctype.'"';
  101. }
  102. if($charset) {
  103. $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
  104. }
  105. return '<form action="'. $action .'" method="'. $method .'"'.
  106. $enctype . $name . $charset . "\">\n";
  107. }