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