Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_condition_rest_response.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
18 
31 {
32 
33 
43  public static function evaluate($settings, &$state)
44  {
45  // the responses are passed in $state
46  // we need something in response to test the condition
47  if (!isset($state['event']['data']) || (is_array($state['event']['data']) && empty($state['event']['data']))) return FALSE;
48 
49  // if any of the options are missing, we can't compare values properly
50  if (!isset($settings['match_target']) || !isset($settings['match_condition_type']) ||
51  !isset($settings['match_type']) || !isset($settings['match_value'])) {
52  return FALSE;
53  }//end if
54 
55  // looks like we have all the values we need to evaluate the condition
56  // let's find out the values we should looked at
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'];
61  break;
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'];
69  }
70  }
71  break;
72  default:
73  // if unsupported option, don't pass this condition test
74  return FALSE;
75  }//end switch ($settings['match_target'])
76 
77  // loop through the target values we found and see if they match other condition options
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;
83  break;
84  case 'rest_response_match_type_contain':
85  $match_result = strpos($target_value, $settings['match_value']) === FALSE ? FALSE : TRUE;
86  break;
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;
90  break;
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;
94  break;
95  case 'rest_response_match_type_regex_match':
96  $match_result = preg_match('/'.$settings['match_value'].'/', $target_value) ? TRUE : FALSE;
97  break;
98  default:
99  $match_result = FALSE;
100  }//end switch ($settings['match_type'])
101 
102  // if 'all match' option is selected but found the target value that does not match the specified value($target_value),
103  // we have found a value does not satisfy the trigger condition
104  if ($settings['match_condition_type'] == 'rest_response_match_condition_all' && !$match_result) return FALSE;
105 
106  // if 'at least one match' option is selected and if we found a target value that matches the specified value ($target_value),
107  // it satisfies the trigger condition
108  if ($settings['match_condition_type'] == 'rest_response_match_condition_one' && $match_result) return TRUE;
109  }//end foreach ($target_values as $target_value)
110 
111  // if 'all match' option is selected and has reached this far, all values matched and satisfies the trigger condition
112  if ($settings['match_condition_type'] == 'rest_response_match_condition_all') return TRUE;
113 
114  // if 'at least one match' option is selected and has reached this far, there was no value that has matched
115  // and does not satisfy the trigger condition
116  if ($settings['match_condition_type'] == 'rest_response_match_condition_one') return FALSE;
117 
118  // nothing should reach this far but if it did, it shouldn't cause any harm by letting this move on to trigger actions
119  return FALSE;
120  }//end evaluate()
121 
122 
133  public static function getInterface($settings, $prefix, $write_access=FALSE)
134  {
135  $conditions = array_get_index($settings, 'conditions', Array());
136 
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'),
140  );
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'),
145  );
146  $match_type = Array(
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'),
152  );
153 
154  // user default values if the setting has not been set
155  if (!array_key_exists('match_condition_type', $settings)) {
156  $settings['match_condition_type'] = 'rest_response_match_condition_all';
157  }
158 
159  if (!array_key_exists('match_target', $settings)) {
160  $settings['match_target'] = 'rest_response_match_target_http_code';
161  }
162 
163  if (!array_key_exists('match_type', $settings)) {
164  $settings['match_type'] = 'rest_response_match_type_match';
165  }
166 
167  if (!array_key_exists('match_value', $settings)) {
168  $settings['match_value'] = '';
169  }
170 
171  // construct html elements
172  ob_start();
173  if ($write_access) {
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);
178  } else {
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'];
183  }//end if
184 
185  $conditions = ob_get_contents();
186  ob_end_clean();
187 
188  return translate('trigger_rest_response', $conditions);
189 
190  }//end getInterface()
191 
192 
202  public static function processInterface(&$settings, $request_data)
203  {
204  // check numeric only values
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');
209  }//end if
210 
211  // save the selected options
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','');
216 
217  return FALSE; // the return value is expected to indicate whether there has been an error or not
218 
219  }//end processInterface()
220 
221 
228  public static function allowMultiple()
229  {
230  return TRUE;
231 
232  }//end allowMultiple()
233 
234 }//end class
235 
236 ?>