forked from jamesiarmes/php-ews
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OAuthSoapClient.php
354 lines (304 loc) · 11.6 KB
/
OAuthSoapClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
* Contains OAuthSoapClient.
*/
/**
* Soap Client using Microsoft's OAuth Authentication.
*
* Adapted from the NTLMSoapClient to use OAuth authentication provided by:
*
* Copyright (c) 2008 Invest-In-France Agency http://www.invest-in-france.org
*
* Author : Thomas Rabaix
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* @link http://rabaix.net/en/articles/2008/03/13/using-soap-php-with-ntlm-authentication
* @author Thomas Rabaix
*
* @package php-ews\Auth
*/
class OAuthSoapClient extends SoapClient
{
/** @var String location for file on disk where downloaded content will be stored. */
public static $last_path;
/** @var String the last 5 characters sliced off to prepend onto the next curl frame. */
protected $last_segment = null;
/**
* The file handler (if one exists) to use for writing curl response to disk
*/
protected $file_handler;
/**
* @var bool Write to file flag
*/
protected $write_to_file;
/**
* @var resource cURL resource used to make the SOAP request
*/
protected $ch;
/**
* @var boolean Whether or not to validate ssl certificates
*/
protected $validate = false;
/**
* @var string User agent string to attach to the requests
*/
protected $user_agent = 'github-php-ews-generic';
/**
* @var bool Whether or not to get response headers
*/
protected $enable_response_headers = false;
/**
* @var array The last response headers that were got
*/
protected $__last_request_headers = [];
/**
* @var array If $enable_response_headers true, the last response headers that were got
*/
protected $__last_response_headers = [];
/**
* @var int The timeout in seconds waiting for a connection, or -1 to not set a connection timeout
*/
protected $connection_timeout = -1;
/**
* @var int The timeout in seconds waiting for a response
*/
protected $response_timeout = 300;
/**
* Performs a SOAP request
*
* @link http://php.net/manual/en/function.soap-soapclient-dorequest.php
*
* @param string $request the xml soap request
* @param string $location the url to request
* @param string $action the soap action.
* @param integer $version the soap version
* @param integer $one_way
* @return string the xml soap response.
* @throws \EWS_Exception
*/
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
self::$last_path = null;
$this->last_segment = null;
$headers = array(
'Method: POST',
'Connection: Keep-Alive',
'User-Agent: ' . $this->user_agent,
'client-request-id: ' . $this->generateClientRequestId(),
'return-client-request-id: ' . true,
'Content-Type: text/xml; charset=utf-8',
'SOAPAction: "' . $action . '"',
'Authorization: Bearer ' . $this->access_token
);
if (!is_null($this->anchor_mailbox))
{
$headers[] = 'X-AnchorMailbox: ' . $this->anchor_mailbox;
$headers[] = 'X-PreferServerAffinity: ' . true;
}
$this->__last_request_headers = $headers;
$this->ch = curl_init($location);
if ($this->connection_timeout != -1)
{
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, $this->connection_timeout);
}
curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->response_timeout);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, $this->validate);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, $this->validate);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
curl_setopt($this->ch, CURLOPT_ENCODING, 'gzip,deflate');
$xml = '';
if ($this->enable_response_headers)
{
curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this, 'curlWriteHeader'));
}
if ($this->write_to_file)
{
$next_five_minute_window = $this->file_output . '/' . date('Y_m_d_H_i', ceil(time() / 300) * 300);
if (!file_exists($next_five_minute_window))
{
mkdir($next_five_minute_window, 0644, true);
}
$file_path = $next_five_minute_window . '/' . md5($action . $this->access_token . time() . getmypid() . rand(0, getmypid())) . '.' . getmypid();
$this->file_handler = fopen($file_path, 'w');
curl_setopt($this->ch, CURLOPT_WRITEFUNCTION, array($this, 'curlWriteFunction'));
self::$last_path = $file_path;
// Return valid xml.
$xml = '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header><h:ServerVersionInfo MajorVersion="15" MinorVersion="1" MajorBuildNumber="497" MinorBuildNumber="14" Version="V2016_04_13" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/></s:Header><s:Body><path>'
. $file_path .
'</path></s:Body></s:Envelope>';
$result = curl_exec($this->ch);
if (!is_null($this->last_segment))
{
fwrite($this->file_handler, $this->last_segment);
}
fclose($this->file_handler);
// check the code and return the proper XML if an issue occured
$code = $this->getResponseCode();
if ($code != 200)
{
$xml = file_get_contents($file_path);
}
}
else
{
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($this->ch);
if ($result)
{
// replaces anything thats not in the list below with ' '
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
$xml = preg_replace("/&#x(([0-8B-CEF])|([1][0-9A-F])|([D][8-9A-F][0-9A-F]{2})|([F][F][F][E-F])|([1-9A-F][1-9A-F][0-9A-F]{4}));/", " ", $result);
}
}
// TODO: Add some real error handling.
// If the response if false than there was an error and we should throw
// an exception.
if ($result === false)
{
throw new EWS_Exception(
'Curl error: ' . curl_error($this->ch),
curl_errno($this->ch)
);
}
elseif ($result == false)
{
throw new EWS_Empty_Exception('response from Microsoft was empty');
}
return $xml;
}
/**
* Generate a guid for the client request.
*
* @return string The guid to use for the request.
*/
public function generateClientRequestId()
{
$rand_string = md5(uniqid(mt_rand(), true));
return substr($rand_string, 0, 8) . '-' .
substr($rand_string, 8, 4) . '-' .
substr($rand_string, 12, 4) . '-' .
substr($rand_string, 16, 4) . '-' .
substr($rand_string, 20);
}
/**
* Callback for curl write response headers, only called if $get_response_headers is true
*
* @param resource $curl_handle The curl handle.
* @param string $data The data received from the curl.
* @return int $length The number of bytes received/processed by the curl.
*/
public function curlWriteHeader($curl_handle, $data)
{
$this->__last_response_headers[] = trim($data);
return strlen($data);
}
/**
* Callback for curl write function. This is used to sanitize invalid input received from microsoft.
*
* @param resource $curl_handle The curl handle.
* @param string $data The data received from the curl.
* @return int $length The number of bytes received/processed by the curl.
*/
public function curlWriteFunction($curl_handle, $data)
{
// keep the expected length around for the return to avoid curl errors
$length = strlen($data);
if ($this->last_segment !== null)
{
$data = $this->last_segment . $data;
}
// sanitize the data
$sanitized_data = preg_replace("/&#x(([0-8B-CEF])|([1][0-9A-F])|([D][8-9A-F][0-9A-F]{2})|([F][F][F][E-F])|([1-9A-F][1-9A-F][0-9A-F]{4}));/", " ", $data);
// store the last segment of the data
$this->last_segment = substr($sanitized_data, -10);
// write the data to the file handler without the last segment
fwrite($this->file_handler, substr($sanitized_data, 0, -10));
return $length;
}
public function __call($function_name, $arguments)
{
$result = parent::__call($function_name, $arguments);
if ($this->write_to_file)
{
return self::$last_path;
}
return $result;
}
/**
* Returns last SOAP request headers
*
* @link http://php.net/manual/en/function.soap-soapclient-getlastrequestheaders.php
*
* @return string the last soap request headers
*/
public function __getLastRequestHeaders()
{
return implode("\n", $this->__last_request_headers) . "\n";
}
/**
* Returns last SOAP response headers
*
* @link http://php.net/manual/en/soapclient.getlastresponseheaders.php
*
* @return string the last soap response headers
*/
public function __getLastResponseHeaders()
{
return implode("\n", $this->__last_response_headers) . "\n";
}
/**
* Sets whether or not to validate ssl certificates
*
* @param boolean $validate
* @return bool
*/
public function validateCertificate($validate = true)
{
$this->validate = $validate;
return true;
}
/**
* Sets whether to download response headers. If set, call __getLastResponseHeaders() to get them
*
* @param bool $enable_response_headers
*/
public function setEnableResponseHeaders($enable_response_headers)
{
$this->enable_response_headers = $enable_response_headers;
}
/**
* Set the user agent to be used for calls
*
* @param string $user_agent The user agent to be used for calls
*/
public function setUserAgent($user_agent)
{
$this->user_agent = $user_agent;
}
/**
* Set the timeouts used for the connection
*
* @param int $connection_timeout The timeout in seconds waiting for a connection, or -1 to not set a connection timeout
* @param int $response_timeout The timeout in seconds waiting for a response
*/
public function setTimeouts($connection_timeout, $response_timeout)
{
$this->connection_timeout = $connection_timeout;
$this->response_timeout = $response_timeout;
}
}