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.
232 public static function currentDate($date, $monthRequired = TRUE) {
233 $config = CRM_Core_Config
::singleton();
235 $d = CRM_Utils_Array
::value('d', $date);
236 $m = CRM_Utils_Array
::value('M', $date);
237 $y = CRM_Utils_Array
::value('Y', $date);
239 if (!$d && !$m && !$y) {
243 // CRM-9017 CiviContribute/CiviMember form with expiration date format 'm Y'
244 if (!$m && !empty($date['m'])) {
245 $m = CRM_Utils_Array
::value('m', $date);
260 // if we have day we need mon, and if we have mon we need year
269 if (!empty($day) ||
!empty($mon) ||
!empty($year)) {
270 $result = checkdate($mon, $day, $year);
277 // ensure we have month if required
278 if ($monthRequired && !$m) {
282 // now make sure this date is greater that today
283 $currentDate = getdate();
284 if ($year > $currentDate['year']) {
287 elseif ($year < $currentDate['year']) {
292 if ($mon > $currentDate['mon']) {
295 elseif ($mon < $currentDate['mon']) {
301 if ($day > $currentDate['mday']) {
304 elseif ($day < $currentDate['mday']) {
313 * Check the validity of a date or datetime (timestamp)
314 * value which is in YYYYMMDD or YYYYMMDDHHMMSS format
316 * Uses PHP checkdate() - params are ( int $month, int $day, int $year )
318 * @param string $date
324 public static function mysqlDate($date) {
325 // allow date to be null
330 if (checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
342 public static function integer($value) {
343 if (is_int($value)) {
348 // ensure number passed is always a string numeral
349 if (!is_numeric($value)) {
353 // note that is_int matches only integer type
354 // and not strings which are only integers
355 // hence we do this here
356 if (preg_match('/^\d+$/', $value)) {
361 $negValue = -1 * $value;
362 if (is_int($negValue)) {
375 public static function positiveInteger($value) {
376 if (is_int($value)) {
377 return ($value < 0) ?
FALSE : TRUE;
381 // ensure number passed is always a string numeral
382 if (!is_numeric($value)) {
386 if (preg_match('/^\d+$/', $value)) {
398 public static function numeric($value) {
399 // lets use a php gatekeeper to ensure this is numeric
400 if (!is_numeric($value)) {
404 return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ?
TRUE : FALSE;
413 public static function numberOfDigit($value, $noOfDigit) {
414 return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ?
TRUE : FALSE;
422 public static function cleanMoney($value) {
423 // first remove all white space
424 $value = str_replace(array(' ', "\t", "\n"), '', $value);
426 $config = CRM_Core_Config
::singleton();
429 $currencySymbols = CRM_Core_PseudoConstant
::get(
430 'CRM_Contribute_DAO_Contribution',
432 'keyColumn' => 'name',
433 'labelColumn' => 'symbol',
435 $value = str_replace($currencySymbols, '', $value);
437 if ($config->monetaryThousandSeparator
) {
438 $mon_thousands_sep = $config->monetaryThousandSeparator
;
441 $mon_thousands_sep = ',';
444 // ugly fix for CRM-6391: do not drop the thousand separator if
445 // it looks like it’s separating decimal part (because a given
446 // value undergoes a second cleanMoney() call, for example)
447 if ($mon_thousands_sep != '.' or substr($value, -3, 1) != '.') {
448 $value = str_replace($mon_thousands_sep, '', $value);
451 if ($config->monetaryDecimalPoint
) {
452 $mon_decimal_point = $config->monetaryDecimalPoint
;
455 $mon_decimal_point = '.';
457 $value = str_replace($mon_decimal_point, '.', $value);
467 public static function money($value) {
468 $config = CRM_Core_Config
::singleton();
470 //only edge case when we have a decimal point in the input money
471 //field and not defined in the decimal Point in config settings
472 if ($config->monetaryDecimalPoint
&&
473 $config->monetaryDecimalPoint
!= '.' &&
474 /* CRM-7122 also check for Thousands Separator in config settings */
475 $config->monetaryThousandSeparator
!= '.' &&
476 substr_count($value, '.')
481 $value = self
::cleanMoney($value);
483 if (self
::integer($value)) {
487 return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ?
TRUE : FALSE;
492 * @param int $maxLength
496 public static function string($value, $maxLength = 0) {
497 if (is_string($value) &&
498 ($maxLength === 0 ||
strlen($value) <= $maxLength)
510 public static function boolean($value) {
512 '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value
521 public static function email($value) {
522 return (bool) filter_var($value, FILTER_VALIDATE_EMAIL
);
530 public static function emailList($list) {
531 $emails = explode(',', $list);
532 foreach ($emails as $email) {
533 $email = trim($email);
534 if (!self
::email($email)) {
542 * allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or
543 * if u disregard the first 0, 4 (thanx excel!)
544 * FIXME: we need to figure out how to localize such rules
549 public static function postalCode($value) {
550 if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) {
557 * See how file rules are written in HTML/QuickForm/file.php
558 * Checks to make sure the uploaded file is ascii
560 * @param array Uploaded file info (from $_FILES)
563 * true if file has been uploaded, false otherwise
565 public static function asciiFile($elementValue) {
566 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
567 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
569 return CRM_Utils_File
::isAscii($elementValue['tmp_name']);
575 * Checks to make sure the uploaded file is in UTF-8, recodes if it's not
577 * @param array Uploaded file info (from $_FILES)
580 * whether file has been uploaded properly and is now in UTF-8
582 public static function utf8File($elementValue) {
585 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
586 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
589 $success = CRM_Utils_File
::isAscii($elementValue['tmp_name']);
591 // if it's a file, but not UTF-8, let's try and recode it
592 // and then make sure it's an UTF-8 file in the end
594 $success = CRM_Utils_File
::toUtf8($elementValue['tmp_name']);
596 $success = CRM_Utils_File
::isAscii($elementValue['tmp_name']);
604 * See how file rules are written in HTML/QuickForm/file.php
605 * Checks to make sure the uploaded file is html
607 * @param array Uploaded file info (from $_FILES)
610 * true if file has been uploaded, false otherwise
612 public static function htmlFile($elementValue) {
613 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
614 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
616 return CRM_Utils_File
::isHtmlFile($elementValue['tmp_name']);
622 * Check if there is a record with the same name in the db
624 * @param string $value
625 * The value of the field we are checking.
626 * @param array $options
627 * The daoName and fieldName (optional ).
630 * true if object exists
633 public static function objectExists($value, $options) {
635 if (isset($options[2])) {
639 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));
648 public static function optionExists($value, $options) {
649 return CRM_Core_OptionValue
::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array
::value(3, $options, 'name'));
658 public static function creditCardNumber($value, $type) {
659 require_once 'Validate/Finance/CreditCard.php';
660 return Validate_Finance_CreditCard
::number($value, $type);
669 public static function cvv($value, $type) {
670 require_once 'Validate/Finance/CreditCard.php';
672 return Validate_Finance_CreditCard
::cvv($value, $type);
680 public static function currencyCode($value) {
681 static $currencyCodes = NULL;
682 if (!$currencyCodes) {
683 $currencyCodes = CRM_Core_PseudoConstant
::currencyCode();
685 if (in_array($value, $currencyCodes)) {
696 public static function xssString($value) {
697 if (is_string($value)) {
698 return preg_match('!<(vb)?script[^>]*>.*</(vb)?script.*>!ims',
712 public static function fileExists($path) {
713 return file_exists($path);
722 public static function autocomplete($value, $options) {
724 $selectOption = CRM_Core_BAO_CustomOption
::valuesByID($options['fieldID'], $options['optionGroupID']);
726 if (!in_array($value, $selectOption)) {
735 * @param null $actualElementValue
739 public static function validContact($value, $actualElementValue = NULL) {
740 if ($actualElementValue) {
741 $value = $actualElementValue;
744 if ($value && !is_numeric($value)) {
751 * Check the validity of the date (in qf format)
752 * note that only a year is valid, or a mon-year is
753 * also valid in addition to day-mon-year
761 public static function qfDate($date) {
762 $config = CRM_Core_Config
::singleton();
764 $d = CRM_Utils_Array
::value('d', $date);
765 $m = CRM_Utils_Array
::value('M', $date);
766 $y = CRM_Utils_Array
::value('Y', $date);
767 if (isset($date['h']) ||
770 $m = CRM_Utils_Array
::value('M', $date);
773 if (!$d && !$m && !$y) {
789 // if we have day we need mon, and if we have mon we need year
797 if (!empty($day) ||
!empty($mon) ||
!empty($year)) {
798 return checkdate($mon, $day, $year);
808 public static function qfKey($key) {
809 return ($key) ? CRM_Core_Key
::valid($key) : FALSE;