Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_make_soap_call.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
18 require_once SQ_FUDGE_PATH.'/general/text.inc';
19 require_once SQ_INCLUDE_PATH.'/general.inc';
20 require_once dirname(dirname(dirname(__FILE__))).'/lib/soap_common.inc';
21 
22 
33 {
34 
35 
45  public static function execute($settings, &$state)
46  {
47  $success = TRUE;
48 
49  // Must have a wsdl
50  if (empty($settings['wsdl'])) {
51  return FALSE;
52  }
53 
54  // The SOAP service must have actions
55  if (empty($settings['actions'])) {
56  return FALSE;
57  }//end if
58 
59  // The action we try to use must be specified
60  if (!isset($settings['action'])) {
61  return FALSE;
62  }//end if
63 
64  // Expect at least one parameter
65  if (!isset ($settings[$settings['action'].'parameters0'])) {
66  return FALSE;
67  }//end if
68 
69  if(!isset($settings['actions'][$settings['action']])) {
70  return FALSE;
71  }
72 
73  if (empty($state['asset'])) {
74  // grab the asset if assetid is given, but not the asset.
75  if (empty($state['assetid'])) {
76  return FALSE;
77  } else {
78  $state['asset'] = &$GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
79  }
80  }
81 
82  $function = $settings['action'];
83  $wsdl = $settings['wsdl'];
84  $options = SOAP_Common::_getSoapOptions($settings);
85 
86  // Work out the parameters of that soap function
87  for($i = 0, $param = Array(); $i < count($settings['actions'][$settings['action']]); $i++) {
88  if(isset($settings[$settings['action'].'parameters'.$i])) {
89  $soap_message = self::getKeywordReplacementsFromAsset($settings[$settings['action'].'parameters'.$i], $state['asset']);
90  $object = @simplexml_load_string($soap_message);
91 
92  // Convert XML to array, replace file content keywords, xml is not friendly for binary data
93  $object = SOAP_Common::toArray($object);
94 
95  $param[] = $object;
96  }
97  }
98 
99  // Get an array of parameters
100  $eval_string = '';
101  foreach ($param as $index => $object) {
102  $eval_string .= $index == 0 ? '' : ', ';
103  $eval_string .= '$param['.$index.']';
104  }
105 
106  // Enable backtrace option
107  $options['trace'] = 1;
108 
109  // If SOAP exceptions are to be silenced
110  if ($settings['mute_exceptions']) {
111  $options['exceptions'] = 0;
112  }
113 
114  // Make SOAP call
115  try {
116  $client = new SoapClient($wsdl, $options);
117  eval('$data = $client->$function('.$eval_string.');');
118  }
119  catch (Exception $e) {
120  // SOAP error should be treated as normal error
121  trigger_error($e->getMessage());
122  // also store the error in our global_session_vars
123  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['soap_trigger_action_error'] = $e->getMessage();
124  return FALSE;
125  }
126 
127  if ($settings['mute_exceptions'] && get_class($data) == 'SoapFault') {
128  $soap_response = $client->__getLastResponse();
129  $data = NULL;
130  // Create the SOAP call return object from last SOAP request
131  if (preg_match('|<([^:]+:)?Body>(.*)</([^:]+:)?Body>|msi', $soap_response, $match)) {
132  $data = simplexml_load_string($match[2]);
133  }
134  }
135 
136  // Store results in session
137  $soap_response_keywords = Array();
138  $file_saving_keywords = array_get_index($settings, $settings['action'].'_file_response_to_save', Array());
139  SOAP_Common::_store_results($data, 'soap_', $soap_response_keywords, $file_saving_keywords);
140 
141  // Remove prefix from response keywords
142  foreach($soap_response_keywords as $key => $val) {
143  $soap_response_keywords[substr_replace($key, '', 0, 5)] = $val;
144  unset($soap_response_keywords[$key]);
145  }
146 
147  // If the SOAP response has one of the non-empty "Check Response" fields, return error
148  if (!empty($settings[$settings['action'].'_response_error_fields'])) {
149  foreach($settings[$settings['action'].'_response_error_fields'] as $check_field => $error_msg) {
150  if (!empty($soap_response_keywords[$check_field])) {
151  $warning_level = $settings[$settings['action'].'_treat_as_fatal'] ? E_USER_ERROR : E_USER_WARNING;
152  $_SESSION[SQ_SESSION_SANDBOX_INDEX]['soap_trigger_action_error'] = $error_msg;
153  trigger_error($error_msg, $warning_level);
154 
155  $success = FALSE;
156  }
157  }//end foreach
158  }//end if
159 
160  return $success;
161 
162  }//end execute()
163 
164 
175  public static function getInterface($settings, $prefix, $write_access=FALSE)
176  {
177  ob_start();
178  ?>
179 
180  <p class="sq-backend-section-subheading">Connection Details</p>
181 
182  <table class="sq-backend-table" >
183  <tr>
184  <td width="120"><p>WSDL</p></td>
185  <td><p>
186  <?php
187  $wsdl = array_get_index($settings, 'wsdl', '');
188  if ($write_access) {
189  text_box($prefix.'[wsdl]', $wsdl, 120);
190  } else {
191  echo $wsdl;
192  }
193  ?></p></td>
194  </tr>
195  <tr>
196  <td><?php echo translate('login'); ?></td>
197  <td><p>
198  <?php
199  $login = array_get_index($settings, 'login', '');
200  if ($write_access) {
201  text_box($prefix.'[login]', $login, 35);
202  } else {
203  echo $login;
204  }
205  ?>
206  </p></td>
207  </tr>
208  <tr>
209  <td><?php echo translate('password'); ?></td>
210  <td><p>
211  <?php
212  if($write_access) password_box($prefix.'[password]', array_get_index($settings, 'password', ''), 35);
213  ?>
214  </p></td>
215  </tr>
216  <tr>
217  <td colspan="2"><?php echo translate('use_http_authentication'); ?></td>
218  </tr>
219  <tr>
220  <td><?php echo translate('mute_exception'); ?></td>
221  <td><p>
222  <?php
223  $mute_exp = array_get_index($settings, 'mute_exceptions', 0);
224  if ($write_access) {
225  check_box($prefix.'[mute_exceptions]', 1, $mute_exp);
226  } else {
227  $mute_exp_status = ($mute_exp == '1') ? 'tick' : 'cross';
228  echo '<img class="sq-icon" width="16" height="16" src="'.sq_web_path('lib').'/web/images/'.$mute_exp_status.'.gif" alt="" style="border: 0;" />';
229  }
230  ?>
231  </p></td>
232  </tr>
233  </table>
234 
235  <table class="sq-backend-table" >
236  <tr>
237  <td><p><?php echo translate('local_cert'); ?></p></td>
238  <td><p>
239  <?php
240  $local_cert = array_get_index($settings, 'local_cert', 0);
241  if ($write_access) {
242  asset_finder($prefix.'[local_cert]', $local_cert, Array('file'=>'D'));
243  } else {
244  if ($local_cert) echo get_asset_tag_line($local_cert);
245  }
246  ?>
247  </p></td>
248  </tr>
249  <tr>
250  <td><p><?php echo translate('passphrase'); ?></p></td>
251  <td><p>
252  <?php
253  if ($write_access) password_box($prefix.'[passphrase]', array_get_index($settings, 'passphrase', ''), 35);
254  ?>
255  </p></td>
256  </tr>
257  <tr>
258  <td colspan="2"><?php echo translate('use_https'); ?></td>
259  </tr>
260  </table>
261 
262  <p class="sq-backend-section-subheading">Test Response</p>
263  <div id="<?php echo $prefix ?>_test_request_response" style="padding: 0.5em 0px 2em 30px;">
264  <table class="sq-backend-table" >
265  <tr>
266  <td width="120"><?php echo translate('run_test'); ?></td>
267  <td><p>
268  <?php
269  $run_test = array_get_index($settings, 'run_test', 0);
270  if ($write_access) {
271  check_box($prefix.'[run_test]', 1, $run_test);
272  } else {
273  $run_test_status = ($mute_exp == '1') ? 'tick' : 'cross';
274  echo '<img class="sq-icon" width="16" height="16" src="'.sq_web_path('lib').'/web/images/'.$run_test_status.'.gif" alt="" style="border: 0;" />';
275  }
276  ?>
277  </p></td>
278  </tr>
279  <tr>
280  <td colspan="2"><?php echo translate('run_test_note'); ?></td>
281  </tr>
282  </table>
283  </div>
284 
285  <div width="120">
286  <?php
287  if ($write_access && $run_test && !empty($settings['test_results'])) {
288  foreach($settings['test_results'] as $soap_func => $info) {
289  echo "<b><u>$soap_func</u></b><br /><br />";
290  echo "<b>Request:</b><br />";
291  echo (nl2br(str_replace(' ', '&nbsp;&nbsp;', htmlentities($info['request']))));
292  echo "<br />";
293  echo "<b>Response:</b><br />";
294  echo (nl2br(str_replace(' ', '&nbsp;&nbsp;', htmlentities($info['response']))));
295  echo "<br />";
296 
297  }
298  echo "<br />";
299  }
300  ?>
301  </div>
302 
303 
304  <?php if (isset($settings['actions']) && !empty($settings['actions'])) { ?>
305  <p class="sq-backend-section-subheading"><?php echo translate('request_message'); ?></p>
306 
307  <table class="sq-backend-table" >
308  <tr>
309  <td><strong><?php echo translate('action');?></strong><br /><br /></td>
310  <td>
311  <?php
312  $action_selected = array_get_index($settings, 'action', '');
313  foreach ($settings['actions'] as $name => $detail) {
314  $action_list[$name] = $name;
315  }
316  if ($write_access) {
317  combo_box($prefix.'[action]', $action_list, FALSE, $action_selected);
318  } else {
319  echo $action_selected;
320  }
321  ?>
322  <br /><br />
323  </td>
324  </tr>
325  <tr>
326  <td><span style="padding-top:10px;"><strong><?php echo translate('params');?></strong></span></td>
327  <td>
328  <?php
329  $i = 0;
330  if(isset($settings['actions'][$settings['action']])) {
331  foreach ($settings['actions'][$settings['action']] as $parameter_xml) {
332  if ($write_access) text_area($prefix.'['.$settings['action'].'parameters'.$i.']', array_get_index($settings,$settings['action'].'parameters'.$i, $parameter_xml), 80, 15);
333  echo '<br/>';
334  $i++;
335  }
336  }
337  ?>
338  <br />
339  </td>
340  </tr>
341  <tr>
342  <td colspan="2">
343  <?php
344  echo translate('keyword_usable_note');
345  ?>
346  </td>
347  </tr>
348  </table>
349 
350  <p class="sq-backend-section-subheading"><?php echo translate('response_message'); ?></p>
351 
352  <table class="sq-backend-table" >
353  <tr>
354  <td>
355  <?php
356  $i = 0;
357  if(isset($settings['responses'][$settings['action']])) {
358  echo (nl2br(str_replace(' ', '&nbsp;&nbsp;', htmlentities($settings['responses'][$settings['action']]))));
359  }
360  ?>
361  <br />
362  </td>
363  </tr>
364  <tr>
365  <td>
366  <?php echo translate('response_message_note'); ?>
367  </td>
368  </tr>
369  <tr>
370  </tr>
371  </table>
372 
373  <p class="sq-backend-section-subheading"><?php echo translate('response_keywords'); ?></p>
374 
375  <table class="sq-backend-table" >
376  <tr>
377  <td>
378  <?php
379  $i = 0;
380  if(isset($settings['response_keywords'][$settings['action']])) {
381  echo ($settings['response_keywords'][$settings['action']]);
382  }
383  ?>
384  <br />
385  </td>
386  </tr>
387  <tr>
388  </tr>
389  </table>
390 
391  <p class="sq-backend-section-subheading"><?php echo translate('check_response')?></p>
392 
393  <table class="sq-backend-table" >
394  <tr width="120">
395  <td>
396  <?php
397  $selected_fields = isset($settings[$settings['action'].'_response_error_fields']) ? $settings[$settings['action'].'_response_error_fields'] : Array();
398  $selected_field_names = Array();
399  if (empty($selected_fields)) {
400  echo translate('no_field_selected');
401  } else {
402  ?>
403  <table class="sq-backend-table">
404  <tr>
405  <td class="sq-backend-table-header"><?php echo translate('field_name'); ?></td>
406  <td class="sq-backend-table-header"><?php echo translate('error_text'); ?></td>
407  <td class="sq-backend-table-header"><?php echo translate('delete_question'); ?></td>
408  </tr>
409  <?php
410  foreach ($selected_fields as $selected_field => $error_msg) {
411  ?>
412  <tr>
413  <td class="sq-backend-table-cell">
414  <?php
415  echo $selected_field;
416  $selected_field_names[] = $selected_field;
417  ?>
418  </td>
419  <td class="sq-backend-table-cell">
420  <?php
421  if ($write_access) {
422  text_box($prefix.'['.$settings['action'].'_error_msg_'.$selected_field.']', $error_msg, 110);
423  } else {
424  echo $error_msg;
425  }
426  ?>
427  </td>
428 
429  <td class="sq-backend-table-cell">
430  <?php if ($write_access) check_box($prefix.'['.$settings['action'].'_delete_field_'.$selected_field.']'); ?>
431  </td>
432  </tr>
433  <?php
434  }//end foreach
435  ?>
436  </table>
437  <?php
438  }//end else
439 
440  if ($write_access) {
441  echo translate('add_new_field');
442  $response_fields = Array('' => '-- Select Field --');
443  if (isset($settings['response_keywords'][$settings['action']])) {
444  $response_keywords = explode('<br/>', $settings['response_keywords'][$settings['action']]);
445  foreach($response_keywords as $val) {
446  $val = trim($val, '%');
447  $val = substr_replace($val, '', 0, strlen('globals_session_soap_'));
448 
449  if ($val) {
450  $response_fields[$val] = $val;
451  }
452  }//end foreach
453  }
454  combo_box($prefix.'['.$settings['action'].'_response_error_fields'.']', $response_fields, TRUE, $selected_field_names);
455  }
456  ?>
457  </td>
458  </tr>
459  <tr>
460  <td>
461  <?php
462  $treating_fatal = array_get_index($settings, $settings['action'].'_treat_as_fatal', 0);
463  if ($write_access) {
464  check_box($prefix.'['.$settings['action'].'_treat_as_fatal]', '1', $treating_fatal);
465  } else {
466  $treat_fatal_status = ($treating_fatal == '1') ? 'tick' : 'cross';
467  echo '<img class="sq-icon" width="16" height="16" src="'.sq_web_path('lib').'/web/images/'.$treat_fatal_status.'.gif" alt="" style="border: 0;" />';
468  }
469  echo translate('treat_as_fatal_note');
470  ?>
471  </td>
472  </tr>
473  <tr>
474  <td>
475  <?php
476  echo translate('check_response_note');
477  ?>
478  </td>
479  </tr>
480  </table>
481  <p class="sq-backend-section-subheading"><?php echo translate('response_to_download_as_file'); ?></p>
482  <table class="sq-backend-table">
483  <tr>
484  <td class="sq-backend-table-header"><?php echo translate('field_keyword_tags'); ?></td>
485  </tr>
486  <?php
487  $response_to_file = array_get_index($settings, $settings['action'].'_file_response_to_save', Array());
488  $currently_configured = count($response_to_file);
489 
490  foreach ($response_to_file as $i => $response) { ?>
491  <tr>
492  <td>
493  <?php
494  if ($write_access) {
495  text_box($prefix.'['.$settings['action'].'_file_response_to_save]['.$i.']', $response, 50);
496 
497  if (in_array($response, $settings[$settings['action'].'_invalid_keywords_for_file'])) {
498  // let the user know if one of the keywords entered was invalid
499  echo "<span style=\"color:#FF0000\"><b> *invalid keyword</b></span>";
500  }
501  } else {
502  echo $response;
503  }
504 
505  ?>
506  </td>
507  </tr>
508  <?php } ?>
509  <tr>
510  <td>
511  <?php
512  $count = $currently_configured++;
513  if ($write_access) text_box($prefix.'['.$settings['action'].'_file_response_to_save]['.$count.']', '', 50);
514  ?>
515  </td>
516  </tr>
517  <tr>
518  <td><?php echo translate('response_to_download_as_file_note'); ?></td>
519  </tr>
520  </table>
521  <?php
522  }
523 
524  return ob_get_clean();
525 
526  }//end getInterface()
527 
528 
540  public static function processInterface(&$settings, $request_data)
541  {
542  $settings['wsdl'] = array_get_index($request_data, 'wsdl', '');
543  $settings['login'] = array_get_index($request_data, 'login', '');
544  $settings['password'] = array_get_index($request_data, 'password', '');
545  $settings['mute_exceptions'] = array_get_index($request_data, 'mute_exceptions', FALSE);
546  $settings['local_cert'] = array_get_index($request_data['local_cert'], 'assetid','0');
547  $settings['passphrase'] = array_get_index($request_data, 'passphrase', '');
548  $settings['run_test'] = array_get_index($request_data, 'run_test', 0);
549  $settings['action'] = array_get_index($request_data, 'action', NULL);
550 
551  if(isset ($settings['wsdl']) && !empty($settings['wsdl'])) {
552 
553  $options = SOAP_Common::_getSoapOptions($settings);
554 
555  // if we are asked to use certificate but its not valid, complain !
556  if (!$options) {
557  trigger_error(taranslate('no_cert'), E_USER_WARNING);
558  return TRUE;
559  }
560 
561  try {
562  $client = new SoapClient($settings['wsdl'], $options);
563  $functions = $client->__getFunctions();
564  $types = $client->__getTypes();
565  }
566  catch (Exception $e) {
567  trigger_error($e->getMessage());
568  return TRUE;
569  }
570 
571  $function_list = Array();
572  if(!empty($functions)) {
573  foreach ($functions as $function) {
574  $function_name = preg_replace('/^\w+ (\w+)\(.*\)/', '$1', $function);
575  $function_params = explode(', ', preg_replace('/^\w+ \w+\((.*)\)/', '$1', $function));
576  $function_response = preg_replace('/^(\w+) \w+\(.*\)/', '$1', $function);
577 
578  // Fixed duplicate function definition issue of PHP SoapClient
579  if(isset($function_list[$function_name])) continue;
580 
581  // parse parameters
582  $parsed_params = Array();
583  foreach ($function_params as $param) {
584  if (!trim($param)) continue;
585 
586  // Get XML representation of params of the functions
587  $xml_param = SOAP_Common::_parse_type ($param, $types);
588  $parsed_params[$param] = SOAP_Common::_formatXmlString(empty($xml_param) ? $param : $xml_param->asXML());
589  }
590  $function_list[$function_name] = $parsed_params;
591 
592  // parse response
593  $xml_response = SOAP_Common::_parse_type ($function_response, $types);
594 
595  if ($xml_response === TRUE || $xml_response === FALSE) {
596  $response_list[$function_name] = SOAP_Common::_formatXmlString('<?xml version=\"1.0\"?'.'>'."<$function_name>??</$function_name>");
597  $keywords = 'globals_session_soap'.($xml_response ? '0' : '');
598  } else {
599  $response_list[$function_name] = SOAP_Common::_formatXmlString(empty($xml_response) ? '' : $xml_response->asXML());
600  $keywords = SOAP_Common::_parse_response_keywords ($xml_response, 'globals_session_soap_');
601  }
602  // parse response keywords
603  $keywords_list[$function_name] = str_replace('[ARRAY_ELEMENT]', '0', $keywords);
604  }
605 
606  $settings['actions'] = $function_list;
607  $settings['responses'] = $response_list;
608  $settings['response_keywords'] = $keywords_list;
609  }
610  }
611 
612  $settings[$settings['action'].'_treat_as_fatal'] = array_get_index($request_data, $request_data['action'].'_treat_as_fatal',0);
613 
614  // Currently selected "Check response" fields
615  $current_selected_fields = (isset($request_data[$settings['action'].'_response_error_fields']) && $request_data[$settings['action'].'_response_error_fields'] != '')? $request_data[$settings['action'].'_response_error_fields'] : Array();
616 
617  $field_error_msgs = Array();
618 
619  // String containg all the SOAP response keywords
620  $keywords_string = '';
621  if (isset($settings['response_keywords'])) {
622  $keywords_string = implode(';',$settings['response_keywords']);
623  }
624 
625  foreach ($current_selected_fields as $index => $selected_field) {
626 
627  // If the field is deleted or dosen't exists in SOAP response anymore
628  if ((!isset($request_data[$settings['action'].'_delete_field_'.$selected_field]) || !$request_data[$settings['action'].'_delete_field_'.$selected_field]) && strpos($keywords_string, '%globals_session_soap_'.$selected_field.'%') !== FALSE) {
629  if (isset($request_data[$settings['action'].'_error_msg_'.$selected_field])) {
630  $field_error_msgs[$selected_field] = trim($request_data[$settings['action'].'_error_msg_'.$selected_field]);
631  } else {
632  $field_error_msgs[$selected_field] = '';
633  }//end else
634  }//end if
635  }//end foreach
636 
637  // Set up current params for current action
638  if(isset($settings['actions'][$settings['action']])) {
639  $settings[$settings['action'].'_response_error_fields'] = $field_error_msgs;
640  for($i = 0; $i < count($settings['actions'][$settings['action']]); $i++) {
641  if(isset($request_data[$settings['action'].'parameters'.$i])) {
642  $parameters_passed = $request_data[$settings['action'].'parameters'.$i];
643  $settings[$settings['action'].'parameters'.$i] = $request_data[$settings['action'].'parameters'.$i];
644  }
645  }
646  }
647 
648  $settings[$settings['action'].'_invalid_keywords_for_file'] = Array();
649  if (isset($settings['actions'][$settings['action']])) {
650  $save_file_response = array_get_index($request_data, $settings['action'].'_file_response_to_save', Array());
651  foreach ($save_file_response as $index => $response_kwd) {
652  // see if the keyword mentioned here is blnk or not from current response
653  // if so remove it and also let user know of any invalid keywords put in
654  if (trim($response_kwd) == '') {
655  unset($save_file_response[$index]);
656  continue;
657  }
658 
659  if (strpos($settings['response_keywords'][$settings['action']], $response_kwd) === FALSE) {
660  $settings[$settings['action'].'_invalid_keywords_for_file'][] = $response_kwd;
661  }
662  }
663  $settings[$settings['action'].'_file_response_to_save'] = $save_file_response;
664  }
665 
666  // Run test request
667  $test_results = Array();
668  if ($settings['run_test']) {
669  // run test only for test selected
670  foreach($functions as $function) {
671  $function_name = preg_replace('/^\w+ (\w+)\(.*\)/', '$1', $function);
672  if($function_name != $settings['action']) continue;
673  $test_results[$function_name] = SOAP_Common::testSoapRequest($client, $settings, $function_name);
674  }
675  }//end if
676  $settings['test_results'] = $test_results;
677 
678 
679  return FALSE;
680 
681  }//end processInterface()
682 
683 
690  public static function getKeywordReplacementsFromAsset($content, &$asset)
691  {
692  $replacements = Array();
693 
694  // replace any global keywords before we go any further
695  replace_global_keywords($content);
696 
697  $keyword_list = extract_keywords($content);
698 
699  if (empty($keyword_list)) return $content;
700 
701  foreach ($keyword_list as $keyword) {
702  $modified_keyword = parse_keyword($keyword, $modifiers);
703  $replacement[$keyword] = $asset->getKeywordReplacement($modified_keyword);
704  if (!empty($modifiers)) {
705  apply_keyword_modifiers($replacement[$keyword], $modifiers, Array('assetid' => $asset->id));
706  }
707 
708  }
709  if (!empty($replacement)) replace_keywords($content, $replacement);
710 
711  return $content;
712 
713  }//end getKeywordReplacementsFromAsset()
714 
715 
716 }//end class
717 
718 ?>