-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSimplePHPForm.php
More file actions
514 lines (441 loc) · 16.5 KB
/
Copy pathSimplePHPForm.php
File metadata and controls
514 lines (441 loc) · 16.5 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/**
* Simple PHP Form
*
* Open source automatic PHP form handling module with validation, helpers, warnings and more.
* Supports text fields, text areas, dropdowns, checkboxes, radio buttons and hidden fields.
* Validation flags supported: required, email, phone, number, lengthmax *, lengthmin *, sizemax *, sizemin *
*
* See ./examples/basic.php and ./examples/advanced.php and ./examples/centered.php for usage.
*
* @author Nathaniel Sabanski
* @link http://github.com/gnat/simple-php-form
* @license zlib/libpng license
*/
class SimplePHPForm
{
const STATE_NEW = 0;
const STATE_SUCCESS = 1;
const STATE_VALIDATE = 2;
const STATE_FAIL = 3;
const STATE_ERROR = 4;
const STATE_DUPLICATE = 5;
var $state = 0;
var $input_list = array();
var $url_action = '';
var $message_new = 'Registration Form';
var $message_success = 'Form submitted successfully!';
var $message_success_2 ='You should receive a confirmation email shortly!';
var $message_fail = 'Oops! We had trouble accepting your form. Details below.';
var $message_error = 'You have discovered an internal error. Please contact us!';
var $message_duplicate = 'You are already registered. If there is an issue, please contact us.';
/**
* Constructor.
* @param string $url_action Override re-direct URL.
*/
function __construct($url_action = '')
{
// Set custom <form> action.
$this->url_action = $url_action;
}
/**
* Add new field to form.
* @param string $type Field type.
* @param string $name Field name which can be refernced internally.
* @param string $data Default data displayed in field.
* @param string $data_validation_flags Data entered will be sanitized against this list of tests.
* @param string $text_title Title of field.
* @param string $text_help Helper text which is displayed to the User at fill-out time.
* @param string $text_error Error text which is displayed to the User when validation fails.
*/
function Add($type, $name, $data, $data_validation_flags, $text_title, $text_help, $text_error)
{
// Set default data.
$data_default = $data;
// Get form submission data from $_POST if it exists.
if(isset($_POST['simplephpform_'.$name]))
{
$data = $_POST['simplephpform_'.$name];
$this->state = self::STATE_VALIDATE; // We've got data during this pass. Form should be validated.
}
$this->input_list[$name] = new SimplePHPFormInput($type, $name, $data, $data_validation_flags, $data_default, $text_title, $text_help, $text_error);
// Special logic for checkbox types because browsers simply do not $_POST them if they are unchecked.
if($type == 'checkbox' && $this->state != self::STATE_NEW)
{
if(isset($_POST['simplephpform_'.$name]))
$this->input_list[$name]->data = true;
else
$this->input_list[$name]->data = false;
}
}
/**
* Add new dropdown field to form.
* @param string $name Field name which can be refernced internally.
* @param string $data Default data for the field.
* @param string $value Default display value for the field.
*/
function AddDropdownEntry($name, $data, $value)
{
$this->input_list[$name]->dropdown_entries[$value] = $data;
}
/**
* Add new radio button field to form.
* @param string $name Field name which can be refernced internally.
* @param string $value Default display value for the field.
* @param string $data Default data for the field.
*/
function AddRadioButton($name, $value, $data)
{
$this->input_list[$name]->radio_entries[$value] = $data;
}
/**
* Display form state to User.
* @return string Display in HTML format.
*/
function DisplayState()
{
$output = '';
if($this->state == self::STATE_NEW) {
$output = '<div class="simplephpform_state_untouched">'.$this->message_new.'</div>';
} if($this->state == self::STATE_SUCCESS) {
$output = '<div class="simplephpform_state_success">'.$this->message_success.'</div> <p>'.$this->message_success_2.'</p> <br />';
} if($this->state == self::STATE_FAIL) {
$output = '<div class="simplephpform_state_fail">'.$this->message_fail.'</div>';
} if($this->state == self::STATE_ERROR) {
$output = '<div class="simplephpform_state_fail">'.$this->message_error.'</div>';
} if($this->state == self::STATE_DUPLICATE) {
$output = '<div class="simplephpform_state_success">'.$this->message_duplicate.'</div>';
}
return $output."\n";
}
/**
* Display form field.
* @param string Given field name.
* @return string Display in HTML format.
*/
function Display($name = '')
{
// No InputEntry specified? Return them all in the order they were defined.
if($name == '')
{
$output = '';
$output .= $this->DisplayState();
$output .= '<form method="post" action="'.$this->url_action.'" class="simplephpform">';
foreach($this->input_list as $input)
$output .= $this->Display($input->name)."\n";
$output .= '<input type="submit" value="Submit Form" class="simplephpform_submit" />';
$output .= '</form>';
return $output;
}
// Generate output if the specified Form Input exists.
if(array_key_exists($name, $this->input_list))
{
$output = '';
$type = strtolower(trim($this->input_list[$name]->type));
if($type == 'textarea') // Text area.
{
$output .= '<div class="simplephpform_title">'.$this->input_list[$name]->text_title.'</div>'."\n";
$output .= '<textarea name="simplephpform_'.$this->input_list[$name]->name.'" rows="'.$this->input_list[$name]->rows.'" cols="'.$this->input_list[$name]->columns.'">'.$this->input_list[$name]->data.'</textarea>'."\n";
// Helper or error message?
if($this->input_list[$name]->state != self::STATE_FAIL && $this->input_list[$name]->text_error != NULL)
$output .= '<div class="simplephpform_error">'.$this->input_list[$name]->text_error.'</div>'."\n";
else if($this->input_list[$name]->text_help != NULL)
$output .= '<div class="simplephpform_help">'.$this->input_list[$name]->text_help.'</div>'."\n";
}
else if($type == 'dropdown') // Drop down menu.
{
$output .= '<div class="simplephpform_title">'.$this->input_list[$name]->text_title.'</div>'."\n";
$output .= '<select name="simplephpform_'.$this->input_list[$name]->name.'">'."\n";
foreach($this->input_list[$name]->dropdown_entries as $drop_down_value => $drop_down_name)
{
if($this->input_list[$name]->data == $drop_down_value)
$output .= '<option value="'.$drop_down_value.'" selected="selected">'.$drop_down_name.'</option>'."\n";
else
$output .= '<option value="'.$drop_down_value.'">'.$drop_down_name.'</option>'."\n";
}
$output .= '</select>'."\n";
// Helper or error message?
if($this->input_list[$name]->state == self::STATE_FAIL)
$output .= '<div class="simplephpform_error">'.$this->input_list[$name]->text_error.'</div>'."\n";
else if($this->input_list[$name]->text_help != NULL)
$output .= '<div class="simplephpform_help">'.$this->input_list[$name]->text_help.'</div>'."\n";
}
else if($type == 'radio') // Radio button.
{
$output .= '<div class="simplephpform_title">'.$this->input_list[$name]->text_title.'</div><div class="simplephpform_radiobox">'."\n";
foreach($this->input_list[$name]->radio_entries as $radio_name => $radio_value)
{
if($this->input_list[$name]->data == $radio_value)
$output .= '<label><input type="radio" name="simplephpform_'.$this->input_list[$name]->name.'" value="'.$radio_value.'" checked="checked" > '.$radio_name."</label></input>\n";
else
$output .= '<label><input type="radio" name="simplephpform_'.$this->input_list[$name]->name.'" value="'.$radio_value.'" > '.$radio_name."</label></input>\n";
}
$output .= '</div>';
// Helper or error message?
if($this->input_list[$name]->state == self::STATE_FAIL)
$output .= '<div class="simplephpform_error">'.$this->input_list[$name]->text_error.'</div>'."\n";
else if($this->input_list[$name]->text_help != NULL)
$output .= '<div class="simplephpform_help">'.$this->input_list[$name]->text_help.'</div>'."\n";
}
else if($type == 'checkbox') // Check box. Never needs an error message. Will never need an error or info message.
{
$output .= '<div class="simplephpform_title"></div><div style="">'."\n";
if(boolval($this->input_list[$name]->data))
$output .= '<label><input type="'.$this->input_list[$name]->type.'" name="simplephpform_'.$this->input_list[$name]->name.'" checked="checked" />'.$this->input_list[$name]->text_title."</label>\n";
else
$output .= '<label><input type="'.$this->input_list[$name]->type.'" name="simplephpform_'.$this->input_list[$name]->name.'" />'.$this->input_list[$name]->text_title."</label>\n";
$output .= '</div>';
}
else if($type == 'hidden') // Hidden type, for metadata, etc.
{
$output .= '<input type="'.$this->input_list[$name]->type.'" name="simplephpform_'.$this->input_list[$name]->name.'" value="'.$this->input_list[$name]->data.'" />'."\n";
}
else // Default. Textbox, password, etc.
{
$output .= '<div class="simplephpform_title">'.$this->input_list[$name]->text_title.'</div>'."\n";
$output .= '<label><input type="'.$this->input_list[$name]->type.'" name="simplephpform_'.$this->input_list[$name]->name.'" value="'.$this->input_list[$name]->data.'" />'."\n";
if($this->input_list[$name]->state == self::STATE_FAIL)
$output .= '<div class="simplephpform_error">'.$this->input_list[$name]->text_error.'</div>'."\n";
else if($this->input_list[$name]->text_help != NULL)
$output .= '<div class="simplephpform_help">'.$this->input_list[$name]->text_help.'</div>'."\n";
$output .= '</label>';
}
$output .= '<div class="simplephpform_clear"></div>';
return $output;
}
}
/**
* Reset all form data to defaults.
*/
function Reset()
{
foreach($this->input_list as $input)
$input->data = $input->data_default;
}
/**
* Validate form data.
* @return boolean True = Success. False = Failure.
*/
function Validate()
{
// Was this form submitted? Or is this page new?
if($this->state == self::STATE_NEW)
return false; // Invalid by default.
// Set state as successfull first, then run validation test gauntlet ...
$this->state = self::STATE_SUCCESS;
foreach($this->input_list as $input)
{
// Set individual input entry state successful at first, then run validation test gauntlet ...
$input->state = self::STATE_SUCCESS;
// What validation tests need to be run?
for($i = 0; $i < count($input->data_validation_flags); $i += 1)
if(!empty($input->data_validation_flags[$i]))
{
// Sanitize flag by stripping whitespace, and making lowercase.
$flag = strtolower(trim($input->data_validation_flags[$i]));
// *** If we have a test for this flag, run it! ***
// Test: Is the entry required?
if($flag == 'required')
if(!$this->ValidateExists($input->data))
$input->state = self::STATE_FAIL;
// Test: Is the entry an email?
if($flag == 'email')
if(!$this->ValidateEmail($input->data))
$input->state = self::STATE_FAIL;
// Test: Is the entry a phone number?
if($flag == 'phone')
if(!$this->ValidatePhone($input->data))
$input->state = self::STATE_FAIL;
// Test: Is the entry a number?
if($flag == 'number')
if(!$this->ValidateNumber($input->data))
$input->state = self::STATE_FAIL;
// Process multi-part flags.
$flag_parts = explode(' ', $flag);
// Test: Is there a max string length?
if($flag_parts[0] == 'lengthmax')
if(isset($flag_parts[1]))
if(!$this->ValidateLengthMax($input->data, $flag_parts[1]))
$input->state = self::STATE_FAIL;
// Test: Is there a min string length?
if($flag_parts[0] == 'lengthmin')
if(isset($flag_parts[1]))
if(!$this->ValidateLengthMin($input->data, $flag_parts[1]))
$input->state = self::STATE_FAIL;
// Test: Is there a max number size?
if($flag_parts[0] == 'sizemax')
if(isset($flag_parts[1]))
if(!$this->ValidateSizeMax($input->data, $flag_parts[1]))
$input->state = self::STATE_FAIL;
// Test: Is there a min number size?
if($flag_parts[0] == 'sizemin')
if(isset($flag_parts[1]))
if(!$this->ValidateSizeMin($input->data, $flag_parts[1]))
$input->state = self::STATE_FAIL;
}
}
// Did ALL individual input entries validate successfully? If no, set form state to fail.
foreach($this->input_list as $input)
if($input->state == self::STATE_FAIL)
$this->state = self::STATE_FAIL;
// No input entries? Also fail.
if(count($this->input_list) < 1)
$this->state = self::STATE_FAIL;
if($this->state == self::STATE_SUCCESS)
return true;
else
return false;
}
/**
* Validation test. Does the data exist?
* @param string Data.
* @return boolean True = Yes. False = No.
*/
function ValidateExists($data)
{
if($data != '')
return true;
else
return false;
}
/**
* Validation test. Valid email?
* @param string Data.
* @return boolean True = Yes. False = No.
*/
function ValidateEmail($data)
{
if(strlen($data) < 5 || strpos($data, '@') == false || strpos($data, '.') == false || stripos($data, ' ') != false)
return false;
else
return true;
}
/**
* Validation test. Valid phone number?
* @param string Data.
* @return boolean True = Yes. False = No.
*/
function ValidatePhone($data)
{
if(!intval($data) || strlen($data) < 10 || strlen($data) > 30)
return false;
else
return true;
}
/**
* Validation test. Valid number?
* @param string Data.
* @return boolean True = Yes. False = No.
*/
function ValidateNumber($data)
{
if(is_numeric($data))
return true;
else
return false;
}
/**
* Validation test. Valid maximum string length?
* @param string Data.
* @param string Max size allowed.
* @return boolean True = Yes. False = No.
*/
function ValidateLengthMax($data, $size)
{
if(strlen($data) > $size)
return false;
else
return true;
}
/**
* Validation test. Valid minimum string length?
* @param string Data.
* @param string Minimum size allowed.
* @return boolean True = Yes. False = No.
*/
function ValidateLengthMin($data, $size)
{
if(strlen($data) < $size)
return false;
else
return true;
}
/**
* Validation test. Valid maximum number size?
* @param string Data.
* @param string Maximum number allowed.
* @return boolean True = Yes. False = No.
*/
function ValidateSizeMax($data, $size)
{
if(is_numeric($data))
if($data > $size)
return false;
return true;
}
/**
* Validation test. Valid minimum number size?
* @param string Data.
* @param string Minimum number allowed.
* @return boolean True = Yes. False = No.
*/
function ValidateSizeMin($data, $size)
{
if(is_numeric($data))
if($data < $size)
return false;
return true;
}
}
/**
* Used internally by SimplePHPForm to hold field data.
*/
class SimplePHPFormInput
{
var $type = 'text';
var $name = NULL;
var $data = '';
var $data_default = '';
var $data_validation_flags = array();
var $state = SimplePHPForm::STATE_NEW;
var $text_title = '';
var $text_help = '';
var $text_error = '';
// Special variables used for specific input types.
var $rows = 3;
var $columns = 30;
var $dropdown_entries = array();
var $radio_entries = array();
function __construct($type, $name, $data, $data_validation_flags, $data_default, $text_title, $text_help, $text_error)
{
$this->type = $type;
$this->name = $name;
$this->data = $data;
$this->data_default = $data_default;
$this->data_validation_flags = $data_validation_flags;
$this->text_title = $text_title;
$this->text_help = $text_help;
$this->text_error = $text_error;
// If checkbox, $data needs to be true or false.
if($type == 'checkbox')
{
$this->data = boolval($data);
$this->data_default = boolval($data_default);
}
}
}
// For PHP < 5.5.0
if (!function_exists('boolval'))
{
function boolval($in)
{
$out = false;
if(is_string($in))
$in = strtolower($in);
if (in_array($in, array('false', 'no', 'n', '0', 'off', false, 0), true) || !$in)
$out = false;
if (in_array($in, array('true', 'yes', 'y', '1', 'on', true, 1), true))
$out = true;
return $out;
}
}