SoapClient.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\service;
12 
14 
15 /**
16  * SoapClient is used to communicate with wCMF soap services.
17  *
18  * @author ingo herwig <ingo@wemove.com>
19  */
20 class SoapClient extends \SoapClient {
21 
22  const OASIS = "http://docs.oasis-open.org/wss/2004/01";
23 
24  private $_user;
25  private $_password;
26 
27  private static $_logger = null;
28 
29  /**
30  * Constructor
31  * @param $wsdl
32  * @param $user
33  * @param $password
34  * @param $options
35  */
36  public function __construct($wsdl, $user, $password, $options) {
37  parent::__construct($wsdl, $options);
38  if (self::$_logger == null) {
39  self::$_logger = LogManager::getLogger(__CLASS__);
40  }
41  $this->_user = $user;
42  $this->_password = $password;
43  }
44 
45  /**
46  * Call the given soap method
47  * @param $method
48  * @param $params (optional, default: empty array)
49  */
50  public function call($method, $params=array()) {
51  $header = $this->generateWSSecurityHeader($this->_user, $this->_password);
52  $response = $this->__soapCall($method, sizeof($params) > 0 ? array($params) : array(), null, $header);
53  // in document/literal style the "return" parameter holds the result
54  return property_exists($response, 'return') ? $response->return : $response;
55  }
56 
57  /**
58  * Overridden in order to strip bom characters
59  * @see SoapClient::__doRequest
60  */
61  public function __doRequest($request, $location, $action, $version, $one_way=0){
62  if (self::$_logger->isDebugEnabled()) {
63  self::$_logger->debug("Request:");
64  self::$_logger->debug($request);
65  }
66  $response = trim(parent::__doRequest($request, $location, $action, $version, $one_way));
67  if (self::$_logger->isDebugEnabled()) {
68  self::$_logger->debug("Response:");
69  self::$_logger->debug($response);
70  self::$_logger->debug($this->getDebugInfos());
71  }
72  $parsedResponse = preg_replace('/^(\x00\x00\xFE\xFF|\xFF\xFE\x00\x00|\xFE\xFF|\xFF\xFE|\xEF\xBB\xBF)/', "", $response);
73  // fix missing last e> caused by php's built-in webserver
74  if (preg_match('/^<\?xml/', $parsedResponse) && !preg_match('/e>$/', $parsedResponse)) {
75  $parsedResponse .= 'e>';
76  }
77  return $parsedResponse;
78  }
79 
80  /**
81  * Create the WS-Security authentication header for the given credentials
82  * @param $user
83  * @param $password
84  * @return SoapHeader
85  */
86  private function generateWSSecurityHeader($user, $password) {
87  $nonce = sha1(mt_rand());
88  $xml = '<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="'.self::OASIS.'/oasis-200401-wss-wssecurity-secext-1.0.xsd">
89  <wsse:UsernameToken>
90  <wsse:Username>'.$user.'</wsse:Username>
91  <wsse:Password Type="'.self::OASIS.'/oasis-200401-wss-username-token-profile-1.0#PasswordText">'.$password.'</wsse:Password>
92  <wsse:Nonce EncodingType="'.self::OASIS.'/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'.$nonce.'</wsse:Nonce>
93  </wsse:UsernameToken>
94  </wsse:Security>';
95  return new \SoapHeader(self::OASIS.'/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', new \SoapVar($xml, XSD_ANYXML), true);
96  }
97 
98  /**
99  * Get informations about the last request. Available
100  * if constructor options contain 'trace' => 1
101  * @return String
102  */
103  public function getDebugInfos() {
104  $requestHeaders = $this->__getLastRequestHeaders();
105  $request = $this->__getLastRequest();
106  $responseHeaders = $this->__getLastResponseHeaders();
107  $response = $this->__getLastResponse();
108 
109  $msg = '';
110  $msg .= "Request Headers:\n" . $requestHeaders . "\n";
111  $msg .= "Request:\n" . $request . "\n";
112 
113  $msg .= "Response Headers:\n" . $responseHeaders . "\n";
114  $msg .= "Response:\n" . $response . "\n";
115  return $msg;
116  }
117 }
118 ?>
getDebugInfos()
Get informations about the last request.
Definition: SoapClient.php:103
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:35
Service related interfaces and classes.
Definition: namespaces.php:92
call($method, $params=array())
Call the given soap method.
Definition: SoapClient.php:50
SoapClient is used to communicate with wCMF soap services.
Definition: SoapClient.php:20
__doRequest($request, $location, $action, $version, $one_way=0)
Overridden in order to strip bom characters.
Definition: SoapClient.php:61
__construct($wsdl, $user, $password, $options)
Constructor.
Definition: SoapClient.php:36