1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
<?php
/*
* Model web-forms and simplify the process of accepting, validating, and sanitizing input
*/
class Form
{
/*
* Constructor
*/
function __construct()
{
$this->textFields = array();
$this->numbFields = array();
$this->enumFields = array();
$this->errorlist = array();
}
/*
* Log an error
*/
function logError($str)
{
$this->errorlist[] = $str;
}
/*
* Add new text field to the form
*/
function field_text($name, $deflt = null, $req = true)
{
if ($req !== true)
$req = false;
$this->textFields[] = array(
'name' => $name,
'deflt' => $deflt,
'req' => $req
);
}
/*
* Add new numeric field to the form
*/
function field_numeric($name, $min = null, $max = null, $deflt = null, $integer = true, $req = true)
{
if ($req !== true)
$req = false;
if ($integer !== true)
$integer = false;
$this->numbFields[] = array(
'name' => $name,
'min' => $min,
'max' => $max,
'deflt' => $deflt,
'int' => $integer,
'req' => $req
);
}
/*
* Add new enumeration field to the form
*/
function field_enum($name, $values, $deflt = null, $req = true)
{
if ($req !== true)
$req = false;
$this->enumFields[] = array(
'name' => $name,
'vals' => $values,
'deflt' => $deflt,
'req' => $req
);
}
/*
* Add new boolean field to the form
*/
function field_bool($name)
{
$this->field_enum($name, array("1", "0"), "0");
}
/*
* Populate the form with input data from web page
*/
function populate($input)
{
/* detect duplicate names */
$names = array();
foreach ($this->textFields as $fld)
$names[] = $fld['name'];
foreach ($this->numbFields as $fld)
$names[] = $fld['name'];
foreach ($this->enumFields as $fld)
$names[] = $fld['name'];
if (count(array_unique($names)) != count($names))
{
$this->logError("Internal error: Duplicate field names defined in form");
return false;
}
/* init text fields */
foreach ($this->textFields as $fld)
{
if (isset($input[$fld['name']]) && $input[$fld['name']] != "")
$this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES);
else if (!is_null($fld['deflt']))
$this->$fld['name'] = $fld['deflt'];
else if ($fld['req'])
$this->logError($fld['name'] . " is required");
}
/* init numeric fields */
foreach ($this->numbFields as $fld)
{
if (isset($input[$fld['name']]) && $input[$fld['name']] != "")
{
if (!is_numeric($input[$fld['name']]))
{
$this->logError($fld['name'] . " must be numeric");
continue;
}
if ($fld['int'] && (floor($input[$fld['name']]) != $input[$fld['name']]))
{
$this->logError($fld['name'] . " must be an integer");
continue;
}
if (!is_null($fld['min']) && ($input[$fld['name']] < $fld['min']))
{
$this->logError($fld['name'] . " must be no less than " . $fld['min']);
continue;
}
if (!is_null($fld['max']) && ($input[$fld['name']] > $fld['max']))
{
$this->logError($fld['name'] . " must be no more than " . $fld['max']);
continue;
}
$this->$fld['name'] = $input[$fld['name']];
}
else if (!is_null($fld['deflt']))
$this->$fld['name'] = $fld['deflt'];
else if ($fld['req'])
$this->logError($fld['name'] . " is required");
}
/* init enum fields */
foreach ($this->enumFields as $fld)
{
if (isset($input[$fld['name']]) && $input[$fld['name']] != "")
{
if (array_search($input[$fld['name']], $fld['vals']) === false)
{
$this->logError($fld['name'] . " is not an appropriate value");
continue;
}
$this->$fld['name'] = $input[$fld['name']];
}
else if (!is_null($fld['deflt']))
$this->$fld['name'] = $fld['deflt'];
else if ($fld['req'])
$this->logError($fld['name'] . " is required");
}
/* return */
return count($this->errorlist) == 0;
}
/*
* Handle an uploaded file
*/
function saveFile($file, $maxsize, $allowed_mime, $path, $req = false)
{
if (isset($file) && !is_null($file))
{
if ($file['error'] > 0)
{
$this->logError("An unknown error occurred");
return false;
}
if ($file['size'] > $maxsize)
{
$this->logError("File must be no larger than " . $maxsize . " bytes");
return false;
}
if (is_array($allowed_mime) && array_search($file['type'], $allowed_mime) === false)
{
$this->logError("File type is not supported");
return false;
}
if (!move_uploaded_file($file['tmp_name'], $path))
{
$this->logError("Error saving uploaded file");
return false;
}
}
else if ($req)
{
$this->logError("File upload is required");
return false;
}
return true;
}
}
?>
|