forms.php 3.5 KB

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