X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FUtils%2FRule.php;h=4c27b896e9a6c3299ec0ce521848dc9dd93ae99d;hb=cc6662100de9040ec0245b43fdbbaba0fea3aa17;hp=98c8b96a275bed2905426f06b6f8137e0234e07d;hpb=66c6206c9d19f2aa13ff0f7051c9952b6fbdad50;p=civicrm-core.git diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index 98c8b96a27..4c27b896e9 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -1,9 +1,9 @@ 31) { @@ -69,6 +89,11 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $str + * + * @return bool + */ static function qfVariable($str) { // check length etc //if ( empty( $str ) || strlen( $str ) > 31 ) { @@ -85,6 +110,11 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $phone + * + * @return bool + */ static function phone($phone) { // check length etc if (empty($phone) || strlen($phone) > 16) { @@ -98,6 +128,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $query + * + * @return bool + */ static function query($query) { // check length etc if (empty($query) || strlen($query) < 3 || strlen($query) > 127) { @@ -112,15 +147,30 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $url + * + * @return bool + */ static function url($url) { return (bool) filter_var($url, FILTER_VALIDATE_URL); } + /** + * @param $string + * + * @return bool + */ static function wikiURL($string) { $items = explode(' ', trim($string), 2); return self::url($items[0]); } + /** + * @param $domain + * + * @return bool + */ static function domain($domain) { // not perfect, but better than the previous one; see CRM-1502 if (!preg_match('/^[A-Za-z0-9]([A-Za-z0-9\.\-]*[A-Za-z0-9])?$/', $domain)) { @@ -129,6 +179,12 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $value + * @param null $default + * + * @return null + */ static function date($value, $default = NULL) { if (is_string($value) && preg_match('/^\d\d\d\d-?\d\d-?\d\d$/', $value) @@ -138,6 +194,12 @@ class CRM_Utils_Rule { return $default; } + /** + * @param $value + * @param null $default + * + * @return null|string + */ static function dateTime($value, $default = NULL) { $result = $default; if (is_string($value) && @@ -174,7 +236,7 @@ class CRM_Utils_Rule { } // CRM-9017 CiviContribute/CiviMember form with expiration date format 'm Y' - if (!$m && CRM_Utils_Array::value('m', $date)) { + if (!$m && !empty($date['m'])) { $m = CRM_Utils_Array::value('m', $date); } @@ -267,45 +329,91 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function integer($value) { if (is_int($value)) { return TRUE; } - if (($value < 0)) { + // CRM-13460 + // ensure number passed is always a string numeral + if (!is_numeric($value)) { + return FALSE; + } + + // note that is_int matches only integer type + // and not strings which are only integers + // hence we do this here + if (preg_match('/^\d+$/', $value)) { + return TRUE; + } + + if ($value < 0) { $negValue = -1 * $value; if (is_int($negValue)) { return TRUE; } } - if (is_numeric($value) && preg_match('/^\d+$/', $value)) { - return TRUE; - } - return FALSE; } + /** + * @param $value + * + * @return bool + */ static function positiveInteger($value) { if (is_int($value)) { return ($value < 0) ? FALSE : TRUE; } - if (is_numeric($value) && preg_match('/^\d+$/', $value)) { + // CRM-13460 + // ensure number passed is always a string numeral + if (!is_numeric($value)) { + return FALSE; + } + + if (preg_match('/^\d+$/', $value)) { return TRUE; } return FALSE; } + /** + * @param $value + * + * @return bool + */ static function numeric($value) { + // lets use a php gatekeeper to ensure this is numeric + if (!is_numeric($value)) { + return FALSE; + } + return preg_match('/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/', $value) ? TRUE : FALSE; } + /** + * @param $value + * @param $noOfDigit + * + * @return bool + */ static function numberOfDigit($value, $noOfDigit) { return preg_match('/^\d{' . $noOfDigit . '}$/', $value) ? TRUE : FALSE; } + /** + * @param $value + * + * @return mixed + */ static function cleanMoney($value) { // first remove all white space $value = str_replace(array(' ', "\t", "\n"), '', $value); @@ -337,6 +445,11 @@ class CRM_Utils_Rule { return $value; } + /** + * @param $value + * + * @return bool + */ static function money($value) { $config = CRM_Core_Config::singleton(); @@ -360,6 +473,12 @@ class CRM_Utils_Rule { return preg_match('/(^-?\d+\.\d?\d?$)|(^-?\.\d\d?$)/', $value) ? TRUE : FALSE; } + /** + * @param $value + * @param int $maxLength + * + * @return bool + */ static function string($value, $maxLength = 0) { if (is_string($value) && ($maxLength === 0 || strlen($value) <= $maxLength) @@ -369,16 +488,31 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function boolean($value) { return preg_match( '/(^(1|0)$)|(^(Y(es)?|N(o)?)$)|(^(T(rue)?|F(alse)?)$)/i', $value ) ? TRUE : FALSE; } + /** + * @param $value + * + * @return bool + */ static function email($value) { return (bool) filter_var($value, FILTER_VALIDATE_EMAIL); } + /** + * @param $list + * + * @return bool + */ static function emailList($list) { $emails = explode(',', $list); foreach ($emails as $email) { @@ -393,6 +527,11 @@ class CRM_Utils_Rule { // allow between 4-6 digits as postal code since india needs 6 and US needs 5 (or // if u disregard the first 0, 4 (thanx excel!) // FIXME: we need to figure out how to localize such rules + /** + * @param $value + * + * @return bool + */ static function postalCode($value) { if (preg_match('/^\d{4,6}(-\d{4})?$/', $value)) { return TRUE; @@ -484,21 +623,44 @@ class CRM_Utils_Rule { 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)); } + /** + * @param $value + * @param $options + * + * @return bool + */ static function optionExists($value, $options) { return CRM_Core_OptionValue::optionExists($value, $options[0], $options[1], $options[2], CRM_Utils_Array::value(3, $options, 'name')); } + /** + * @param $value + * @param $type + * + * @return bool + */ static function creditCardNumber($value, $type) { require_once 'Validate/Finance/CreditCard.php'; return Validate_Finance_CreditCard::number($value, $type); } + /** + * @param $value + * @param $type + * + * @return bool + */ static function cvv($value, $type) { require_once 'Validate/Finance/CreditCard.php'; return Validate_Finance_CreditCard::cvv($value, $type); } + /** + * @param $value + * + * @return bool + */ static function currencyCode($value) { static $currencyCodes = NULL; if (!$currencyCodes) { @@ -510,6 +672,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $value + * + * @return bool + */ static function xssString($value) { if (is_string($value)) { return preg_match('!<(vb)?script[^>]*>.*!ims', @@ -521,10 +688,21 @@ class CRM_Utils_Rule { } } + /** + * @param $path + * + * @return bool + */ static function fileExists($path) { return file_exists($path); } + /** + * @param $value + * @param $options + * + * @return bool + */ static function autocomplete($value, $options) { if ($value) { $selectOption = CRM_Core_BAO_CustomOption::valuesByID($options['fieldID'], $options['optionGroupID']); @@ -536,6 +714,12 @@ class CRM_Utils_Rule { return TRUE; } + /** + * @param $value + * @param null $actualElementValue + * + * @return bool + */ static function validContact($value, $actualElementValue = NULL) { if ($actualElementValue) { $value = $actualElementValue; @@ -600,6 +784,11 @@ class CRM_Utils_Rule { return FALSE; } + /** + * @param $key + * + * @return bool + */ static function qfKey($key) { return ($key) ? CRM_Core_Key::valid($key) : FALSE; }