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