issue 1522
[civicrm-core.git] / CRM / Utils / Rule.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 require_once 'HTML/QuickForm/Rule/Email.php';
19
20 /**
21 * Class CRM_Utils_Rule
22 */
23 class CRM_Utils_Rule {
24
25 /**
26 * @param $str
27 * @param int $maxLength
28 *
29 * @return bool
30 */
31 public static function title($str, $maxLength = 127) {
32
33 // check length etc
34 if (empty($str) || strlen($str) > $maxLength) {
35 return FALSE;
36 }
37
38 // Make sure it include valid characters, alpha numeric and underscores
39 if (!preg_match('/^\w[\w\s\'\&\,\$\#\-\.\"\?\!]+$/i', $str)) {
40 return FALSE;
41 }
42
43 return TRUE;
44 }
45
46 /**
47 * @param $str
48 *
49 * @return bool
50 */
51 public static function longTitle($str) {
52 return self::title($str, 255);
53 }
54
55 /**
56 * @param $str
57 *
58 * @return bool
59 */
60 public static function variable($str) {
61 // check length etc
62 if (empty($str) || strlen($str) > 31) {
63 return FALSE;
64 }
65
66 // make sure it includes valid characters, alpha numeric and underscores
67 if (!preg_match('/^[\w]+$/i', $str)) {
68 return FALSE;
69 }
70
71 return TRUE;
72 }
73
74 /**
75 * Validate that a string is a valid MySQL column name or alias.
76 *
77 * @param $str
78 *
79 * @return bool
80 */
81 public static function mysqlColumnNameOrAlias($str) {
82 // Check not empty.
83 if (empty($str)) {
84 return FALSE;
85 }
86
87 // Ensure $str conforms to expected format. Not a complete expression of
88 // what MySQL permits; this should permit the formats CiviCRM generates.
89 //
90 // * Table name prefix is optional.
91 // * Table & column names & aliases:
92 // * Composed of alphanumeric chars, underscore and hyphens.
93 // * Maximum length of 64 chars.
94 // * Optionally surrounded by backticks, in which case spaces also OK.
95 if (!preg_match('/^((`[-\w ]{1,64}`|[-\w]{1,64})\.)?(`[-\w ]{1,64}`|[-\w]{1,64})$/i', $str)) {
96 return FALSE;
97 }
98
99 return TRUE;
100 }
101
102 /**
103 * Validate that a string is ASC or DESC.
104 *
105 * Empty string should be treated as invalid and ignored => default = ASC.
106 *
107 * @param $str
108 * @return bool
109 */
110 public static function mysqlOrderByDirection($str) {
111 if (!preg_match('/^(asc|desc)$/i', $str)) {
112 return FALSE;
113 }
114
115 return TRUE;
116 }
117
118 /**
119 * Validate that a string is valid order by clause.
120 *
121 * @param $str
122 * @return bool
123 */
124 public static function mysqlOrderBy($str) {
125 $matches = [];
126 // Using the field function in order by is valid.
127 // Look for a string like field(contribution_status_id,3,4,6).
128 // or field(civicrm_contribution.contribution_status_id,3,4,6)
129 if (preg_match('/field\([a-z_.]+,[0-9,]+\)/', $str, $matches)) {
130 // We have checked these. Remove them as they will fail the next lot.
131 // Our check currently only permits numbers & no back ticks. If we get a
132 // need for strings or backticks we can add.
133 $str = str_replace($matches, '', $str);
134 }
135 $str = trim($str);
136 if (!empty($matches) && empty($str)) {
137 // nothing left to check after the field check.
138 return TRUE;
139 }
140 // Making a regex for a comma separated list is quite hard and not readable
141 // at all, so we split and loop over.
142 $parts = explode(',', $str);
143 foreach ($parts as $part) {
144 if (!preg_match('/^((`[\w-]{1,64}`|[\w-]{1,64})\.)*(`[\w-]{1,64}`|[\w-]{1,64})( (asc|desc))?$/i', trim($part))) {
145 return FALSE;
146 }
147 }
148
149 return TRUE;
150 }
151
152 /**
153 * @param $str
154 *
155 * @return bool
156 */
157 public static function qfVariable($str) {
158 // check length etc
159 //if ( empty( $str ) || strlen( $str ) > 31 ) {
160 if (strlen(trim($str)) == 0 || strlen($str) > 31) {
161 return FALSE;
162 }
163
164 // make sure it includes valid characters, alpha numeric and underscores
165 // added (. and ,) option (CRM-1336)
166 if (!preg_match('/^[\w\s\.\,]+$/i', $str)) {
167 return FALSE;
168 }
169
170 return TRUE;
171 }
172
173 /**
174 * @param $phone
175 *
176 * @return bool
177 */
178 public static function phone($phone) {
179 // check length etc
180 if (empty($phone) || strlen($phone) > 16) {
181 return FALSE;
182 }
183
184 // make sure it includes valid characters, (, \s and numeric
185 if (preg_match('/^[\d\(\)\-\.\s]+$/', $phone)) {
186 return TRUE;
187 }
188 return FALSE;
189 }
190
191 /**
192 * @param $query
193 *
194 * @return bool
195 */
196 public static function query($query) {
197 // check length etc
198 if (empty($query) || strlen($query) < 3 || strlen($query) > 127) {
199 return FALSE;
200 }
201
202 // make sure it includes valid characters, alpha numeric and underscores
203 if (!preg_match('/^[\w\s\%\'\&\,\$\#]+$/i', $query)) {
204 return FALSE;
205 }
206
207 return TRUE;
208 }
209
210 /**
211 * @param $url
212 *
213 * @return bool
214 */
215 public static function url($url) {
216 if (!$url) {
217 // If this is required then that should be checked elsewhere - here we are not assuming it is required.
218 return TRUE;
219 }
220 if (preg_match('/^\//', $url)) {
221 // allow relative URL's (CRM-15598)
222 $url = 'http://' . $_SERVER['HTTP_HOST'] . $url;
223 }
224 return (bool) filter_var($url, FILTER_VALIDATE_URL);
225 }
226
227 /**
228 * @param $url
229 *
230 * @return bool
231 */
232 public static function urlish($url) {
233 if (empty($url)) {
234 return TRUE;
235 }
236 $url = Civi::paths()->getUrl($url, 'absolute');
237 return (bool) filter_var($url, FILTER_VALIDATE_URL);
238 }
239
240 /**
241 * @param $string
242 *
243 * @return bool
244 */
245 public static function wikiURL($string) {
246 $items = explode(' ', trim($string), 2);
247 return self::url($items[0]);
248 }
249
250 /**
251 * @param $domain
252 *
253 * @return bool
254 */
255 public static function domain($domain) {
256 // not perfect, but better than the previous one; see CRM-1502
257 if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) {
258 return FALSE;
259 }
260 return TRUE;
261 }
262
263 /**
264 * @param $value
265 * @param null $default
266 *
267 * @return null
268 */
269 public static function date($value, $default = NULL) {
270 if (is_string($value) &&
271 preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value)
272 ) {
273 return $value;
274 }
275 return $default;
276 }
277
278 /**
279 * @param $value
280 * @param null $default
281 *
282 * @return null|string
283 */
284 public static function dateTime($value, $default = NULL) {
285 $result = $default;
286 if (is_string($value) &&
287 preg_match('/^\d\d\d\d-?\d\d-?\d\d(\s\d\d:\d\d(:\d\d)?|\d\d\d\d(\d\d)?)?$/', $value)
288 ) {
289 $result = $value;
290 }
291
292 return $result;
293 }
294
295 /**
296 * Check the validity of the date (in qf format)
297 * note that only a year is valid, or a mon-year is
298 * also valid in addition to day-mon-year. The date
299 * specified has to be beyond today. (i.e today or later)
300 *
301 * @param array $date
302 * @param bool $monthRequired
303 * Check whether month is mandatory.
304 *
305 * @return bool
306 * true if valid date
307 */
308 public static function currentDate($date, $monthRequired = TRUE) {
309 $config = CRM_Core_Config::singleton();
310
311 $d = CRM_Utils_Array::value('d', $date);
312 $m = CRM_Utils_Array::value('M', $date);
313 $y = CRM_Utils_Array::value('Y', $date);
314
315 if (!$d && !$m && !$y) {
316 return TRUE;
317 }
318
319 // CRM-9017 CiviContribute/CiviMember form with expiration date format 'm Y'
320 if (!$m && !empty($date['m'])) {
321 $m = CRM_Utils_Array::value('m', $date);
322 }
323
324 $day = $mon = 1;
325 $year = 0;
326 if ($d) {
327 $day = $d;
328 }
329 if ($m) {
330 $mon = $m;
331 }
332 if ($y) {
333 $year = $y;
334 }
335
336 // if we have day we need mon, and if we have mon we need year
337 if (($d && !$m) ||
338 ($d && !$y) ||
339 ($m && !$y)
340 ) {
341 return FALSE;
342 }
343
344 $result = FALSE;
345 if (!empty($day) || !empty($mon) || !empty($year)) {
346 $result = checkdate($mon, $day, $year);
347 }
348
349 if (!$result) {
350 return FALSE;
351 }
352
353 // ensure we have month if required
354 if ($monthRequired && !$m) {
355 return FALSE;
356 }
357
358 // now make sure this date is greater that today
359 $currentDate = getdate();
360 if ($year > $currentDate['year']) {
361 return TRUE;
362 }
363 elseif ($year < $currentDate['year']) {
364 return FALSE;
365 }
366
367 if ($m) {
368 if ($mon > $currentDate['mon']) {
369 return TRUE;
370 }
371 elseif ($mon < $currentDate['mon']) {
372 return FALSE;
373 }
374 }
375
376 if ($d) {
377 if ($day > $currentDate['mday']) {
378 return TRUE;
379 }
380 elseif ($day < $currentDate['mday']) {
381 return FALSE;
382 }
383 }
384
385 return TRUE;
386 }
387
388 /**
389 * Check the validity of a date or datetime (timestamp)
390 * value which is in YYYYMMDD or YYYYMMDDHHMMSS format
391 *
392 * Uses PHP checkdate() - params are ( int $month, int $day, int $year )
393 *
394 * @param string $date
395 *
396 * @return bool
397 * true if valid date
398 */
399 public static function mysqlDate($date) {
400 // allow date to be null
401 if ($date == NULL) {
402 return TRUE;
403 }
404
405 if (checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
406 return TRUE;
407 }
408
409 return FALSE;
410 }
411
412 /**
413 * @param $value
414 *
415 * @return bool
416 */
417 public static function integer($value) {
418 if (is_int($value)) {
419 return TRUE;
420 }
421
422 // CRM-13460
423 // ensure number passed is always a string numeral
424 if (!is_numeric($value)) {
425 return FALSE;
426 }
427
428 // note that is_int matches only integer type
429 // and not strings which are only integers
430 // hence we do this here
431 if (preg_match('/^\d+$/', $value)) {
432 return TRUE;
433 }
434
435 if ($value < 0) {
436 $negValue = -1 * $value;
437 if (is_int($negValue)) {
438 return TRUE;
439 }
440 }
441
442 return FALSE;
443 }
444
445 /**
446 * @param $value
447 *
448 * @return bool
449 */
450 public static function positiveInteger($value) {
451 if (is_int($value)) {
452 return ($value < 0) ? FALSE : TRUE;
453 }
454
455 // CRM-13460
456 // ensure number passed is always a string numeral
457 if (!is_numeric($value)) {
458 return FALSE;
459 }
460
461 if (preg_match('/^\d+$/', $value)) {
462 return TRUE;
463 }
464
465 return FALSE;
466 }
467
468 /**
469 * @param $value
470 *
471 * @return bool
472 */
473 public static function commaSeparatedIntegers($value) {
474 foreach (explode(',', $value) as $val) {
475 // Remove any Whitespace around the key.
476 $val = trim($val);
477 if (!self::positiveInteger($val)) {
478 return FALSE;
479 }
480 }
481 return TRUE;
482 }
483
484 /**
485 * @param $value
486 *
487 * @return bool
488 */
489 public static function numeric($value) {
490 // lets use a php gatekeeper to ensure this is numeric
491 if (!is_numeric($value)) {
492 return FALSE;
493 }
494
495 return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ? TRUE : FALSE;
496 }
497
498 /**
499 * Test whether $value is alphanumeric.
500 *
501 * Underscores and dashes are also allowed!
502 *
503 * This is the type of string you could expect to see in URL parameters
504 * like `?mode=live` vs `?mode=test`. This function exists so that we can be
505 * strict about what we accept for such values, thus mitigating against
506 * potential security issues.
507 *
508 * @see \CRM_Utils_RuleTest::alphanumericData
509 * for examples of vales that give TRUE/FALSE here
510 *
511 * @param $value
512 *
513 * @return bool
514 */
515 public static function alphanumeric($value) {
516 return preg_match('/^[a-zA-Z0-9_-]*$/', $value) ? TRUE : FALSE;
517 }
518
519 /**
520 * @param $value
521 * @param $noOfDigit
522 *
523 * @return bool
524 */
525 public static function numberOfDigit($value, $noOfDigit) {
526 return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ? TRUE : FALSE;
527 }
528
529 /**
530 * Strict validation of 6-digit hex color notation per html5 <input type="color">
531 *
532 * @param $value
533 * @return bool
534 */
535 public static function color($value) {
536 return (bool) preg_match('/^#([\da-fA-F]{6})$/', $value);
537 }
538
539 /**
540 * Strip thousand separator from a money string.
541 *
542 * Note that this should be done at the form layer. Once we are processing
543 * money at the BAO or processor layer we should be working with something that
544 * is already in a normalised format.
545 *
546 * @param string $value
547 *
548 * @return string
549 */
550 public static function cleanMoney($value) {
551 // first remove all white space
552 $value = str_replace([' ', "\t", "\n"], '', $value);
553
554 $config = CRM_Core_Config::singleton();
555
556 //CRM-14868
557 $currencySymbols = CRM_Core_PseudoConstant::get(
558 'CRM_Contribute_DAO_Contribution',
559 'currency', [
560 'keyColumn' => 'name',
561 'labelColumn' => 'symbol',
562 ]
563 );
564 $value = str_replace($currencySymbols, '', $value);
565
566 if ($config->monetaryThousandSeparator) {
567 $mon_thousands_sep = $config->monetaryThousandSeparator;
568 }
569 else {
570 $mon_thousands_sep = ',';
571 }
572
573 // ugly fix for CRM-6391: do not drop the thousand separator if
574 // it looks like it’s separating decimal part (because a given
575 // value undergoes a second cleanMoney() call, for example)
576 // CRM-15835 - in case the amount/value contains 0 after decimal
577 // eg 150.5 the following if condition will pass
578 if ($mon_thousands_sep != '.' or (substr($value, -3, 1) != '.' && substr($value, -2, 1) != '.')) {
579 $value = str_replace($mon_thousands_sep, '', $value);
580 }
581
582 if ($config->monetaryDecimalPoint) {
583 $mon_decimal_point = $config->monetaryDecimalPoint;
584 }
585 else {
586 $mon_decimal_point = '.';
587 }
588 $value = str_replace($mon_decimal_point, '.', $value);
589
590 return $value;
591 }
592
593 /**
594 * @param $value
595 *
596 * @return bool
597 */
598 public static function money($value) {
599 $value = self::cleanMoney($value);
600
601 if (self::integer($value)) {
602 return TRUE;
603 }
604
605 // Allow values such as -0, 1.024555, -.1
606 // We need to support multiple decimal places here, not just the number allowed by locale
607 // otherwise tax calculations break when you want the inclusive amount to be a round number (eg. £10 inc. VAT requires 8.333333333 here).
608 return preg_match('/(^-?\d+\.?\d*$)|(^-?\.\d+$)/', $value) ? TRUE : FALSE;
609 }
610
611 /**
612 * @param $value
613 * @param int $maxLength
614 *
615 * @return bool
616 */
617 public static function string($value, $maxLength = 0) {
618 if (is_string($value) &&
619 ($maxLength === 0 || strlen($value) <= $maxLength)
620 ) {
621 return TRUE;
622 }
623 return FALSE;
624 }
625
626 /**
627 * @param $value
628 *
629 * @return bool
630 */
631 public static function boolean($value) {
632 return preg_match(
633 '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value
634 ) ? TRUE : FALSE;
635 }
636
637 /**
638 * @param $value
639 *
640 * @return bool
641 */
642 public static function email($value) {
643 return (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
644 }
645
646 /**
647 * @param $list
648 *
649 * @return bool
650 */
651 public static function emailList($list) {
652 $emails = explode(',', $list);
653 foreach ($emails as $email) {
654 $email = trim($email);
655 if (!self::email($email)) {
656 return FALSE;
657 }
658 }
659 return TRUE;
660 }
661
662 /**
663 * allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or
664 * if u disregard the first 0, 4 (thanx excel!)
665 * FIXME: we need to figure out how to localize such rules
666 * @param $value
667 *
668 * @return bool
669 */
670 public static function postalCode($value) {
671 if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) {
672 return TRUE;
673 }
674 return FALSE;
675 }
676
677 /**
678 * See how file rules are written in HTML/QuickForm/file.php
679 * Checks to make sure the uploaded file is ascii
680 *
681 * @param string $elementValue
682 *
683 * @return bool
684 * True if file has been uploaded, false otherwise
685 */
686 public static function asciiFile($elementValue) {
687 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
688 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
689 ) {
690 return CRM_Utils_File::isAscii($elementValue['tmp_name']);
691 }
692 return FALSE;
693 }
694
695 /**
696 * Checks to make sure the uploaded file is in UTF-8, recodes if it's not
697 *
698 * @param array $elementValue
699 *
700 * @return bool
701 * Whether file has been uploaded properly and is now in UTF-8.
702 */
703 public static function utf8File($elementValue) {
704 $success = FALSE;
705
706 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
707 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
708 ) {
709
710 $success = CRM_Utils_File::isAscii($elementValue['tmp_name']);
711
712 // if it's a file, but not UTF-8, let's try and recode it
713 // and then make sure it's an UTF-8 file in the end
714 if (!$success) {
715 $success = CRM_Utils_File::toUtf8($elementValue['tmp_name']);
716 if ($success) {
717 $success = CRM_Utils_File::isAscii($elementValue['tmp_name']);
718 }
719 }
720 }
721 return $success;
722 }
723
724 /**
725 * See how file rules are written in HTML/QuickForm/file.php
726 * Checks to make sure the uploaded file is html
727 *
728 * @param array $elementValue
729 *
730 * @return bool
731 * True if file has been uploaded, false otherwise
732 */
733 public static function htmlFile($elementValue) {
734 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
735 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
736 ) {
737 return CRM_Utils_File::isHtmlFile($elementValue['tmp_name']);
738 }
739 return FALSE;
740 }
741
742 /**
743 * Check if there is a record with the same name in the db.
744 *
745 * @param string $value
746 * The value of the field we are checking.
747 * @param array $options
748 * The daoName, fieldName (optional) and DomainID (optional).
749 *
750 * @return bool
751 * true if object exists
752 */
753 public static function objectExists($value, $options) {
754 $name = 'name';
755 if (isset($options[2])) {
756 $name = $options[2];
757 }
758
759 return CRM_Core_DAO::objectExists($value, CRM_Utils_Array::value(0, $options), CRM_Utils_Array::value(1, $options), CRM_Utils_Array::value(2, $options, $name), CRM_Utils_Array::value(3, $options));
760 }
761
762 /**
763 * @param $value
764 * @param $options
765 *
766 * @return bool
767 */
768 public static function optionExists($value, $options) {
769 return CRM_Core_OptionValue::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array::value(3, $options, 'name'), CRM_Utils_Array::value(4, $options, FALSE));
770 }
771
772 /**
773 * @param $value
774 * @param $type
775 *
776 * @return bool
777 */
778 public static function creditCardNumber($value, $type) {
779 return Validate_Finance_CreditCard::number($value, $type);
780 }
781
782 /**
783 * @param $value
784 * @param $type
785 *
786 * @return bool
787 */
788 public static function cvv($value, $type) {
789 return Validate_Finance_CreditCard::cvv($value, $type);
790 }
791
792 /**
793 * @param $value
794 *
795 * @return bool
796 */
797 public static function currencyCode($value) {
798 static $currencyCodes = NULL;
799 if (!$currencyCodes) {
800 $currencyCodes = CRM_Core_PseudoConstant::currencyCode();
801 }
802 if (in_array($value, $currencyCodes)) {
803 return TRUE;
804 }
805 return FALSE;
806 }
807
808 /**
809 * @param $value
810 *
811 * @return bool
812 */
813 public static function xssString($value) {
814 if (is_string($value)) {
815 return preg_match('!<(vb)?script[^>]*>.*</(vb)?script.*>!ims',
816 $value
817 ) ? FALSE : TRUE;
818 }
819 else {
820 return TRUE;
821 }
822 }
823
824 /**
825 * Validate json string for xss
826 *
827 * @param string $value
828 *
829 * @return bool
830 * False if invalid, true if valid / safe.
831 */
832 public static function json($value) {
833 if (!self::xssString($value)) {
834 return FALSE;
835 }
836 $array = json_decode($value, TRUE);
837 if (!$array || !is_array($array)) {
838 return FALSE;
839 }
840 return self::arrayValue($array);
841 }
842
843 /**
844 * @param $path
845 *
846 * @return bool
847 */
848 public static function fileExists($path) {
849 return file_exists($path);
850 }
851
852 /**
853 * Determine whether the value contains a valid reference to a directory.
854 *
855 * Paths stored in the setting system may be absolute -- or may be
856 * relative to the default data directory.
857 *
858 * @param string $path
859 * @return bool
860 */
861 public static function settingPath($path) {
862 return is_dir(Civi::paths()->getPath($path));
863 }
864
865 /**
866 * @param $value
867 * @param null $actualElementValue
868 *
869 * @return bool
870 */
871 public static function validContact($value, $actualElementValue = NULL) {
872 if ($actualElementValue) {
873 $value = $actualElementValue;
874 }
875
876 return CRM_Utils_Rule::positiveInteger($value);
877 }
878
879 /**
880 * Check the validity of the date (in qf format)
881 * note that only a year is valid, or a mon-year is
882 * also valid in addition to day-mon-year
883 *
884 * @param array $date
885 *
886 * @return bool
887 * true if valid date
888 */
889 public static function qfDate($date) {
890 $config = CRM_Core_Config::singleton();
891
892 $d = CRM_Utils_Array::value('d', $date);
893 $m = CRM_Utils_Array::value('M', $date);
894 $y = CRM_Utils_Array::value('Y', $date);
895 if (isset($date['h']) ||
896 isset($date['g'])
897 ) {
898 $m = CRM_Utils_Array::value('M', $date);
899 }
900
901 if (!$d && !$m && !$y) {
902 return TRUE;
903 }
904
905 $day = $mon = 1;
906 $year = 0;
907 if ($d) {
908 $day = $d;
909 }
910 if ($m) {
911 $mon = $m;
912 }
913 if ($y) {
914 $year = $y;
915 }
916
917 // if we have day we need mon, and if we have mon we need year
918 if (($d && !$m) ||
919 ($d && !$y) ||
920 ($m && !$y)
921 ) {
922 return FALSE;
923 }
924
925 if (!empty($day) || !empty($mon) || !empty($year)) {
926 return checkdate($mon, $day, $year);
927 }
928 return FALSE;
929 }
930
931 /**
932 * @param $key
933 *
934 * @return bool
935 */
936 public static function qfKey($key) {
937 return ($key) ? CRM_Core_Key::valid($key) : FALSE;
938 }
939
940 /**
941 * Check if the values in the date range are in correct chronological order.
942 *
943 * @param array $fields
944 * Fields of the form.
945 * @param $fieldName
946 * Name of date range field.
947 * @param $errors
948 * The error array.
949 * @param $title
950 * Title of the date range to be displayed in the error message.
951 */
952 public static function validDateRange($fields, $fieldName, &$errors, $title) {
953 $lowDate = strtotime($fields[$fieldName . '_low']);
954 $highDate = strtotime($fields[$fieldName . '_high']);
955
956 if ($lowDate > $highDate) {
957 $errors[$fieldName . '_range_error'] = ts('%1: Please check that your date range is in correct chronological order.', [1 => $title]);
958 }
959 }
960
961 /**
962 * @param string $key Extension Key to check
963 * @return bool
964 */
965 public static function checkExtensionKeyIsValid($key = NULL) {
966 if (!empty($key) && !preg_match('/^[0-9a-zA-Z._-]+$/', $key)) {
967 return FALSE;
968 }
969 return TRUE;
970 }
971
972 /**
973 * Validate array recursively checking keys and values.
974 *
975 * @param array $array
976 * @return bool
977 */
978 protected static function arrayValue($array) {
979 foreach ($array as $key => $item) {
980 if (is_array($item)) {
981 if (!self::xssString($key) || !self::arrayValue($item)) {
982 return FALSE;
983 }
984 }
985 if (!self::xssString($key) || !self::xssString($item)) {
986 return FALSE;
987 }
988 }
989 return TRUE;
990 }
991
992 }