-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathValidate.php
105 lines (99 loc) · 3.85 KB
/
Validate.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
<?php
/*
* PHP Input Validation
* Version: 0.2.0
* Source: https://github.com/kek91/PHP-Input-Validation
*
*/
class Validate
{
private $_passed = false,
$_errors = array();
public function check($source, $items = array())
{
foreach($items as $item => $rules)
{
$value = htmlspecialchars(trim($source[$item]), ENT_QUOTES, 'UTF-8');
foreach($rules as $rule => $rule_value)
{
switch($rule)
{
case 'required':
if(empty($value)) {
$this->add_error("<b>{$item}</b> is required.");
}
break;
case 'length_min':
if(strlen($value) < $rule_value) {
$this->add_error("<b>{$item}</b> must contain minimum <b>{$rule_value}</b> characters");
}
break;
case 'length_max':
if(strlen($value) > $rule_value) {
$this->add_error("<b>{$item}</b> can't contain more than <b>{$rule_value}</b> characters");
}
break;
case 'matches':
if($value != htmlspecialchars(trim($source[$rule_value]), ENT_QUOTES, 'UTF-8')) {
$this->add_error("<b>{$item}</b> must match <b>{$rule_value}</b>");
}
break;
case 'mailcheck':
if(!filter_var($value, FILTER_VALIDATE_EMAIL)) {
$this->add_error("<b>{$value}</b> is not a valid email address");
}
break;
case 'numeric':
if(!ctype_digit(str_replace('+', '', $value))) {
$this->add_error("<b>{$item}</b> contains illegal characters. Only numbers 0-9 and \"+\"-sign are allowed");
}
break;
case 'alphabetic':
if (!ctype_alpha(str_replace(array(' ', "'", '-'), '', $value))) {
$this->add_error("<b>{$item}</b> contains illegal characters. Only alphabetic letters A-Z, \"'\", \" \" and \"-\" are allowed");
}
break;
case 'alphanumeric':
if(!ctype_alnum($value)) {
$this->add_error("<b>{$item}</b> contains illegal characters. Only alphabetic and numeric characters (A-Z and 0-9) are allowed");
}
break;
case 'blacklist':
foreach($rule_value as $blocked_word) {
if($value == $blocked_word) {
$this->add_error("<b>{$value}</b> is blocked");
}
}
break;
case 'whitelist':
foreach($rule_value as $approved_word) {
if($value == $approved_word) {
$match = true;
break;
}
}
if(!$match) {
$this->add_error("<b>{$value}</b> is blocked");
}
}
}
}
if(empty($this->_errors))
{
$this->_passed = true;
}
return $this;
}
private function add_error($error)
{
$this->_errors[] = $error;
}
public function errors()
{
return $this->_errors;
}
public function passed()
{
return $this->_passed;
}
}