Removed unnecessary parts of the Mysql regexes.
[civicrm-core.git] / CRM / Utils / Rule.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
fa938177 31 * @copyright CiviCRM LLC (c) 2004-2016
6a488035
TO
32 */
33
34require_once 'HTML/QuickForm/Rule/Email.php';
f942c321 35
5bc392e6
EM
36/**
37 * Class CRM_Utils_Rule
38 */
6a488035
TO
39class CRM_Utils_Rule {
40
5bc392e6
EM
41 /**
42 * @param $str
43 * @param int $maxLength
44 *
45 * @return bool
46 */
00be9182 47 public static function title($str, $maxLength = 127) {
6a488035
TO
48
49 // check length etc
50 if (empty($str) || strlen($str) > $maxLength) {
51 return FALSE;
52 }
53
54 // Make sure it include valid characters, alpha numeric and underscores
55 if (!preg_match('/^\w[\w\s\'\&\,\$\#\-\.\"\?\!]+$/i', $str)) {
56 return FALSE;
57 }
58
59 return TRUE;
60 }
61
5bc392e6
EM
62 /**
63 * @param $str
64 *
65 * @return bool
66 */
00be9182 67 public static function longTitle($str) {
6a488035
TO
68 return self::title($str, 255);
69 }
70
5bc392e6
EM
71 /**
72 * @param $str
73 *
74 * @return bool
75 */
00be9182 76 public static function variable($str) {
6a488035
TO
77 // check length etc
78 if (empty($str) || strlen($str) > 31) {
79 return FALSE;
80 }
81
50bfb460 82 // make sure it includes valid characters, alpha numeric and underscores
6a488035
TO
83 if (!preg_match('/^[\w]+$/i', $str)) {
84 return FALSE;
85 }
86
87 return TRUE;
88 }
89
00f11506
MM
90 /**
91 * @param $str
92 *
93 * @return bool
94 */
537e8cb5 95 public static function mysqlColumnNameLoose($str) {
00f11506
MM
96 // check the length.
97 // This check can be incorrect for the <table>.<column> format, which can be
98 // a problem.
99 if (empty($str) || strlen($str) > 64) {
100 return FALSE;
101 }
102
103 return TRUE;
104 }
105
106 /**
b794b580
CB
107 * Validate an acceptable column name for sorting results.
108 *
00f11506
MM
109 * @param $str
110 *
111 * @return bool
112 */
537e8cb5 113 public static function mysqlColumnName($str) {
b794b580 114 // Check the length.
00f11506
MM
115 if (empty($str) || strlen($str) > 64) {
116 return FALSE;
117 }
118
b794b580
CB
119 // Make sure it only contains valid characters (alphanumeric and underscores).
120 //
121 // MySQL permits column names that don't match this (eg containing spaces),
122 // but CiviCRM won't create those ...
f5f2b1ce 123 if (!preg_match('/^[\w]+(\.[\w]+)?$/i', $str)) {
00f11506
MM
124 return FALSE;
125 }
126
127 return TRUE;
128 }
129
130 /**
b794b580
CB
131 * Validate that a string is ASC or DESC.
132 *
133 * Empty string should be treated as invalid and ignored => default = ASC.
00f11506 134 *
b794b580 135 * @param $str
00f11506
MM
136 * @return bool
137 */
b794b580 138 public static function mysqlOrderByDirection($str) {
00f11506
MM
139 if (!preg_match('/^(asc|desc)$/i', $str)) {
140 return FALSE;
141 }
142
143 return TRUE;
144 }
145
0fa4baf0
MM
146 /**
147 * Validate that a string is valid order by clause.
148 *
149 * @param $str
150 * @return bool
151 */
152 public static function mysqlOrderBy($str) {
153 // Making a regex for a comma separated list is quite hard and not readable
154 // at all, so we split and loop over.
155 $parts = explode(',', $str);
156 foreach ($parts as $part) {
f5f2b1ce 157 if (!preg_match('/^(([\w]+)((\.)([\w]+))?( (asc|desc))?)$/i', trim($part))) {
0fa4baf0
MM
158 return FALSE;
159 }
160 }
161
162 return TRUE;
163 }
164
5bc392e6
EM
165 /**
166 * @param $str
167 *
168 * @return bool
169 */
00be9182 170 public static function qfVariable($str) {
6a488035
TO
171 // check length etc
172 //if ( empty( $str ) || strlen( $str ) > 31 ) {
173 if (strlen(trim($str)) == 0 || strlen($str) > 31) {
174 return FALSE;
175 }
176
50bfb460 177 // make sure it includes valid characters, alpha numeric and underscores
6a488035
TO
178 // added (. and ,) option (CRM-1336)
179 if (!preg_match('/^[\w\s\.\,]+$/i', $str)) {
180 return FALSE;
181 }
182
183 return TRUE;
184 }
185
5bc392e6
EM
186 /**
187 * @param $phone
188 *
189 * @return bool
190 */
00be9182 191 public static function phone($phone) {
6a488035
TO
192 // check length etc
193 if (empty($phone) || strlen($phone) > 16) {
194 return FALSE;
195 }
196
50bfb460 197 // make sure it includes valid characters, (, \s and numeric
6a488035
TO
198 if (preg_match('/^[\d\(\)\-\.\s]+$/', $phone)) {
199 return TRUE;
200 }
201 return FALSE;
202 }
203
5bc392e6
EM
204 /**
205 * @param $query
206 *
207 * @return bool
208 */
00be9182 209 public static function query($query) {
6a488035
TO
210 // check length etc
211 if (empty($query) || strlen($query) < 3 || strlen($query) > 127) {
212 return FALSE;
213 }
214
50bfb460 215 // make sure it includes valid characters, alpha numeric and underscores
6a488035
TO
216 if (!preg_match('/^[\w\s\%\'\&\,\$\#]+$/i', $query)) {
217 return FALSE;
218 }
219
220 return TRUE;
221 }
222
5bc392e6
EM
223 /**
224 * @param $url
225 *
226 * @return bool
227 */
00be9182 228 public static function url($url) {
1136a401 229 if (preg_match('/^\//', $url)) {
230 // allow relative URL's (CRM-15598)
231 $url = 'http://' . $_SERVER['HTTP_HOST'] . $url;
232 }
6a488035
TO
233 return (bool) filter_var($url, FILTER_VALIDATE_URL);
234 }
235
d9d7e7dd
TO
236 /**
237 * @param $url
238 *
239 * @return bool
240 */
241 public static function urlish($url) {
242 if (empty($url)) {
243 return TRUE;
244 }
e3d28c74 245 $url = Civi::paths()->getUrl($url, 'absolute');
d9d7e7dd
TO
246 return (bool) filter_var($url, FILTER_VALIDATE_URL);
247 }
248
5bc392e6
EM
249 /**
250 * @param $string
251 *
252 * @return bool
253 */
00be9182 254 public static function wikiURL($string) {
6a488035
TO
255 $items = explode(' ', trim($string), 2);
256 return self::url($items[0]);
257 }
258
5bc392e6
EM
259 /**
260 * @param $domain
261 *
262 * @return bool
263 */
00be9182 264 public static function domain($domain) {
6a488035
TO
265 // not perfect, but better than the previous one; see CRM-1502
266 if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) {
267 return FALSE;
268 }
269 return TRUE;
270 }
271
5bc392e6
EM
272 /**
273 * @param $value
274 * @param null $default
275 *
276 * @return null
277 */
00be9182 278 public static function date($value, $default = NULL) {
6a488035
TO
279 if (is_string($value) &&
280 preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value)
281 ) {
282 return $value;
283 }
284 return $default;
285 }
286
5bc392e6
EM
287 /**
288 * @param $value
289 * @param null $default
290 *
291 * @return null|string
292 */
00be9182 293 public static function dateTime($value, $default = NULL) {
6a488035
TO
294 $result = $default;
295 if (is_string($value) &&
296 preg_match('/^\d\d\d\d-?\d\d-?\d\d(\s\d\d:\d\d(:\d\d)?|\d\d\d\d(\d\d)?)?$/', $value)
297 ) {
298 $result = $value;
299 }
300
301 return $result;
302 }
303
304 /**
100fef9d 305 * Check the validity of the date (in qf format)
6a488035
TO
306 * note that only a year is valid, or a mon-year is
307 * also valid in addition to day-mon-year. The date
308 * specified has to be beyond today. (i.e today or later)
309 *
310 * @param array $date
77855840
TO
311 * @param bool $monthRequired
312 * Check whether month is mandatory.
6a488035 313 *
a6c01b45
CW
314 * @return bool
315 * true if valid date
6a488035 316 */
00be9182 317 public static function currentDate($date, $monthRequired = TRUE) {
6a488035
TO
318 $config = CRM_Core_Config::singleton();
319
320 $d = CRM_Utils_Array::value('d', $date);
321 $m = CRM_Utils_Array::value('M', $date);
322 $y = CRM_Utils_Array::value('Y', $date);
323
324 if (!$d && !$m && !$y) {
325 return TRUE;
326 }
327
328 // CRM-9017 CiviContribute/CiviMember form with expiration date format 'm Y'
8cc574cf 329 if (!$m && !empty($date['m'])) {
6a488035
TO
330 $m = CRM_Utils_Array::value('m', $date);
331 }
332
333 $day = $mon = 1;
334 $year = 0;
335 if ($d) {
336 $day = $d;
337 }
338 if ($m) {
339 $mon = $m;
340 }
341 if ($y) {
342 $year = $y;
343 }
344
345 // if we have day we need mon, and if we have mon we need year
346 if (($d && !$m) ||
347 ($d && !$y) ||
348 ($m && !$y)
349 ) {
350 return FALSE;
351 }
352
353 $result = FALSE;
354 if (!empty($day) || !empty($mon) || !empty($year)) {
355 $result = checkdate($mon, $day, $year);
356 }
357
358 if (!$result) {
359 return FALSE;
360 }
361
362 // ensure we have month if required
363 if ($monthRequired && !$m) {
364 return FALSE;
365 }
366
367 // now make sure this date is greater that today
368 $currentDate = getdate();
369 if ($year > $currentDate['year']) {
370 return TRUE;
371 }
372 elseif ($year < $currentDate['year']) {
373 return FALSE;
374 }
375
376 if ($m) {
377 if ($mon > $currentDate['mon']) {
378 return TRUE;
379 }
380 elseif ($mon < $currentDate['mon']) {
381 return FALSE;
382 }
383 }
384
385 if ($d) {
386 if ($day > $currentDate['mday']) {
387 return TRUE;
388 }
389 elseif ($day < $currentDate['mday']) {
390 return FALSE;
391 }
392 }
393
394 return TRUE;
395 }
396
397 /**
100fef9d 398 * Check the validity of a date or datetime (timestamp)
6a488035
TO
399 * value which is in YYYYMMDD or YYYYMMDDHHMMSS format
400 *
401 * Uses PHP checkdate() - params are ( int $month, int $day, int $year )
402 *
403 * @param string $date
404 *
a6c01b45
CW
405 * @return bool
406 * true if valid date
6a488035 407 */
00be9182 408 public static function mysqlDate($date) {
6a488035
TO
409 // allow date to be null
410 if ($date == NULL) {
411 return TRUE;
412 }
413
414 if (checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
415 return TRUE;
416 }
417
418 return FALSE;
419 }
420
5bc392e6
EM
421 /**
422 * @param $value
423 *
424 * @return bool
425 */
00be9182 426 public static function integer($value) {
6a488035
TO
427 if (is_int($value)) {
428 return TRUE;
429 }
430
f942c321
DL
431 // CRM-13460
432 // ensure number passed is always a string numeral
433 if (!is_numeric($value)) {
434 return FALSE;
435 }
436
437 // note that is_int matches only integer type
438 // and not strings which are only integers
439 // hence we do this here
440 if (preg_match('/^\d+$/', $value)) {
441 return TRUE;
442 }
443
444 if ($value < 0) {
6a488035
TO
445 $negValue = -1 * $value;
446 if (is_int($negValue)) {
447 return TRUE;
448 }
449 }
450
6a488035
TO
451 return FALSE;
452 }
453
5bc392e6
EM
454 /**
455 * @param $value
456 *
457 * @return bool
458 */
00be9182 459 public static function positiveInteger($value) {
6a488035
TO
460 if (is_int($value)) {
461 return ($value < 0) ? FALSE : TRUE;
462 }
463
f942c321
DL
464 // CRM-13460
465 // ensure number passed is always a string numeral
466 if (!is_numeric($value)) {
467 return FALSE;
468 }
469
470 if (preg_match('/^\d+$/', $value)) {
6a488035
TO
471 return TRUE;
472 }
473
474 return FALSE;
475 }
476
5bc392e6
EM
477 /**
478 * @param $value
479 *
480 * @return bool
481 */
00be9182 482 public static function numeric($value) {
f942c321
DL
483 // lets use a php gatekeeper to ensure this is numeric
484 if (!is_numeric($value)) {
485 return FALSE;
486 }
487
6a488035
TO
488 return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ? TRUE : FALSE;
489 }
490
5bc392e6
EM
491 /**
492 * @param $value
493 * @param $noOfDigit
494 *
495 * @return bool
496 */
00be9182 497 public static function numberOfDigit($value, $noOfDigit) {
6a488035
TO
498 return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ? TRUE : FALSE;
499 }
500
5bc392e6
EM
501 /**
502 * @param $value
503 *
504 * @return mixed
505 */
00be9182 506 public static function cleanMoney($value) {
6a488035
TO
507 // first remove all white space
508 $value = str_replace(array(' ', "\t", "\n"), '', $value);
509
510 $config = CRM_Core_Config::singleton();
511
e7292422 512 //CRM-14868
ef88f444 513 $currencySymbols = CRM_Core_PseudoConstant::get(
353ffa53
TO
514 'CRM_Contribute_DAO_Contribution',
515 'currency', array(
516 'keyColumn' => 'name',
517 'labelColumn' => 'symbol',
e70a7fc0
TO
518 )
519 );
e7292422 520 $value = str_replace($currencySymbols, '', $value);
ef88f444 521
6a488035
TO
522 if ($config->monetaryThousandSeparator) {
523 $mon_thousands_sep = $config->monetaryThousandSeparator;
524 }
525 else {
526 $mon_thousands_sep = ',';
527 }
528
529 // ugly fix for CRM-6391: do not drop the thousand separator if
530 // it looks like it’s separating decimal part (because a given
531 // value undergoes a second cleanMoney() call, for example)
b81f42da 532 // CRM-15835 - in case the amount/value contains 0 after decimal
533 // eg 150.5 the following if condition will pass
534 if ($mon_thousands_sep != '.' or (substr($value, -3, 1) != '.' && substr($value, -2, 1) != '.')) {
6a488035
TO
535 $value = str_replace($mon_thousands_sep, '', $value);
536 }
537
538 if ($config->monetaryDecimalPoint) {
539 $mon_decimal_point = $config->monetaryDecimalPoint;
540 }
541 else {
542 $mon_decimal_point = '.';
543 }
544 $value = str_replace($mon_decimal_point, '.', $value);
545
546 return $value;
547 }
548
5bc392e6
EM
549 /**
550 * @param $value
551 *
552 * @return bool
553 */
00be9182 554 public static function money($value) {
6a488035
TO
555 $config = CRM_Core_Config::singleton();
556
50bfb460
SB
557 // only edge case when we have a decimal point in the input money
558 // field and not defined in the decimal Point in config settings
6a488035
TO
559 if ($config->monetaryDecimalPoint &&
560 $config->monetaryDecimalPoint != '.' &&
50bfb460 561 // CRM-7122 also check for Thousands Separator in config settings
6a488035
TO
562 $config->monetaryThousandSeparator != '.' &&
563 substr_count($value, '.')
564 ) {
565 return FALSE;
566 }
567
568 $value = self::cleanMoney($value);
569
570 if (self::integer($value)) {
571 return TRUE;
572 }
573
574 return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ? TRUE : FALSE;
575 }
576
5bc392e6
EM
577 /**
578 * @param $value
579 * @param int $maxLength
580 *
581 * @return bool
582 */
00be9182 583 public static function string($value, $maxLength = 0) {
6a488035
TO
584 if (is_string($value) &&
585 ($maxLength === 0 || strlen($value) <= $maxLength)
586 ) {
587 return TRUE;
588 }
589 return FALSE;
590 }
591
5bc392e6
EM
592 /**
593 * @param $value
594 *
595 * @return bool
596 */
00be9182 597 public static function boolean($value) {
6a488035
TO
598 return preg_match(
599 '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value
600 ) ? TRUE : FALSE;
601 }
602
5bc392e6
EM
603 /**
604 * @param $value
605 *
606 * @return bool
607 */
00be9182 608 public static function email($value) {
6a488035
TO
609 return (bool) filter_var($value, FILTER_VALIDATE_EMAIL);
610 }
611
5bc392e6
EM
612 /**
613 * @param $list
614 *
615 * @return bool
616 */
00be9182 617 public static function emailList($list) {
6a488035
TO
618 $emails = explode(',', $list);
619 foreach ($emails as $email) {
620 $email = trim($email);
621 if (!self::email($email)) {
622 return FALSE;
623 }
624 }
625 return TRUE;
626 }
627
5bc392e6 628 /**
4f1f1f2a
CW
629 * allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or
630 * if u disregard the first 0, 4 (thanx excel!)
631 * FIXME: we need to figure out how to localize such rules
5bc392e6
EM
632 * @param $value
633 *
634 * @return bool
635 */
00be9182 636 public static function postalCode($value) {
6a488035
TO
637 if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) {
638 return TRUE;
639 }
640 return FALSE;
641 }
642
643 /**
100fef9d 644 * See how file rules are written in HTML/QuickForm/file.php
6a488035
TO
645 * Checks to make sure the uploaded file is ascii
646 *
ea3ddccf 647 * @param string $elementValue
648 *
a6c01b45 649 * @return bool
ea3ddccf 650 * True if file has been uploaded, false otherwise
6a488035 651 */
00be9182 652 public static function asciiFile($elementValue) {
6a488035
TO
653 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
654 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
655 ) {
656 return CRM_Utils_File::isAscii($elementValue['tmp_name']);
657 }
658 return FALSE;
659 }
660
661 /**
662 * Checks to make sure the uploaded file is in UTF-8, recodes if it's not
663 *
ea3ddccf 664 * @param array $elementValue
665 *
a6c01b45 666 * @return bool
ea3ddccf 667 * Whether file has been uploaded properly and is now in UTF-8.
6a488035 668 */
00be9182 669 public static function utf8File($elementValue) {
6a488035
TO
670 $success = FALSE;
671
672 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
673 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
674 ) {
675
676 $success = CRM_Utils_File::isAscii($elementValue['tmp_name']);
677
678 // if it's a file, but not UTF-8, let's try and recode it
679 // and then make sure it's an UTF-8 file in the end
680 if (!$success) {
681 $success = CRM_Utils_File::toUtf8($elementValue['tmp_name']);
682 if ($success) {
683 $success = CRM_Utils_File::isAscii($elementValue['tmp_name']);
684 }
685 }
686 }
687 return $success;
688 }
689
690 /**
100fef9d 691 * See how file rules are written in HTML/QuickForm/file.php
6a488035
TO
692 * Checks to make sure the uploaded file is html
693 *
ea3ddccf 694 * @param array $elementValue
695 *
a6c01b45 696 * @return bool
ea3ddccf 697 * True if file has been uploaded, false otherwise
6a488035 698 */
00be9182 699 public static function htmlFile($elementValue) {
6a488035
TO
700 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
701 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
702 ) {
703 return CRM_Utils_File::isHtmlFile($elementValue['tmp_name']);
704 }
705 return FALSE;
706 }
707
708 /**
fe482240 709 * Check if there is a record with the same name in the db.
6a488035 710 *
77855840
TO
711 * @param string $value
712 * The value of the field we are checking.
713 * @param array $options
714 * The daoName and fieldName (optional ).
6a488035 715 *
408b79bf 716 * @return bool
a6c01b45 717 * true if object exists
6a488035 718 */
00be9182 719 public static function objectExists($value, $options) {
6a488035
TO
720 $name = 'name';
721 if (isset($options[2])) {
722 $name = $options[2];
723 }
724
725 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));
726 }
727
5bc392e6
EM
728 /**
729 * @param $value
730 * @param $options
731 *
732 * @return bool
733 */
00be9182 734 public static function optionExists($value, $options) {
6a488035
TO
735 return CRM_Core_OptionValue::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array::value(3, $options, 'name'));
736 }
737
5bc392e6
EM
738 /**
739 * @param $value
740 * @param $type
741 *
742 * @return bool
743 */
00be9182 744 public static function creditCardNumber($value, $type) {
6a488035
TO
745 require_once 'Validate/Finance/CreditCard.php';
746 return Validate_Finance_CreditCard::number($value, $type);
747 }
748
5bc392e6
EM
749 /**
750 * @param $value
751 * @param $type
752 *
753 * @return bool
754 */
00be9182 755 public static function cvv($value, $type) {
6a488035
TO
756 require_once 'Validate/Finance/CreditCard.php';
757
758 return Validate_Finance_CreditCard::cvv($value, $type);
759 }
760
5bc392e6
EM
761 /**
762 * @param $value
763 *
764 * @return bool
765 */
00be9182 766 public static function currencyCode($value) {
6a488035
TO
767 static $currencyCodes = NULL;
768 if (!$currencyCodes) {
769 $currencyCodes = CRM_Core_PseudoConstant::currencyCode();
770 }
771 if (in_array($value, $currencyCodes)) {
772 return TRUE;
773 }
774 return FALSE;
775 }
776
5bc392e6
EM
777 /**
778 * @param $value
779 *
780 * @return bool
781 */
00be9182 782 public static function xssString($value) {
6a488035
TO
783 if (is_string($value)) {
784 return preg_match('!<(vb)?script[^>]*>.*</(vb)?script.*>!ims',
785 $value
786 ) ? FALSE : TRUE;
787 }
788 else {
789 return TRUE;
790 }
791 }
792
5bc392e6
EM
793 /**
794 * @param $path
795 *
796 * @return bool
797 */
00be9182 798 public static function fileExists($path) {
6a488035
TO
799 return file_exists($path);
800 }
801
d9d7e7dd
TO
802 /**
803 * Determine whether the value contains a valid reference to a directory.
804 *
805 * Paths stored in the setting system may be absolute -- or may be
806 * relative to the default data directory.
807 *
808 * @param string $path
809 * @return bool
810 */
811 public static function settingPath($path) {
e3d28c74 812 return is_dir(Civi::paths()->getPath($path));
d9d7e7dd
TO
813 }
814
5bc392e6
EM
815 /**
816 * @param $value
817 * @param null $actualElementValue
818 *
819 * @return bool
820 */
00be9182 821 public static function validContact($value, $actualElementValue = NULL) {
6a488035
TO
822 if ($actualElementValue) {
823 $value = $actualElementValue;
824 }
825
258570f7 826 return CRM_Utils_Rule::positiveInteger($value);
6a488035
TO
827 }
828
829 /**
100fef9d 830 * Check the validity of the date (in qf format)
6a488035
TO
831 * note that only a year is valid, or a mon-year is
832 * also valid in addition to day-mon-year
833 *
834 * @param array $date
835 *
a6c01b45
CW
836 * @return bool
837 * true if valid date
6a488035 838 */
00be9182 839 public static function qfDate($date) {
6a488035
TO
840 $config = CRM_Core_Config::singleton();
841
842 $d = CRM_Utils_Array::value('d', $date);
843 $m = CRM_Utils_Array::value('M', $date);
844 $y = CRM_Utils_Array::value('Y', $date);
845 if (isset($date['h']) ||
846 isset($date['g'])
847 ) {
848 $m = CRM_Utils_Array::value('M', $date);
849 }
850
851 if (!$d && !$m && !$y) {
852 return TRUE;
853 }
854
855 $day = $mon = 1;
856 $year = 0;
857 if ($d) {
858 $day = $d;
859 }
860 if ($m) {
861 $mon = $m;
862 }
863 if ($y) {
864 $year = $y;
865 }
866
867 // if we have day we need mon, and if we have mon we need year
868 if (($d && !$m) ||
869 ($d && !$y) ||
870 ($m && !$y)
871 ) {
872 return FALSE;
873 }
874
875 if (!empty($day) || !empty($mon) || !empty($year)) {
876 return checkdate($mon, $day, $year);
877 }
878 return FALSE;
879 }
880
5bc392e6
EM
881 /**
882 * @param $key
883 *
884 * @return bool
885 */
00be9182 886 public static function qfKey($key) {
6a488035
TO
887 return ($key) ? CRM_Core_Key::valid($key) : FALSE;
888 }
96025800 889
79326ee2
SB
890 /**
891 * Check if the values in the date range are in correct chronological order.
892 *
893 * @param array $fields
894 * Fields of the form.
895 * @param $fieldName
896 * Name of date range field.
897 * @param $errors
898 * The error array.
899 * @param $title
900 * Title of the date range to be displayed in the error message.
901 */
902 public static function validDateRange($fields, $fieldName, &$errors, $title) {
903 $lowDate = strtotime($fields[$fieldName . '_low']);
904 $highDate = strtotime($fields[$fieldName . '_high']);
905
906 if ($lowDate > $highDate) {
907 $errors[$fieldName . '_range_error'] = ts('%1: Please check that your date range is in correct chronological order.', array(1 => $title));
908 }
909 }
910
6a488035 911}