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