1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
20: if (!class_exists('Multibyte')) {
21: App::import('Core', 'Multibyte', false);
22: }
23: 24: 25: 26: 27: 28: 29:
30: class Validation extends Object {
31:
32: 33: 34: 35: 36: 37:
38: var $check = null;
39:
40: 41: 42: 43: 44: 45: 46:
47: var $regex = null;
48:
49: 50: 51: 52: 53: 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: 61: 62: 63: 64: 65:
66: var $country = null;
67:
68: 69: 70: 71: 72: 73:
74: var $deep = null;
75:
76: 77: 78: 79: 80: 81:
82: var $type = null;
83:
84: 85: 86: 87: 88: 89: 90:
91: var $errors = array();
92:
93: 94: 95: 96: 97: 98: 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: 111: 112: 113: 114: 115: 116: 117: 118: 119: 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: 139: 140: 141: 142: 143: 144: 145: 146: 147: 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: 167: 168: 169: 170: 171: 172: 173: 174: 175:
176: function between($check, $min, $max) {
177: $length = mb_strlen($check);
178: return ($length >= $min && $length <= $max);
179: }
180:
181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 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: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 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: 287: 288: 289: 290: 291: 292: 293: 294: 295: 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: 350: 351: 352: 353: 354: 355: 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: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 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: 420: 421: 422: 423: 424: 425: 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: 438: 439: 440: 441: 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: 450: 451: 452: 453: 454: 455: 456: 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: 476: 477: 478: 479: 480: 481: 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: 517: 518: 519: 520: 521: 522:
523: function equalTo($check, $comparedTo) {
524: return ($check === $comparedTo);
525: }
526:
527: 528: 529: 530: 531: 532: 533: 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: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 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: 576: 577: 578: 579: 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: 593: 594: 595: 596: 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: 610: 611: 612: 613: 614: 615:
616: function minLength($check, $min) {
617: $length = mb_strlen($check);
618: return ($length >= $min);
619: }
620:
621: 622: 623: 624: 625: 626: 627: 628:
629: function maxLength($check, $max) {
630: $length = mb_strlen($check);
631: return ($length <= $max);
632: }
633:
634: 635: 636: 637: 638: 639: 640: 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: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 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: 693: 694: 695: 696: 697:
698: function numeric($check) {
699: return is_numeric($check);
700: }
701:
702: 703: 704: 705: 706: 707: 708: 709: 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:
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: 738: 739: 740: 741: 742: 743: 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: 785: 786: 787: 788: 789: 790: 791: 792: 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: 806: 807: 808: 809: 810: 811: 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: 843: 844: 845: 846: 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: 857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 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: 889: 890: 891: 892: 893: 894:
895: function inList($check, $list) {
896: return in_array($check, $list);
897: }
898:
899: 900: 901: 902: 903: 904: 905: 906: 907: 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: 915: 916: 917: 918: 919: 920: 921: 922: 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: 940: 941: 942: 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: 957: 958: 959: 960: 961: 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: 986: 987: 988: 989: 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: 1016: 1017: 1018: 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: 1047: 1048: 1049: 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: }
1061: