1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Contains ExchangeWebServices. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
namespace garethp\ews\API; |
8
|
|
|
|
9
|
|
|
use garethp\ews\API\Exception\ExchangeException; |
10
|
|
|
use garethp\ews\API\Exception\NoResponseReturnedException; |
11
|
|
|
use garethp\ews\API\Exception\ServiceUnavailableException; |
12
|
|
|
use garethp\ews\API\Exception\UnauthorizedException; |
13
|
|
|
use garethp\ews\API\Message; |
14
|
|
|
use garethp\ews\API\Type\EmailAddressType; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Base class of the Exchange Web Services application. |
18
|
|
|
* |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* @package php-ews\Client |
22
|
|
|
* |
23
|
|
|
* @method Type AddDelegate($request) |
24
|
|
|
* @method Type ApplyConversationAction($request) |
25
|
|
|
* @method Type ConvertId($request) |
26
|
|
|
* @method Type CopyFolder($request) |
27
|
|
|
* @method Type CopyItem($request) |
28
|
|
|
* @method Type CreateAttachment($request) |
29
|
|
|
* @method Type CreateFolder($request) |
30
|
|
|
* @method Type CreateItem($request) |
31
|
|
|
* @method Type CreateManagedFolder($request) |
32
|
|
|
* @method Type CreateUserConfiguration($request) |
33
|
|
|
* @method Type DeleteAttachment($request) |
34
|
|
|
* @method Type DeleteFolder($request) |
35
|
|
|
* @method Type DeleteItem($request) |
36
|
|
|
* @method Type DeleteUserConfiguration($request) |
37
|
|
|
* @method Type DisconnectPhoneCall($request) |
38
|
|
|
* @method Type EmptyFolder($request) |
39
|
|
|
* @method Type ExpandDL($request) |
40
|
|
|
* @method Type ExportItems($request) |
41
|
|
|
* @method Type FindConversation($request) |
42
|
|
|
* @method Type FindFolder($request) |
43
|
|
|
* @method Type FindItem($request) |
44
|
|
|
* @method Type FindMessageTrackingReport($request) |
45
|
|
|
* @method Type GetAttachment($request) |
46
|
|
|
* @method Type GetDelegate($request) |
47
|
|
|
* @method Type GetEvents($request) |
48
|
|
|
* @method Type GetFolder($request) |
49
|
|
|
* @method Type GetInboxRules($request) |
50
|
|
|
* @method Type GetItem($request) |
51
|
|
|
* @method Type GetMailTips($request) |
52
|
|
|
* @method Type GetMessageTrackingReport($request) |
53
|
|
|
* @method Type GetPasswordExpirationDate($request) |
54
|
|
|
* @method Type GetPhoneCallInformation($request) |
55
|
|
|
* @method Type GetRoomLists($request) |
56
|
|
|
* @method Type GetRooms($request) |
57
|
|
|
* @method Type GetServerTimeZones($request) |
58
|
|
|
* @method Type GetServiceConfiguration($request) |
59
|
|
|
* @method Type GetSharingFolder($request) |
60
|
|
|
* @method Type GetSharingMetadata($request) |
61
|
|
|
* @method Type GetStreamingEvents($request) |
62
|
|
|
* @method Type GetUserAvailability($request) |
63
|
|
|
* @method Type GetUserConfiguration($request) |
64
|
|
|
* @method Type GetUserOofSettings($request) |
65
|
|
|
* @method Type MoveFolder($request) |
66
|
|
|
* @method Type MoveItem($request) |
67
|
|
|
* @method Type PlayOnPhone($request) |
68
|
|
|
* @method Type RefreshSharingFolder($request) |
69
|
|
|
* @method Type RemoveDelegate($request) |
70
|
|
|
* @method Type ResolveNames($request) |
71
|
|
|
* @method Type SendItem($request) |
72
|
|
|
* @method Type SetUserOofSettings($request) |
73
|
|
|
* @method Type Subscribe($request) |
74
|
|
|
* @method Type SyncFolderHierarchy($request) |
75
|
|
|
* @method Type SyncFolderItems($request) |
76
|
|
|
* @method Type Unsubscribe($request) |
77
|
|
|
* @method Type UpdateDelegate($request) |
78
|
|
|
* @method Type UpdateFolder($request) |
79
|
|
|
* @method Type UpdateInboxRules($request) |
80
|
|
|
* @method Type UpdateItem($request) |
81
|
|
|
* @method Type UpdateUserConfiguration($request) |
82
|
|
|
* @method Type UploadItems($request) |
83
|
|
|
*/ |
84
|
|
|
class ExchangeWebServices |
85
|
|
|
{ |
86
|
|
|
const VERSION_2007 = 'Exchange2007'; |
87
|
|
|
|
88
|
|
|
const VERSION_2007_SP1 = 'Exchange2007_SP1'; |
89
|
|
|
|
90
|
|
|
const VERSION_2010 = 'Exchange2010'; |
91
|
|
|
|
92
|
|
|
const VERSION_2010_SP1 = 'Exchange2010_SP1'; |
93
|
|
|
|
94
|
|
|
const VERSION_2010_SP2 = 'Exchange2010_SP2'; |
95
|
|
|
|
96
|
|
|
const VERSION_2013 = 'Exchange2013'; |
97
|
|
|
|
98
|
|
|
const VERSION_2013_SP1 = 'Exchange2013_SP1'; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Password to use when connecting to the Exchange server. |
102
|
|
|
* |
103
|
|
|
* @var string |
104
|
|
|
*/ |
105
|
|
|
protected $password = null; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Location of the Exchange server. |
109
|
|
|
* |
110
|
|
|
* @var string |
111
|
|
|
*/ |
112
|
|
|
protected $server = null; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* SOAP client used to make the request |
116
|
|
|
* |
117
|
|
|
* @var NTLMSoapClient |
118
|
|
|
*/ |
119
|
|
|
protected $soap = null; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Username to use when connecting to the Exchange server. |
123
|
|
|
* |
124
|
|
|
* @var string |
125
|
|
|
*/ |
126
|
|
|
protected $username = null; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @var EmailAddressType |
130
|
|
|
*/ |
131
|
|
|
protected $primarySmtpMailbox = null; |
132
|
|
|
|
133
|
|
|
protected static $middlewareStack = []; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* A setting to check whether or not responses should be drilled down before being |
137
|
|
|
* returned. Setting this to false |
138
|
|
|
* will return the raw responses without any filtering |
139
|
|
|
* |
140
|
|
|
* @var bool |
141
|
|
|
*/ |
142
|
|
|
protected $drillDownResponses = true; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Miscrosoft Exchange version that we are going to connect to |
146
|
|
|
* |
147
|
|
|
* @var string |
148
|
|
|
*/ |
149
|
|
|
protected $version = null; |
150
|
|
|
|
151
|
|
|
protected $options = null; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The timezone for the client |
155
|
|
|
* |
156
|
|
|
* @var bool |
157
|
|
|
*/ |
158
|
|
|
protected $timezone = false; |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return EmailAddressType |
162
|
|
|
*/ |
163
|
25 |
|
public function getPrimarySmtpMailbox() |
164
|
|
|
{ |
165
|
25 |
|
return $this->primarySmtpMailbox; |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
public function getPrimarySmtpEmailAddress() |
169
|
|
|
{ |
170
|
1 |
|
if ($this->primarySmtpMailbox == null) { |
171
|
1 |
|
return null; |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
return $this->primarySmtpMailbox->getEmailAddress(); |
175
|
|
|
} |
176
|
|
|
|
177
|
2 |
|
public function setPrimarySmtpEmailAddress($emailAddress) |
178
|
|
|
{ |
179
|
2 |
|
$mailbox = new EmailAddressType(); |
180
|
2 |
|
$mailbox->setEmailAddress($emailAddress); |
181
|
2 |
|
$this->primarySmtpMailbox = $mailbox; |
182
|
|
|
|
183
|
2 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param boolean $timezone |
188
|
|
|
*/ |
189
|
|
|
public function setTimezone($timezone) |
190
|
|
|
{ |
191
|
|
|
$this->timezone = $timezone; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
|
|
public function getVersion() |
198
|
|
|
{ |
199
|
|
|
return $this->version; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function getServer() |
206
|
|
|
{ |
207
|
|
|
return $this->server; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Constructor for the ExchangeWebServices class |
212
|
|
|
* |
213
|
|
|
* @param string $server |
214
|
|
|
* @param string $username |
215
|
|
|
* @param string $password |
216
|
|
|
* @param array $options |
217
|
|
|
*/ |
218
|
33 |
|
protected function __construct($server = null, $username = null, $password = null, $options = array()) |
219
|
|
|
{ |
220
|
33 |
|
if ($server !== null) { |
221
|
|
|
$this->createClient( |
222
|
|
|
$server, |
223
|
|
|
ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), |
224
|
|
|
$options |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
|
228
|
33 |
|
$this->buildMiddlewareStack(); |
229
|
33 |
|
} |
230
|
|
|
|
231
|
32 |
|
public static function fromUsernameAndPassword($server, $username, $password, $options) |
232
|
|
|
{ |
233
|
32 |
|
$self = new self(); |
234
|
32 |
|
$self->createClient($server, ExchangeWebServicesAuth::fromUsernameAndPassword($username, $password), $options); |
235
|
32 |
|
$self->options = $options; |
236
|
|
|
|
237
|
32 |
|
return $self; |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
public static function fromCallbackToken($server, $token, $options) |
241
|
|
|
{ |
242
|
1 |
|
$self = new self(); |
243
|
1 |
|
$self->createClient($server, ExchangeWebServicesAuth::fromCallbackToken($token), $options); |
244
|
1 |
|
$self->options = $options; |
245
|
|
|
|
246
|
1 |
|
return $self; |
247
|
|
|
} |
248
|
|
|
|
249
|
33 |
|
protected function createClient($server, $auth, $options) |
250
|
|
|
{ |
251
|
33 |
|
$location = 'https://' . $this->cleanServerUrl($server) . '/EWS/Exchange.asmx'; |
252
|
|
|
|
253
|
33 |
|
$options = array_replace_recursive([ |
254
|
33 |
|
'version' => self::VERSION_2007, |
255
|
33 |
|
'trace' => 1, |
256
|
33 |
|
'exceptions' => true, |
257
|
33 |
|
'classmap' => ClassMap::getClassMap(), |
258
|
|
|
'drillDownResponses' => true |
259
|
33 |
|
], $options); |
260
|
|
|
|
261
|
33 |
|
$this->server = $server; |
262
|
33 |
|
$this->version = $options['version']; |
263
|
|
|
|
264
|
33 |
|
$this->soap = new NTLMSoapClient( |
265
|
33 |
|
$location, |
266
|
33 |
|
$auth, |
267
|
33 |
|
dirname(__FILE__) . '/../../Resources/wsdl/services.wsdl', |
268
|
|
|
$options |
269
|
33 |
|
); |
270
|
|
|
|
271
|
33 |
|
if (isset($options['primarySmtpEmailAddress'])) { |
272
|
1 |
|
$this->setPrimarySmtpEmailAddress($options['primarySmtpEmailAddress']); |
273
|
1 |
|
} |
274
|
|
|
|
275
|
33 |
|
if (isset($options['impersonation'])) { |
276
|
1 |
|
$this->setPrimarySmtpEmailAddress($options['impersonation']); |
277
|
1 |
|
} |
278
|
|
|
|
279
|
33 |
|
$this->drillDownResponses = $options['drillDownResponses']; |
280
|
33 |
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @codeCoverageIgnore |
284
|
|
|
* |
285
|
|
|
* @param $name |
286
|
|
|
* @param $arguments |
287
|
|
|
* @return Type |
288
|
|
|
* @throws \garethp\ews\API\Exception |
289
|
|
|
*/ |
290
|
|
|
public function __call($name, $arguments) |
291
|
|
|
{ |
292
|
|
|
$request = $arguments[0]; |
293
|
|
|
list ($name, $request) = $this->executeMiddlewareStack(self::$middlewareStack, $name, $request, $this->options); |
294
|
|
|
$arguments[0] = $request; |
295
|
|
|
|
296
|
|
|
$response = $this->getClient()->__call($name, $arguments); |
|
|
|
|
297
|
|
|
|
298
|
|
|
return $this->processResponse($response); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Returns the SOAP Client that may be used to make calls against the server |
303
|
|
|
* |
304
|
|
|
* @return NTLMSoapClient |
305
|
|
|
*/ |
306
|
31 |
|
public function getClient() |
307
|
|
|
{ |
308
|
31 |
|
return $this->soap; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Sets the client |
313
|
|
|
* |
314
|
|
|
* @param NTLMSoapClient $client |
315
|
|
|
* @return $this |
316
|
|
|
*/ |
317
|
2 |
|
public function setClient($client) |
318
|
|
|
{ |
319
|
2 |
|
$this->soap = $client; |
320
|
|
|
|
321
|
2 |
|
return $this; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Cleans the server URL for usage |
326
|
|
|
* |
327
|
|
|
* @param $server |
328
|
|
|
* @return string |
329
|
|
|
*/ |
330
|
40 |
|
public function cleanServerUrl($server) |
331
|
|
|
{ |
332
|
40 |
|
$url = parse_url($server); |
333
|
40 |
|
if (!isset($url['host']) && isset($url['path'])) { |
334
|
35 |
|
$url['host'] = $url['path']; |
335
|
35 |
|
unset($url['path']); |
336
|
35 |
|
} |
337
|
|
|
|
338
|
40 |
|
$server = $url['host']; |
339
|
40 |
|
if (isset($url['port'])) { |
340
|
2 |
|
$server .= ':' . $url['port']; |
341
|
2 |
|
} |
342
|
|
|
|
343
|
40 |
|
if (isset($url['path'])) { |
344
|
4 |
|
$server .= $url['path']; |
345
|
4 |
|
} |
346
|
|
|
|
347
|
40 |
|
$server = rtrim($server, "/"); |
348
|
|
|
|
349
|
40 |
|
return $server; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Process a response to verify that it succeeded and take the appropriate |
354
|
|
|
* action |
355
|
|
|
* |
356
|
|
|
* @param \garethp\ews\API\Message\BaseResponseMessageType $response |
357
|
|
|
* @return Type[] |
358
|
|
|
* @throws \garethp\ews\API\Exception |
359
|
|
|
*/ |
360
|
29 |
|
protected function processResponse($response) |
361
|
|
|
{ |
362
|
|
|
// If the soap call failed then we need to thow an exception. |
363
|
29 |
|
$code = $this->getClient()->getResponseCode(); |
364
|
29 |
|
$this->handleNonSuccessfulResponses($response, $code); |
365
|
|
|
|
366
|
27 |
|
if (!$this->drillDownResponses) { |
367
|
|
|
return $response; |
368
|
|
|
} |
369
|
|
|
|
370
|
27 |
|
if (!$response->exists('responseMessages')) { |
371
|
|
|
return $response; |
372
|
|
|
} |
373
|
|
|
|
374
|
27 |
|
$response = $response->getResponseMessages(); |
375
|
27 |
|
$response = $this->drillDownResponseLevels($response); |
376
|
|
|
|
377
|
27 |
|
return $response; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param $response |
382
|
|
|
* @return array |
383
|
|
|
* @throws \garethp\ews\API\Exception |
384
|
|
|
*/ |
385
|
27 |
|
public function drillDownResponseLevels($response) |
386
|
|
|
{ |
387
|
27 |
|
$items = $this->getItemsFromResponse($response); |
388
|
|
|
|
389
|
27 |
|
if (count($items) == 1) { |
390
|
27 |
|
reset($items); |
391
|
27 |
|
$key = key($items); |
392
|
27 |
|
$methodName = "get$key"; |
393
|
27 |
|
$response = $response->$methodName(); |
394
|
|
|
|
395
|
27 |
|
return $this->drillDownResponseLevels($response); |
396
|
|
|
} |
397
|
|
|
|
398
|
27 |
|
if (is_array($items) && isset($items[1]) && $items[1] instanceof Message\ResponseMessageType) { |
|
|
|
|
399
|
1 |
|
$response = array(); |
400
|
1 |
|
foreach ($items as $responseItem) { |
401
|
1 |
|
$response[] = $this->drillDownResponseLevels($responseItem); |
402
|
1 |
|
} |
403
|
|
|
|
404
|
1 |
|
return $response; |
405
|
|
|
} |
406
|
|
|
|
407
|
27 |
|
return $response; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* @param $response |
412
|
|
|
* @return array |
413
|
|
|
* @throws ExchangeException |
414
|
|
|
*/ |
415
|
27 |
|
protected function getItemsFromResponse($response) |
416
|
|
|
{ |
417
|
27 |
|
$items = array(); |
418
|
27 |
|
if ($response instanceof Type) { |
419
|
27 |
|
$items = $response->getNonNullItems(); |
420
|
27 |
|
} |
421
|
|
|
|
422
|
27 |
|
if (is_array($response)) { |
423
|
1 |
|
$items = $response; |
424
|
1 |
|
} |
425
|
|
|
|
426
|
27 |
|
if ($response instanceof Message\ResponseMessageType) { |
|
|
|
|
427
|
27 |
|
if ($response->getResponseClass() !== "Success") { |
428
|
1 |
|
throw new ExchangeException($response->getMessageText()); |
429
|
|
|
} |
430
|
|
|
|
431
|
27 |
|
unset($items['responseClass']); |
432
|
27 |
|
unset($items['responseCode']); |
433
|
27 |
|
} |
434
|
|
|
|
435
|
27 |
|
return $items; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* @param Message\BaseResponseMessageType $response |
440
|
|
|
* @param $code |
441
|
|
|
* @throws ExchangeException |
442
|
|
|
* @throws NoResponseReturnedException |
443
|
|
|
* @throws ServiceUnavailableException |
444
|
|
|
* @throws UnauthorizedException |
445
|
|
|
*/ |
446
|
29 |
|
protected function handleNonSuccessfulResponses($response, $code) |
447
|
|
|
{ |
448
|
29 |
|
if ($code == 401) { |
449
|
|
|
throw new UnauthorizedException(); |
450
|
|
|
} |
451
|
|
|
|
452
|
29 |
|
if ($code == 503) { |
453
|
|
|
throw new ServiceUnavailableException(); |
454
|
|
|
} |
455
|
|
|
|
456
|
29 |
|
if ($code >= 300) { |
457
|
2 |
|
throw new ExchangeException('SOAP client returned status of ' . $code, $code); |
458
|
|
|
} |
459
|
|
|
|
460
|
27 |
|
if (empty($response) || empty($response->getNonNullResponseMessages())) { |
461
|
|
|
throw new NoResponseReturnedException(); |
462
|
|
|
} |
463
|
27 |
|
} |
464
|
|
|
|
465
|
33 |
|
protected function buildMiddlewareStack() |
466
|
|
|
{ |
467
|
33 |
|
if (self::$middlewareStack === false) { |
468
|
|
|
self::$middlewareStack = [ |
469
|
|
|
//Transform an objcet of type Type to an XML Object |
470
|
|
|
function ($name, $request, $options) { |
471
|
|
|
if ($request instanceof Type) { |
472
|
|
|
$request = $request->toXmlObject(); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
return [$name, $request, $options]; |
476
|
|
|
}, |
477
|
|
|
|
478
|
|
|
//The SyncScope option isn't available for Exchange 2007 SP1 and below |
479
|
|
|
function ($name, $request, $options) { |
480
|
|
|
$version2007SP1 = ($options['version'] == ExchangeWebServices::VERSION_2007 |
481
|
|
|
|| $options['version'] == ExchangeWebServices::VERSION_2007_SP1); |
482
|
|
|
if ($name == "SyncFolderItems" && $version2007SP1 && isset($request->SyncScope)) { |
483
|
|
|
unset($request->SyncScope); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
return [$name, $request, $options]; |
487
|
|
|
} |
488
|
|
|
]; |
489
|
|
|
} |
490
|
33 |
|
} |
491
|
|
|
|
492
|
27 |
|
protected function executeMiddlewareStack(array $middlewareStack, $name, $request, $options) |
493
|
|
|
{ |
494
|
27 |
|
$middlewareStack = array_reverse($middlewareStack); |
495
|
|
|
|
496
|
27 |
|
foreach ($middlewareStack as $middleware) { |
497
|
|
|
list ($name, $request, $options) = $middleware($name, $request, $options); |
498
|
27 |
|
} |
499
|
|
|
|
500
|
27 |
|
return [$name, $request, $options]; |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: