/** * 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; }
/** * Validate a value against a CustomField type * * @param string $type The type of the data * @param string $value The data to be validated * * @return boolean True if the value is of the specified type * @access 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': return CRM_Utils_Rule::date($value); case 'Boolean': return CRM_Utils_Rule::boolean($value); case 'StateProvince': return array_key_exists(strtolower($value), array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvinceAbbreviation()), CASE_LOWER)) || array_key_exists(strtolower($value), array_change_key_case(array_flip(CRM_Core_PseudoConstant::stateProvince()), CASE_LOWER)); case 'Country': return array_key_exists(strtolower($value), array_change_key_case(array_flip(CRM_Core_PseudoConstant::countryIsoCode()), CASE_LOWER)) || array_key_exists(strtolower($value), array_change_key_case(array_flip(CRM_Core_PseudoConstant::country()), CASE_LOWER)); } return false; }
/** * Get sort, limit etc options from the params - supporting old & new formats. * * Get returnProperties for legacy * * @param array $params * Params array as passed into civicrm_api. * @param bool $queryObject * Is this supporting a queryObject api (e.g contact) - if so we support more options. * for legacy report & return a unique fields array * * @param string $entity * @param string $action * * @throws API_Exception * @return array * options extracted from params */ function _civicrm_api3_get_options_from_params(&$params, $queryObject = FALSE, $entity = '', $action = '') { $lowercase_entity = _civicrm_api_get_entity_name_from_camel($entity); $is_count = FALSE; $sort = CRM_Utils_Array::value('sort', $params, 0); $sort = CRM_Utils_Array::value('option.sort', $params, $sort); $sort = CRM_Utils_Array::value('option_sort', $params, $sort); $offset = CRM_Utils_Array::value('offset', $params, 0); $offset = CRM_Utils_Array::value('option.offset', $params, $offset); // dear PHP thought it would be a good idea to transform a.b into a_b in the get/post $offset = CRM_Utils_Array::value('option_offset', $params, $offset); $limit = CRM_Utils_Array::value('rowCount', $params, 25); $limit = CRM_Utils_Array::value('option.limit', $params, $limit); $limit = CRM_Utils_Array::value('option_limit', $params, $limit); if (is_array(CRM_Utils_Array::value('options', $params))) { // is count is set by generic getcount not user $is_count = CRM_Utils_Array::value('is_count', $params['options']); $offset = CRM_Utils_Array::value('offset', $params['options'], $offset); $limit = CRM_Utils_Array::value('limit', $params['options'], $limit); $sort = CRM_Utils_Array::value('sort', $params['options'], $sort); } $returnProperties = array(); // handle the format return =sort_name,display_name... if (array_key_exists('return', $params)) { if (is_array($params['return'])) { $returnProperties = array_fill_keys($params['return'], 1); } else { $returnProperties = explode(',', str_replace(' ', '', $params['return'])); $returnProperties = array_fill_keys($returnProperties, 1); } } if ($entity && $action == 'get') { if (!empty($returnProperties['id'])) { $returnProperties[$lowercase_entity . '_id'] = 1; unset($returnProperties['id']); } switch (trim(strtolower($sort))) { case 'id': case 'id desc': case 'id asc': $sort = str_replace('id', $lowercase_entity . '_id', $sort); } } $options = array('offset' => CRM_Utils_Rule::integer($offset) ? $offset : NULL, 'sort' => CRM_Utils_Rule::string($sort) ? $sort : NULL, 'limit' => CRM_Utils_Rule::integer($limit) ? $limit : NULL, 'is_count' => $is_count, 'return' => !empty($returnProperties) ? $returnProperties : array()); if ($options['sort'] && stristr($options['sort'], 'SELECT')) { throw new API_Exception('invalid string in sort options'); } if (!$queryObject) { return $options; } //here comes the legacy support for $returnProperties, $inputParams e.g for contat_get // if the queryobject is being used this should be used $inputParams = array(); $legacyreturnProperties = array(); $otherVars = array('sort', 'offset', 'rowCount', 'options', 'return', 'version', 'prettyprint', 'check_permissions', 'sequential'); foreach ($params as $n => $v) { if (substr($n, 0, 7) == 'return.') { $legacyreturnProperties[substr($n, 7)] = $v; } elseif ($n == 'id') { $inputParams[$lowercase_entity . '_id'] = $v; } elseif (in_array($n, $otherVars)) { } else { $inputParams[$n] = $v; if ($v && !is_array($v) && stristr($v, 'SELECT')) { throw new API_Exception('invalid string'); } } } $options['return'] = array_merge($returnProperties, $legacyreturnProperties); $options['input_params'] = $inputParams; return $options; }