/** * Prepare query string and request body using variables to be sent in get and request_structure to be sent as JSON in body * from method definition. * * @param bool $params Receives parameters to be used in GET and scope array to create JSON body after parsing $_definition['request_structure'] object * * @return array|bool Returns an array with data to be used in request to server or false on error */ public function prepare_for_request($params = false) { if (!$this->validate_definition()) { return false; } if (empty($params) or !is_array($params)) { $params = array(); } if (empty($params['skip_regexps'])) { $params['skip_regexps'] = false; } else { $params['skip_regexps'] = !empty($params['skip_regexps']) ? true : false; } if (empty($params['allow_remote_calls'])) { $params['allow_remote_calls'] = false; } if (empty($params['get_variables']) or !is_array($params['get_variables'])) { $params['get_variables'] = array(); } if (empty($params['method_params']) or !is_array($params['method_params'])) { $params['method_params'] = array(); } if (empty($params['custom_validators']) or !is_array($params['custom_validators'])) { $params['custom_validators'] = array(); } if ($internal_params = $this->request_parameters()) { if (!empty($internal_params['get_variables']) and is_array($internal_params['get_variables'])) { $params['get_variables'] = array_merge($internal_params['get_variables'], $params['get_variables']); } if (!empty($internal_params['method_params']) and is_array($internal_params['method_params'])) { $params['method_params'] = array_merge($internal_params['method_params'], $params['method_params']); } } $return_arr = array(); $return_arr['func'] = $this->_functionality; $return_arr['full_query'] = $this->_definition['url_suffix']; $return_arr['http_method'] = $this->_definition['http_method']; $return_arr['query_string'] = ''; $return_arr['url_variables'] = array(); $return_arr['get_variables'] = array(); $return_arr['request_body'] = ''; if (!empty($this->_definition['get_variables']) and is_array($this->_definition['get_variables'])) { $value_source_obj = new S2P_SDK_Values_Source(); if (!empty($params['allow_remote_calls'])) { $value_source_obj->remote_calls(true); } else { $value_source_obj->remote_calls(false); } foreach ($this->_definition['get_variables'] as $get_var) { if (!array_key_exists($get_var['name'], $params['get_variables'])) { if (!empty($get_var['mandatory'])) { $this->set_error(self::ERR_MANDATORY, self::s2p_t('Variable %s is mandatory for method %s.', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name'], $this->_definition['name'])); return false; } continue; } $var_value = S2P_SDK_Scope_Variable::scalar_value($get_var['type'], $params['get_variables'][$get_var['name']], $get_var['array_type'], $get_var['array_numeric_keys']); $default_var_value = null; if (array_key_exists('default', $get_var)) { $default_var_value = S2P_SDK_Scope_Variable::scalar_value($get_var['type'], $get_var['default'], $get_var['array_type'], $get_var['array_numeric_keys']); } if (!empty($get_var['skip_if_default']) and $var_value === $default_var_value) { continue; } if (empty($params['skip_regexps']) and !empty($get_var['regexp']) and !preg_match('/' . $get_var['regexp'] . '/', $var_value)) { $this->set_error(self::ERR_REGEXP, self::s2p_t('Get variable %s is invalid.', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name']), sprintf('Get variable [%s] failed regular exp [%s].', $get_var['name'], $get_var['regexp'])); return false; } if (!empty($get_var['value_source']) and $value_source_obj::valid_type($get_var['value_source'])) { $value_source_obj->source_type($get_var['value_source']); if (!$value_source_obj->valid_value($var_value)) { $this->set_error(self::ERR_VALUE_SOURCE, self::s2p_t('Variable %s contains invalid value [%s].', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name'], $var_value)); return false; } } if (!empty($get_var['move_in_url'])) { $return_arr['url_variables'][$get_var['external_name']] = $var_value; } else { $return_arr['get_variables'][$get_var['external_name']] = $var_value; } } } // Replace any URL variables, even if we don't currently have $return_arr['url_variables'] set if ($replacement_result = $this->replace_url_variables($return_arr['full_query'], $return_arr['url_variables'])) { if (!empty($replacement_result['url_variables']) and is_array($replacement_result['url_variables'])) { $return_arr['get_variables'] = array_merge($return_arr['get_variables'], $replacement_result['url_variables']); } $return_arr['full_query'] = $replacement_result['url']; } if (!empty($return_arr['get_variables'])) { $return_arr['query_string'] = http_build_query($return_arr['get_variables']); } if (!empty($return_arr['query_string'])) { if (strstr($return_arr['full_query'], '?') === false) { $return_arr['full_query'] .= '?'; } $return_arr['full_query'] .= $return_arr['query_string']; } if (!empty($this->_definition['request_structure'])) { /** @var S2P_SDK_Scope_Structure $request_structure */ $request_structure = $this->_definition['request_structure']; $request_to_array_params = array(); $request_to_array_params['output_null_values'] = false; $request_to_array_params['nullify_full_object'] = false; if (!($json_array = $request_structure->prepare_info_for_request_to_array($params['method_params'], $request_to_array_params)) or !is_array($json_array)) { if ($parsing_error = $request_structure->get_parsing_error()) { $this->copy_error_from_array($parsing_error); } else { $this->set_error(self::ERR_REQUEST_DATA, self::s2p_t('Couldn\'t extract request data or request data is empty.')); } return false; } if (!empty($this->_definition['mandatory_in_request']) and is_array($this->_definition['mandatory_in_request'])) { if (!$this->check_mandatory_fields($json_array, $this->_definition['mandatory_in_request'], array('scope_arr_type' => 'request', 'structure_obj' => $request_structure))) { if (!$this->has_error()) { $this->set_error(self::ERR_REQUEST_MANDATORY, self::s2p_t('Mandatory fields not found in request.')); } return false; } } if (!empty($this->_definition['hide_in_request']) and is_array($this->_definition['hide_in_request'])) { $json_array = $this->remove_fields($json_array, $this->_definition['hide_in_request'], array('scope_arr_type' => 'request')); } $return_arr['request_body'] = @json_encode($json_array); } return $return_arr; }
private function validate_form_method_get_params_fields($post_arr, $submit_result_arr) { if (empty($this->_method) or empty($this->_method_func) or !($func_details = $this->_method_func_details) or empty($func_details['get_variables']) or !is_array($func_details['get_variables'])) { return $submit_result_arr; } $post_arr = self::validate_post_data($post_arr); $submit_result_arr = self::validate_submit_result($submit_result_arr); $value_source_obj = new S2P_SDK_Values_Source(); foreach ($func_details['get_variables'] as $get_var) { if (!array_key_exists($get_var['name'], $post_arr['gvars'])) { if (!empty($get_var['mandatory'])) { $submit_result_arr['errors_arr']['gvars'][$get_var['name']] = self::s2p_t('Mandatory field %s not provided.', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name']); } continue; } if (!empty($get_var['regexp']) and !@preg_match('/' . $get_var['regexp'] . '/', $post_arr['gvars'][$get_var['name']])) { $submit_result_arr['errors_arr']['gvars'][$get_var['name']] = self::s2p_t('Field %s failed regular expression %s.', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name'], $get_var['regexp']); continue; } $var_value = S2P_SDK_Scope_Variable::scalar_value($get_var['type'], $post_arr['gvars'][$get_var['name']], $get_var['array_type'], $get_var['array_numeric_keys']); $default_var_value = null; if (array_key_exists('default', $get_var)) { $default_var_value = S2P_SDK_Scope_Variable::scalar_value($get_var['type'], $get_var['default'], $get_var['array_type'], $get_var['array_numeric_keys']); } if (!empty($get_var['skip_if_default']) and $var_value === $default_var_value) { continue; } if (!empty($get_var['value_source']) and $value_source_obj::valid_type($get_var['value_source'])) { $value_source_obj->source_type($get_var['value_source']); if (!$value_source_obj->valid_value($var_value)) { $submit_result_arr['errors_arr']['gvars'][$get_var['name']] = self::s2p_t('Variable %s contains invalid value [%s].', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name'], $var_value); continue; } } } return $submit_result_arr; }