850d58b8d31546fbbfbcb40ec77ab40f8ce8d2fb
[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 * $Id$
12 * @package squirrelmail
13 */
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, $value, $attributes = '') {
20 return '<input type="'.$type.'" name="'.htmlentities($name).'" '.
21 ' value="'.htmlentities($value).'"'.
22 $attributes . ">\n";
23 }
24
25
26 /**
27 * Form checkbox
28 */
29 function addCheckBox($name, $checked = false, $value='') {
30 return addInputField('checkbox', $name, $value,
31 ($checked ? ' checked' : ''));
32 }
33
34 /**
35 * Form radio box
36 */
37 function addRadioBox($name, $checked = false, $value='') {
38 return addInputField('radio', $name, $value,
39 ($checked ? ' checked' : ''));
40 }
41
42 /**
43 * A hidden form field.
44 */
45 function addHidden($name, $value) {
46 return addInputField('hidden', $name, $value);
47 }
48
49 /**
50 * An input textbox.
51 */
52 function addInput($name, $value = '', $size = 0, $maxlength = 0) {
53
54 $attr = '';
55 if ($size) {
56 $attr.= ' size="'.(int)$size.'"';
57 }
58 if ($maxlength) {
59 $attr.= ' maxlength="'.(int)$maxlength .'"';
60 }
61
62 return addInputField('text', $name, $value, $attr);
63 }
64
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
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 htmlentities($v) . "\n";
81 }
82
83 $ret = '<select name="'.htmlentities($name) . "\">\n";
84 foreach ($values as $k => $v) {
85 if(!$usekeys) $k = $v;
86 $ret .= '<option value="' .
87 htmlentities( $k ) . '"' .
88 (($default == $k) ? ' selected':'') .
89 '>' . htmlentities($v) ."</option>\n";
90 }
91 $ret .= "</select>\n";
92
93 return $ret;
94 }
95
96 /**
97 * Textarea form element.
98 */
99 function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
100 return '<textarea name="'.htmlentities($name).'" '.
101 'rows="'.(int)$rows .'" cols="'.(int)$cols.'"'.
102 $attr . '">'.htmlentities($text) ."</textarea>\n";
103 }
104
105 /**
106 * Make a <form> start-tag.
107 */
108 function addForm($action, $method = 'POST', $name = '', $enctype = '', $charset = '')
109 {
110 if($name) {
111 $name = ' name="'.$name.'"';
112 }
113 if($enctype) {
114 $enctype = ' enctype="'.$enctype.'"';
115 }
116 if($charset) {
117 $charset = ' accept-charset="'.htmlspecialchars($charset).'"';
118 }
119
120 return '<form action="'. $action .'" method="'. $method .'"'.
121 $enctype . $name . $charset . "\">\n";
122 }
123
124