Ejemplo n.º 1
0
 /**
  * Validate a value against a CustomField type.
  *
  * @param string $type
  *   The type of the data.
  * @param string $value
  *   The data to be validated.
  *
  * @return bool
  *   True if the value is of the specified type
  */
 public static function typecheck($type, $value)
 {
     switch ($type) {
         case 'Memo':
             return TRUE;
         case 'String':
             return CRM_Utils_Rule::string($value);
         case 'Int':
             return CRM_Utils_Rule::integer($value);
         case 'Float':
         case 'Money':
             return CRM_Utils_Rule::numeric($value);
         case 'Date':
             if (is_numeric($value)) {
                 return CRM_Utils_Rule::dateTime($value);
             } else {
                 return CRM_Utils_Rule::date($value);
             }
         case 'Boolean':
             return CRM_Utils_Rule::boolean($value);
         case 'ContactReference':
             return CRM_Utils_Rule::validContact($value);
         case 'StateProvince':
             //fix for multi select state, CRM-3437
             $valid = FALSE;
             $mulValues = explode(',', $value);
             foreach ($mulValues as $key => $state) {
                 $valid = array_key_exists(strtolower(trim($state)), array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvinceAbbreviation()), CASE_LOWER)) || array_key_exists(strtolower(trim($state)), array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvince()), CASE_LOWER));
                 if (!$valid) {
                     break;
                 }
             }
             return $valid;
         case 'Country':
             //fix multi select country, CRM-3437
             $valid = FALSE;
             $mulValues = explode(',', $value);
             foreach ($mulValues as $key => $country) {
                 $valid = array_key_exists(strtolower(trim($country)), array_change_key_case(array_flip(CRM_Core_PseudoConstant::countryIsoCode()), CASE_LOWER)) || array_key_exists(strtolower(trim($country)), array_change_key_case(array_flip(CRM_Core_PseudoConstant::country()), CASE_LOWER));
                 if (!$valid) {
                     break;
                 }
             }
             return $valid;
         case 'Link':
             return CRM_Utils_Rule::url($value);
     }
     return FALSE;
 }
Ejemplo n.º 2
0
 /**
  * Verify that a variable is of a given type.
  *
  * @param mixed $data
  *   The value to validate.
  * @param string $type
  *   The type to validate against.
  * @param bool $abort
  *   If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.
  * @name string $name
  *   The name of the attribute
  *
  * @return mixed
  *   The data, escaped if necessary
  */
 public static function validate($data, $type, $abort = TRUE, $name = 'One of parameters ')
 {
     switch ($type) {
         case 'Integer':
         case 'Int':
             if (CRM_Utils_Rule::integer($data)) {
                 return (int) $data;
             }
             break;
         case 'Positive':
             if (CRM_Utils_Rule::positiveInteger($data)) {
                 return $data;
             }
             break;
         case 'Boolean':
             if (CRM_Utils_Rule::boolean($data)) {
                 return $data;
             }
             break;
         case 'Float':
         case 'Money':
             if (CRM_Utils_Rule::numeric($data)) {
                 return $data;
             }
             break;
         case 'Text':
         case 'String':
         case 'Link':
         case 'Memo':
             return $data;
         case 'Date':
             // a null date is valid
             if (strlen(trim($data)) == 0) {
                 return trim($data);
             }
             if (preg_match('/^\\d{8}$/', $data) && CRM_Utils_Rule::mysqlDate($data)) {
                 return $data;
             }
             break;
         case 'Timestamp':
             // a null timestamp is valid
             if (strlen(trim($data)) == 0) {
                 return trim($data);
             }
             if ((preg_match('/^\\d{14}$/', $data) || preg_match('/^\\d{8}$/', $data)) && CRM_Utils_Rule::mysqlDate($data)) {
                 return $data;
             }
             break;
         case 'ContactReference':
             // null is valid
             if (strlen(trim($data)) == 0) {
                 return trim($data);
             }
             if (CRM_Utils_Rule::validContact($data)) {
                 return $data;
             }
             break;
         default:
             CRM_Core_Error::fatal("Cannot recognize {$type} for {$data}");
             break;
     }
     if ($abort) {
         $data = htmlentities($data);
         CRM_Core_Error::fatal("{$name} (value: {$data}) is not of the type {$type}");
     }
     return NULL;
 }
Ejemplo n.º 3
0
 /**
  * Verify that a variable is of a given type
  * 
  * @param mixed   $data         The variable
  * @param string  $type         The type
  * @param boolean $abort        Should we abort if invalid
  * @return mixed                The data, escaped if necessary
  * @access public
  * @static
  */
 public static function escape($data, $type, $abort = true)
 {
     require_once 'CRM/Utils/Rule.php';
     switch ($type) {
         case 'Integer':
         case 'Int':
             if (CRM_Utils_Rule::integer($data)) {
                 return $data;
             }
             break;
         case 'Positive':
             if (CRM_Utils_Rule::positiveInteger($data)) {
                 return $data;
             }
             break;
         case 'Boolean':
             if (CRM_Utils_Rule::boolean($data)) {
                 return $data;
             }
             break;
         case 'Float':
         case 'Money':
             if (CRM_Utils_Rule::numeric($data)) {
                 return $data;
             }
             break;
         case 'String':
         case 'Memo':
             return CRM_Core_DAO::escapeString($data);
             break;
         case 'Date':
         case 'Timestamp':
             // a null date or timestamp is valid
             if (strlen(trim($data)) == 0) {
                 return trim($data);
             }
             if ((preg_match('/^\\d{8}$/', $data) || preg_match('/^\\d{14}$/', $data)) && CRM_Utils_Rule::mysqlDate($data)) {
                 return $data;
             }
             break;
         case 'ContactReference':
             if (strlen(trim($data)) == 0) {
                 return trim($data);
             }
             if (CRM_Utils_Rule::validContact($data)) {
                 return $data;
             }
             break;
         default:
             CRM_Core_Error::fatal("Cannot recognize {$type} for {$data}");
             break;
     }
     if ($abort) {
         CRM_Core_Error::fatal("{$data} is not of the type {$type}");
     }
     return null;
 }