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