forms.php 3.8 KB

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