-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rule.php
458 lines (442 loc) · 12.6 KB
/
Rule.php
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
<?php
//
// +----------------------------------------------------------------------+
// | Sitellite Content Management System |
// +----------------------------------------------------------------------+
// | Copyright (c) 2010 Sitellite.org Community |
// +----------------------------------------------------------------------+
// | This software is released under the GNU GPL License. |
// | Please see the accompanying file docs/LICENSE for licensing details. |
// | |
// | You should have received a copy of the GNU GPL License |
// | along with this program; if not, visit www.sitellite.org. |
// | The license text is also available at the following web site |
// | address: <http://www.sitellite.org/index/license |
// +----------------------------------------------------------------------+
// | Authors: John Luxford <john.luxford@gmail.com> |
// +----------------------------------------------------------------------+
//
// Handles validation of form fields for the MailForm package.
//
/**
* Handles validation of form fields for the MailForm package.
*
* Rule Format:
*
* type "value"
*
* Validation Rules:
* - is "value"
* - contains "some value"
* - regex "some regex" -- uses ereg()
* - preg "/some regex/" -- uses preg_match()
* - equals "anotherfield"
* - empty
* - length "6+" (eg: 6, 6+, 6-12, 12-)
* - gt "value"
* - ge "value"
* - lt "value"
* - le "value"
* - func "func_name" (or function "func_name")
* - unique "dbtablename.columnname"
* - exists "path/to/directory"
* - numeric
* - email
* - header
*
* Note: Any rule may be negated by preceeding it with a 'not', for example:
* - not empty
* - not contains "some value"
*
* New in 1.2:
* - Added a 'unique' rule, which compares the value against a specified field
* in a database table.
* - Fixed a bug in the 'length' rule evaluation.
*
* New in 1.4:
* - Added 'exists' and 'not exists' rules, which checks if the value given
* exists (or doesn't) as a file name in the path provided by the rule.
*
* New in 1.6:
* - Abstracted 'not empty' and 'not exists' so that 'not' now negates any
* rule, and 'empty' and 'exists' are ordinary rules now. This required
* the addition of two new methods, _validate(), and _validateNegated(),
* and a new $negated property.
*
* New in 1.8:
* - Added warning notices when rules fail the syntax parser.
* - Added new rule type "numeric", which checks the data type of the value
* to see whether it is a valid number or not.
*
* New in 2.0:
* - New rules: 'email' and 'header' which help prevent form abuse by spammers.
* 'email' checks that it is a valid email, and 'header' checks that there
* are no newlines in the field so that it can't pass extra headers to your
* mail() function.
*
* New in 2.2:
* - New rule 'preg' uses preg_match() instead of ereg(). Be sure to include
* slashes in the regular expression now.
* - Custom validation functions can receive the field name as a second parameter.
* e.g. function custom_validation_rule ($values, $field) {}
*
* <code>
* <?php
*
* $widget = new MF_Widget ('name');
* $widget->addRule ('is "foo"', 'You must enter "foo" to pass!');
*
* // note: MailFormRule is never accessed directly.
*
* ? >
* </code>
*
* @package MailForm
* @author John Luxford <john.luxford@gmail.com>
* @license http://www.sitellite.org/index/license GNU GPL License
* @version 2.2, 2008-10-25, $Id: Rule.php,v 1.5 2007/10/06 00:06:30 lux Exp $
* @access public
*
*/
class MailFormRule {
/**
* The original unmodified rule definition.
*
* @access public
*
*/
var $rule;
/**
* The name of the widget.
*
* @access public
*
*/
var $name;
/**
* The error message for this rule.
*
* @access public
*
*/
var $msg;
/**
* The rule type. Can be 'is', 'contains', 'regex', 'not empty',
* 'equals', 'length', 'gt', 'ge', 'lt', 'le', or 'func'.
*
* @access public
*
*/
var $type;
/**
* The rule value. This corresponds to the part of the rule
* in double quotes (ie. type "value").
*
* @access public
*
*/
var $value;
/**
* If a 'not' is present at the start of the rule, this will be
* set to true, otherwise false.
*
* @access public
*
*/
var $negated = false;
/**
* Constructor Method.
*
* @access public
* @param string $rule
* @param string $name
* @param string $msg
*
*/
function MailFormRule ($rule, $name, $msg = '') {
$this->rule = $rule;
$this->name = $name;
if (! empty ($msg)) {
$this->msg = $msg;
} else {
$this->msg = 'Oops! The following field was not filled in correctly: ' . $this->name . '. Please fix this to continue.';
}
$this->parseRuleStatement ($rule);
}
/**
* Parses the original rule into the $type and $value properties.
*
* @access public
* @param string $rule
*
*/
function parseRuleStatement ($rule) {
if (preg_match ('/^(not )?(empty|length|unique|contains|func|function|regex|preg|equals|is|gt|lt|ge|le|exists|numeric|alpha|email|header|ext)( ("|\')(.*)\4)?$/', $rule, $regs)) {
if ($regs[2] == 'function') {
$this->type = 'func';
} else {
$this->type = $regs[2];
}
if ($regs[1] == 'not ') {
$this->negated = true;
}
$this->value = $regs[5];
} else {
trigger_error ('Invalid MailForm rule (' . $this->name . '): ' . $rule, E_USER_WARNING);
}
}
/**
* Validates the value given against itself. Returns false on
* failure and true on success.
*
* @access public
* @param string $value
* @param object $form
* @param object $cgi
* @return boolean
*
*/
function validate ($value, $form, $cgi) {
if ($this->negated) {
return $this->_validateNegated ($value, $form, $cgi);
} else {
return $this->_validate ($value, $form, $cgi);
}
}
/**
* Validates the value given against itself. Returns false on
* failure and true on success.
*
* @access public
* @param string $value
* @param object $form
* @param object $cgi
* @return boolean
*
*/
function _validate ($value, $form, $cgi) {
if ($this->type == 'empty') {
if (! empty ($value)) {
return false;
}
} elseif ($this->type == 'length') {
if (preg_match ('/^([0-9]+)([+-]?)([0-9]*)$/', $this->value, $regs)) {
if (! empty ($regs[3])) {
if (strlen ($value) < $regs[1] || strlen ($value) > $regs[3]) {
return false;
}
} elseif ($regs[2] == '+' && strlen ($value) < $regs[1]) {
return false;
} elseif ($regs[2] == '-' && strlen ($value) > $regs[1]) {
return false;
} elseif (empty ($regs[2]) && strlen ($value) != $regs[1]) {
return false;
}
}
} elseif ($this->type == 'unique') {
list ($table, $column) = preg_split ('/[\.:\/]/', $this->value);
global $db;
$res = $db->fetch ('select ' . $column . ' from ' . $table . ' where ' . $column . ' = ??', $value);
if ($db->rows > 0) {
// it's not unique
return false;
} elseif (! $res && $db->error) {
return false;
}
} elseif ($this->type == 'exists') {
if (! empty ($value)) {
if (! @file_exists ($this->value . '/' . $value)) {
// file name does not exist
return false;
}
}
} elseif ($this->type == 'contains') {
if (! stristr ($value, $this->value)) {
return false;
}
} elseif ($this->type == 'func') {
$GLOBALS['mailform_current_form'] =& $form;
$func = $this->value;
if (! $func ($form->getValues ($cgi), $this->name)) {
return false;
}
} elseif ($this->type == 'regex') {
// Fix for issue #185
// This MIGHT introduce a new bug
$new_regex = "/" . $this->value . "/";
if (! preg_match ($new_regex, $value)) {
return false;
}
} elseif ($this->type == 'preg') {
if (! preg_match ($this->value, $value)) {
return false;
}
} elseif ($this->type == 'equals') {
if ($value != $form->widgets[$this->value]->getValue ($cgi)) {
return false;
}
} elseif ($this->type == 'is') {
if ($value != $this->value) {
return false;
}
} elseif ($this->type == 'gt') {
if ($value <= $this->value) {
return false;
}
} elseif ($this->type == 'lt') {
if ($value >= $this->value) {
return false;
}
} elseif ($this->type == 'ge') {
if ($value < $this->value) {
return false;
}
} elseif ($this->type == 'le') {
if ($value > $this->value) {
return false;
}
} elseif ($this->type == 'numeric') {
if (! is_numeric ($value)) {
return false;
}
} elseif ($this->type == 'alpha') {
if (!preg_match("/^([a-zA-Z]*)$/",$value)) {
return false;
}
} elseif ($this->type == 'email') {
if (strpos ($value, '.@') !== false) {
return false;
} elseif (preg_match ('/\.$/', $value)) {
return false;
} elseif (! preg_match ("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+\.([a-zA-Z0-9\._-]+)+$/" , $value)) {
return false;
}
} elseif ($this->type == 'header') {
if (preg_match ("/[\r\n]/s" , $value)) {
return false;
}
}
elseif ($this->type == 'ext') {
$compare = end(explode(".", $value->name));
if ($compare != $this->value && !empty($compare)) {
unset($compare);
return false;
}
}
return true;
}
/**
* Validates the value given against itself. Returns false on
* failure and true on success.
*
* @access public
* @param string $value
* @param object $form
* @param object $cgi
* @return boolean
*
*/
function _validateNegated ($value, $form, $cgi) {
if ($this->type == 'empty') {
if (empty ($value)) {
return false;
}
} elseif ($this->type == 'length') {
if (preg_match ('/^([0-9]+)([+-]?)([0-9]*)$/', $this->value, $regs)) {
if (! empty ($regs[3])) {
if (strlen ($value) >= $regs[1] || strlen ($value) <= $regs[3]) {
return false;
}
} elseif ($regs[2] == '+' && strlen ($value) >= $regs[1]) {
return false;
} elseif ($regs[2] == '-' && strlen ($value) <= $regs[1]) {
return false;
} elseif (empty ($regs[2]) && strlen ($value) == $regs[1]) {
return false;
}
}
} elseif ($this->type == 'unique') {
list ($table, $column) = preg_split ('/[\.:\/]/', $this->value);
global $db;
$res = $db->fetch ('select ' . $column . ' from ' . $table . ' where ' . $column . ' = ??', $value);
if ($db->rows <= 0) {
// it's not unique
return false;
} elseif (! $res && $db->error) {
return false;
}
} elseif ($this->type == 'exists') {
if (! empty ($value)) {
if (@file_exists ($this->value . '/' . $value)) {
// file name does not exist
return false;
}
}
} elseif ($this->type == 'contains') {
if (stristr ($value, $this->value)) {
return false;
}
} elseif ($this->type == 'func') {
$func = $this->value;
if ($func ($form->getValues ($cgi), $this->name)) {
return false;
}
} elseif ($this->type == 'regex') {
if (preg_match ($this->value, $value)) {
return false;
}
} elseif ($this->type == 'preg') {
if (preg_match ($this->value, $value)) {
return false;
}
} elseif ($this->type == 'equals') {
if ($value == $form->widgets[$this->value]->getValue ($cgi)) {
return false;
}
} elseif ($this->type == 'is') {
if ($value == $this->value) {
return false;
}
} elseif ($this->type == 'gt') {
if ($value > $this->value) {
return false;
}
} elseif ($this->type == 'lt') {
if ($value < $this->value) {
return false;
}
} elseif ($this->type == 'ge') {
if ($value >= $this->value) {
return false;
}
} elseif ($this->type == 'le') {
if ($value <= $this->value) {
return false;
}
} elseif ($this->type == 'numeric') {
if (is_numeric ($value)) {
return false;
}
} elseif ($this->type == 'alpha') {
if (preg_match("/^([a-zA-Z]*)$/",$value)) {
return false;
}
} elseif ($this->type == 'email') {
if (strpos ($value, '.@') === false && ! preg_match ('/\.$/', $value) && preg_match ("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+\.([a-zA-Z0-9\._-]+)+$/" , $value)) {
return false;
}
} elseif ($this->type == 'header') {
if (! preg_match ("/[\r\n]/s" , $value)) {
return false;
}
} elseif ($this->type == 'ext') {
$compare = end(explode(".", $value->name));
if ($compare == $this->value && !empty($compare)) {
unset($compare);
return false;
}
}
return true;
}
}
?>