SoapClient.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 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=[]) {
51  $header = $this->generateWSSecurityHeader($this->user, $this->password);
52  $response = $this->__soapCall($method, sizeof($params) > 0 ? [$params] : [], 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, $oneway=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, $oneway));
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  * NOTE The password is sent as clear text and therefore this message should be sent over a confidential channel.
83  * @param $user
84  * @param $password
85  * @return SoapHeader
86  */
87  private function generateWSSecurityHeader($user, $password) {
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:UsernameToken>
93  </wsse:Security>';
94  return new \SoapHeader(self::OASIS.'/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', new \SoapVar($xml, XSD_ANYXML), true);
95  }
96 
97  /**
98  * Get informations about the last request. Available
99  * if constructor options contain 'trace' => 1
100  * @return String
101  */
102  public function getDebugInfos() {
103  $requestHeaders = $this->__getLastRequestHeaders();
104  $request = $this->__getLastRequest();
105  $responseHeaders = $this->__getLastResponseHeaders();
106  $response = $this->__getLastResponse();
107 
108  $msg = '';
109  $msg .= "Request Headers:\n" . $requestHeaders . "\n";
110  $msg .= "Request:\n" . $request . "\n";
111 
112  $msg .= "Response Headers:\n" . $responseHeaders . "\n";
113  $msg .= "Response:\n" . $response . "\n";
114  return $msg;
115  }
116 }
117 ?>
__construct($wsdl, $user, $password, $options)
Constructor.
Definition: SoapClient.php:36
getDebugInfos()
Get informations about the last request.
Definition: SoapClient.php:102
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:37
Service related interfaces and classes.
Definition: namespaces.php:92
call($method, $params=[])
Call the given soap method.
Definition: SoapClient.php:50
__doRequest($request, $location, $action, $version, $oneway=0)
Overridden in order to strip bom characters.
Definition: SoapClient.php:61
LogManager is used to retrieve Logger instances.
Definition: LogManager.php:20
SoapClient is used to communicate with wCMF soap services.
Definition: SoapClient.php:20