3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
36 require_once 'HTML/QuickForm/Rule/Email.php';
38 class CRM_Utils_Rule
{
40 static function title($str, $maxLength = 127) {
43 if (empty($str) ||
strlen($str) > $maxLength) {
47 // Make sure it include valid characters, alpha numeric and underscores
48 if (!preg_match('/^\w[\w\s\'\&\,\$\#\-\.\"\?\!]+$/i', $str)) {
55 static function longTitle($str) {
56 return self
::title($str, 255);
59 static function variable($str) {
61 if (empty($str) ||
strlen($str) > 31) {
65 // make sure it include valid characters, alpha numeric and underscores
66 if (!preg_match('/^[\w]+$/i', $str)) {
73 static function qfVariable($str) {
75 //if ( empty( $str ) || strlen( $str ) > 31 ) {
76 if (strlen(trim($str)) == 0 ||
strlen($str) > 31) {
80 // make sure it include valid characters, alpha numeric and underscores
81 // added (. and ,) option (CRM-1336)
82 if (!preg_match('/^[\w\s\.\,]+$/i', $str)) {
89 static function phone($phone) {
91 if (empty($phone) ||
strlen($phone) > 16) {
95 // make sure it include valid characters, (, \s and numeric
96 if (preg_match('/^[\d\(\)\-\.\s]+$/', $phone)) {
102 static function query($query) {
104 if (empty($query) ||
strlen($query) < 3 ||
strlen($query) > 127) {
108 // make sure it include valid characters, alpha numeric and underscores
109 if (!preg_match('/^[\w\s\%\'\&\,\$\#]+$/i', $query)) {
116 static function url($url) {
117 return (bool) filter_var($url, FILTER_VALIDATE_URL
);
120 static function wikiURL($string) {
121 $items = explode(' ', trim($string), 2);
122 return self
::url($items[0]);
125 static function domain($domain) {
126 // not perfect, but better than the previous one; see CRM-1502
127 if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) {
133 static function date($value, $default = NULL) {
134 if (is_string($value) &&
135 preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value)
142 static function dateTime($value, $default = NULL) {
144 if (is_string($value) &&
145 preg_match('/^\d\d\d\d-?\d\d-?\d\d(\s\d\d:\d\d(:\d\d)?|\d\d\d\d(\d\d)?)?$/', $value)
154 * check the validity of the date (in qf format)
155 * note that only a year is valid, or a mon-year is
156 * also valid in addition to day-mon-year. The date
157 * specified has to be beyond today. (i.e today or later)
160 * @param bool $monthRequired check whether month is mandatory
162 * @return bool true if valid date
166 static function currentDate($date, $monthRequired = TRUE) {
167 $config = CRM_Core_Config
::singleton();
169 $d = CRM_Utils_Array
::value('d', $date);
170 $m = CRM_Utils_Array
::value('M', $date);
171 $y = CRM_Utils_Array
::value('Y', $date);
173 if (!$d && !$m && !$y) {
177 // CRM-9017 CiviContribute/CiviMember form with expiration date format 'm Y'
178 if (!$m && CRM_Utils_Array
::value('m', $date)) {
179 $m = CRM_Utils_Array
::value('m', $date);
194 // if we have day we need mon, and if we have mon we need year
203 if (!empty($day) ||
!empty($mon) ||
!empty($year)) {
204 $result = checkdate($mon, $day, $year);
211 // ensure we have month if required
212 if ($monthRequired && !$m) {
216 // now make sure this date is greater that today
217 $currentDate = getdate();
218 if ($year > $currentDate['year']) {
221 elseif ($year < $currentDate['year']) {
226 if ($mon > $currentDate['mon']) {
229 elseif ($mon < $currentDate['mon']) {
235 if ($day > $currentDate['mday']) {
238 elseif ($day < $currentDate['mday']) {
247 * check the validity of a date or datetime (timestamp)
248 * value which is in YYYYMMDD or YYYYMMDDHHMMSS format
250 * Uses PHP checkdate() - params are ( int $month, int $day, int $year )
252 * @param string $date
254 * @return bool true if valid date
258 static function mysqlDate($date) {
259 // allow date to be null
264 if (checkdate(substr($date, 4, 2), substr($date, 6, 2), substr($date, 0, 4))) {
271 static function integer($value) {
272 if (is_int($value)) {
277 // ensure number passed is always a string numeral
278 if (!is_numeric($value)) {
282 // note that is_int matches only integer type
283 // and not strings which are only integers
284 // hence we do this here
285 if (preg_match('/^\d+$/', $value)) {
290 $negValue = -1 * $value;
291 if (is_int($negValue)) {
299 static function positiveInteger($value) {
300 if (is_int($value)) {
301 return ($value < 0) ?
FALSE : TRUE;
305 // ensure number passed is always a string numeral
306 if (!is_numeric($value)) {
310 if (preg_match('/^\d+$/', $value)) {
317 static function numeric($value) {
318 // lets use a php gatekeeper to ensure this is numeric
319 if (!is_numeric($value)) {
323 return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ?
TRUE : FALSE;
326 static function numberOfDigit($value, $noOfDigit) {
327 return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ?
TRUE : FALSE;
330 static function cleanMoney($value) {
331 // first remove all white space
332 $value = str_replace(array(' ', "\t", "\n"), '', $value);
334 $config = CRM_Core_Config
::singleton();
336 if ($config->monetaryThousandSeparator
) {
337 $mon_thousands_sep = $config->monetaryThousandSeparator
;
340 $mon_thousands_sep = ',';
343 // ugly fix for CRM-6391: do not drop the thousand separator if
344 // it looks like it’s separating decimal part (because a given
345 // value undergoes a second cleanMoney() call, for example)
346 if ($mon_thousands_sep != '.' or substr($value, -3, 1) != '.') {
347 $value = str_replace($mon_thousands_sep, '', $value);
350 if ($config->monetaryDecimalPoint
) {
351 $mon_decimal_point = $config->monetaryDecimalPoint
;
354 $mon_decimal_point = '.';
356 $value = str_replace($mon_decimal_point, '.', $value);
361 static function money($value) {
362 $config = CRM_Core_Config
::singleton();
364 //only edge case when we have a decimal point in the input money
365 //field and not defined in the decimal Point in config settings
366 if ($config->monetaryDecimalPoint
&&
367 $config->monetaryDecimalPoint
!= '.' &&
368 /* CRM-7122 also check for Thousands Separator in config settings */
369 $config->monetaryThousandSeparator
!= '.' &&
370 substr_count($value, '.')
375 $value = self
::cleanMoney($value);
377 if (self
::integer($value)) {
381 return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ?
TRUE : FALSE;
384 static function string($value, $maxLength = 0) {
385 if (is_string($value) &&
386 ($maxLength === 0 ||
strlen($value) <= $maxLength)
393 static function boolean($value) {
395 '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value
399 static function email($value) {
400 return (bool) filter_var($value, FILTER_VALIDATE_EMAIL
);
403 static function emailList($list) {
404 $emails = explode(',', $list);
405 foreach ($emails as $email) {
406 $email = trim($email);
407 if (!self
::email($email)) {
414 // allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or
415 // if u disregard the first 0, 4 (thanx excel!)
416 // FIXME: we need to figure out how to localize such rules
417 static function postalCode($value) {
418 if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) {
425 * see how file rules are written in HTML/QuickForm/file.php
426 * Checks to make sure the uploaded file is ascii
428 * @param array Uploaded file info (from $_FILES)
431 * @return bool true if file has been uploaded, false otherwise
433 static function asciiFile($elementValue) {
434 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
435 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
437 return CRM_Utils_File
::isAscii($elementValue['tmp_name']);
443 * Checks to make sure the uploaded file is in UTF-8, recodes if it's not
445 * @param array Uploaded file info (from $_FILES)
448 * @return bool whether file has been uploaded properly and is now in UTF-8
450 static function utf8File($elementValue) {
453 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
454 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
457 $success = CRM_Utils_File
::isAscii($elementValue['tmp_name']);
459 // if it's a file, but not UTF-8, let's try and recode it
460 // and then make sure it's an UTF-8 file in the end
462 $success = CRM_Utils_File
::toUtf8($elementValue['tmp_name']);
464 $success = CRM_Utils_File
::isAscii($elementValue['tmp_name']);
472 * see how file rules are written in HTML/QuickForm/file.php
473 * Checks to make sure the uploaded file is html
475 * @param array Uploaded file info (from $_FILES)
478 * @return bool true if file has been uploaded, false otherwise
480 static function htmlFile($elementValue) {
481 if ((isset($elementValue['error']) && $elementValue['error'] == 0) ||
482 (!empty($elementValue['tmp_name']) && $elementValue['tmp_name'] != 'none')
484 return CRM_Utils_File
::isHtmlFile($elementValue['tmp_name']);
490 * Check if there is a record with the same name in the db
492 * @param string $value the value of the field we are checking
493 * @param array $options the daoName and fieldName (optional )
495 * @return boolean true if object exists
499 static function objectExists($value, $options) {
501 if (isset($options[2])) {
505 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));
508 static function optionExists($value, $options) {
509 return CRM_Core_OptionValue
::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array
::value(3, $options, 'name'));
512 static function creditCardNumber($value, $type) {
513 require_once 'Validate/Finance/CreditCard.php';
514 return Validate_Finance_CreditCard
::number($value, $type);
517 static function cvv($value, $type) {
518 require_once 'Validate/Finance/CreditCard.php';
520 return Validate_Finance_CreditCard
::cvv($value, $type);
523 static function currencyCode($value) {
524 static $currencyCodes = NULL;
525 if (!$currencyCodes) {
526 $currencyCodes = CRM_Core_PseudoConstant
::currencyCode();
528 if (in_array($value, $currencyCodes)) {
534 static function xssString($value) {
535 if (is_string($value)) {
536 return preg_match('!<(vb)?script[^>]*>.*</(vb)?script.*>!ims',
545 static function fileExists($path) {
546 return file_exists($path);
549 static function autocomplete($value, $options) {
551 $selectOption = CRM_Core_BAO_CustomOption
::valuesByID($options['fieldID'], $options['optionGroupID']);
553 if (!in_array($value, $selectOption)) {
560 static function validContact($value, $actualElementValue = NULL) {
561 if ($actualElementValue) {
562 $value = $actualElementValue;
565 if ($value && !is_numeric($value)) {
572 * check the validity of the date (in qf format)
573 * note that only a year is valid, or a mon-year is
574 * also valid in addition to day-mon-year
578 * @return bool true if valid date
582 static function qfDate($date) {
583 $config = CRM_Core_Config
::singleton();
585 $d = CRM_Utils_Array
::value('d', $date);
586 $m = CRM_Utils_Array
::value('M', $date);
587 $y = CRM_Utils_Array
::value('Y', $date);
588 if (isset($date['h']) ||
591 $m = CRM_Utils_Array
::value('M', $date);
594 if (!$d && !$m && !$y) {
610 // if we have day we need mon, and if we have mon we need year
618 if (!empty($day) ||
!empty($mon) ||
!empty($year)) {
619 return checkdate($mon, $day, $year);
624 static function qfKey($key) {
625 return ($key) ? CRM_Core_Key
::valid($key) : FALSE;