17 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
43 public static function evaluate($settings, &$state)
47 if (!isset($state[
'event'][
'data']) || (is_array($state[
'event'][
'data']) && empty($state[
'event'][
'data'])))
return FALSE;
50 if (!isset($settings[
'match_target']) || !isset($settings[
'match_condition_type']) ||
51 !isset($settings[
'match_type']) || !isset($settings[
'match_value'])) {
57 $target_values = Array();
58 switch ($settings[
'match_target']) {
59 case 'rest_response_match_target_req_urls':
60 $target_values = $state[
'event'][
'data'][
'request'][
'urls'];
62 case 'rest_response_match_target_http_code':
63 case 'rest_response_match_target_body':
64 foreach ($state[
'event'][
'data'][
'responses'] as $response) {
65 if ($settings[
'match_target'] ==
'rest_response_match_target_http_code') {
66 $target_values[] = $response[
'info'][
'http_code'];
67 }
else if ($settings[
'match_target'] ==
'rest_response_match_target_body') {
68 $target_values[] = $response[
'body'];
78 foreach ($target_values as $target_value) {
79 $match_result = FALSE;
80 switch ($settings[
'match_type']) {
81 case 'rest_response_match_type_match':
82 $match_result = strval($target_value) === $settings[
'match_value'] ? TRUE : FALSE;
84 case 'rest_response_match_type_contain':
85 $match_result = strpos($target_value, $settings[
'match_value']) === FALSE ? FALSE : TRUE;
87 case 'rest_response_match_type_gt':
88 if (!is_numeric($target_value) || !is_numeric($settings[
'match_value']))
return FALSE;
89 $match_result = intval($target_value) > intval($settings[
'match_value']) ? TRUE : FALSE;
91 case 'rest_response_match_type_lt':
92 if (!is_numeric($target_value) || !is_numeric($settings[
'match_value']))
return FALSE;
93 $match_result = intval($target_value) < intval($settings[
'match_value']) ? TRUE : FALSE;
95 case 'rest_response_match_type_regex_match':
96 $match_result = preg_match(
'/'.$settings[
'match_value'].
'/', $target_value) ? TRUE : FALSE;
99 $match_result = FALSE;
104 if ($settings[
'match_condition_type'] ==
'rest_response_match_condition_all' && !$match_result)
return FALSE;
108 if ($settings[
'match_condition_type'] ==
'rest_response_match_condition_one' && $match_result)
return TRUE;
112 if ($settings[
'match_condition_type'] ==
'rest_response_match_condition_all')
return TRUE;
116 if ($settings[
'match_condition_type'] ==
'rest_response_match_condition_one')
return FALSE;
133 public static function getInterface($settings, $prefix, $write_access=FALSE)
135 $conditions = array_get_index($settings,
'conditions', Array());
137 $match_condition_type = Array(
138 'rest_response_match_condition_all' => translate(
'rest_response_match_condition_all'),
139 'rest_response_match_condition_one' => translate(
'rest_response_match_condition_one'),
141 $match_target = Array(
142 'rest_response_match_target_req_urls' => translate(
'rest_response_match_target_req_urls'),
143 'rest_response_match_target_http_code' => translate(
'rest_response_match_target_http_code'),
144 'rest_response_match_target_body' => translate(
'rest_response_match_target_body'),
147 'rest_response_match_type_match' => translate(
'rest_response_match_type_match'),
148 'rest_response_match_type_contain' => translate(
'rest_response_match_type_contain'),
149 'rest_response_match_type_gt' => translate(
'rest_response_match_type_gt'),
150 'rest_response_match_type_lt' => translate(
'rest_response_match_type_lt'),
151 'rest_response_match_type_regex_match' => translate(
'rest_response_match_type_regex_match'),
155 if (!array_key_exists(
'match_condition_type', $settings)) {
156 $settings[
'match_condition_type'] =
'rest_response_match_condition_all';
159 if (!array_key_exists(
'match_target', $settings)) {
160 $settings[
'match_target'] =
'rest_response_match_target_http_code';
163 if (!array_key_exists(
'match_type', $settings)) {
164 $settings[
'match_type'] =
'rest_response_match_type_match';
167 if (!array_key_exists(
'match_value', $settings)) {
168 $settings[
'match_value'] =
'';
174 combo_box($prefix.
'[match_condition_type]', $match_condition_type, FALSE, $settings[
'match_condition_type']);
175 combo_box($prefix.
'[match_target]', $match_target, FALSE, $settings[
'match_target']);
176 combo_box($prefix.
'[match_type]', $match_type, FALSE, $settings[
'match_type']);
177 text_box($prefix.
'[match_value]', array_get_index($settings,
'match_value'), 80);
179 echo $match_condition_type[$settings[
'match_condition_type']].
' ';
180 echo $match_target[$settings[
'match_target']].
' ';
181 echo $match_type[$settings[
'match_type']].
' ';
182 echo $settings[
'match_value'];
185 $conditions = ob_get_contents();
188 return translate(
'trigger_rest_response', $conditions);
205 if (($request_data[
'match_type'] ==
'rest_response_match_type_gt' ||
206 $request_data[
'match_type'] ==
'rest_response_match_type_lt') &&
207 !is_numeric($request_data[
'match_value'])) {
208 return translate(
'trigger_numeric_only');
212 $settings[
'match_condition_type'] = array_get_index($request_data,
'match_condition_type',
'rest_response_match_condition_all');;
213 $settings[
'match_target'] = array_get_index($request_data,
'match_target',
'rest_response_match_target_http_code');
214 $settings[
'match_type'] = array_get_index($request_data,
'match_type',
'rest_response_match_type_match');
215 $settings[
'match_value'] = array_get_index($request_data,
'match_value',
'');