-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.php
More file actions
144 lines (136 loc) · 5.36 KB
/
validate.php
File metadata and controls
144 lines (136 loc) · 5.36 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
<?php
function validate($type, &$value, $convert = true, array $options = [], $identifier = '<no-id-given>')
{
/* check accepted values */
$list = tool_call_simple($options, 'accept');
if ($list !== null) {
if (!is_array($list)) {
log_error('Invalid validator description, guard "accept" should be an array, {0} received, debug identifier: {1}', [gettype($list), $identifier]);
return false;
}
if (!in_array($value, $list)) {
return false;
}
}
/* check min and max */
$min = tool_call_simple($options, 'min');
if (is_numeric($min)) {
if (validate_has_type($type, 'string')) {
/* need nested if's here so next numeric if won't mix up things */
if (strlen($value) < $min) {
return false;
}
} else if (is_numeric($value) && $value < $min) {
return false;
}
} else if ($min !== null) {
log_error('Invalid validator description, guard "min" should be a number, {0} received, debug identifier: {1}', [gettype($min), $identifier]);
return false;
}
$max = tool_call_simple($options, 'max');
if (is_numeric($max)) {
if (validate_has_type($type, 'string')) {
/* need nested if's here so next numeric if won't mix up things */
if (strlen($value) > $max) {
return false;
}
} else if (is_numeric($value) && $value > $max) {
return false;
}
} else if ($max !== null) {
log_error('Invalid validator description, guard "max" should be a number, {0} received, debug identifier: {1}', [gettype($max), $identifier]);
return false;
}
/* check type */
if (validate_has_type($type, 'string') && is_string($value)) {
return true;
} else if (validate_has_type($type, 'int') && filter_var($value, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL) !== false) {
/* allow "octal" so that string starting with zero is accepted */
$value = $convert ? intval($value) : $value;
return true;
} else if (validate_has_type($type, 'float') && filter_var($value, FILTER_VALIDATE_FLOAT) !== false) {
$value = $convert ? floatval($value) : $value;
return true;
} else if (validate_has_type($type, 'number') && is_numeric($value)) {
$value = $convert ? floatval($value) : $value;
return true;
} else if (validate_has_type($type, 'bool') && is_bool($value)) {
return true;
} else if (validate_has_type($type, 'null') && is_null($value)) {
return true;
} else if (validate_has_type($type, 'array') && is_array($value)) {
return true;
} else if (validate_has_type($type, 'object') && is_object($value)) {
return true;
} else if (validate_has_type($type, 'email') && filter_var($value, FILTER_VALIDATE_EMAIL)) {
return true;
} else if (validate_has_type($type, 'ip') && filter_var($value, FILTER_VALIDATE_IP)) {
return true;
} else if (validate_has_type($type, 'ipv4') && filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return true;
} else if (validate_has_type($type, 'ipv6') && filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
return true;
} else if (validate_has_type($type, 'url') && filter_var($value, FILTER_VALIDATE_URL)) {
return true;
} else if (validate_has_type($type, 'datetime')) {
if (is_a($value, 'DateTime')) {
return true;
}
if (!is_string($value)) {
return false;
}
/* optional default timezone if timezone is not specified in value */
$timezone = tool_call_simple($options, 'timezone');
if (is_string($timezone)) {
$timezone = new DateTimeZone($timezone);
}
/* check for a valid datetime value */
$t = date_create($value, $timezone);
if ($t !== false) {
$value = $convert ? $t : $value;
return true;
}
return false;
} else if (validate_has_type($type, 'timestamp')) {
/* allow "octal" so that string starting with zero is accepted */
$v = filter_var($value, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_OCTAL);
if ($v === false) {
return false;
}
$v = date_create('@' . $v);
if ($v !== false) {
$value = $convert ? $v : $value;
return true;
}
return false;
} else if (validate_has_type($type, 'fqdn') && validate_fqdn($value) !== false) {
return true;
} else if (validate_has_type($type, 'fqdn-wildcard') && validate_fqdn($value, true) !== false) {
return true;
}
return false;
}
function validate_fqdn($domain, $allow_wildcard = false)
{
if (!is_string($domain)) {
return false;
}
if ($allow_wildcard and substr($domain, 0, 2) == '*.') {
$domain = substr($domain, 2);
}
$pattern = '/(?=^.{1,254}$)(^(?:(?!\d|-)[a-zA-Z0-9\-_]{1,63}(?<!-)\.?)+(?:[a-zA-Z]{2,})$)/i';
if (!strpbrk($domain, '.')) {
return false;
}
return !empty($domain) && preg_match($pattern, $domain) > 0;
}
function validate_has_type($types, string $has_type)
{
if (empty($types)) {
return $has_type == 'string';
}
if (!is_array($types)) {
$types = explode('|', $types);
}
return in_array($has_type, $types);
}