3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 require_once 'HTML/QuickForm/Rule/Email.php';
39 * Class CRM_Utils_Rule
41 class CRM_Utils_Rule
{
45 * @param int $maxLength
49 public static function title($str, $maxLength = 127) {
52 if (empty($str) ||
strlen($str) > $maxLength) {
56 // Make sure it include valid characters, alpha numeric and underscores
57 if (!preg_match('/^\w[\w\s\'\&\,\$\#\-\.\"\?\!]+$/i', $str)) {
69 public static function longTitle($str) {
70 return self
::title($str, 255);
78 public static function variable($str) {
80 if (empty($str) ||
strlen($str) > 31) {
84 // make sure it include valid characters, alpha numeric and underscores
85 if (!preg_match('/^[\w]+$/i', $str)) {
97 public static function qfVariable($str) {
99 //if ( empty( $str ) || strlen( $str ) > 31 ) {
100 if (strlen(trim($str)) == 0 ||
strlen($str) > 31) {
104 // make sure it include valid characters, alpha numeric and underscores
105 // added (. and ,) option (CRM-1336)
106 if (!preg_match('/^[\w\s\.\,]+$/i', $str)) {
118 public static function phone($phone) {
120 if (empty($phone) ||
strlen($phone) > 16) {
124 // make sure it include valid characters, (, \s and numeric
125 if (preg_match('/^[\d\(\)\-\.\s]+$/', $phone)) {
136 public static function query($query) {
138 if (empty($query) ||
strlen($query) < 3 ||
strlen($query) > 127) {
142 // make sure it include valid characters, alpha numeric and underscores
143 if (!preg_match('/^[\w\s\%\'\&\,\$\#]+$/i', $query)) {
155 public static function url($url) {
156 if (preg_match('/^\//', $url)) {
157 // allow relative URL's (CRM-15598)
158 $url = 'http://' . $_SERVER['HTTP_HOST'] . $url;
160 return (bool) filter_var($url, FILTER_VALIDATE_URL
);
168 public static function wikiURL($string) {
169 $items = explode(' ', trim($string), 2);
170 return self
::url($items[0]);
178 public static function domain($domain) {
179 // not perfect, but better than the previous one; see CRM-1502
180 if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) {
188 * @param null $default
192 public static function date($value, $default = NULL) {
193 if (is_string($value) &&
194 preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value)
203 * @param null $default
205 * @return null|string
207 public static function dateTime($value, $default = NULL) {
209 if (is_string($value) &&
210 preg_match('/^\d\d\d\d-?\d\d-?\d\d(\s\d\d:\d\d(:\d\d)?|\d\d\d\d(\d\d)?)?$/', $value)
219 * Check the validity of the date (in qf format)
220 * note that only a year is valid, or a mon-year is
221 * also valid in addition to day-mon-year. The date
222 * specified has to be beyond today. (i.e today or later)
225 * @param bool $monthRequired
226 * Check whether month is mandatory.
231 public static function currentDate($date, $monthRequired = TRUE) {
232 $config = CRM_Core_Config
::singleton();
234 $d = CRM_Utils_Array
::value('d', $date);
235 $m = CRM_Utils_Array
::value('M', $date);
236 $y = CRM_Utils_Array
::value('Y', $date);
238 if (!$d && !$m && !$y) {
242 // CRM-9017 CiviContribute/CiviMember form with expiration date format 'm Y'
243 if (!$m && !empty($date['m'])) {
244 $m = CRM_Utils_Array
::value('m', $date);
259 // if we have day we need mon, and if we have mon we need year
268 if (!empty($day) ||
!empty($mon) ||
!empty($year)) {
269 $result = checkdate($mon, $day, $year);
276 // ensure we have month if required
277 if ($monthRequired && !$m) {
281 // now make sure this date is greater that today
282 $currentDate = getdate();
283 if ($year > $currentDate['year']) {
286 elseif ($year < $currentDate['year']) {
291 if ($mon > $currentDate['mon']) {
294 elseif ($mon < $currentDate['mon']) {
300 if ($day > $currentDate['mday']) {
303 elseif ($day < $currentDate['mday']) {
312 * Check the validity of a date or datetime (timestamp)
313 * value which is in YYYYMMDD or YYYYMMDDHHMMSS format
315 * Uses PHP checkdate() - params are ( int $month, int $day, int $year )
317 * @param string $date
322 public static function mysqlDate($date) {
323 // allow date to be null
328 if (checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
340 public static function integer($value) {
341 if (is_int($value)) {
346 // ensure number passed is always a string numeral
347 if (!is_numeric($value)) {
351 // note that is_int matches only integer type
352 // and not strings which are only integers
353 // hence we do this here
354 if (preg_match('/^\d+$/', $value)) {
359 $negValue = -1 * $value;
360 if (is_int($negValue)) {
373 public static function positiveInteger($value) {
374 if (is_int($value)) {
375 return ($value < 0) ?
FALSE : TRUE;
379 // ensure number passed is always a string numeral
380 if (!is_numeric($value)) {
384 if (preg_match('/^\d+$/', $value)) {
396 public static function numeric($value) {
397 // lets use a php gatekeeper to ensure this is numeric
398 if (!is_numeric($value)) {
402 return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ?
TRUE : FALSE;
411 public static function numberOfDigit($value, $noOfDigit) {
412 return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ?
TRUE : FALSE;
420 public static function cleanMoney($value) {
421 // first remove all white space
422 $value = str_replace(array(' ', "\t", "\n"), '', $value);
424 $config = CRM_Core_Config
::singleton();
427 $currencySymbols = CRM_Core_PseudoConstant
::get(
428 'CRM_Contribute_DAO_Contribution',
430 'keyColumn' => 'name',
431 'labelColumn' => 'symbol',
434 $value = str_replace($currencySymbols, '', $value);
436 if ($config->monetaryThousandSeparator
) {
437 $mon_thousands_sep = $config->monetaryThousandSeparator
;
440 $mon_thousands_sep = ',';
443 // ugly fix for CRM-6391: do not drop the thousand separator if
444 // it looks like it’s separating decimal part (because a given
445 // value undergoes a second cleanMoney() call, for example)
446 // CRM-15835 - in case the amount/value contains 0 after decimal
447 // eg 150.5 the following if condition will pass
448 if ($mon_thousands_sep != '.' or (substr($value, -3, 1) != '.' && substr($value, -2, 1) != '.')) {
449 $value = str_replace($mon_thousands_sep, '', $value);
452 if ($config->monetaryDecimalPoint
) {
453 $mon_decimal_point = $config->monetaryDecimalPoint
;
456 $mon_decimal_point = '.';
458 $value = str_replace($mon_decimal_point, '.', $value);
468 public static function money($value) {
469 $config = CRM_Core_Config
::singleton();
471 //only edge case when we have a decimal point in the input money
472 //field and not defined in the decimal Point in config settings
473 if ($config->monetaryDecimalPoint
&&
474 $config->monetaryDecimalPoint
!= '.' &&
475 /* CRM-7122 also check for Thousands Separator in config settings */
476 $config->monetaryThousandSeparator
!= '.' &&
477 substr_count($value, '.')
482 $value = self
::cleanMoney($value);
484 if (self
::integer($value)) {
488 return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ?
TRUE : FALSE;
493 * @param int $maxLength
497 public static function string($value, $maxLength = 0) {
498 if (is_string($value) &&
499 ($maxLength === 0 ||
strlen($value) <= $maxLength)
511 public static function boolean($value) {
513 '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value
522 public static function email($value) {
523 return (bool) filter_var($value, FILTER_VALIDATE_EMAIL
);
531 public static function emailList($list) {
532 $emails = explode(',', $list);
533 foreach ($emails as $email) {
534 $email = trim($email);
535 if (!self
::email($email)) {
543 * allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or
544 * if u disregard the first 0, 4 (thanx excel!)
545 * FIXME: we need to figure out how to localize such rules
550 public static function postalCode($value) {
551 if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) {
558 * See how file rules are written in HTML/QuickForm/file.php
559 * Checks to make sure the uploaded file is ascii
562 * true if file has been uploaded, false otherwise
564 public static function asciiFile($elementValue) {
565 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
566 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
568 return CRM_Utils_File
::isAscii($elementValue['tmp_name']);
574 * Checks to make sure the uploaded file is in UTF-8, recodes if it's not
577 * whether file has been uploaded properly and is now in UTF-8
579 public static function utf8File($elementValue) {
582 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
583 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
586 $success = CRM_Utils_File
::isAscii($elementValue['tmp_name']);
588 // if it's a file, but not UTF-8, let's try and recode it
589 // and then make sure it's an UTF-8 file in the end
591 $success = CRM_Utils_File
::toUtf8($elementValue['tmp_name']);
593 $success = CRM_Utils_File
::isAscii($elementValue['tmp_name']);
601 * See how file rules are written in HTML/QuickForm/file.php
602 * Checks to make sure the uploaded file is html
605 * true if file has been uploaded, false otherwise
607 public static function htmlFile($elementValue) {
608 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
609 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
611 return CRM_Utils_File
::isHtmlFile($elementValue['tmp_name']);
617 * Check if there is a record with the same name in the db.
619 * @param string $value
620 * The value of the field we are checking.
621 * @param array $options
622 * The daoName and fieldName (optional ).
625 * true if object exists
627 public static function objectExists($value, $options) {
629 if (isset($options[2])) {
633 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));
642 public static function optionExists($value, $options) {
643 return CRM_Core_OptionValue
::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array
::value(3, $options, 'name'));
652 public static function creditCardNumber($value, $type) {
653 require_once 'Validate/Finance/CreditCard.php';
654 return Validate_Finance_CreditCard
::number($value, $type);
663 public static function cvv($value, $type) {
664 require_once 'Validate/Finance/CreditCard.php';
666 return Validate_Finance_CreditCard
::cvv($value, $type);
674 public static function currencyCode($value) {
675 static $currencyCodes = NULL;
676 if (!$currencyCodes) {
677 $currencyCodes = CRM_Core_PseudoConstant
::currencyCode();
679 if (in_array($value, $currencyCodes)) {
690 public static function xssString($value) {
691 if (is_string($value)) {
692 return preg_match('!<(vb)?script[^>]*>.*</(vb)?script.*>!ims',
706 public static function fileExists($path) {
707 return file_exists($path);
716 public static function autocomplete($value, $options) {
718 $selectOption = CRM_Core_BAO_CustomOption
::valuesByID($options['fieldID'], $options['optionGroupID']);
720 if (!in_array($value, $selectOption)) {
729 * @param null $actualElementValue
733 public static function validContact($value, $actualElementValue = NULL) {
734 if ($actualElementValue) {
735 $value = $actualElementValue;
738 if ($value && !is_numeric($value)) {
745 * Check the validity of the date (in qf format)
746 * note that only a year is valid, or a mon-year is
747 * also valid in addition to day-mon-year
754 public static function qfDate($date) {
755 $config = CRM_Core_Config
::singleton();
757 $d = CRM_Utils_Array
::value('d', $date);
758 $m = CRM_Utils_Array
::value('M', $date);
759 $y = CRM_Utils_Array
::value('Y', $date);
760 if (isset($date['h']) ||
763 $m = CRM_Utils_Array
::value('M', $date);
766 if (!$d && !$m && !$y) {
782 // if we have day we need mon, and if we have mon we need year
790 if (!empty($day) ||
!empty($mon) ||
!empty($year)) {
791 return checkdate($mon, $day, $year);
801 public static function qfKey($key) {
802 return ($key) ? CRM_Core_Key
::valid($key) : FALSE;