forms.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * @version $Id$
  12. * @package squirrelmail
  13. * @subpackage forms
  14. */
  15. /**
  16. * Helper function to create form fields, not to be called directly,
  17. * only by other functions below.
  18. */
  19. function addInputField($type, $name = null, $value = null, $attributes = '') {
  20. return '<input type="'.$type.'"'.
  21. ($name !== null ? ' name="'.htmlspecialchars($name).'"' : '').
  22. ($value !== null ? ' value="'.htmlspecialchars($value).'"' : '').
  23. $attributes . " />\n";
  24. }
  25. /**
  26. * Password input field
  27. */
  28. function addPwField($name , $value = null) {
  29. return addInputField('password', $name , $value);
  30. }
  31. /**
  32. * Form checkbox
  33. */
  34. function addCheckBox($name, $checked = false, $value = null) {
  35. return addInputField('checkbox', $name, $value,
  36. ($checked ? ' checked="checked"' : ''));
  37. }
  38. /**
  39. * Form radio box
  40. */
  41. function addRadioBox($name, $checked = false, $value = null) {
  42. return addInputField('radio', $name, $value,
  43. ($checked ? ' checked="checked"' : ''));
  44. }
  45. /**
  46. * A hidden form field.
  47. */
  48. function addHidden($name, $value) {
  49. return addInputField('hidden', $name, $value);
  50. }
  51. /**
  52. * An input textbox.
  53. */
  54. function addInput($name, $value = '', $size = 0, $maxlength = 0) {
  55. $attr = '';
  56. if ($size) {
  57. $attr.= ' size="'.(int)$size.'"';
  58. }
  59. if ($maxlength) {
  60. $attr.= ' maxlength="'.(int)$maxlength .'"';
  61. }
  62. return addInputField('text', $name, $value, $attr);
  63. }
  64. /**
  65. * Function to create a selectlist from an array.
  66. * Usage:
  67. * name: html name attribute
  68. * values: array ( key => value ) -> <option value="key">value
  69. * default: the key that will be selected
  70. * usekeys: use the keys of the array as option value or not
  71. */
  72. function addSelect($name, $values, $default = null, $usekeys = false)
  73. {
  74. // only one element
  75. if(count($values) == 1) {
  76. $k = key($values); $v = array_pop($values);
  77. return addHidden($name, ($usekeys ? $k:$v)).
  78. htmlspecialchars($v) . "\n";
  79. }
  80. $ret = '<select name="'.htmlspecialchars($name) . "\">\n";
  81. foreach ($values as $k => $v) {
  82. if(!$usekeys) $k = $v;
  83. $ret .= '<option value="' .
  84. htmlspecialchars( $k ) . '"' .
  85. (($default == $k) ? ' selected="selected"':'') .
  86. '>' . htmlspecialchars($v) ."</option>\n";
  87. }
  88. $ret .= "</select>\n";
  89. return $ret;
  90. }
  91. /**
  92. * Form submission button
  93. * Note the switched value/name parameters!
  94. */
  95. function addSubmit($value, $name = null) {
  96. return addInputField('submit', $name, $value);
  97. }
  98. /**
  99. * Form reset button, $value = caption
  100. */
  101. function addReset($value) {
  102. return addInputField('reset', null, $value);
  103. }
  104. /**
  105. * Textarea form element.
  106. */
  107. function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
  108. return '<textarea name="'.htmlspecialchars($name).'" '.
  109. 'rows="'.(int)$rows .'" cols="'.(int)$cols.'"'.
  110. $attr . '">'.htmlspecialchars($text) ."</textarea>\n";
  111. }
  112. /**
  113. * Make a <form> start-tag.
  114. */
  115. function addForm($action, $method = 'POST', $name = '', $enctype = '', $charset = '')
  116. {
  117. if($name) {
  118. $name = ' name="'.$name.'"';
  119. }
  120. if($enctype) {
  121. $enctype = ' enctype="'.$enctype.'"';
  122. }
  123. if($charset) {
  124. $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
  125. }
  126. return '<form action="'. $action .'" method="'. $method .'"'.
  127. $enctype . $name . $charset . "\">\n";
  128. }
  129. ?>