/
Validator.php
396 lines (356 loc) · 9.49 KB
/
Validator.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
<?php
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao;
/**
* Validates arbitrary data
*
* Usage:
*
* if (Validator::isEmail($recipient))
* {
* $email->sendTo($recipient);
* }
*/
class Validator
{
/**
* Numeric characters (including full stop [.] and minus [-])
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is numeric
*/
public static function isNumeric($varValue)
{
return preg_match('/^-?\d+(\.\d+)?$/', $varValue);
}
/**
* Natural numbers (nonnegative integers)
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a natural number
*/
public static function isNatural($varValue)
{
return preg_match('/^\d+$/', $varValue);
}
/**
* Alphabetic characters (including full stop [.] minus [-] and space [ ])
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is alphabetic
*/
public static function isAlphabetic($varValue)
{
return preg_match('/^[\pL .-]+$/u', $varValue);
}
/**
* Alphanumeric characters (including full stop [.] minus [-], underscore [_] and space [ ])
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is alphanumeric
*/
public static function isAlphanumeric($varValue)
{
return preg_match('/^[\w .-]+$/u', $varValue);
}
/**
* Characters that are usually encoded by the Input class: #<>()\=
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value does not match the characters
*/
public static function isExtendedAlphanumeric($varValue)
{
return !preg_match('/[#<>()\\\\=]/', $varValue);
}
/**
* Valid date formats
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid date format
*/
public static function isDate($varValue)
{
return preg_match('~^' . Date::getRegexp(Date::getNumericDateFormat()) . '$~i', $varValue);
}
/**
* Valid time formats
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid time format
*/
public static function isTime($varValue)
{
return preg_match('~^' . Date::getRegexp(Date::getNumericTimeFormat()) . '$~i', $varValue);
}
/**
* Valid date and time formats
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid date and time format
*/
public static function isDatim($varValue)
{
return preg_match('~^' . Date::getRegexp(Date::getNumericDatimFormat()) . '$~i', $varValue);
}
/**
* Valid e-mail address
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid e-mail address
*/
public static function isEmail($varValue)
{
/*
* The regex below is based on a regex by Michael Rushton adjusted by
* Rasmus Lerdorf to only consider routeable addresses as valid. We
* have also added Unicode support for the local part.
*
* Michael's regex carries this copyright:
*
* Copyright © Michael Rushton 2009-10
* http://squiloople.com/
* Feel free to use and redistribute this code. But please keep this copyright notice.
*
* @see https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L601
*/
return preg_match('/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E\pL\pN]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F\pL\pN]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E\pL\pN]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F\pL\pN]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))]))$/iDu', Idna::encodeEmail($varValue));
}
/**
* Valid URL with special characters allowed (see #6402)
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid URL
*/
public static function isUrl($varValue)
{
try
{
$varValue = Idna::encodeUrl($varValue);
}
catch (\InvalidArgumentException $e)
{
}
return preg_match('/^[\w\/.*+?$#%:,;{}()[\]@&!=~|-]+$/u', $varValue);
}
/**
* Valid alias name
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid alias name
*/
public static function isAlias($varValue)
{
return preg_match('/^[\w.-]+$/u', $varValue);
}
/**
* Valid folder alias name
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid folder alias name
*/
public static function isFolderAlias($varValue)
{
return preg_match('(^(?!/)[\w/.-]+(?<!/)$)u', $varValue);
}
/**
* Valid phone number
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid phone number
*/
public static function isPhone($varValue)
{
return preg_match('/^[+(]?(\d+[ +()\/-]*)+$/', $varValue);
}
/**
* Valid percentage
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid percentage
*/
public static function isPercent($varValue)
{
return is_numeric($varValue) && $varValue >= 0 && $varValue <= 100;
}
/**
* Valid locale
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid locale
*/
public static function isLocale($varValue)
{
return preg_match('/^[a-z]{2}(_[A-Z]{2})?$/', $varValue);
}
/**
* Valid language code
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a valid language code
*/
public static function isLanguage($varValue)
{
return preg_match('/^[a-z]{2}(-[A-Z]{2})?$/', $varValue);
}
/**
* Valid UUID (version 1)
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a UUID
*/
public static function isUuid($varValue)
{
return static::isBinaryUuid($varValue) || static::isStringUuid($varValue);
}
/**
* Valid binary UUID (version 1)
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a binary UUID
*/
public static function isBinaryUuid($varValue)
{
if (\is_string($varValue) && \strlen($varValue) == 16)
{
return ($varValue & hex2bin('000000000000F000C000000000000000')) === hex2bin('00000000000010008000000000000000');
}
return false;
}
/**
* Valid string UUID (version 1)
*
* @param mixed $varValue The value to be validated
*
* @return boolean True if the value is a string UUID
*/
public static function isStringUuid($varValue)
{
if (\is_string($varValue) && \strlen($varValue) == 36)
{
return preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-1[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/', $varValue);
}
return false;
}
/**
* Valid Google+ ID or vanity name
*
* @param mixed $varValue The numeric ID or vanity name
*
* @return boolean True if the value is a Google+ ID
*/
public static function isGooglePlusId($varValue)
{
return preg_match('/^([0-9]{21}|\+[\w-]+)$/u', $varValue);
}
/**
* Insecure path potentially containing directory traversal
*
* @param string $strPath The file path
*
* @return boolean True if the file path is insecure
*/
public static function isInsecurePath($strPath)
{
// Normalize backslashes
$strPath = strtr($strPath, '\\', '/');
$strPath = preg_replace('#//+#', '/', $strPath);
// Equals ..
if ($strPath == '..')
{
return true;
}
// Begins with ./
if (0 === strncmp($strPath, './', 2))
{
return true;
}
// Begins with ../
if (0 === strncmp($strPath, '../', 3))
{
return true;
}
// Ends with /.
if (substr($strPath, -2) == '/.')
{
return true;
}
// Ends with /..
if (substr($strPath, -3) == '/..')
{
return true;
}
// Contains /../
if (strpos($strPath, '/../') !== false)
{
return true;
}
return false;
}
/**
* Valid file name
*
* @param mixed $strName The file name
*
* @return boolean True if the file name is valid
*/
public static function isValidFileName($strName)
{
if (!$strName)
{
return false;
}
// Special characters not supported on e.g. Windows
if (preg_match('@[\\\\/:*?"<>|]@', $strName))
{
return false;
}
// Invisible control characters or unused code points
if (preg_match('/[\pC]/u', $strName) !== 0)
{
return false;
}
// Must not be longer than 255 characters
if (mb_strlen($strName) > 255)
{
return false;
}
return true;
}
/**
* Valid form field name
*
* @param mixed $strName The form field name
*
* @return boolean True if the form field name is valid
*/
public static function isFieldName($strName)
{
return preg_match('/^[A-Za-z0-9[\]_-]+$/', $strName);
}
}
class_alias(Validator::class, 'Validator');