Increment year in copyright notice.
[squirrelmail.git] / functions / forms.php
CommitLineData
493b168d 1<?php
2/**
3 * forms.php
4 *
6c84ba1e 5 * Copyright (c) 2004-2005 The SquirrelMail Project Team
493b168d 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 20function 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 30function addPwField($name , $value = null) {
31 return addInputField('password', $name , $value);
10ff256e 32}
33
493b168d 34
35/**
36 * Form checkbox
37 */
d4b2cc0a 38function 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 46function addRadioBox($name, $checked = false, $value = null) {
324ac3c5 47 return addInputField('radio', $name, $value,
fad34a61 48 ($checked ? ' checked="checked"' : ''));
493b168d 49}
50
51/**
52 * A hidden form field.
53 */
54function addHidden($name, $value) {
55 return addInputField('hidden', $name, $value);
56}
57
58/**
59 * An input textbox.
60 */
61function 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
f79fb61d 79 * values: array ( key => value ) -> <option value="key">value</option>
493b168d 80 * default: the key that will be selected
81 * usekeys: use the keys of the array as option value or not
82 */
83function 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 ) . '"' .
f79fb61d 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 */
109function addSubmit($value, $name = null) {
110 return addInputField('submit', $name, $value);
111}
112/**
113 * Form reset button, $value = caption
114 */
115function addReset($value) {
116 return addInputField('reset', null, $value);
117}
118
493b168d 119/**
120 * Textarea form element.
121 */
122function addTextArea($name, $text = '', $cols = 40, $rows = 10, $attr = '') {
745eb9e2 123 return '<textarea name="'.htmlspecialchars($name).'" '.
8a9f3f9d 124 'rows="'.(int)$rows .'" cols="'.(int)$cols.'" '.
125 $attr . '>'.htmlspecialchars($text) ."</textarea>\n";
493b168d 126}
127
128/**
129 * Make a <form> start-tag.
130 */
39bfea8f 131function addForm($action, $method = 'post', $name = '', $enctype = '', $charset = '')
493b168d 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 .'"'.
9d6b3b72 144 $enctype . $name . $charset . ">\n";
493b168d 145}
146
f79fb61d 147?>