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