1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('Multibyte', 'I18n');
21:
22: if (!function_exists('mb_strlen')) {
23: class_exists('Multibyte');
24: }
25:
26: 27: 28: 29: 30: 31:
32: class Validation {
33:
34: 35: 36: 37: 38:
39: protected static $_pattern = array(
40: 'hostname' => '(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)'
41: );
42:
43: 44: 45: 46: 47: 48:
49: public static $errors = array();
50:
51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61:
62: public static function notEmpty($check) {
63: if (is_array($check)) {
64: extract(self::_defaults($check));
65: }
66:
67: if (empty($check) && $check != '0') {
68: return false;
69: }
70: return self::_check($check, '/[^\s]+/m');
71: }
72:
73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83:
84: public static function alphaNumeric($check) {
85: if (is_array($check)) {
86: extract(self::_defaults($check));
87: }
88:
89: if (empty($check) && $check != '0') {
90: return false;
91: }
92: return self::_check($check, '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu');
93: }
94:
95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
105: public static function between($check, $min, $max) {
106: $length = mb_strlen($check);
107: return ($length >= $min && $length <= $max);
108: }
109:
110: 111: 112: 113: 114: 115: 116: 117: 118: 119:
120: public static function blank($check) {
121: if (is_array($check)) {
122: extract(self::_defaults($check));
123: }
124: return !self::_check($check, '/[^\\s]/');
125: }
126:
127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139:
140: public static function cc($check, $type = 'fast', $deep = false, $regex = null) {
141: if (is_array($check)) {
142: extract(self::_defaults($check));
143: }
144:
145: $check = str_replace(array('-', ' '), '', $check);
146: if (mb_strlen($check) < 13) {
147: return false;
148: }
149:
150: if (!is_null($regex)) {
151: if (self::_check($check, $regex)) {
152: return self::luhn($check, $deep);
153: }
154: }
155: $cards = array(
156: 'all' => array(
157: 'amex' => '/^3[4|7]\\d{13}$/',
158: 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
159: 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
160: 'disc' => '/^(?:6011|650\\d)\\d{12}$/',
161: 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
162: 'enroute' => '/^2(?:014|149)\\d{11}$/',
163: 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/',
164: 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/',
165: 'mc' => '/^5[1-5]\\d{14}$/',
166: 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
167: '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})?)$/',
168: 'visa' => '/^4\\d{12}(\\d{3})?$/',
169: 'voyager' => '/^8699[0-9]{11}$/'
170: ),
171: '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})$/'
172: );
173:
174: if (is_array($type)) {
175: foreach ($type as $value) {
176: $regex = $cards['all'][strtolower($value)];
177:
178: if (self::_check($check, $regex)) {
179: return self::luhn($check, $deep);
180: }
181: }
182: } elseif ($type == 'all') {
183: foreach ($cards['all'] as $value) {
184: $regex = $value;
185:
186: if (self::_check($check, $regex)) {
187: return self::luhn($check, $deep);
188: }
189: }
190: } else {
191: $regex = $cards['fast'];
192:
193: if (self::_check($check, $regex)) {
194: return self::luhn($check, $deep);
195: }
196: }
197: return false;
198: }
199:
200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210:
211: public static function comparison($check1, $operator = null, $check2 = null) {
212: if (is_array($check1)) {
213: extract($check1, EXTR_OVERWRITE);
214: }
215: $operator = str_replace(array(' ', "\t", "\n", "\r", "\0", "\x0B"), '', strtolower($operator));
216:
217: switch ($operator) {
218: case 'isgreater':
219: case '>':
220: if ($check1 > $check2) {
221: return true;
222: }
223: break;
224: case 'isless':
225: case '<':
226: if ($check1 < $check2) {
227: return true;
228: }
229: break;
230: case 'greaterorequal':
231: case '>=':
232: if ($check1 >= $check2) {
233: return true;
234: }
235: break;
236: case 'lessorequal':
237: case '<=':
238: if ($check1 <= $check2) {
239: return true;
240: }
241: break;
242: case 'equalto':
243: case '==':
244: if ($check1 == $check2) {
245: return true;
246: }
247: break;
248: case 'notequal':
249: case '!=':
250: if ($check1 != $check2) {
251: return true;
252: }
253: break;
254: default:
255: self::$errors[] = __d('cake_dev', 'You must define the $operator parameter for Validation::comparison()');
256: break;
257: }
258: return false;
259: }
260:
261: 262: 263: 264: 265: 266: 267: 268:
269: public static function custom($check, $regex = null) {
270: if (is_array($check)) {
271: extract(self::_defaults($check));
272: }
273: if ($regex === null) {
274: self::$errors[] = __d('cake_dev', 'You must define a regular expression for Validation::custom()');
275: return false;
276: }
277: return self::_check($check, $regex);
278: }
279:
280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295:
296: public static function date($check, $format = 'ymd', $regex = null) {
297: if (!is_null($regex)) {
298: return self::_check($check, $regex);
299: }
300:
301: $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})$%';
302: $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})$%';
303: $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]))))$%';
304: $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})$/';
305: $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}))$/';
306: $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})$%';
307: $regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%';
308:
309: $format = (is_array($format)) ? array_values($format) : array($format);
310: foreach ($format as $key) {
311: if (self::_check($check, $regex[$key]) === true) {
312: return true;
313: }
314: }
315: return false;
316: }
317:
318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338:
339: public static function datetime($check, $dateFormat = 'ymd', $regex = null) {
340: $valid = false;
341: $parts = explode(' ', $check);
342: if (!empty($parts) && count($parts) > 1) {
343: $time = array_pop($parts);
344: $date = implode(' ', $parts);
345: $valid = self::date($date, $dateFormat, $regex) && self::time($time);
346: }
347: return $valid;
348: }
349:
350: 351: 352: 353: 354: 355: 356: 357:
358: public static function time($check) {
359: return self::_check($check, '%^((0?[1-9]|1[012])(:[0-5]\d){0,2} ?([AP]M|[ap]m))$|^([01]\d|2[0-3])(:[0-5]\d){0,2}$%');
360: }
361:
362: 363: 364: 365: 366: 367:
368: public static function boolean($check) {
369: $booleanList = array(0, 1, '0', '1', true, false);
370: return in_array($check, $booleanList, true);
371: }
372:
373: 374: 375: 376: 377: 378: 379: 380: 381:
382: public static function decimal($check, $places = null, $regex = null) {
383: if (is_null($regex)) {
384: if (is_null($places)) {
385: $regex = '/^[-+]?[0-9]*\\.{1}[0-9]+(?:[eE][-+]?[0-9]+)?$/';
386: } else {
387: $regex = '/^[-+]?[0-9]*\\.{1}[0-9]{' . $places . '}$/';
388: }
389: }
390: return self::_check($check, $regex);
391: }
392:
393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403:
404: public static function email($check, $deep = false, $regex = null) {
405: if (is_array($check)) {
406: extract(self::_defaults($check));
407: }
408:
409: if (is_null($regex)) {
410: $regex = '/^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@' . self::$_pattern['hostname'] . '$/i';
411: }
412: $return = self::_check($check, $regex);
413: if ($deep === false || $deep === null) {
414: return $return;
415: }
416:
417: if ($return === true && preg_match('/@(' . self::$_pattern['hostname'] . ')$/i', $check, $regs)) {
418: if (function_exists('getmxrr') && getmxrr($regs[1], $mxhosts)) {
419: return true;
420: }
421: if (function_exists('checkdnsrr') && checkdnsrr($regs[1], 'MX')) {
422: return true;
423: }
424: return is_array(gethostbynamel($regs[1]));
425: }
426: return false;
427: }
428:
429: 430: 431: 432: 433: 434: 435:
436: public static function equalTo($check, $comparedTo) {
437: return ($check === $comparedTo);
438: }
439:
440: 441: 442: 443: 444: 445: 446:
447: public static function extension($check, $extensions = array('gif', 'jpeg', 'png', 'jpg')) {
448: if (is_array($check)) {
449: return self::extension(array_shift($check), $extensions);
450: }
451: $pathSegments = explode('.', $check);
452: $extension = strtolower(array_pop($pathSegments));
453: foreach ($extensions as $value) {
454: if ($extension == strtolower($value)) {
455: return true;
456: }
457: }
458: return false;
459: }
460:
461: 462: 463: 464: 465: 466: 467:
468: public static function ip($check, $type = 'both') {
469: $type = strtolower($type);
470: $flags = array();
471: if ($type === 'ipv4' || $type === 'both') {
472: $flags[] = FILTER_FLAG_IPV4;
473: }
474: if ($type === 'ipv6' || $type === 'both') {
475: $flags[] = FILTER_FLAG_IPV6;
476: }
477: return (boolean)filter_var($check, FILTER_VALIDATE_IP, array('flags' => $flags));
478: }
479:
480: 481: 482: 483: 484: 485: 486:
487: public static function minLength($check, $min) {
488: return mb_strlen($check) >= $min;
489: }
490:
491: 492: 493: 494: 495: 496: 497:
498: public static function maxLength($check, $max) {
499: return mb_strlen($check) <= $max;
500: }
501:
502: 503: 504: 505: 506: 507: 508:
509: public static function money($check, $symbolPosition = 'left') {
510: $money = '(?!0,?\d)(?:\d{1,3}(?:([, .])\d{3})?(?:\1\d{3})*|(?:\d+))((?!\1)[,.]\d{2})?';
511: if ($symbolPosition == 'right') {
512: $regex = '/^' . $money . '(?<!\x{00a2})\p{Sc}?$/u';
513: } else {
514: $regex = '/^(?!\x{00a2})\p{Sc}?' . $money . '$/u';
515: }
516: return self::_check($check, $regex);
517: }
518:
519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531:
532: public static function multiple($check, $options = array()) {
533: $defaults = array('in' => null, 'max' => null, 'min' => null);
534: $options = array_merge($defaults, $options);
535: $check = array_filter((array)$check);
536: if (empty($check)) {
537: return false;
538: }
539: if ($options['max'] && count($check) > $options['max']) {
540: return false;
541: }
542: if ($options['min'] && count($check) < $options['min']) {
543: return false;
544: }
545: if ($options['in'] && is_array($options['in'])) {
546: foreach ($check as $val) {
547: if (!in_array($val, $options['in'])) {
548: return false;
549: }
550: }
551: }
552: return true;
553: }
554:
555: 556: 557: 558: 559: 560:
561: public static function numeric($check) {
562: return is_numeric($check);
563: }
564:
565: 566: 567: 568: 569: 570: 571: 572:
573: public static function phone($check, $regex = null, $country = 'all') {
574: if (is_array($check)) {
575: extract(self::_defaults($check));
576: }
577:
578: if (is_null($regex)) {
579: switch ($country) {
580: case 'us':
581: case 'all':
582: case 'can':
583:
584: $regex = '/^(?:\+?1)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/';
585: break;
586: }
587: }
588: if (empty($regex)) {
589: return self::_pass('phone', $check, $country);
590: }
591: return self::_check($check, $regex);
592: }
593:
594: 595: 596: 597: 598: 599: 600: 601:
602: public static function postal($check, $regex = null, $country = 'us') {
603: if (is_array($check)) {
604: extract(self::_defaults($check));
605: }
606:
607: if (is_null($regex)) {
608: switch ($country) {
609: case 'uk':
610: $regex = '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i';
611: break;
612: case 'ca':
613: $regex = '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i';
614: break;
615: case 'it':
616: case 'de':
617: $regex = '/^[0-9]{5}$/i';
618: break;
619: case 'be':
620: $regex = '/^[1-9]{1}[0-9]{3}$/i';
621: break;
622: case 'us':
623: $regex = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i';
624: break;
625: }
626: }
627: if (empty($regex)) {
628: return self::_pass('postal', $check, $country);
629: }
630: return self::_check($check, $regex);
631: }
632:
633: 634: 635: 636: 637: 638: 639: 640: 641: 642:
643: public static function range($check, $lower = null, $upper = null) {
644: if (!is_numeric($check)) {
645: return false;
646: }
647: if (isset($lower) && isset($upper)) {
648: return ($check > $lower && $check < $upper);
649: }
650: return is_finite($check);
651: }
652:
653: 654: 655: 656: 657: 658: 659: 660:
661: public static function ssn($check, $regex = null, $country = null) {
662: if (is_array($check)) {
663: extract(self::_defaults($check));
664: }
665:
666: if (is_null($regex)) {
667: switch ($country) {
668: case 'dk':
669: $regex = '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i';
670: break;
671: case 'nl':
672: $regex = '/\\A\\b[0-9]{9}\\b\\z/i';
673: break;
674: case 'us':
675: $regex = '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i';
676: break;
677: }
678: }
679: if (empty($regex)) {
680: return self::_pass('ssn', $check, $country);
681: }
682: return self::_check($check, $regex);
683: }
684:
685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701:
702: public static function url($check, $strict = false) {
703: self::_populateIp();
704: $validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=~[]') . '\/0-9a-z\p{L}\p{N}]|(%[0-9a-f]{2}))';
705: $regex = '/^(?:(?:https?|ftps?|sftp|file|news|gopher):\/\/)' . (!empty($strict) ? '' : '?') .
706: '(?:' . self::$_pattern['IPv4'] . '|\[' . self::$_pattern['IPv6'] . '\]|' . self::$_pattern['hostname'] . ')(?::[1-9][0-9]{0,4})?' .
707: '(?:\/?|\/' . $validChars . '*)?' .
708: '(?:\?' . $validChars . '*)?' .
709: '(?:#' . $validChars . '*)?$/iu';
710: return self::_check($check, $regex);
711: }
712:
713: 714: 715: 716: 717: 718: 719:
720: public static function inList($check, $list) {
721: return in_array($check, $list);
722: }
723:
724: 725: 726: 727: 728: 729: 730: 731: 732:
733: public static function userDefined($check, $object, $method, $args = null) {
734: return call_user_func_array(array($object, $method), array($check, $args));
735: }
736:
737: 738: 739: 740: 741: 742:
743: public static function uuid($check) {
744: $regex = '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i';
745: return self::_check($check, $regex);
746: }
747:
748: 749: 750: 751: 752: 753: 754: 755: 756: 757:
758: protected static function _pass($method, $check, $classPrefix) {
759: $className = ucwords($classPrefix) . 'Validation';
760: if (!class_exists($className)) {
761: trigger_error(__d('cake_dev', 'Could not find %s class, unable to complete validation.', $className), E_USER_WARNING);
762: return false;
763: }
764: if (!method_exists($className, $method)) {
765: trigger_error(__d('cake_dev', 'Method %s does not exist on %s unable to complete validation.', $method, $className), E_USER_WARNING);
766: return false;
767: }
768: $check = (array)$check;
769: return call_user_func_array(array($className, $method), $check);
770: }
771:
772: 773: 774: 775: 776: 777: 778:
779: protected static function _check($check, $regex) {
780: if (preg_match($regex, $check)) {
781: self::$errors[] = false;
782: return true;
783: } else {
784: self::$errors[] = true;
785: return false;
786: }
787: }
788:
789: 790: 791: 792: 793: 794: 795:
796: protected static function _defaults($params) {
797: self::_reset();
798: $defaults = array(
799: 'check' => null,
800: 'regex' => null,
801: 'country' => null,
802: 'deep' => false,
803: 'type' => null
804: );
805: $params = array_merge($defaults, $params);
806: if ($params['country'] !== null) {
807: $params['country'] = mb_strtolower($params['country']);
808: }
809: return $params;
810: }
811:
812: 813: 814: 815: 816: 817: 818: 819:
820: public static function luhn($check, $deep = false) {
821: if (is_array($check)) {
822: extract(self::_defaults($check));
823: }
824: if ($deep !== true) {
825: return true;
826: }
827: if ($check == 0) {
828: return false;
829: }
830: $sum = 0;
831: $length = strlen($check);
832:
833: for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
834: $sum += $check[$position];
835: }
836:
837: for ($position = ($length % 2); $position < $length; $position += 2) {
838: $number = $check[$position] * 2;
839: $sum += ($number < 10) ? $number : $number - 9;
840: }
841:
842: return ($sum % 10 == 0);
843: }
844:
845: 846: 847: 848: 849:
850: protected static function _populateIp() {
851: if (!isset(self::$_pattern['IPv6'])) {
852: $pattern = '((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}';
853: $pattern .= '(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})';
854: $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})';
855: $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}:)';
856: $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}))';
857: $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}';
858: $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|';
859: $pattern .= '((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}';
860: $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2}))';
861: $pattern .= '{3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4})';
862: $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})?)';
863: $pattern .= '|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]';
864: $pattern .= '\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4})';
865: $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})))(%.+)?';
866:
867: self::$_pattern['IPv6'] = $pattern;
868: }
869: if (!isset(self::$_pattern['IPv4'])) {
870: $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])';
871: self::$_pattern['IPv4'] = $pattern;
872: }
873: }
874:
875: 876: 877: 878: 879:
880: protected static function _reset() {
881: self::$errors = array();
882: }
883: }
884: