1 | <?php |
---|
2 | /** |
---|
3 | * Validation Class. Used for validation of model data |
---|
4 | * |
---|
5 | * PHP versions 4 and 5 |
---|
6 | * |
---|
7 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
---|
8 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
9 | * |
---|
10 | * Licensed under The MIT License |
---|
11 | * Redistributions of files must retain the above copyright notice. |
---|
12 | * |
---|
13 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
14 | * @link http://cakephp.org CakePHP(tm) Project |
---|
15 | * @package cake |
---|
16 | * @subpackage cake.cake.libs |
---|
17 | * @since CakePHP(tm) v 1.2.0.3830 |
---|
18 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
---|
19 | */ |
---|
20 | if (!class_exists('Multibyte')) { |
---|
21 | App::import('Core', 'Multibyte', false); |
---|
22 | } |
---|
23 | /** |
---|
24 | * Offers different validation methods. |
---|
25 | * |
---|
26 | * @package cake |
---|
27 | * @subpackage cake.cake.libs |
---|
28 | * @since CakePHP v 1.2.0.3830 |
---|
29 | */ |
---|
30 | class Validation extends Object { |
---|
31 | |
---|
32 | /** |
---|
33 | * Set the value of methods $check param. |
---|
34 | * |
---|
35 | * @var string |
---|
36 | * @access public |
---|
37 | */ |
---|
38 | var $check = null; |
---|
39 | |
---|
40 | /** |
---|
41 | * Set to a valid regular expression in the class methods. |
---|
42 | * Can be set from $regex param also |
---|
43 | * |
---|
44 | * @var string |
---|
45 | * @access public |
---|
46 | */ |
---|
47 | var $regex = null; |
---|
48 | |
---|
49 | /** |
---|
50 | * Some complex patterns needed in multiple places |
---|
51 | * |
---|
52 | * @var array |
---|
53 | * @access private |
---|
54 | */ |
---|
55 | var $__pattern = array( |
---|
56 | 'hostname' => '(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)' |
---|
57 | ); |
---|
58 | |
---|
59 | /** |
---|
60 | * Some class methods use a country to determine proper validation. |
---|
61 | * This can be passed to methods in the $country param |
---|
62 | * |
---|
63 | * @var string |
---|
64 | * @access public |
---|
65 | */ |
---|
66 | var $country = null; |
---|
67 | |
---|
68 | /** |
---|
69 | * Some class methods use a deeper validation when set to true |
---|
70 | * |
---|
71 | * @var string |
---|
72 | * @access public |
---|
73 | */ |
---|
74 | var $deep = null; |
---|
75 | |
---|
76 | /** |
---|
77 | * Some class methods use the $type param to determine which validation to perfom in the method |
---|
78 | * |
---|
79 | * @var string |
---|
80 | * @access public |
---|
81 | */ |
---|
82 | var $type = null; |
---|
83 | |
---|
84 | /** |
---|
85 | * Holds an array of errors messages set in this class. |
---|
86 | * These are used for debugging purposes |
---|
87 | * |
---|
88 | * @var array |
---|
89 | * @access public |
---|
90 | */ |
---|
91 | var $errors = array(); |
---|
92 | |
---|
93 | /** |
---|
94 | * Gets a reference to the Validation object instance |
---|
95 | * |
---|
96 | * @return object Validation instance |
---|
97 | * @access public |
---|
98 | * @static |
---|
99 | */ |
---|
100 | function &getInstance() { |
---|
101 | static $instance = array(); |
---|
102 | |
---|
103 | if (!$instance) { |
---|
104 | $instance[0] =& new Validation(); |
---|
105 | } |
---|
106 | return $instance[0]; |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * Checks that a string contains something other than whitespace |
---|
111 | * |
---|
112 | * Returns true if string contains something other than whitespace |
---|
113 | * |
---|
114 | * $check can be passed as an array: |
---|
115 | * array('check' => 'valueToCheck'); |
---|
116 | * |
---|
117 | * @param mixed $check Value to check |
---|
118 | * @return boolean Success |
---|
119 | * @access public |
---|
120 | */ |
---|
121 | function notEmpty($check) { |
---|
122 | $_this =& Validation::getInstance(); |
---|
123 | $_this->__reset(); |
---|
124 | $_this->check = $check; |
---|
125 | |
---|
126 | if (is_array($check)) { |
---|
127 | $_this->_extract($check); |
---|
128 | } |
---|
129 | |
---|
130 | if (empty($_this->check) && $_this->check != '0') { |
---|
131 | return false; |
---|
132 | } |
---|
133 | $_this->regex = '/[^\s]+/m'; |
---|
134 | return $_this->_check(); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Checks that a string contains only integer or letters |
---|
139 | * |
---|
140 | * Returns true if string contains only integer or letters |
---|
141 | * |
---|
142 | * $check can be passed as an array: |
---|
143 | * array('check' => 'valueToCheck'); |
---|
144 | * |
---|
145 | * @param mixed $check Value to check |
---|
146 | * @return boolean Success |
---|
147 | * @access public |
---|
148 | */ |
---|
149 | function alphaNumeric($check) { |
---|
150 | $_this =& Validation::getInstance(); |
---|
151 | $_this->__reset(); |
---|
152 | $_this->check = $check; |
---|
153 | |
---|
154 | if (is_array($check)) { |
---|
155 | $_this->_extract($check); |
---|
156 | } |
---|
157 | |
---|
158 | if (empty($_this->check) && $_this->check != '0') { |
---|
159 | return false; |
---|
160 | } |
---|
161 | $_this->regex = '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu'; |
---|
162 | return $_this->_check(); |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Checks that a string length is within s specified range. |
---|
167 | * Spaces are included in the character count. |
---|
168 | * Returns true is string matches value min, max, or between min and max, |
---|
169 | * |
---|
170 | * @param string $check Value to check for length |
---|
171 | * @param integer $min Minimum value in range (inclusive) |
---|
172 | * @param integer $max Maximum value in range (inclusive) |
---|
173 | * @return boolean Success |
---|
174 | * @access public |
---|
175 | */ |
---|
176 | function between($check, $min, $max) { |
---|
177 | $length = mb_strlen($check); |
---|
178 | return ($length >= $min && $length <= $max); |
---|
179 | } |
---|
180 | |
---|
181 | /** |
---|
182 | * Returns true if field is left blank -OR- only whitespace characters are present in it's value |
---|
183 | * Whitespace characters include Space, Tab, Carriage Return, Newline |
---|
184 | * |
---|
185 | * $check can be passed as an array: |
---|
186 | * array('check' => 'valueToCheck'); |
---|
187 | * |
---|
188 | * @param mixed $check Value to check |
---|
189 | * @return boolean Success |
---|
190 | * @access public |
---|
191 | */ |
---|
192 | function blank($check) { |
---|
193 | $_this =& Validation::getInstance(); |
---|
194 | $_this->__reset(); |
---|
195 | $_this->check = $check; |
---|
196 | |
---|
197 | if (is_array($check)) { |
---|
198 | $_this->_extract($check); |
---|
199 | } |
---|
200 | |
---|
201 | $_this->regex = '/[^\\s]/'; |
---|
202 | return !$_this->_check(); |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Validation of credit card numbers. |
---|
207 | * Returns true if $check is in the proper credit card format. |
---|
208 | * |
---|
209 | * @param mixed $check credit card number to validate |
---|
210 | * @param mixed $type 'all' may be passed as a sting, defaults to fast which checks format of most major credit cards |
---|
211 | * if an array is used only the values of the array are checked. |
---|
212 | * Example: array('amex', 'bankcard', 'maestro') |
---|
213 | * @param boolean $deep set to true this will check the Luhn algorithm of the credit card. |
---|
214 | * @param string $regex A custom regex can also be passed, this will be used instead of the defined regex values |
---|
215 | * @return boolean Success |
---|
216 | * @access public |
---|
217 | * @see Validation::_luhn() |
---|
218 | */ |
---|
219 | function cc($check, $type = 'fast', $deep = false, $regex = null) { |
---|
220 | $_this =& Validation::getInstance(); |
---|
221 | $_this->__reset(); |
---|
222 | $_this->check = $check; |
---|
223 | $_this->type = $type; |
---|
224 | $_this->deep = $deep; |
---|
225 | $_this->regex = $regex; |
---|
226 | |
---|
227 | if (is_array($check)) { |
---|
228 | $_this->_extract($check); |
---|
229 | } |
---|
230 | $_this->check = str_replace(array('-', ' '), '', $_this->check); |
---|
231 | |
---|
232 | if (mb_strlen($_this->check) < 13) { |
---|
233 | return false; |
---|
234 | } |
---|
235 | |
---|
236 | if (!is_null($_this->regex)) { |
---|
237 | if ($_this->_check()) { |
---|
238 | return $_this->_luhn(); |
---|
239 | } |
---|
240 | } |
---|
241 | $cards = array( |
---|
242 | 'all' => array( |
---|
243 | 'amex' => '/^3[4|7]\\d{13}$/', |
---|
244 | 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/', |
---|
245 | 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/', |
---|
246 | 'disc' => '/^(?:6011|650\\d)\\d{12}$/', |
---|
247 | 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/', |
---|
248 | 'enroute' => '/^2(?:014|149)\\d{11}$/', |
---|
249 | 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/', |
---|
250 | 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/', |
---|
251 | 'mc' => '/^5[1-5]\\d{14}$/', |
---|
252 | 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/', |
---|
253 | 'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/', |
---|
254 | 'visa' => '/^4\\d{12}(\\d{3})?$/', |
---|
255 | 'voyager' => '/^8699[0-9]{11}$/' |
---|
256 | ), |
---|
257 | 'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/' |
---|
258 | ); |
---|
259 | |
---|
260 | if (is_array($_this->type)) { |
---|
261 | foreach ($_this->type as $value) { |
---|
262 | $_this->regex = $cards['all'][strtolower($value)]; |
---|
263 | |
---|
264 | if ($_this->_check()) { |
---|
265 | return $_this->_luhn(); |
---|
266 | } |
---|
267 | } |
---|
268 | } elseif ($_this->type == 'all') { |
---|
269 | foreach ($cards['all'] as $value) { |
---|
270 | $_this->regex = $value; |
---|
271 | |
---|
272 | if ($_this->_check()) { |
---|
273 | return $_this->_luhn(); |
---|
274 | } |
---|
275 | } |
---|
276 | } else { |
---|
277 | $_this->regex = $cards['fast']; |
---|
278 | |
---|
279 | if ($_this->_check()) { |
---|
280 | return $_this->_luhn(); |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | /** |
---|
286 | * Used to compare 2 numeric values. |
---|
287 | * |
---|
288 | * @param mixed $check1 if string is passed for a string must also be passed for $check2 |
---|
289 | * used as an array it must be passed as array('check1' => value, 'operator' => 'value', 'check2' -> value) |
---|
290 | * @param string $operator Can be either a word or operand |
---|
291 | * is greater >, is less <, greater or equal >= |
---|
292 | * less or equal <=, is less <, equal to ==, not equal != |
---|
293 | * @param integer $check2 only needed if $check1 is a string |
---|
294 | * @return boolean Success |
---|
295 | * @access public |
---|
296 | */ |
---|
297 | function comparison($check1, $operator = null, $check2 = null) { |
---|
298 | if (is_array($check1)) { |
---|
299 | extract($check1, EXTR_OVERWRITE); |
---|
300 | } |
---|
301 | $operator = str_replace(array(' ', "\t", "\n", "\r", "\0", "\x0B"), '', strtolower($operator)); |
---|
302 | |
---|
303 | switch ($operator) { |
---|
304 | case 'isgreater': |
---|
305 | case '>': |
---|
306 | if ($check1 > $check2) { |
---|
307 | return true; |
---|
308 | } |
---|
309 | break; |
---|
310 | case 'isless': |
---|
311 | case '<': |
---|
312 | if ($check1 < $check2) { |
---|
313 | return true; |
---|
314 | } |
---|
315 | break; |
---|
316 | case 'greaterorequal': |
---|
317 | case '>=': |
---|
318 | if ($check1 >= $check2) { |
---|
319 | return true; |
---|
320 | } |
---|
321 | break; |
---|
322 | case 'lessorequal': |
---|
323 | case '<=': |
---|
324 | if ($check1 <= $check2) { |
---|
325 | return true; |
---|
326 | } |
---|
327 | break; |
---|
328 | case 'equalto': |
---|
329 | case '==': |
---|
330 | if ($check1 == $check2) { |
---|
331 | return true; |
---|
332 | } |
---|
333 | break; |
---|
334 | case 'notequal': |
---|
335 | case '!=': |
---|
336 | if ($check1 != $check2) { |
---|
337 | return true; |
---|
338 | } |
---|
339 | break; |
---|
340 | default: |
---|
341 | $_this =& Validation::getInstance(); |
---|
342 | $_this->errors[] = __('You must define the $operator parameter for Validation::comparison()', true); |
---|
343 | break; |
---|
344 | } |
---|
345 | return false; |
---|
346 | } |
---|
347 | |
---|
348 | /** |
---|
349 | * Used when a custom regular expression is needed. |
---|
350 | * |
---|
351 | * @param mixed $check When used as a string, $regex must also be a valid regular expression. |
---|
352 | * As and array: array('check' => value, 'regex' => 'valid regular expression') |
---|
353 | * @param string $regex If $check is passed as a string, $regex must also be set to valid regular expression |
---|
354 | * @return boolean Success |
---|
355 | * @access public |
---|
356 | */ |
---|
357 | function custom($check, $regex = null) { |
---|
358 | $_this =& Validation::getInstance(); |
---|
359 | $_this->__reset(); |
---|
360 | $_this->check = $check; |
---|
361 | $_this->regex = $regex; |
---|
362 | if (is_array($check)) { |
---|
363 | $_this->_extract($check); |
---|
364 | } |
---|
365 | if ($_this->regex === null) { |
---|
366 | $_this->errors[] = __('You must define a regular expression for Validation::custom()', true); |
---|
367 | return false; |
---|
368 | } |
---|
369 | return $_this->_check(); |
---|
370 | } |
---|
371 | |
---|
372 | /** |
---|
373 | * Date validation, determines if the string passed is a valid date. |
---|
374 | * keys that expect full month, day and year will validate leap years |
---|
375 | * |
---|
376 | * @param string $check a valid date string |
---|
377 | * @param mixed $format Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc) |
---|
378 | * Keys: dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash |
---|
379 | * mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash |
---|
380 | * ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash |
---|
381 | * dMy 27 December 2006 or 27 Dec 2006 |
---|
382 | * Mdy December 27, 2006 or Dec 27, 2006 comma is optional |
---|
383 | * My December 2006 or Dec 2006 |
---|
384 | * my 12/2006 separators can be a space, period, dash, forward slash |
---|
385 | * @param string $regex If a custom regular expression is used this is the only validation that will occur. |
---|
386 | * @return boolean Success |
---|
387 | * @access public |
---|
388 | */ |
---|
389 | function date($check, $format = 'ymd', $regex = null) { |
---|
390 | $_this =& Validation::getInstance(); |
---|
391 | $_this->__reset(); |
---|
392 | $_this->check = $check; |
---|
393 | $_this->regex = $regex; |
---|
394 | |
---|
395 | if (!is_null($_this->regex)) { |
---|
396 | return $_this->_check(); |
---|
397 | } |
---|
398 | |
---|
399 | $regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%'; |
---|
400 | $regex['mdy'] = '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%'; |
---|
401 | $regex['ymd'] = '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))))$%'; |
---|
402 | $regex['dMy'] = '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})$/'; |
---|
403 | $regex['Mdy'] = '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep)(tember)?|(Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/'; |
---|
404 | $regex['My'] = '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]|[2-9]\\d)\\d{2})$%'; |
---|
405 | $regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%'; |
---|
406 | |
---|
407 | $format = (is_array($format)) ? array_values($format) : array($format); |
---|
408 | foreach ($format as $key) { |
---|
409 | $_this->regex = $regex[$key]; |
---|
410 | |
---|
411 | if ($_this->_check() === true) { |
---|
412 | return true; |
---|
413 | } |
---|
414 | } |
---|
415 | return false; |
---|
416 | } |
---|
417 | |
---|
418 | /** |
---|
419 | * Time validation, determines if the string passed is a valid time. |
---|
420 | * Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m) |
---|
421 | * Does not allow/validate seconds. |
---|
422 | * |
---|
423 | * @param string $check a valid time string |
---|
424 | * @return boolean Success |
---|
425 | * @access public |
---|
426 | */ |
---|
427 | |
---|
428 | function time($check) { |
---|
429 | $_this =& Validation::getInstance(); |
---|
430 | $_this->__reset(); |
---|
431 | $_this->check = $check; |
---|
432 | $_this->regex = '%^((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M|[ap]m))$|^([01]\d|2[0-3])(:[0-5]\d){0,2}$%'; |
---|
433 | return $_this->_check(); |
---|
434 | } |
---|
435 | |
---|
436 | /** |
---|
437 | * Boolean validation, determines if value passed is a boolean integer or true/false. |
---|
438 | * |
---|
439 | * @param string $check a valid boolean |
---|
440 | * @return boolean Success |
---|
441 | * @access public |
---|
442 | */ |
---|
443 | function boolean($check) { |
---|
444 | $booleanList = array(0, 1, '0', '1', true, false); |
---|
445 | return in_array($check, $booleanList, true); |
---|
446 | } |
---|
447 | |
---|
448 | /** |
---|
449 | * Checks that a value is a valid decimal. If $places is null, the $check is allowed to be a scientific float |
---|
450 | * If no decimal point is found a false will be returned. Both the sign and exponent are optional. |
---|
451 | * |
---|
452 | * @param integer $check The value the test for decimal |
---|
453 | * @param integer $places if set $check value must have exactly $places after the decimal point |
---|
454 | * @param string $regex If a custom regular expression is used this is the only validation that will occur. |
---|
455 | * @return boolean Success |
---|
456 | * @access public |
---|
457 | */ |
---|
458 | function decimal($check, $places = null, $regex = null) { |
---|
459 | $_this =& Validation::getInstance(); |
---|
460 | $_this->__reset(); |
---|
461 | $_this->regex = $regex; |
---|
462 | $_this->check = $check; |
---|
463 | |
---|
464 | if (is_null($_this->regex)) { |
---|
465 | if (is_null($places)) { |
---|
466 | $_this->regex = '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/'; |
---|
467 | } else { |
---|
468 | $_this->regex = '/^[-+]?[0-9]*\\.{1}[0-9]{'.$places.'}$/'; |
---|
469 | } |
---|
470 | } |
---|
471 | return $_this->_check(); |
---|
472 | } |
---|
473 | |
---|
474 | /** |
---|
475 | * Validates for an email address. |
---|
476 | * |
---|
477 | * @param string $check Value to check |
---|
478 | * @param boolean $deep Perform a deeper validation (if true), by also checking availability of host |
---|
479 | * @param string $regex Regex to use (if none it will use built in regex) |
---|
480 | * @return boolean Success |
---|
481 | * @access public |
---|
482 | */ |
---|
483 | function email($check, $deep = false, $regex = null) { |
---|
484 | $_this =& Validation::getInstance(); |
---|
485 | $_this->__reset(); |
---|
486 | $_this->check = $check; |
---|
487 | $_this->regex = $regex; |
---|
488 | $_this->deep = $deep; |
---|
489 | |
---|
490 | if (is_array($check)) { |
---|
491 | $_this->_extract($check); |
---|
492 | } |
---|
493 | |
---|
494 | if (is_null($_this->regex)) { |
---|
495 | $_this->regex = '/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@' . $_this->__pattern['hostname'] . '$/i'; |
---|
496 | } |
---|
497 | $return = $_this->_check(); |
---|
498 | |
---|
499 | if ($_this->deep === false || $_this->deep === null) { |
---|
500 | return $return; |
---|
501 | } |
---|
502 | |
---|
503 | if ($return === true && preg_match('/@(' . $_this->__pattern['hostname'] . ')$/i', $_this->check, $regs)) { |
---|
504 | if (function_exists('getmxrr') && getmxrr($regs[1], $mxhosts)) { |
---|
505 | return true; |
---|
506 | } |
---|
507 | if (function_exists('checkdnsrr') && checkdnsrr($regs[1], 'MX')) { |
---|
508 | return true; |
---|
509 | } |
---|
510 | return is_array(gethostbynamel($regs[1])); |
---|
511 | } |
---|
512 | return false; |
---|
513 | } |
---|
514 | |
---|
515 | /** |
---|
516 | * Check that value is exactly $comparedTo. |
---|
517 | * |
---|
518 | * @param mixed $check Value to check |
---|
519 | * @param mixed $comparedTo Value to compare |
---|
520 | * @return boolean Success |
---|
521 | * @access public |
---|
522 | */ |
---|
523 | function equalTo($check, $comparedTo) { |
---|
524 | return ($check === $comparedTo); |
---|
525 | } |
---|
526 | |
---|
527 | /** |
---|
528 | * Check that value has a valid file extension. |
---|
529 | * |
---|
530 | * @param mixed $check Value to check |
---|
531 | * @param array $extensions file extenstions to allow |
---|
532 | * @return boolean Success |
---|
533 | * @access public |
---|
534 | */ |
---|
535 | function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) { |
---|
536 | if (is_array($check)) { |
---|
537 | return Validation::extension(array_shift($check), $extensions); |
---|
538 | } |
---|
539 | $extension = strtolower(array_pop(explode('.', $check))); |
---|
540 | foreach ($extensions as $value) { |
---|
541 | if ($extension == strtolower($value)) { |
---|
542 | return true; |
---|
543 | } |
---|
544 | } |
---|
545 | return false; |
---|
546 | } |
---|
547 | |
---|
548 | /** |
---|
549 | * Validation of an IP address. |
---|
550 | * |
---|
551 | * Valid IP version strings for type restriction are: |
---|
552 | * - both: Check both IPv4 and IPv6, return true if the supplied address matches either version |
---|
553 | * - IPv4: Version 4 (Eg: 127.0.0.1, 192.168.10.123, 203.211.24.8) |
---|
554 | * - IPv6: Version 6 (Eg: ::1, 2001:0db8::1428:57ab) |
---|
555 | * |
---|
556 | * @param string $check The string to test. |
---|
557 | * @param string $type The IP Version to test against |
---|
558 | * @return boolean Success |
---|
559 | * @access public |
---|
560 | */ |
---|
561 | function ip($check, $type = 'both') { |
---|
562 | $_this =& Validation::getInstance(); |
---|
563 | $success = false; |
---|
564 | $type = strtolower($type); |
---|
565 | if ($type === 'ipv4' || $type === 'both') { |
---|
566 | $success |= $_this->_ipv4($check); |
---|
567 | } |
---|
568 | if ($type === 'ipv6' || $type === 'both') { |
---|
569 | $success |= $_this->_ipv6($check); |
---|
570 | } |
---|
571 | return $success; |
---|
572 | } |
---|
573 | |
---|
574 | /** |
---|
575 | * Validation of IPv4 addresses. |
---|
576 | * |
---|
577 | * @param string $check IP Address to test |
---|
578 | * @return boolean Success |
---|
579 | * @access protected |
---|
580 | */ |
---|
581 | function _ipv4($check) { |
---|
582 | if (function_exists('filter_var')) { |
---|
583 | return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV4)) !== false; |
---|
584 | } |
---|
585 | $this->__populateIp(); |
---|
586 | $this->check = $check; |
---|
587 | $this->regex = '/^' . $this->__pattern['IPv4'] . '$/'; |
---|
588 | return $this->_check(); |
---|
589 | } |
---|
590 | |
---|
591 | /** |
---|
592 | * Validation of IPv6 addresses. |
---|
593 | * |
---|
594 | * @param string $check IP Address to test |
---|
595 | * @return boolean Success |
---|
596 | * @access protected |
---|
597 | */ |
---|
598 | function _ipv6($check) { |
---|
599 | if (function_exists('filter_var')) { |
---|
600 | return filter_var($check, FILTER_VALIDATE_IP, array('flags' => FILTER_FLAG_IPV6)) !== false; |
---|
601 | } |
---|
602 | $this->__populateIp(); |
---|
603 | $this->check = $check; |
---|
604 | $this->regex = '/^' . $this->__pattern['IPv6'] . '$/'; |
---|
605 | return $this->_check(); |
---|
606 | } |
---|
607 | |
---|
608 | /** |
---|
609 | * Checks whether the length of a string is greater or equal to a minimal length. |
---|
610 | * |
---|
611 | * @param string $check The string to test |
---|
612 | * @param integer $min The minimal string length |
---|
613 | * @return boolean Success |
---|
614 | * @access public |
---|
615 | */ |
---|
616 | function minLength($check, $min) { |
---|
617 | $length = mb_strlen($check); |
---|
618 | return ($length >= $min); |
---|
619 | } |
---|
620 | |
---|
621 | /** |
---|
622 | * Checks whether the length of a string is smaller or equal to a maximal length.. |
---|
623 | * |
---|
624 | * @param string $check The string to test |
---|
625 | * @param integer $max The maximal string length |
---|
626 | * @return boolean Success |
---|
627 | * @access public |
---|
628 | */ |
---|
629 | function maxLength($check, $max) { |
---|
630 | $length = mb_strlen($check); |
---|
631 | return ($length <= $max); |
---|
632 | } |
---|
633 | |
---|
634 | /** |
---|
635 | * Checks that a value is a monetary amount. |
---|
636 | * |
---|
637 | * @param string $check Value to check |
---|
638 | * @param string $symbolPosition Where symbol is located (left/right) |
---|
639 | * @return boolean Success |
---|
640 | * @access public |
---|
641 | */ |
---|
642 | function money($check, $symbolPosition = 'left') { |
---|
643 | $_this =& Validation::getInstance(); |
---|
644 | $_this->check = $check; |
---|
645 | |
---|
646 | if ($symbolPosition == 'right') { |
---|
647 | $_this->regex = '/^(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?(?<!\x{00a2})\p{Sc}?$/u'; |
---|
648 | } else { |
---|
649 | $_this->regex = '/^(?!\x{00a2})\p{Sc}?(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?$/u'; |
---|
650 | } |
---|
651 | return $_this->_check(); |
---|
652 | } |
---|
653 | |
---|
654 | /** |
---|
655 | * Validate a multiple select. |
---|
656 | * |
---|
657 | * Valid Options |
---|
658 | * |
---|
659 | * - in => provide a list of choices that selections must be made from |
---|
660 | * - max => maximun number of non-zero choices that can be made |
---|
661 | * - min => minimum number of non-zero choices that can be made |
---|
662 | * |
---|
663 | * @param mixed $check Value to check |
---|
664 | * @param mixed $options Options for the check. |
---|
665 | * @return boolean Success |
---|
666 | * @access public |
---|
667 | */ |
---|
668 | function multiple($check, $options = array()) { |
---|
669 | $defaults = array('in' => null, 'max' => null, 'min' => null); |
---|
670 | $options = array_merge($defaults, $options); |
---|
671 | $check = array_filter((array)$check); |
---|
672 | if (empty($check)) { |
---|
673 | return false; |
---|
674 | } |
---|
675 | if ($options['max'] && count($check) > $options['max']) { |
---|
676 | return false; |
---|
677 | } |
---|
678 | if ($options['min'] && count($check) < $options['min']) { |
---|
679 | return false; |
---|
680 | } |
---|
681 | if ($options['in'] && is_array($options['in'])) { |
---|
682 | foreach ($check as $val) { |
---|
683 | if (!in_array($val, $options['in'])) { |
---|
684 | return false; |
---|
685 | } |
---|
686 | } |
---|
687 | } |
---|
688 | return true; |
---|
689 | } |
---|
690 | |
---|
691 | /** |
---|
692 | * Checks if a value is numeric. |
---|
693 | * |
---|
694 | * @param string $check Value to check |
---|
695 | * @return boolean Succcess |
---|
696 | * @access public |
---|
697 | */ |
---|
698 | function numeric($check) { |
---|
699 | return is_numeric($check); |
---|
700 | } |
---|
701 | |
---|
702 | /** |
---|
703 | * Check that a value is a valid phone number. |
---|
704 | * |
---|
705 | * @param mixed $check Value to check (string or array) |
---|
706 | * @param string $regex Regular expression to use |
---|
707 | * @param string $country Country code (defaults to 'all') |
---|
708 | * @return boolean Success |
---|
709 | * @access public |
---|
710 | */ |
---|
711 | function phone($check, $regex = null, $country = 'all') { |
---|
712 | $_this =& Validation::getInstance(); |
---|
713 | $_this->check = $check; |
---|
714 | $_this->regex = $regex; |
---|
715 | $_this->country = $country; |
---|
716 | if (is_array($check)) { |
---|
717 | $_this->_extract($check); |
---|
718 | } |
---|
719 | |
---|
720 | if (is_null($_this->regex)) { |
---|
721 | switch ($_this->country) { |
---|
722 | case 'us': |
---|
723 | case 'all': |
---|
724 | case 'can': |
---|
725 | // includes all NANPA members. see http://en.wikipedia.org/wiki/North_American_Numbering_Plan#List_of_NANPA_countries_and_territories |
---|
726 | $_this->regex = '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/'; |
---|
727 | break; |
---|
728 | } |
---|
729 | } |
---|
730 | if (empty($_this->regex)) { |
---|
731 | return $_this->_pass('phone', $check, $country); |
---|
732 | } |
---|
733 | return $_this->_check(); |
---|
734 | } |
---|
735 | |
---|
736 | /** |
---|
737 | * Checks that a given value is a valid postal code. |
---|
738 | * |
---|
739 | * @param mixed $check Value to check |
---|
740 | * @param string $regex Regular expression to use |
---|
741 | * @param string $country Country to use for formatting |
---|
742 | * @return boolean Success |
---|
743 | * @access public |
---|
744 | */ |
---|
745 | function postal($check, $regex = null, $country = null) { |
---|
746 | $_this =& Validation::getInstance(); |
---|
747 | $_this->check = $check; |
---|
748 | $_this->regex = $regex; |
---|
749 | $_this->country = $country; |
---|
750 | if (is_array($check)) { |
---|
751 | $_this->_extract($check); |
---|
752 | } |
---|
753 | if (empty($country)) { |
---|
754 | $_this->country = 'us'; |
---|
755 | } |
---|
756 | |
---|
757 | if (is_null($_this->regex)) { |
---|
758 | switch ($_this->country) { |
---|
759 | case 'uk': |
---|
760 | $_this->regex = '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i'; |
---|
761 | break; |
---|
762 | case 'ca': |
---|
763 | $_this->regex = '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] ?[0-9][A-Z][0-9]\\b\\z/i'; |
---|
764 | break; |
---|
765 | case 'it': |
---|
766 | case 'de': |
---|
767 | $_this->regex = '/^[0-9]{5}$/i'; |
---|
768 | break; |
---|
769 | case 'be': |
---|
770 | $_this->regex = '/^[1-9]{1}[0-9]{3}$/i'; |
---|
771 | break; |
---|
772 | case 'us': |
---|
773 | $_this->regex = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i'; |
---|
774 | break; |
---|
775 | } |
---|
776 | } |
---|
777 | if (empty($_this->regex)) { |
---|
778 | return $_this->_pass('postal', $check, $country); |
---|
779 | } |
---|
780 | return $_this->_check(); |
---|
781 | } |
---|
782 | |
---|
783 | /** |
---|
784 | * Validate that a number is in specified range. |
---|
785 | * if $lower and $upper are not set, will return true if |
---|
786 | * $check is a legal finite on this platform |
---|
787 | * |
---|
788 | * @param string $check Value to check |
---|
789 | * @param integer $lower Lower limit |
---|
790 | * @param integer $upper Upper limit |
---|
791 | * @return boolean Success |
---|
792 | * @access public |
---|
793 | */ |
---|
794 | function range($check, $lower = null, $upper = null) { |
---|
795 | if (!is_numeric($check)) { |
---|
796 | return false; |
---|
797 | } |
---|
798 | if (isset($lower) && isset($upper)) { |
---|
799 | return ($check > $lower && $check < $upper); |
---|
800 | } |
---|
801 | return is_finite($check); |
---|
802 | } |
---|
803 | |
---|
804 | /** |
---|
805 | * Checks that a value is a valid Social Security Number. |
---|
806 | * |
---|
807 | * @param mixed $check Value to check |
---|
808 | * @param string $regex Regular expression to use |
---|
809 | * @param string $country Country |
---|
810 | * @return boolean Success |
---|
811 | * @access public |
---|
812 | */ |
---|
813 | function ssn($check, $regex = null, $country = null) { |
---|
814 | $_this =& Validation::getInstance(); |
---|
815 | $_this->check = $check; |
---|
816 | $_this->regex = $regex; |
---|
817 | $_this->country = $country; |
---|
818 | if (is_array($check)) { |
---|
819 | $_this->_extract($check); |
---|
820 | } |
---|
821 | |
---|
822 | if (is_null($_this->regex)) { |
---|
823 | switch ($_this->country) { |
---|
824 | case 'dk': |
---|
825 | $_this->regex = '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i'; |
---|
826 | break; |
---|
827 | case 'nl': |
---|
828 | $_this->regex = '/\\A\\b[0-9]{9}\\b\\z/i'; |
---|
829 | break; |
---|
830 | case 'us': |
---|
831 | $_this->regex = '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i'; |
---|
832 | break; |
---|
833 | } |
---|
834 | } |
---|
835 | if (empty($_this->regex)) { |
---|
836 | return $_this->_pass('ssn', $check, $country); |
---|
837 | } |
---|
838 | return $_this->_check(); |
---|
839 | } |
---|
840 | |
---|
841 | /** |
---|
842 | * Checks that a value is a valid uuid - http://tools.ietf.org/html/rfc4122 |
---|
843 | * |
---|
844 | * @param string $check Value to check |
---|
845 | * @return boolean Success |
---|
846 | * @access public |
---|
847 | */ |
---|
848 | function uuid($check) { |
---|
849 | $_this =& Validation::getInstance(); |
---|
850 | $_this->check = $check; |
---|
851 | $_this->regex = '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i'; |
---|
852 | return $_this->_check(); |
---|
853 | } |
---|
854 | |
---|
855 | /** |
---|
856 | * Checks that a value is a valid URL according to http://www.w3.org/Addressing/URL/url-spec.txt |
---|
857 | * |
---|
858 | * The regex checks for the following component parts: |
---|
859 | * |
---|
860 | * - a valid, optional, scheme |
---|
861 | * - a valid ip address OR |
---|
862 | * a valid domain name as defined by section 2.3.1 of http://www.ietf.org/rfc/rfc1035.txt |
---|
863 | * with an optional port number |
---|
864 | * - an optional valid path |
---|
865 | * - an optional query string (get parameters) |
---|
866 | * - an optional fragment (anchor tag) |
---|
867 | * |
---|
868 | * @param string $check Value to check |
---|
869 | * @param boolean $strict Require URL to be prefixed by a valid scheme (one of http(s)/ftp(s)/file/news/gopher) |
---|
870 | * @return boolean Success |
---|
871 | * @access public |
---|
872 | */ |
---|
873 | function url($check, $strict = false) { |
---|
874 | $_this =& Validation::getInstance(); |
---|
875 | $_this->__populateIp(); |
---|
876 | $_this->check = $check; |
---|
877 | $validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=~[]') . '\/0-9a-z\p{L}\p{N}]|(%[0-9a-f]{2}))'; |
---|
878 | $_this->regex = '/^(?:(?:https?|ftps?|file|news|gopher):\/\/)' . (!empty($strict) ? '' : '?') . |
---|
879 | '(?:' . $_this->__pattern['IPv4'] . '|\[' . $_this->__pattern['IPv6'] . '\]|' . $_this->__pattern['hostname'] . ')' . |
---|
880 | '(?::[1-9][0-9]{0,4})?' . |
---|
881 | '(?:\/?|\/' . $validChars . '*)?' . |
---|
882 | '(?:\?' . $validChars . '*)?' . |
---|
883 | '(?:#' . $validChars . '*)?$/iu'; |
---|
884 | return $_this->_check(); |
---|
885 | } |
---|
886 | |
---|
887 | /** |
---|
888 | * Checks if a value is in a given list. |
---|
889 | * |
---|
890 | * @param string $check Value to check |
---|
891 | * @param array $list List to check against |
---|
892 | * @return boolean Succcess |
---|
893 | * @access public |
---|
894 | */ |
---|
895 | function inList($check, $list) { |
---|
896 | return in_array($check, $list); |
---|
897 | } |
---|
898 | |
---|
899 | /** |
---|
900 | * Runs an user-defined validation. |
---|
901 | * |
---|
902 | * @param mixed $check value that will be validated in user-defined methods. |
---|
903 | * @param object $object class that holds validation method |
---|
904 | * @param string $method class method name for validation to run |
---|
905 | * @param array $args arguments to send to method |
---|
906 | * @return mixed user-defined class class method returns |
---|
907 | * @access public |
---|
908 | */ |
---|
909 | function userDefined($check, $object, $method, $args = null) { |
---|
910 | return call_user_func_array(array(&$object, $method), array($check, $args)); |
---|
911 | } |
---|
912 | |
---|
913 | /** |
---|
914 | * Attempts to pass unhandled Validation locales to a class starting with $classPrefix |
---|
915 | * and ending with Validation. For example $classPrefix = 'nl', the class would be |
---|
916 | * `NlValidation`. |
---|
917 | * |
---|
918 | * @param string $method The method to call on the other class. |
---|
919 | * @param mixed $check The value to check or an array of parameters for the method to be called. |
---|
920 | * @param string $classPrefix The prefix for the class to do the validation. |
---|
921 | * @return mixed Return of Passed method, false on failure |
---|
922 | * @access protected |
---|
923 | **/ |
---|
924 | function _pass($method, $check, $classPrefix) { |
---|
925 | $className = ucwords($classPrefix) . 'Validation'; |
---|
926 | if (!class_exists($className)) { |
---|
927 | trigger_error(sprintf(__('Could not find %s class, unable to complete validation.', true), $className), E_USER_WARNING); |
---|
928 | return false; |
---|
929 | } |
---|
930 | if (!is_callable(array($className, $method))) { |
---|
931 | trigger_error(sprintf(__('Method %s does not exist on %s unable to complete validation.', true), $method, $className), E_USER_WARNING); |
---|
932 | return false; |
---|
933 | } |
---|
934 | $check = (array)$check; |
---|
935 | return call_user_func_array(array($className, $method), $check); |
---|
936 | } |
---|
937 | |
---|
938 | /** |
---|
939 | * Runs a regular expression match. |
---|
940 | * |
---|
941 | * @return boolean Success of match |
---|
942 | * @access protected |
---|
943 | */ |
---|
944 | function _check() { |
---|
945 | $_this =& Validation::getInstance(); |
---|
946 | if (preg_match($_this->regex, $_this->check)) { |
---|
947 | $_this->error[] = false; |
---|
948 | return true; |
---|
949 | } else { |
---|
950 | $_this->error[] = true; |
---|
951 | return false; |
---|
952 | } |
---|
953 | } |
---|
954 | |
---|
955 | /** |
---|
956 | * Get the values to use when value sent to validation method is |
---|
957 | * an array. |
---|
958 | * |
---|
959 | * @param array $params Parameters sent to validation method |
---|
960 | * @return void |
---|
961 | * @access protected |
---|
962 | */ |
---|
963 | function _extract($params) { |
---|
964 | $_this =& Validation::getInstance(); |
---|
965 | extract($params, EXTR_OVERWRITE); |
---|
966 | |
---|
967 | if (isset($check)) { |
---|
968 | $_this->check = $check; |
---|
969 | } |
---|
970 | if (isset($regex)) { |
---|
971 | $_this->regex = $regex; |
---|
972 | } |
---|
973 | if (isset($country)) { |
---|
974 | $_this->country = mb_strtolower($country); |
---|
975 | } |
---|
976 | if (isset($deep)) { |
---|
977 | $_this->deep = $deep; |
---|
978 | } |
---|
979 | if (isset($type)) { |
---|
980 | $_this->type = $type; |
---|
981 | } |
---|
982 | } |
---|
983 | |
---|
984 | /** |
---|
985 | * Luhn algorithm |
---|
986 | * |
---|
987 | * @see http://en.wikipedia.org/wiki/Luhn_algorithm |
---|
988 | * @return boolean Success |
---|
989 | * @access protected |
---|
990 | */ |
---|
991 | function _luhn() { |
---|
992 | $_this =& Validation::getInstance(); |
---|
993 | if ($_this->deep !== true) { |
---|
994 | return true; |
---|
995 | } |
---|
996 | if ($_this->check == 0) { |
---|
997 | return false; |
---|
998 | } |
---|
999 | $sum = 0; |
---|
1000 | $length = strlen($_this->check); |
---|
1001 | |
---|
1002 | for ($position = 1 - ($length % 2); $position < $length; $position += 2) { |
---|
1003 | $sum += $_this->check[$position]; |
---|
1004 | } |
---|
1005 | |
---|
1006 | for ($position = ($length % 2); $position < $length; $position += 2) { |
---|
1007 | $number = $_this->check[$position] * 2; |
---|
1008 | $sum += ($number < 10) ? $number : $number - 9; |
---|
1009 | } |
---|
1010 | |
---|
1011 | return ($sum % 10 == 0); |
---|
1012 | } |
---|
1013 | |
---|
1014 | /* |
---|
1015 | * Lazily popualate the IP address patterns used for validations |
---|
1016 | * |
---|
1017 | * @return void |
---|
1018 | * @access private |
---|
1019 | */ |
---|
1020 | function __populateIp() { |
---|
1021 | if (!isset($this->__pattern['IPv6'])) { |
---|
1022 | $pattern = '((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}'; |
---|
1023 | $pattern .= '(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})'; |
---|
1024 | $pattern .= '|(:[0-9A-Fa-f]{1,4})))|(([0-9A-Fa-f]{1,4}:){5}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})'; |
---|
1025 | $pattern .= '(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)'; |
---|
1026 | $pattern .= '{4}(:[0-9A-Fa-f]{1,4}){0,1}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2}))'; |
---|
1027 | $pattern .= '{3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){3}(:[0-9A-Fa-f]{1,4}){0,2}'; |
---|
1028 | $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|'; |
---|
1029 | $pattern .= '((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}'; |
---|
1030 | $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2}))'; |
---|
1031 | $pattern .= '{3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4})'; |
---|
1032 | $pattern .= '{0,4}((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)'; |
---|
1033 | $pattern .= '|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]'; |
---|
1034 | $pattern .= '\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4})'; |
---|
1035 | $pattern .= '{1,2})))|(((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})))(%.+)?'; |
---|
1036 | |
---|
1037 | $this->__pattern['IPv6'] = $pattern; |
---|
1038 | } |
---|
1039 | if (!isset($this->__pattern['IPv4'])) { |
---|
1040 | $pattern = '(?:(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|(?:(?:1[0-9])?|[1-9]?)[0-9])'; |
---|
1041 | $this->__pattern['IPv4'] = $pattern; |
---|
1042 | } |
---|
1043 | } |
---|
1044 | |
---|
1045 | /** |
---|
1046 | * Reset internal variables for another validation run. |
---|
1047 | * |
---|
1048 | * @return void |
---|
1049 | * @access private |
---|
1050 | */ |
---|
1051 | function __reset() { |
---|
1052 | $this->check = null; |
---|
1053 | $this->regex = null; |
---|
1054 | $this->country = null; |
---|
1055 | $this->deep = null; |
---|
1056 | $this->type = null; |
---|
1057 | $this->error = array(); |
---|
1058 | $this->errors = array(); |
---|
1059 | } |
---|
1060 | } |
---|