493b168d |
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 | * |
74f5d33f |
11 | * @version $Id$ |
493b168d |
12 | * @package squirrelmail |
74f5d33f |
13 | * @subpackage forms |
493b168d |
14 | */ |
15 | |
16 | /** |
17 | * Helper function to create form fields, not to be called directly, |
18 | * only by other functions below. |
19 | */ |
10ff256e |
20 | function addInputField($type, $name = null, $value = null, $attributes = '') { |
21 | return '<input type="'.$type.'"'. |
745eb9e2 |
22 | ($name !== null ? ' name="'.htmlspecialchars($name).'"' : ''). |
23 | ($value !== null ? ' value="'.htmlspecialchars($value).'"' : ''). |
fad34a61 |
24 | $attributes . " />\n"; |
493b168d |
25 | } |
26 | |
10ff256e |
27 | /** |
28 | * Password input field |
29 | */ |
6424bf75 |
30 | function addPwField($name , $value = null) { |
31 | return addInputField('password', $name , $value); |
10ff256e |
32 | } |
33 | |
493b168d |
34 | |
35 | /** |
36 | * Form checkbox |
37 | */ |
d4b2cc0a |
38 | function addCheckBox($name, $checked = false, $value = null) { |
493b168d |
39 | return addInputField('checkbox', $name, $value, |
fad34a61 |
40 | ($checked ? ' checked="checked"' : '')); |
493b168d |
41 | } |
42 | |
43 | /** |
44 | * Form radio box |
45 | */ |
d4b2cc0a |
46 | function addRadioBox($name, $checked = false, $value = null) { |
493b168d |
47 | return addInputField('radio', $name, $value, |
fad34a61 |
48 | ($checked ? ' checked="checked"' : '')); |
493b168d |
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)). |
745eb9e2 |
89 | htmlspecialchars($v) . "\n"; |
493b168d |
90 | } |
91 | |
745eb9e2 |
92 | $ret = '<select name="'.htmlspecialchars($name) . "\">\n"; |
493b168d |
93 | foreach ($values as $k => $v) { |
94 | if(!$usekeys) $k = $v; |
95 | $ret .= '<option value="' . |
745eb9e2 |
96 | htmlspecialchars( $k ) . '"' . |
fad34a61 |
97 | (($default == $k) ? ' selected="selected"':'') . |
745eb9e2 |
98 | '>' . htmlspecialchars($v) ."</option>\n"; |
493b168d |
99 | } |
100 | $ret .= "</select>\n"; |
101 | |
102 | return $ret; |
103 | } |
104 | |
10ff256e |
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 | |
493b168d |
119 | /** |
120 | * Textarea form element. |
121 | */ |
122 | function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') { |
745eb9e2 |
123 | return '<textarea name="'.htmlspecialchars($name).'" '. |
493b168d |
124 | 'rows="'.(int)$rows .'" cols="'.(int)$cols.'"'. |
745eb9e2 |
125 | $attr . '">'.htmlspecialchars($text) ."</textarea>\n"; |
493b168d |
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 | |
fad34a61 |
147 | ?> |