Garethp /
php-ews
| 1 | <?php |
||
| 2 | |||
| 3 | namespace garethp\ews\API; |
||
| 4 | |||
| 5 | use garethp\ews\API\Type\ExchangeImpersonation; |
||
| 6 | use SoapClient; |
||
| 7 | use SoapHeader; |
||
| 8 | use garethp\HttpPlayback\Factory; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Contains NTLMSoapClient. |
||
| 12 | */ |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Soap Client using Microsoft's NTLM Authentication. |
||
| 16 | * |
||
| 17 | * Copyright (c) 2008 Invest-In-France Agency http://www.invest-in-france.org |
||
| 18 | * |
||
| 19 | * Author : Thomas Rabaix |
||
| 20 | * |
||
| 21 | * Permission to use, copy, modify, and distribute this software for any |
||
| 22 | * purpose with or without fee is hereby granted, provided that the above |
||
| 23 | * copyright notice and this permission notice appear in all copies. |
||
| 24 | * |
||
| 25 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
||
| 26 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
||
| 27 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
||
| 28 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
||
| 29 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
||
| 30 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
||
| 31 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
||
| 32 | * |
||
| 33 | * @link http://rabaix.net/en/articles/2008/03/13/using-soap-php-with-ntlm-authentication |
||
| 34 | * @author Thomas Rabaix |
||
| 35 | * |
||
| 36 | * @package php-ews\Auth |
||
| 37 | * |
||
| 38 | * @property array __default_headers |
||
| 39 | */ |
||
| 40 | class NTLMSoapClient extends SoapClient |
||
| 41 | { |
||
| 42 | /** |
||
| 43 | * Username for authentication on the exchnage server |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $user; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Password for authentication on the exchnage server |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $password; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Whether or not to validate ssl certificates |
||
| 58 | * |
||
| 59 | * @var boolean |
||
| 60 | */ |
||
| 61 | protected $validate = false; |
||
| 62 | |||
| 63 | private $httpClient; |
||
| 64 | |||
| 65 | protected $__last_request_headers; |
||
| 66 | |||
| 67 | protected $_responseCode; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * An array of headers for us to store or use. Since not all requests use all headers (DeleteItem and SyncItems |
||
| 71 | * don't allow you to pass a Timezone for example), we need to be able to smartly decide what headers to include |
||
| 72 | * and exclude from a request. Until we have propper selection (an array of all known operations and what headers |
||
| 73 | * are allowed for example), this seems like a decent solution for storing the headers before we decide if they |
||
| 74 | * belong in the request or not) |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $ewsHeaders = array( |
||
| 79 | 'version' => null, |
||
| 80 | 'impersonation' => null, |
||
| 81 | 'timezone' => null |
||
| 82 | ); |
||
| 83 | |||
| 84 | protected $auth; |
||
| 85 | |||
| 86 | protected $callsWithoutTimezone = array( |
||
| 87 | 'DeleteItem', |
||
| 88 | 'SyncFolderItems', |
||
| 89 | 'GetServerTimeZones', |
||
| 90 | 'ConvertId', |
||
| 91 | 'MoveItem' |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @TODO: Make this smarter. It should know and search what headers to remove on what actions |
||
| 96 | * |
||
| 97 | * @param string $name |
||
| 98 | * @param string $args |
||
| 99 | * @return mixed |
||
| 100 | */ |
||
| 101 | 38 | #[\ReturnTypeWillChange] |
|
| 102 | public function __call($name, $args) |
||
| 103 | { |
||
| 104 | 38 | $this->__setSoapHeaders(null); |
|
| 105 | |||
| 106 | 38 | $headers = array( |
|
| 107 | 38 | $this->ewsHeaders['version'], |
|
| 108 | 38 | $this->ewsHeaders['impersonation'], |
|
| 109 | 38 | ); |
|
| 110 | |||
| 111 | 38 | if (!in_array($name, $this->callsWithoutTimezone)) { |
|
| 112 | 37 | $headers[] = $this->ewsHeaders['timezone']; |
|
| 113 | } |
||
| 114 | |||
| 115 | 38 | $headers = array_filter($headers, function ($header) { |
|
| 116 | 38 | return $header instanceof SoapHeader; |
|
| 117 | 38 | }); |
|
| 118 | |||
| 119 | 38 | $this->__setSoapHeaders($headers); |
|
| 120 | 38 | return parent::__call($name, $args); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $location |
||
| 125 | * @param string $wsdl |
||
| 126 | * @param array $options |
||
| 127 | */ |
||
| 128 | 46 | public function __construct($location, $auth, $wsdl, $options = array()) |
|
| 129 | { |
||
| 130 | 46 | $this->auth = $auth; |
|
| 131 | |||
| 132 | 46 | $options = array_replace_recursive([ |
|
| 133 | 46 | 'httpPlayback' => [ |
|
| 134 | 46 | 'mode' => null |
|
| 135 | 46 | ] |
|
| 136 | 46 | ], $options); |
|
| 137 | |||
| 138 | 46 | $options['location'] = $location; |
|
| 139 | |||
| 140 | // If a version was set then add it to the headers. |
||
| 141 | 46 | if (!empty($options['version'])) { |
|
| 142 | 45 | $this->ewsHeaders['version'] = new SoapHeader( |
|
| 143 | 45 | 'http://schemas.microsoft.com/exchange/services/2006/types', |
|
| 144 | 45 | 'RequestServerVersion Version="'.$options['version'].'"' |
|
| 145 | 45 | ); |
|
| 146 | } |
||
| 147 | |||
| 148 | // If impersonation was set then add it to the headers. |
||
| 149 | 46 | if (!empty($options['impersonation'])) { |
|
| 150 | 1 | $impersonation = $options['impersonation']; |
|
| 151 | 1 | if (is_string($impersonation)) { |
|
| 152 | 1 | $impersonation = ExchangeImpersonation::fromEmailAddress($options['impersonation']); |
|
| 153 | } |
||
| 154 | |||
| 155 | 1 | $this->ewsHeaders['impersonation'] = new SoapHeader( |
|
| 156 | 1 | 'http://schemas.microsoft.com/exchange/services/2006/types', |
|
| 157 | 1 | 'ExchangeImpersonation', |
|
| 158 | 1 | $impersonation->toXmlObject() |
|
| 159 | 1 | ); |
|
| 160 | } |
||
| 161 | |||
| 162 | 46 | if (!empty($options['timezone'])) { |
|
| 163 | $this->ewsHeaders['timezone'] = new SoapHeader( |
||
| 164 | 'http://schemas.microsoft.com/exchange/services/2006/types', |
||
| 165 | 'TimeZoneContext', |
||
| 166 | array( |
||
| 167 | 'TimeZoneDefinition' => array( |
||
| 168 | 'Id' => $options['timezone'], |
||
| 169 | 'Periods' => [], |
||
| 170 | ) |
||
| 171 | ) |
||
| 172 | ); |
||
| 173 | } |
||
| 174 | |||
| 175 | 46 | $this->httpClient = Factory::getInstance($options['httpPlayback']); |
|
| 176 | |||
| 177 | 46 | parent::__construct($wsdl, $options); |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Performs a SOAP request |
||
| 182 | * |
||
| 183 | * @link http://php.net/manual/en/function.soap-soapclient-dorequest.php |
||
| 184 | * |
||
| 185 | * @param string $request the xml soap request |
||
| 186 | * @param string $location the url to request |
||
| 187 | * @param string $action the soap action. |
||
| 188 | * @param integer $version the soap version |
||
| 189 | * @param integer $one_way |
||
| 190 | * @return string the xml soap response. |
||
| 191 | */ |
||
| 192 | 38 | #[\ReturnTypeWillChange] |
|
| 193 | public function __doRequest($request, $location, $action, $version, $one_way = 0) |
||
| 194 | { |
||
| 195 | 38 | $postOptions = array( |
|
| 196 | 38 | 'body' => $request, |
|
| 197 | 38 | 'headers' => array( |
|
| 198 | 38 | 'Connection' => 'Keep-Alive', |
|
| 199 | 38 | 'User-Agent' => 'PHP-SOAP-CURL', |
|
| 200 | 38 | 'Content-Type' => 'text/xml; charset=utf-8', |
|
| 201 | 38 | 'SOAPAction' => $action |
|
| 202 | 38 | ), |
|
| 203 | 38 | 'verify' => $this->validate, |
|
| 204 | 38 | 'http_errors' => false |
|
| 205 | 38 | ); |
|
| 206 | |||
| 207 | 38 | $postOptions = array_replace_recursive($postOptions, $this->auth); |
|
| 208 | |||
| 209 | 38 | $response = $this->httpClient->post($location, $postOptions); |
|
| 210 | |||
| 211 | 38 | $this->__last_request_headers = $postOptions['headers']; |
|
| 212 | 38 | $this->_responseCode = $response->getStatusCode(); |
|
| 213 | |||
| 214 | 38 | $responseStr = $response->getBody()->__toString(); |
|
| 215 | 38 | if ($this->_responseCode < 200 || $this->_responseCode >= 300) { |
|
| 216 | return $responseStr; |
||
| 217 | } |
||
| 218 | |||
| 219 | 38 | libxml_use_internal_errors(true); |
|
| 220 | 38 | $dom = new \DOMDocument("1.0", "UTF-8"); |
|
| 221 | 38 | $dom->strictErrorChecking = false; |
|
| 222 | 38 | $dom->validateOnParse = false; |
|
| 223 | 38 | $dom->recover = true; |
|
| 224 | 38 | $dom->loadXML($responseStr); |
|
| 225 | 38 | $xml = simplexml_import_dom($dom); |
|
| 226 | 38 | libxml_clear_errors(); |
|
| 227 | 38 | libxml_use_internal_errors(false); |
|
| 228 | |||
| 229 | 38 | return $xml->asXML(); |
|
|
0 ignored issues
–
show
|
|||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Returns last SOAP request headers |
||
| 234 | * |
||
| 235 | * @link http://php.net/manual/en/function.soap-soapclient-getlastrequestheaders.php |
||
| 236 | * |
||
| 237 | * @return string the last soap request headers |
||
| 238 | */ |
||
| 239 | #[\ReturnTypeWillChange] |
||
| 240 | public function __getLastRequestHeaders() |
||
| 241 | { |
||
| 242 | return implode('n', $this->__last_request_headers)."\n"; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Set validation certificate |
||
| 247 | * |
||
| 248 | * @param bool $validate |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 1 | public function validateCertificate($validate = true) |
|
| 252 | { |
||
| 253 | 1 | $this->validate = $validate; |
|
| 254 | |||
| 255 | 1 | return $this; |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns the response code from the last request |
||
| 260 | * |
||
| 261 | * @return integer |
||
| 262 | */ |
||
| 263 | 38 | public function getResponseCode() |
|
| 264 | { |
||
| 265 | 38 | return $this->_responseCode; |
|
| 266 | } |
||
| 267 | } |
||
| 268 |