Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_action_soap_call.inc
1 <?php
18 require_once SQ_PACKAGES_PATH.'/cms/form/form_action/form_action.inc';
19 require_once SQ_FUDGE_PATH.'/general/text.inc';
20 require_once dirname(dirname(dirname(__FILE__))).'/lib/soap_common.inc';
21 require_once dirname(dirname(dirname(__FILE__))).'/lib/soap_form_based_common.inc';
22 
23 
38 {
39 
44  function __construct()
45  {
46  parent::__construct();
47 
48  }//end constructor
49 
50 
56  public static function paintInlineInterface(Form $form, $settings, Backend_Outputter $o, $prefix)
57  {
58  ?>
59 
60  <p class="sq-backend-section-subheading">Connection Details</p>
61 
62  <div id="<?php echo $prefix ?>_connection_div" style="padding: 0.5em 0px 2em 30px;">
63  <table class="sq-backend-table" >
64  <tr>
65  <td width="120"><p>WSDL</p></td>
66  <td><p><?php text_box($prefix.'_wsdl', array_get_index($settings, 'wsdl', ''), 120); ?></p></td>
67  </tr>
68  <tr>
69  <td><?php echo translate('login'); ?></td>
70  <td><p><?php text_box($prefix.'_login', array_get_index($settings, 'login', ''), 35); ?><br />
71  </p></td>
72  </tr>
73  <tr>
74  <td><?php echo translate('password'); ?></td>
75  <td><p><?php password_box($prefix.'_password', array_get_index($settings, 'password', ''), 35); ?></p></td>
76  </tr>
77  <tr>
78  <td colspan="2"><?php echo translate('use_http_authentication'); ?></td>
79  </tr>
80  <tr>
81  <td><?php echo translate('mute_exceptions'); ?></td>
82  <td><p><?php check_box($prefix.'_mute_exceptions', 1, array_get_index($settings, 'mute_exceptions', 0)); ?></p></td>
83  </tr>
84  </table>
85  </div>
86 
87  <div id="<?php echo $prefix ?>_https_div" style="padding: 0.5em 0px 2em 30px;">
88  <table class="sq-backend-table" >
89  <tr>
90  <td><p><?php echo translate('local_cert'); ?></p></td>
91  <td><p><?php asset_finder($prefix.'_local_cert', array_get_index($settings, 'local_cert', ''), Array('file'=>'D')); ?><br/>
92  </tr>
93  <tr>
94  <td><p><?php echo translate('passphrase'); ?></p></td>
95  <td><p><?php password_box($prefix.'_passphrase', array_get_index($settings, 'passphrase', ''), 35); ?></p></td>
96  </tr>
97  <tr>
98  <td colspan="2"><?php echo translate('use_https'); ?></td>
99  </tr>
100  </table>
101  </div>
102 
103  <p class="sq-backend-section-subheading">Test Response</p>
104  <div id="<?php echo $prefix ?>_test_request_response" style="padding: 0.5em 0px 2em 30px;">
105  <table class="sq-backend-table" >
106  <tr>
107  <td width="120">Run Test</td>
108  <td><p>
109  <?php
110  $run_test = array_get_index($settings, 'run_test', 0);
111  check_box($prefix.'_run_test', 1, $run_test);
112  ?>
113  </p></td>
114  </tr>
115  <tr>
116  <td colspan="2">The test will be based on the request content in the "SOAP Request Message" below</td>
117  </tr>
118  </table>
119  </div>
120 
121  <div width="120">
122  <?php
123  if ($run_test && !empty($settings['test_results'])) {
124  foreach($settings['test_results'] as $soap_func => $info) {
125  echo "<b><u>$soap_func</u></b><br /><br />";
126  echo "<b>Request:</b><br />";
127  echo (nl2br(str_replace(' ', '&nbsp;&nbsp;', htmlentities($info['request']))));
128  echo "<br />";
129  echo "<b>Response:</b><br />";
130  echo (nl2br(str_replace(' ', '&nbsp;&nbsp;', htmlentities($info['response']))));
131  echo "<br />";
132 
133  }
134  echo "<br />";
135  }
136  ?>
137  </div>
138 
139  <?php if(!isset($settings['actions']) || empty($settings['actions'])) return;?>
140 
141  <p class="sq-backend-section-subheading"><?php echo translate('request_message'); ?></p>
142 
143  <div id="<?php echo $prefix ?>_action_selection" style="padding: 0.5em 0px 2em 30px;">
144  <table class="sq-backend-table" >
145  <tr>
146  <td><strong><?php echo translate('action');?></strong><br /><br /></td>
147  <td>
148  <?php
149  foreach ($settings['actions'] as $name => $detail) {
150  $action_list[$name] = $name;
151  }
152  combo_box($prefix.'_action', $action_list, FALSE, array_get_index($settings, 'action', ''));
153  ?>
154  <br /><br />
155  </td>
156  </tr>
157  <tr>
158  <td><span style="padding-top:10px;"><strong><?php echo translate('params');?></strong></span></td>
159  <td>
160  <?php
161  $i = 0;
162  if(isset($settings['actions'][$settings['action']])) {
163  foreach ($settings['actions'][$settings['action']] as $parameter_xml) {
164  text_area($prefix.'_'.$settings['action'].'parameters'.$i, array_get_index($settings,$settings['action'].'parameters'.$i, $parameter_xml), 80, 15);
165  echo '<br/>';
166  $i++;
167  }
168  }
169  ?>
170  <br />
171  </td>
172  </tr>
173  <tr>
174  <td colspan="2"><?php echo translate('question_keyword_example'); ?></td>
175  </tr>
176  </table>
177  </div>
178 
179  <p class="sq-backend-section-subheading"><?php echo translate('response_message'); ?></p>
180 
181  <div id="<?php echo $prefix ?>_response" style="padding: 0.5em 0px 2em 30px;">
182  <table class="sq-backend-table" >
183  <tr>
184  <td>
185  <?php
186  $i = 0;
187  if(isset($settings['responses'][$settings['action']])) {
188  // echo the response xml, with style
189  echo (nl2br(str_replace(' ', '&nbsp;&nbsp;', htmlentities(str_replace('[ARRAY_ELEMENT]', '', $settings['responses'][$settings['action']])))));
190  }
191  ?>
192  <br />
193  </td>
194  </tr>
195  <tr>
196  <td>
197  <?php echo translate('response_message_note'); ?>
198  </td>
199  </tr>
200  <tr>
201  </tr>
202  </table>
203  </div>
204 
205  <p class="sq-backend-section-subheading">Check Response</p>
206 
207  <div id="<?php echo $prefix ?>_response_error" style="padding: 0.5em 0px 2em 30px;">
208  <table class="sq-backend-table" >
209  <tr width="120">
210  <td>
211  <?php
212  $selected_fields = isset($settings[$settings['action'].'_response_error_fields']) ? $settings[$settings['action'].'_response_error_fields'] : Array();
213  if (empty($selected_fields)) {
214  echo "No SOAP response fields selected";
215  } else {
216  ?>
217  <table class="sq-backend-table">
218  <tr>
219  <td class="sq-backend-table-header"><?php echo translate('field_name'); ?></td>
220  <td class="sq-backend-table-header"><?php echo translate('error_text'); ?></td>
221  <td class="sq-backend-table-header"><?php echo translate('delete_question'); ?></td>
222  </tr>
223  <?php
225  foreach ($selected_fields as $selected_field => $rule) {
226  $error_msg = array_get_index($rule, 'error_msg', '');
227  $rule_value = array_get_index($rule, 'value', '');
228  $rule_type = array_get_index($rule, 'type', 'must_contain');;
229 
230  $sf_prefix = $prefix.'_'.$settings['action'].'_'.$selected_field;
231  ?>
232  <tr>
233  <td class="sq-backend-table-cell"><?php echo $selected_field; ?></td>
234  <td class="sq-backend-table-cell">
235  <table>
236  <tr>
237  <td>Rule</td>
238  <td><?php
239  combo_box($sf_prefix.'_type', $rules, FALSE, $rule_type);
240  ?>
241  </td>
242  </tr>
243  <tr>
244  <td>Value</td><td><?php text_box($sf_prefix.'_value', $rule_value, 40); ?></td>
245  </tr>
246  <tr>
247  <td>Error Message</td><td><?php text_box($sf_prefix.'_error_msg', $error_msg, 110); ?></td>
248  </tr>
249  </table>
250  </td>
251 
252  <td class="sq-backend-table-cell">
253  <?php check_box($sf_prefix.'_delete_field['.$selected_field.']'); ?>
254  </td>
255  </tr>
256  <?php
257  }//end foreach
258  ?>
259  </table>
260  <?php
261  }//end else
262  ?>
263  <br />
264  <?php
265  echo translate('add_new_field');
266  $response_fields = Array('' => '-- Select Field --');
267  if (isset($settings['response_keywords'][$settings['action']])) {
268  $response_keywords = explode('<br/>', $settings['response_keywords'][$settings['action']]);
269  foreach($response_keywords as $val) {
270  $val = trim($val, '%');
271  $val = substr_replace($val, '', 0, strlen('globals_session_soap_'));
272 
273  if ($val) {
274  $response_fields[$val] = $val;
275  }
276  }//end foreach
277  }
278  combo_box($prefix.'_'.$settings['action'].'_response_error_field', $response_fields, FALSE);
279  ?>
280  </td>
281  </tr>
282 
283  <tr>
284  <td><?php translate('fail_validation_error_msg'); ?></td>
285  </tr>
286  </table>
287  </div>
288 
289  <p class="sq-backend-section-subheading"><?php echo translate('response_keywords'); ?></p>
290 
291  <div id="<?php echo $prefix ?>_response_keywords" style="padding: 0.5em 0px 2em 30px;">
292  <table class="sq-backend-table" >
293  <tr>
294  <td>
295  <?php
296  if(isset($settings['response_keywords'][$settings['action']])) {
297  // echo the response xml, with style
298  echo ($settings['response_keywords'][$settings['action']]);
299  }
300  ?>
301  <br />
302  </td>
303  </tr>
304  <tr>
305  <td><?php echo translate('response_keyword_note'); ?></td>
306  </tr>
307  </table>
308  <p class="sq-backend-section-subheading"><?php echo translate('response_to_download_as_file'); ?></p>
309  <table class="sq-backend-table">
310  <tr>
311  <td class="sq-backend-table-header"><?php echo translate('field_keyword_tags'); ?></td>
312  </tr>
313  <?php
314  $response_to_file = array_get_index($settings, $settings['action'].'_file_response_to_save', Array());
315  $currently_configured = count($response_to_file);
316 
317  foreach ($response_to_file as $i => $response) { ?>
318  <tr>
319  <td>
320  <?php
321  text_box($prefix.'_'.$settings['action'].'_file_response_to_save['.$i.']', $response, 50);
322 
323  if (in_array($response, $settings[$settings['action'].'_invalid_keywords_for_file'])) {
324  // let the user know if one of the keywords entered was invalid
325  echo "<span style=\"color:#FF0000\"><b> *invalid keyword</b></span>";
326  }
327  ?>
328  </td>
329  </tr>
330  <?php } ?>
331  <tr>
332  <td>
333  <?php
334  $count = $currently_configured++;
335  text_box($prefix.'_'.$settings['action'].'_file_response_to_save['.$count.']', '', 50);
336  ?>
337  </td>
338  </tr>
339  <tr>
340  <td><?php echo translate('response_to_download_as_file_note'); ?></td>
341  </tr>
342  </table>
343  </div>
344 
345  <?php
346 
347  }//end paintInlineInterface()
348 
349 
356  public static function processInlineInterface(Form $form, &$settings, Backend_Outputter $o, $prefix)
357  {
358  $settings['local_cert'] = isset($_POST[$prefix.'_local_cert']['assetid']) ? $_POST[$prefix.'_local_cert']['assetid']: NULL;
359  $settings['wsdl'] = array_get_index($_POST, $prefix.'_wsdl', NULL);
360  $settings['login'] = array_get_index($_POST, $prefix.'_login', NULL);
361  $settings['password'] = array_get_index($_POST, $prefix.'_password', NULL);
362  $settings['mute_exceptions'] = array_get_index($_POST, $prefix.'_mute_exceptions', NULL);
363  $settings['run_test'] = array_get_index($_POST, $prefix.'_run_test', NULL);
364  $settings['passphrase'] = array_get_index($_POST, $prefix.'_passphrase', NULL);
365  $settings['action'] = array_get_index($_POST, $prefix.'_action', NULL);
366 
367 
368  // This submission needs to be executed before form get submitted
369  $settings['before_submit'] = TRUE;
370 
371  if(isset ($settings['wsdl']) && !empty($settings['wsdl'])) {
372 
373  $options = SOAP_Common::_getSoapOptions($settings, $form);
374  try {
375  $client = new SoapClient($settings['wsdl'], $options);
376  $functions = $client->__getFunctions();
377  $types = $client->__getTypes();
378  }
379  catch (Exception $e) {
380  trigger_error($e->getMessage());
381  return TRUE;
382  }
383  if(!empty($functions)) {
384  foreach ($functions as $function) {
385  $function_name = preg_replace('/^\w+ (\w+)\(.*\)/', '$1', $function);
386  $function_params = explode(', ', preg_replace('/^\w+ \w+\((.*)\)/', '$1', $function));
387  $function_response = preg_replace('/^(\w+) \w+\(.*\)/', '$1', $function);
388 
389  // Fixed duplicate function definition issue of PHP SoapClient
390  if(isset($function_list[$function_name])) continue;
391 
392  // parse parameters
393  $parsed_params = Array();
394  foreach ($function_params as $param) {
395  if (!trim($param)) continue;
396 
397  // Get XML representation of params of the functions
398  $xml_param = SOAP_Common::_parse_type ($param, $types);
399  $parsed_params[$param] = SOAP_Common::_formatXmlString(empty($xml_param) ? $param : $xml_param->asXML());
400  }
401  $function_list[$function_name] = $parsed_params;
402 
403  // parse response
404  $xml_response = SOAP_Common::_parse_type ($function_response, $types);
405  if ($xml_response === TRUE || $xml_response === FALSE) {
406  $response_list[$function_name] = SOAP_Common::_formatXmlString('<?xml version=\"1.0\"?'.'>'."<$function_name>??</$function_name>");
407  $keywords = 'globals_session_soap'.($xml_response ? '0' : '');
408  } else {
409  $response_list[$function_name] = SOAP_Common::_formatXmlString(empty($xml_response) ? '' : $xml_response->asXML());
410  $keywords = SOAP_Common::_parse_response_keywords ($xml_response, 'globals_session_soap_');
411  }
412  // parse response keywords
413  $keywords_list[$function_name] = str_replace('[ARRAY_ELEMENT]', '0', $keywords);
414  }
415  $settings['actions'] = $function_list;
416  $settings['responses'] = $response_list;
417  $settings['response_keywords'] = $keywords_list;
418  }
419  }
420 
421  // Currently selected "Check response" fields
422  $current_selected_fields = isset($settings[$settings['action'].'_response_error_fields']) ? $settings[$settings['action'].'_response_error_fields'] : Array();
423  $field_rule = Array();
424 
425  // String containg all the SOAP response keywords
426  $keywords_string = '';
427  if (isset($settings['response_keywords'])) {
428  $keywords_string = implode(';',$settings['response_keywords']);
429  }
430 
431  foreach ($current_selected_fields as $selected_field => $error_msg) {
432  $sf_prefix = $prefix.'_'.$settings['action'].'_'.$selected_field;
433 
434  // If the field is deleted or dosen't exists in SOAP response anymore
435  if ((!isset($_POST[$sf_prefix.'_delete_field'][$selected_field]) || !$_POST[$sf_prefix.'_delete_field'][$selected_field]) && strpos($keywords_string, '%globals_session_soap_'.$selected_field.'%') !== FALSE) {
436  if (isset($_POST[$sf_prefix.'_error_msg'])) {
437  $field_rule[$selected_field] = Array(
438  'type' => $_POST[$sf_prefix.'_type'],
439  'value' => $_POST[$sf_prefix.'_value'],
440  'error_msg' => trim($_POST[$sf_prefix.'_error_msg']),
441  );
442  } else {
443  $field_rule[$selected_field] = $rule;
444  }//end else
445  }//end if
446  }//end foreach
447 
448  // Add new "Check response" field
449  if (isset($_POST[$prefix.'_'.$settings['action'].'_response_error_field']) && !empty($_POST[$prefix.'_'.$settings['action'].'_response_error_field'])) {
450  $new_field = $_POST[$prefix.'_'.$settings['action'].'_response_error_field'];
451  $field_rule[$new_field] = Array();
452  }
453 
454  // Set up current params for current action
455  if(isset($settings['actions'][$settings['action']])) {
456  $settings[$settings['action'].'_response_error_fields'] = $field_rule;
457  for($i = 0; $i < count($settings['actions'][$settings['action']]); $i++) {
458  if(isset($_POST[$prefix.'_'.$settings['action'].'parameters'.$i]))
459  $settings[$settings['action'].'parameters'.$i] = $_POST[$prefix.'_'.$settings['action'].'parameters'.$i];
460  }
461  }
462 
463  $settings[$settings['action'].'_invalid_keywords_for_file'] = Array();
464  if (isset($settings['actions'][$settings['action']])) {
465  $save_file_response = array_get_index($_POST, $prefix.'_'.$settings['action'].'_file_response_to_save', Array());
466  foreach ($save_file_response as $index => $response_kwd) {
467  // see if the keyword mentioned here is blnk or not from current response
468  // if so remove it and also let user know of any invalid keywords put in
469  if (trim($response_kwd) == '') {
470  unset($save_file_response[$index]);
471  continue;
472  }
473 
474  if (strpos($settings['response_keywords'][$settings['action']], $response_kwd) === FALSE) {
475  $settings[$settings['action'].'_invalid_keywords_for_file'][] = $response_kwd;
476  }
477  }
478  $settings[$settings['action'].'_file_response_to_save'] = $save_file_response;
479  }
480 
481  // Run test request
482  $test_results = Array();
483  if ($settings['run_test']) {
484  foreach($functions as $function) {
485  $function_name = preg_replace('/^\w+ (\w+)\(.*\)/', '$1', $function);
486  if($function_name != $settings['action']) continue;
487  $test_results[$function_name] = SOAP_Common::testSoapRequest($client, $settings, $function_name, $form);
488  }
489  }//end if
490  $settings['test_results'] = $test_results;
491 
492  return TRUE;
493 
494  }//end processInlineInterface()
495 
496 
503  public static function paintSummary(Form $form, $settings, Backend_Outputter $o, $prefix)
504  {
505 
506  ?><table class="no-borders">
507  <colgroup>
508  <col width="80" />
509  <col/>
510  </colgroup>
511  <tbody>
512  <tr>
513  <td class="sq-backend-table-cell" style="vertical-align: top"><p><strong><?php echo translate('action');?></strong></p></td>
514  <td class="sq-backend-table-cell" style="vertical-align: top"><p><?php
515  if (isset ($settings['action'])) {
516  echo $settings['action'];
517  } else {
518  ?><span class="sq-backend-warning"><?php echo translate('no_soap_action');?></span><?php
519  }
520 
521  if (!isset ($settings[$settings['action'].'parameters0'])) {
522  ?><br/><span class="sq-backend-warning"><?php echo translate('no_parameter');?></span><?php
523  }
524  ?></p></td>
525  </tr>
526  </tbody>
527  </table>
528  <?php
529 
530  }//end paintSummary()
531 
532 
542  public static function execute(Form $form, $settings)
543  {
544  $success = TRUE;
545 
546  if(!isset($form->submission_errors)) {
547  $form->submission_errors = Array();
548  }
549 
550  if(!isset($settings['actions'][$settings['action']])) {
551  $form->submission_errors = array_merge($form->submission_errors, Array(translate('no_soap_action')));
552  return FALSE;
553  }
554  if(!isset($settings['wsdl'])) {
555  $form->submission_errors = array_merge($form->submission_errors, Array(translate('no_wsdl')));
556  return FALSE;
557  }
558 
559  $function = $settings['action'];
560  $wsdl = $settings['wsdl'];
561  $options = SOAP_Common::_getSoapOptions($settings, $form);
562 
563  // Work out the parameters of that soap function
564  for($i = 0, $param = Array(); $i < count($settings['actions'][$settings['action']]); $i++) {
565  if(isset($settings[$settings['action'].'parameters'.$i])) {
566 
567  $soap_message = SOAP_Form_Based_Common::getKeywordReplacementsFromForm($settings[$settings['action'].'parameters'.$i], $form);
568  $object = @simplexml_load_string($soap_message, 'SimpleXMLElement', LIBXML_NOCDATA);
569 
570  if($object === FALSE) {
571  // Just string argument, replace file content keywords
572  $object = SOAP_Form_Based_Common::getFileContentReplacementsFromForm($soap_message, $form);
573  } else {
574  // Convert XML to array, replace file content keywords, xml is not friendly for binary data
575  $object = SOAP_Form_Based_Common::toArray($object, $form);
576  }
577 
578  $param[] = $object;
579  }
580  }
581 
582  // Get an array of parameters
583  $eval_string = '';
584  foreach ($param as $index => $object) {
585  $eval_string .= $index == 0 ? '' : ', ';
586  $eval_string .= '$param['.$index.']';
587  }
588 
589  // Enable backtrace option
590  $options['trace'] = 1;
591 
592  // If SOAP exceptions are to be silenced
593  if ($settings['mute_exceptions']) {
594  $options['exceptions'] = 0;
595  }
596 
597  // Make SOAP call
598  try {
599  $client = new SoapClient($wsdl, $options);
600  eval('$data = $client->$function('.$eval_string.');');
601  }
602  catch (Exception $e) {
603  // SOAP error should be treated as normal form error
604  $form->submission_errors = array_merge($form->submission_errors, Array($e->getMessage()));
605  return FALSE;
606  }
607 
608  if ($settings['mute_exceptions'] && get_class($data) == 'SoapFault') {
609  $soap_response = $client->__getLastResponse();
610  $data = NULL;
611  // Create the SOAP call return object from last SOAP request
612  if (preg_match('|<([^:]+:)?Body>(.*)</([^:]+:)?Body>|msi', $soap_response, $match)) {
613  $data = simplexml_load_string($match[2]);
614  }
615  }
616 
617  // Store results in session
618  $soap_response_keywords = Array();
619  $file_saving_keywords = array_get_index($settings, $settings['action'].'_file_response_to_save', Array());
620  SOAP_Common::_store_results($data, 'soap_', $soap_response_keywords, $file_saving_keywords);
621 
622  // Remove prefix from response keywords
623  foreach($soap_response_keywords as $key => $val) {
624  $soap_response_keywords[substr_replace($key, '', 0, 5)] = $val;
625  unset($soap_response_keywords[$key]);
626  }
627  // If the SOAP response has one of the non-empty "Check Response" fields, return error
628  if (!empty($settings[$settings['action'].'_response_error_fields'])) {
629  foreach($settings[$settings['action'].'_response_error_fields'] as $check_field => $rule) {
630  if (!isset($soap_response_keywords[$check_field]) || !SOAP_Form_Based_Common::validateValue($soap_response_keywords[$check_field], $rule)) {
631  $form->submission_errors = array_merge($form->submission_errors, Array($rule['error_msg']));
632  $success = FALSE;
633  }
634  }//end foreach
635  }//end if
636 
637  return $success;
638 
639  }//end execute()
640 
641 
648  public static function isValid(Form $form, $settings)
649  {
650  // Must have a wsdl
651  if (empty($settings['wsdl'])) {
652  return FALSE;
653  }
654 
655  // The SOAP service must have actions
656  if (empty($settings['actions'])) {
657  return FALSE;
658  }//end if
659 
660  // The action we try to use must be specified
661  if (!isset($settings['action'])) {
662  return FALSE;
663  }//end if
664 
665  // Expect at least one parameter
666  if (!isset ($settings[$settings['action'].'parameters0'])) {
667  return FALSE;
668  }//end if
669 
670  return TRUE;
671 
672  }//end isValid()
673 
674 
675 }//end class
676 
677 ?>