1: | <?php
|
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | |
11: | |
12: | |
13: | |
14: |
|
15: |
|
16: | use LucidFrame\Core\Validation;
|
17: |
|
18: | |
19: | |
20: | |
21: | |
22: | |
23: |
|
24: | function __validation_init()
|
25: | {
|
26: | $validationMessages = array(
|
27: | 'default' => "'%s' needs to be revised.",
|
28: | 'mandatory' => "'%s' is required.",
|
29: | 'mandatoryOne' => "'%s' must be entered/selected at least one.",
|
30: | 'mandatoryAll' => "'%s' is required. All must be entered/selected.",
|
31: | 'notAllowZero' => "'%s' should not be zero.",
|
32: | 'alphaNumeric' => "'%s' should contain only letters and numbers.",
|
33: | 'alphaNumericSpace' => "'%s' should contain only letters, numbers and spaces.",
|
34: | 'alphaNumericDash' => "'%s' should contain only letters, numbers and dashes.",
|
35: | 'numeric' => "'%s' should be a number.",
|
36: | 'numericSpace' => "'%s' should contain only numbers and spaces.",
|
37: | 'numericDash' => "'%s' should contain only numbers and dashes. It should not start or end with a dash.",
|
38: | 'username' => "'%s' should contain only letters, numbers, periods, underscores and dashes.",
|
39: | 'naturalNumber' => "'%s' should be a positive integer. It is not allowed zero.",
|
40: | 'wholeNumber' => "'%s' should be a positive integer.",
|
41: | 'integer' => "'%s' should be a positive or negative integer.",
|
42: | 'rationalNumber' => "'%s' should be an integer or decimal.",
|
43: | 'positiveRationalNumber' => "'%s' should be a positive integer or decimal.",
|
44: | 'email' => "'%s' should be a valid format, e.g., username@example.com",
|
45: | 'domain' => "'%s' should be a valid domain name with letters, numbers and dash only.",
|
46: | 'url' => "'%s' should be a valid website address, e.g., http://www.example.com",
|
47: | 'exactLength' => "'%s' should have exact length of %d.",
|
48: | 'min' => "'%s' should be greater than or equal to %d.",
|
49: | 'max' => "'%s' should be less than or equal to %d.",
|
50: | 'minLength' => "'%s' should have at least %d letters.",
|
51: | 'maxLength' => "'%s' should not exceed %d letters.",
|
52: | 'between' => "'%s' should be between %d and %d.",
|
53: | 'fileMaxSize' => "'%s' cannot exceed the maximum allowed upload size %dMB.",
|
54: | 'fileMaxWidth' => "'%s' cannot exceed the maximum allowed width %dpx.",
|
55: | 'fileMaxHeight' => "'%s' cannot exceed the maximum allowed height %dpx.",
|
56: | 'fileMaxDimension' => "'%s' cannot exceed the maximum allowed dimension %dpx%dpx.",
|
57: | 'fileExactDimension' => "'%s' should have the dimension %dx%dpx.",
|
58: | 'fileExtension' => "'%s' must be one of the file types: %s.",
|
59: | 'date' => "'%s' should be valid for the date format '%s'.",
|
60: | 'time' => "'%s' should be valid for the time format '%s'.",
|
61: | 'datetime' => "'%s' should be valid for the date/time format '%s %s'.",
|
62: | 'unique' => "'%s' already exists. Please try another one.",
|
63: | 'custom' => "'%s' should be a valid format."
|
64: | );
|
65: |
|
66: | if (function_exists('__validation_messages')) {
|
67: | $validationMessages = array_merge($validationMessages, __validation_messages());
|
68: | }
|
69: |
|
70: | $i18nEnabled = function_exists('_t');
|
71: |
|
72: | foreach ($validationMessages as $key => $msg) {
|
73: | $validationMessages[$key] = ($i18nEnabled) ? _t($msg) : $msg;
|
74: | }
|
75: |
|
76: | Validation::set('messages', $validationMessages);
|
77: | }
|
78: |
|
79: | |
80: | |
81: | |
82: | |
83: | |
84: |
|
85: | function validation_set($key, $value = null)
|
86: | {
|
87: | Validation::set($key, $value);
|
88: | }
|
89: |
|
90: | |
91: | |
92: | |
93: | |
94: |
|
95: | function validation_get($key)
|
96: | {
|
97: | return Validation::get($key);
|
98: | }
|
99: |
|
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | |
107: | |
108: | |
109: | |
110: |
|
111: | function validation_check($validations, $data = [], $type = Validation::TYPE_MULTI)
|
112: | {
|
113: | return Validation::check($validations, $data, $type);
|
114: | }
|
115: |
|
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: |
|
124: | function validation_addError($id, $msg)
|
125: | {
|
126: | Validation::addError($id, $msg);
|
127: | }
|
128: |
|
129: | |
130: | |
131: | |
132: | |
133: |
|
134: | function validate_mandatory($value)
|
135: | {
|
136: | if (is_array($value) && count($value) == 0) {
|
137: | return false;
|
138: | }
|
139: |
|
140: | if (is_array($value) && isset($value['name']) && empty($value['name'])) {
|
141: | return false;
|
142: | }
|
143: |
|
144: | if (empty($value) && $value != '0') {
|
145: | return false;
|
146: | }
|
147: |
|
148: | return (is_array($value)) ? true : preg_match('/[^\s]+/', $value);
|
149: | }
|
150: | |
151: | |
152: | |
153: | |
154: |
|
155: | function validate_mandatoryOne($value)
|
156: | {
|
157: | if (is_array($value)) {
|
158: | $value = array_unique($value);
|
159: | $empty = true;
|
160: | foreach ($value as $v) {
|
161: | if (preg_match('/[^\s]+/', $v)) {
|
162: |
|
163: | $empty = false;
|
164: | }
|
165: | }
|
166: | return !$empty;
|
167: | } else {
|
168: | return preg_match('/[^\s]+/', $value);
|
169: | }
|
170: | }
|
171: | |
172: | |
173: | |
174: | |
175: |
|
176: | function validate_mandatoryAll($value)
|
177: | {
|
178: | if (is_array($value)) {
|
179: | $value = array_unique($value);
|
180: | foreach ($value as $v) {
|
181: | if (preg_match('/[\s]+/', $v)) {
|
182: |
|
183: | return false;
|
184: | }
|
185: | }
|
186: | return true;
|
187: | } else {
|
188: | return preg_match('/[^\s]+/', $value);
|
189: | }
|
190: | }
|
191: | |
192: | |
193: | |
194: | |
195: |
|
196: | function validate_notAllowZero($value)
|
197: | {
|
198: | $value = trim($value);
|
199: |
|
200: | return ($value == '0' || $value == 0) ? false : true;
|
201: | }
|
202: | |
203: | |
204: | |
205: | |
206: |
|
207: | function validate_alphaNumeric($value)
|
208: | {
|
209: | $value = trim($value);
|
210: | if ($value == '') {
|
211: | return true;
|
212: | }
|
213: |
|
214: |
|
215: | return preg_match('/^[A-Za-z0-9]+$/', $value);
|
216: | }
|
217: | |
218: | |
219: | |
220: | |
221: |
|
222: | function validate_alphaNumericSpace($value)
|
223: | {
|
224: | $value = trim($value);
|
225: | if ($value == '') {
|
226: | return true;
|
227: | }
|
228: |
|
229: |
|
230: | return preg_match('/^[A-Za-z0-9 ]+$/', $value);
|
231: | }
|
232: | |
233: | |
234: | |
235: | |
236: |
|
237: | function validate_alphaNumericDash($value)
|
238: | {
|
239: | $value = trim($value);
|
240: | if ($value == '') {
|
241: | return true;
|
242: | }
|
243: |
|
244: |
|
245: | return preg_match('/^[A-Za-z0-9\-]+$/', $value);
|
246: | }
|
247: | |
248: | |
249: | |
250: | |
251: |
|
252: | function validate_numeric($value)
|
253: | {
|
254: | $value = trim($value);
|
255: | if ($value == '') {
|
256: | return true;
|
257: | }
|
258: |
|
259: |
|
260: | return is_numeric($value);
|
261: | }
|
262: | |
263: | |
264: | |
265: | |
266: |
|
267: | function validate_numericDash($value)
|
268: | {
|
269: | if (is_numeric($value) && strlen($value) == 1) {
|
270: | return true;
|
271: | }
|
272: |
|
273: | if (empty($value)) {
|
274: | return true;
|
275: | }
|
276: |
|
277: | return preg_match('/^([0-9])+([0-9\-])*([0-9])+$/', $value);
|
278: | }
|
279: | |
280: | |
281: | |
282: | |
283: |
|
284: | function validate_numericSpace($value)
|
285: | {
|
286: | if (is_numeric($value) && strlen($value) == 1) {
|
287: | return true;
|
288: | }
|
289: |
|
290: | if (empty($value)) {
|
291: | return true;
|
292: | }
|
293: |
|
294: | return preg_match('/^[0-9 ]+$/', $value);
|
295: | }
|
296: | |
297: | |
298: | |
299: | |
300: |
|
301: | function validate_username($value)
|
302: | {
|
303: | $value = trim($value);
|
304: | if ($value == '') {
|
305: | return true;
|
306: | }
|
307: |
|
308: |
|
309: | return preg_match('/^([A-Za-z])+([A-Za-z0-9_\-\.])*([A-Za-z0-9])+$/', $value);
|
310: | }
|
311: | |
312: | |
313: | |
314: | |
315: | |
316: | |
317: |
|
318: | function validate_naturalNumber($value)
|
319: | {
|
320: | $value = trim($value);
|
321: | if ($value == '') {
|
322: | return true;
|
323: | }
|
324: |
|
325: | return preg_match('/^[1-9][0-9]*$/', $value);
|
326: | }
|
327: | |
328: | |
329: | |
330: | |
331: | |
332: |
|
333: | function validate_wholeNumber($value)
|
334: | {
|
335: | $value = trim($value);
|
336: | if ($value == '') {
|
337: | return true;
|
338: | }
|
339: |
|
340: | return preg_match('/^(?:0|[1-9][0-9]*)$/', $value);
|
341: | }
|
342: | |
343: | |
344: | |
345: | |
346: | |
347: |
|
348: | function validate_integer($value)
|
349: | {
|
350: | $value = trim($value);
|
351: | if ($value == '') {
|
352: | return true;
|
353: | }
|
354: |
|
355: | return preg_match('/^[-]?(?:0|[1-9][0-9]*)$/', $value);
|
356: | }
|
357: | |
358: | |
359: | |
360: | |
361: | |
362: |
|
363: | function validate_rationalNumber($value)
|
364: | {
|
365: | $value = trim($value);
|
366: | if ($value == '') {
|
367: | return true;
|
368: | }
|
369: |
|
370: | return preg_match('/^[-]?[0-9]*[\.]?[0-9]+$/', $value);
|
371: | }
|
372: | |
373: | |
374: | |
375: | |
376: | |
377: |
|
378: | function validate_positiveRationalNumber($value)
|
379: | {
|
380: | $value = trim($value);
|
381: | if ($value == '') {
|
382: | return true;
|
383: | }
|
384: |
|
385: | return preg_match('/^[0-9]*[\.]?[0-9]+$/', $value);
|
386: | }
|
387: | |
388: | |
389: | |
390: | |
391: |
|
392: | function validate_email($value)
|
393: | {
|
394: | $value = trim($value);
|
395: | if ($value == '') {
|
396: | return true;
|
397: | }
|
398: |
|
399: | return preg_match('/^[A-Za-z0-9]([A-Za-z0-9]|_|\.|\-)*@([a-z0-9]|\.|\-)+\.[a-z]{2,4}$/', $value);
|
400: | }
|
401: | |
402: | |
403: | |
404: | |
405: |
|
406: | function validate_domain($value)
|
407: | {
|
408: | if (empty($value)) {
|
409: | return true;
|
410: | }
|
411: |
|
412: | return preg_match('/^([a-z])+([a-z0-9\-])*([a-z0-9])+$/i', $value);
|
413: | }
|
414: | |
415: | |
416: | |
417: | |
418: |
|
419: | function validate_url($value)
|
420: | {
|
421: | if (empty($value)) {
|
422: | return true;
|
423: | }
|
424: |
|
425: | $value = rtrim($value, '/');
|
426: |
|
427: |
|
428: | preg_match("/^((http|https|ftp):\/\/)?([^\/]+)/i", $value, $matches);
|
429: | $host = $matches[3];
|
430: | $hostParts = explode('.', $host);
|
431: |
|
432: | if (strstr($host, '@') !== false) {
|
433: | return false;
|
434: | }
|
435: |
|
436: | if (preg_match('/^(w+)$/i', $hostParts[0], $matches) && strlen($matches[0]) != 3) {
|
437: | return false;
|
438: | }
|
439: |
|
440: | return preg_match('/^((http|https|ftp):\/\/)?([a-z0-9_\-]+\.)+([a-z]{2,13})(\/[.^\S]+)*$/', $value);
|
441: | }
|
442: | |
443: | |
444: | |
445: | |
446: | |
447: |
|
448: | function validate_exactLength($value, $length)
|
449: | {
|
450: | if (is_array($value)) {
|
451: | return count($value) == $length;
|
452: | }
|
453: |
|
454: | return mb_strlen($value) == $length;
|
455: | }
|
456: | |
457: | |
458: | |
459: | |
460: | |
461: |
|
462: | function validate_minLength($value, $min)
|
463: | {
|
464: | return mb_strlen($value) >= $min;
|
465: | }
|
466: | |
467: | |
468: | |
469: | |
470: | |
471: |
|
472: | function validate_maxLength($value, $max)
|
473: | {
|
474: | $length = mb_strlen($value);
|
475: | return ($length <= $max);
|
476: | }
|
477: | |
478: | |
479: | |
480: | |
481: | |
482: |
|
483: | function validate_min($value, $min)
|
484: | {
|
485: | return $value >= $min;
|
486: | }
|
487: | |
488: | |
489: | |
490: | |
491: | |
492: |
|
493: | function validate_max($value, $max)
|
494: | {
|
495: | return $value <= $max;
|
496: | }
|
497: | |
498: | |
499: | |
500: | |
501: | |
502: | |
503: |
|
504: | function validate_between($value, $min, $max)
|
505: | {
|
506: | return $value >= $min && $value <= $max;
|
507: | }
|
508: | |
509: | |
510: | |
511: | |
512: | |
513: | |
514: | |
515: |
|
516: | function validate_custom($value, $pattern)
|
517: | {
|
518: | if (empty($value) && $value != '0') {
|
519: | return true;
|
520: | }
|
521: |
|
522: | return preg_match($pattern, $value);
|
523: | }
|
524: | |
525: | |
526: | |
527: | |
528: | |
529: |
|
530: | function validate_fileExtension($value, $extensions = array('jpg', 'jpeg', 'png', 'gif'))
|
531: | {
|
532: | if (!is_array($value)) {
|
533: | return true;
|
534: | }
|
535: |
|
536: | if (!file_exists($value['tmp_name'])) {
|
537: | return true;
|
538: | }
|
539: |
|
540: | if (empty($value['name'])) {
|
541: | return true;
|
542: | }
|
543: |
|
544: | $ext = explode('.', $value['name']);
|
545: | $ext = strtolower(end($ext));
|
546: |
|
547: | return in_array($ext, $extensions);
|
548: | }
|
549: | |
550: | |
551: | |
552: | |
553: | |
554: |
|
555: | function validate_fileMaxSize($value, $maxSize = null)
|
556: | {
|
557: | if (!is_array($value)) {
|
558: | return true;
|
559: | }
|
560: |
|
561: | if (is_null($maxSize)) {
|
562: | return true;
|
563: | }
|
564: |
|
565: | $fileSize = $value['size'];
|
566: | $maxSize = $maxSize * 1024 * 1024;
|
567: |
|
568: | return $fileSize <= $maxSize;
|
569: | }
|
570: | |
571: | |
572: | |
573: | |
574: | |
575: | |
576: | |
577: | |
578: |
|
579: | function validate_fileMaxDimension($value, $maxWidth, $maxHeight)
|
580: | {
|
581: | if (!is_array($value)) {
|
582: | return true;
|
583: | }
|
584: |
|
585: | if (!file_exists($value['tmp_name'])) {
|
586: | return true;
|
587: | }
|
588: |
|
589: | list($width, $height) = getimagesize($value['tmp_name']);
|
590: | return $width <= $maxWidth && $height <= $maxHeight;
|
591: | }
|
592: | |
593: | |
594: | |
595: | |
596: | |
597: | |
598: | |
599: | |
600: |
|
601: | function validate_fileExactDimension($value, $width, $height)
|
602: | {
|
603: | if (!is_array($value)) {
|
604: | return true;
|
605: | }
|
606: |
|
607: | if (!file_exists($value['tmp_name'])) {
|
608: | return true;
|
609: | }
|
610: |
|
611: | list($w, $h) = getimagesize($value['tmp_name']);
|
612: |
|
613: | return $w == $width && $h == $height;
|
614: | }
|
615: | |
616: | |
617: | |
618: | |
619: | |
620: | |
621: | |
622: |
|
623: | function validate_fileMaxWidth($value, $maxWidth)
|
624: | {
|
625: | if (!is_array($value)) {
|
626: | return true;
|
627: | }
|
628: |
|
629: | if (!file_exists($value['tmp_name'])) {
|
630: | return true;
|
631: | }
|
632: |
|
633: | list($width, $height) = getimagesize($value['tmp_name']);
|
634: |
|
635: | return $width <= $maxWidth;
|
636: | }
|
637: | |
638: | |
639: | |
640: | |
641: | |
642: | |
643: | |
644: |
|
645: | function validate_fileMaxHeight($value, $maxHeight)
|
646: | {
|
647: | if (!is_array($value)) {
|
648: | return true;
|
649: | }
|
650: |
|
651: | if (!file_exists($value['tmp_name'])) {
|
652: | return true;
|
653: | }
|
654: |
|
655: | list($width, $height) = getimagesize($value['tmp_name']);
|
656: |
|
657: | return $height <= $maxHeight;
|
658: | }
|
659: | |
660: | |
661: | |
662: | |
663: | |
664: |
|
665: | function validate_ip($value, $type = 'both')
|
666: | {
|
667: | $type = strtolower($value);
|
668: | $flags = 0;
|
669: | if ($type === 'v4' || $type === 'ipv4') {
|
670: | $flags = FILTER_FLAG_IPV4;
|
671: | }
|
672: |
|
673: | if ($type === 'v6' || $type === 'ipv6') {
|
674: | $flags = FILTER_FLAG_IPV6;
|
675: | }
|
676: |
|
677: | return (boolean)filter_var($value, FILTER_VALIDATE_IP, array('flags' => $flags));
|
678: | }
|
679: | |
680: | |
681: | |
682: | |
683: | |
684: | |
685: | |
686: | |
687: | |
688: | |
689: | |
690: | |
691: |
|
692: | function validate_date($value, $format = 'y-m-d')
|
693: | {
|
694: | if (empty($value)) {
|
695: | return true;
|
696: | }
|
697: |
|
698: | $value = trim($value);
|
699: | $format = strtolower($format);
|
700: | $separators = array('/', '-', '.');
|
701: | $sepGroup = '([-\/.])';
|
702: | $cleanFormat = preg_replace('/'.$sepGroup.'/', '', $format);
|
703: |
|
704: | if (in_array($cleanFormat, array('dmy', 'mdy'))) {
|
705: | $pattern = '/^([\d]{1,2})'.$sepGroup.'([\d]{1,2})'.$sepGroup.'([\d]{4})$/';
|
706: | } else {
|
707: | $pattern = '/^([\d]{4})'.$sepGroup.'([\d]{1,2})'.$sepGroup.'([\d]{1,2})$/';
|
708: | }
|
709: |
|
710: | if ($pattern && preg_match_all($pattern, $value, $matches)) {
|
711: | if ($matches[2][0] != $matches[4][0]) {
|
712: | return false;
|
713: | }
|
714: |
|
715: | if (!in_array($matches[2][0], $separators)) {
|
716: | return false;
|
717: | }
|
718: |
|
719: | $sep = $matches[2][0];
|
720: | $dt = explode($sep, $value);
|
721: | $format = str_split($cleanFormat);
|
722: | $ft = array_flip($format);
|
723: | $y = $dt[$ft['y']];
|
724: | $m = $dt[$ft['m']];
|
725: | $d = $dt[$ft['d']];
|
726: |
|
727: | return checkdate($m, $d, $y);
|
728: | }
|
729: |
|
730: | return false;
|
731: | }
|
732: | |
733: | |
734: | |
735: | |
736: | |
737: | |
738: | |
739: | |
740: | |
741: | |
742: | |
743: | |
744: | |
745: | |
746: | |
747: |
|
748: | function validate_time($value, $timeFormat = 'both')
|
749: | {
|
750: | if (empty($value)) {
|
751: | return true;
|
752: | }
|
753: |
|
754: | $value = trim($value);
|
755: | $regex = array(
|
756: | '24' => '/^([01]?[0-9]|2[0-3]):([0-5][0-9])(:[0-5][0-9])?$/',
|
757: | '12' => '/^(0?[0-9]|1[0-2]):([0-5][0-9])(:[0-5][0-9])?\s*(am|pm)$/i'
|
758: | );
|
759: |
|
760: | if (!in_array($timeFormat, array('both', '12', '24'))) {
|
761: | $timeFormat = 'both';
|
762: | }
|
763: |
|
764: | if ($timeFormat === 'both') {
|
765: | $test = $regex;
|
766: | } else {
|
767: | $test = array($regex[$timeFormat]);
|
768: | }
|
769: |
|
770: | foreach ($test as $pattern) {
|
771: | if (preg_match($pattern, $value)) {
|
772: | return true;
|
773: | }
|
774: | }
|
775: |
|
776: | return false;
|
777: | }
|
778: | |
779: | |
780: | |
781: | |
782: | |
783: | |
784: | |
785: | |
786: | |
787: | |
788: | |
789: | |
790: | |
791: |
|
792: | function validate_datetime($value, $dateFormat = 'y-m-d', $timeFormat = 'both')
|
793: | {
|
794: | if (empty($value)) {
|
795: | return true;
|
796: | }
|
797: |
|
798: | $value = trim($value);
|
799: | $generalPattern = '/^([\d]{1,4}[-\/.][\d]{1,2}[-\/.][\d]{1,4})(\s+.{4,}\s*(am|pm)?)$/i';
|
800: | if (preg_match_all($generalPattern, $value, $matches)) {
|
801: | $date = $matches[1][0];
|
802: | $time = $matches[2][0];
|
803: | return validate_date($date, $dateFormat) && validate_time($time, $timeFormat);
|
804: | } else {
|
805: | return false;
|
806: | }
|
807: | }
|
808: | |
809: | |
810: | |
811: | |
812: | |
813: | |
814: | |
815: | |
816: |
|
817: | function validate_unique($value, $table, $field, $id = 0)
|
818: | {
|
819: | $value = strtolower($value);
|
820: | if (empty($value)) {
|
821: | return true;
|
822: | }
|
823: |
|
824: | $qb = db_count($table)
|
825: | ->where()
|
826: | ->condition($field, $value);
|
827: |
|
828: | if ($id) {
|
829: | $qb->condition('id !=', $id);
|
830: | }
|
831: |
|
832: | return $qb->fetch() ? false : true;
|
833: | }
|
834: | |