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: App::uses('File', 'Utility');
22:
23: if (!function_exists('mb_strlen')) {
24: class_exists('Multibyte');
25: }
26:
27: 28: 29: 30: 31: 32:
33: class Validation {
34:
35: 36: 37: 38: 39:
40: protected static $_pattern = array(
41: 'hostname' => '(?:[-_a-z0-9][-_a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,})'
42: );
43:
44: 45: 46: 47: 48: 49:
50: public static $errors = array();
51:
52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62:
63: public static function notEmpty($check) {
64: if (is_array($check)) {
65: extract(self::_defaults($check));
66: }
67:
68: if (empty($check) && $check != '0') {
69: return false;
70: }
71: return self::_check($check, '/[^\s]+/m');
72: }
73:
74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
85: public static function alphaNumeric($check) {
86: if (is_array($check)) {
87: extract(self::_defaults($check));
88: }
89:
90: if (empty($check) && $check != '0') {
91: return false;
92: }
93: return self::_check($check, '/^[\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]+$/mu');
94: }
95:
96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
106: public static function between($check, $min, $max) {
107: $length = mb_strlen($check);
108: return ($length >= $min && $length <= $max);
109: }
110:
111: 112: 113: 114: 115: 116: 117: 118: 119: 120:
121: public static function blank($check) {
122: if (is_array($check)) {
123: extract(self::_defaults($check));
124: }
125: return !self::_check($check, '/[^\\s]/');
126: }
127:
128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140:
141: public static function cc($check, $type = 'fast', $deep = false, $regex = null) {
142: if (is_array($check)) {
143: extract(self::_defaults($check));
144: }
145:
146: $check = str_replace(array('-', ' '), '', $check);
147: if (mb_strlen($check) < 13) {
148: return false;
149: }
150:
151: if (!is_null($regex)) {
152: if (self::_check($check, $regex)) {
153: return self::luhn($check, $deep);
154: }
155: }
156: $cards = array(
157: 'all' => array(
158: 'amex' => '/^3[4|7]\\d{13}$/',
159: 'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
160: 'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
161: 'disc' => '/^(?:6011|650\\d)\\d{12}$/',
162: 'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
163: 'enroute' => '/^2(?:014|149)\\d{11}$/',
164: 'jcb' => '/^(3\\d{4}|2100|1800)\\d{11}$/',
165: 'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/',
166: 'mc' => '/^5[1-5]\\d{14}$/',
167: 'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
168: '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})?)$/',
169: 'visa' => '/^4\\d{12}(\\d{3})?$/',
170: 'voyager' => '/^8699[0-9]{11}$/'
171: ),
172: '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})$/'
173: );
174:
175: if (is_array($type)) {
176: foreach ($type as $value) {
177: $regex = $cards['all'][strtolower($value)];
178:
179: if (self::_check($check, $regex)) {
180: return self::luhn($check, $deep);
181: }
182: }
183: } elseif ($type == 'all') {
184: foreach ($cards['all'] as $value) {
185: $regex = $value;
186:
187: if (self::_check($check, $regex)) {
188: return self::luhn($check, $deep);
189: }
190: }
191: } else {
192: $regex = $cards['fast'];
193:
194: if (self::_check($check, $regex)) {
195: return self::luhn($check, $deep);
196: }
197: }
198: return false;
199: }
200:
201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211:
212: public static function comparison($check1, $operator = null, $check2 = null) {
213: if (is_array($check1)) {
214: extract($check1, EXTR_OVERWRITE);
215: }
216: $operator = str_replace(array(' ', "\t", "\n", "\r", "\0", "\x0B"), '', strtolower($operator));
217:
218: switch ($operator) {
219: case 'isgreater':
220: case '>':
221: if ($check1 > $check2) {
222: return true;
223: }
224: break;
225: case 'isless':
226: case '<':
227: if ($check1 < $check2) {
228: return true;
229: }
230: break;
231: case 'greaterorequal':
232: case '>=':
233: if ($check1 >= $check2) {
234: return true;
235: }
236: break;
237: case 'lessorequal':
238: case '<=':
239: if ($check1 <= $check2) {
240: return true;
241: }
242: break;
243: case 'equalto':
244: case '==':
245: if ($check1 == $check2) {
246: return true;
247: }
248: break;
249: case 'notequal':
250: case '!=':
251: if ($check1 != $check2) {
252: return true;
253: }
254: break;
255: default:
256: self::$errors[] = __d('cake_dev', 'You must define the $operator parameter for Validation::comparison()');
257: break;
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 (!is_null($regex)) {
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 (is_null($regex)) {
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 (is_null($regex)) {
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{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 (is_null($regex)) {
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 (is_null($regex)) {
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: $regex = '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i';
648: break;
649: case 'it':
650: case 'de':
651: $regex = '/^[0-9]{5}$/i';
652: break;
653: case 'be':
654: $regex = '/^[1-9]{1}[0-9]{3}$/i';
655: break;
656: case 'us':
657: $regex = '/\\A\\b[0-9]{5}(?:-[0-9]{4})?\\b\\z/i';
658: break;
659: }
660: }
661: if (empty($regex)) {
662: return self::_pass('postal', $check, $country);
663: }
664: return self::_check($check, $regex);
665: }
666:
667: 668: 669: 670: 671: 672: 673: 674: 675: 676:
677: public static function range($check, $lower = null, $upper = null) {
678: if (!is_numeric($check)) {
679: return false;
680: }
681: if (isset($lower) && isset($upper)) {
682: return ($check > $lower && $check < $upper);
683: }
684: return is_finite($check);
685: }
686:
687: 688: 689: 690: 691: 692: 693: 694:
695: public static function ssn($check, $regex = null, $country = null) {
696: if (is_array($check)) {
697: extract(self::_defaults($check));
698: }
699:
700: if (is_null($regex)) {
701: switch ($country) {
702: case 'dk':
703: $regex = '/\\A\\b[0-9]{6}-[0-9]{4}\\b\\z/i';
704: break;
705: case 'nl':
706: $regex = '/\\A\\b[0-9]{9}\\b\\z/i';
707: break;
708: case 'us':
709: $regex = '/\\A\\b[0-9]{3}-[0-9]{2}-[0-9]{4}\\b\\z/i';
710: break;
711: }
712: }
713: if (empty($regex)) {
714: return self::_pass('ssn', $check, $country);
715: }
716: return self::_check($check, $regex);
717: }
718:
719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735:
736: public static function url($check, $strict = false) {
737: self::_populateIp();
738: $validChars = '([' . preg_quote('!"$&\'()*+,-.@_:;=~[]') . '\/0-9a-z\p{L}\p{N}]|(%[0-9a-f]{2}))';
739: $regex = '/^(?:(?:https?|ftps?|sftp|file|news|gopher):\/\/)' . (!empty($strict) ? '' : '?') .
740: '(?:' . self::$_pattern['IPv4'] . '|\[' . self::$_pattern['IPv6'] . '\]|' . self::$_pattern['hostname'] . ')(?::[1-9][0-9]{0,4})?' .
741: '(?:\/?|\/' . $validChars . '*)?' .
742: '(?:\?' . $validChars . '*)?' .
743: '(?:#' . $validChars . '*)?$/iu';
744: return self::_check($check, $regex);
745: }
746:
747: 748: 749: 750: 751: 752: 753: 754:
755: public static function inList($check, $list, $strict = true) {
756: return in_array($check, $list, $strict);
757: }
758:
759: 760: 761: 762: 763: 764: 765: 766: 767:
768: public static function userDefined($check, $object, $method, $args = null) {
769: return call_user_func_array(array($object, $method), array($check, $args));
770: }
771:
772: 773: 774: 775: 776: 777:
778: public static function uuid($check) {
779: $regex = '/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/i';
780: return self::_check($check, $regex);
781: }
782:
783: 784: 785: 786: 787: 788: 789: 790: 791: 792:
793: protected static function _pass($method, $check, $classPrefix) {
794: $className = ucwords($classPrefix) . 'Validation';
795: if (!class_exists($className)) {
796: trigger_error(__d('cake_dev', 'Could not find %s class, unable to complete validation.', $className), E_USER_WARNING);
797: return false;
798: }
799: if (!method_exists($className, $method)) {
800: trigger_error(__d('cake_dev', 'Method %s does not exist on %s unable to complete validation.', $method, $className), E_USER_WARNING);
801: return false;
802: }
803: $check = (array)$check;
804: return call_user_func_array(array($className, $method), $check);
805: }
806:
807: 808: 809: 810: 811: 812: 813:
814: protected static function _check($check, $regex) {
815: if (is_string($regex) && preg_match($regex, $check)) {
816: self::$errors[] = false;
817: return true;
818: } else {
819: self::$errors[] = true;
820: return false;
821: }
822: }
823:
824: 825: 826: 827: 828: 829: 830:
831: protected static function _defaults($params) {
832: self::_reset();
833: $defaults = array(
834: 'check' => null,
835: 'regex' => null,
836: 'country' => null,
837: 'deep' => false,
838: 'type' => null
839: );
840: $params = array_merge($defaults, $params);
841: if ($params['country'] !== null) {
842: $params['country'] = mb_strtolower($params['country']);
843: }
844: return $params;
845: }
846:
847: 848: 849: 850: 851: 852: 853: 854:
855: public static function luhn($check, $deep = false) {
856: if (is_array($check)) {
857: extract(self::_defaults($check));
858: }
859: if ($deep !== true) {
860: return true;
861: }
862: if ($check == 0) {
863: return false;
864: }
865: $sum = 0;
866: $length = strlen($check);
867:
868: for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
869: $sum += $check[$position];
870: }
871:
872: for ($position = ($length % 2); $position < $length; $position += 2) {
873: $number = $check[$position] * 2;
874: $sum += ($number < 10) ? $number : $number - 9;
875: }
876:
877: return ($sum % 10 == 0);
878: }
879:
880: 881: 882: 883: 884: 885: 886: 887:
888: public static function mimeType($check, $mimeTypes = array()) {
889: if (is_array($check) && isset($check['tmp_name'])) {
890: $check = $check['tmp_name'];
891: }
892:
893: $File = new File($check);
894: $mime = $File->mime();
895:
896: if ($mime === false) {
897: throw new CakeException(__d('cake_dev', 'Can not determine the mimetype.'));
898: }
899: return in_array($mime, $mimeTypes);
900: }
901:
902: 903: 904: 905: 906: 907: 908:
909: public static function uploadError($check) {
910: if (is_array($check) && isset($check['error'])) {
911: $check = $check['error'];
912: }
913:
914: return $check === UPLOAD_ERR_OK;
915: }
916:
917: 918: 919: 920: 921:
922: protected static function _populateIp() {
923: if (!isset(self::$_pattern['IPv6'])) {
924: $pattern = '((([0-9A-Fa-f]{1,4}:){7}(([0-9A-Fa-f]{1,4})|:))|(([0-9A-Fa-f]{1,4}:){6}';
925: $pattern .= '(:|((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})';
926: $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})';
927: $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}:)';
928: $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}))';
929: $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}';
930: $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|';
931: $pattern .= '((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:){2}(:[0-9A-Fa-f]{1,4}){0,3}';
932: $pattern .= '((:((25[0-5]|2[0-4]\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2}))';
933: $pattern .= '{3})?)|((:[0-9A-Fa-f]{1,4}){1,2})))|(([0-9A-Fa-f]{1,4}:)(:[0-9A-Fa-f]{1,4})';
934: $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})?)';
935: $pattern .= '|((:[0-9A-Fa-f]{1,4}){1,2})))|(:(:[0-9A-Fa-f]{1,4}){0,5}((:((25[0-5]|2[0-4]';
936: $pattern .= '\d|[01]?\d{1,2})(\.(25[0-5]|2[0-4]\d|[01]?\d{1,2})){3})?)|((:[0-9A-Fa-f]{1,4})';
937: $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})))(%.+)?';
938:
939: self::$_pattern['IPv6'] = $pattern;
940: }
941: if (!isset(self::$_pattern['IPv4'])) {
942: $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])';
943: self::$_pattern['IPv4'] = $pattern;
944: }
945: }
946:
947: 948: 949: 950: 951:
952: protected static function _reset() {
953: self::$errors = array();
954: }
955:
956: }
957: