1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace jamesiarmes\PEWS; |
4
|
|
|
|
5
|
|
|
use jamesiarmes\PEWS\API\ExchangeWebServices; |
6
|
|
|
use jamesiarmes\PEWS\API\ExchangeWebServicesAuth; |
7
|
|
|
use jamesiarmes\PEWS\API\Message\SyncFolderItemsResponseMessageType; |
8
|
|
|
use jamesiarmes\PEWS\API\Type; |
9
|
|
|
use jamesiarmes\PEWS\Calendar\CalendarAPI; |
10
|
|
|
use jamesiarmes\PEWS\Mail\MailAPI; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A base class for APIs |
14
|
|
|
* |
15
|
|
|
* Class BaseAPI |
16
|
|
|
* @package jamesiarmes\PEWS |
17
|
|
|
*/ |
18
|
|
|
class API |
19
|
|
|
{ |
20
|
|
|
protected static $defaultClientOptions = array( |
21
|
|
|
'version' => ExchangeWebServices::VERSION_2010 |
22
|
|
|
); |
23
|
23 |
|
|
24
|
|
|
public function __construct(ExchangeWebServices $client = null) |
25
|
23 |
|
{ |
26
|
10 |
|
if ($client) { |
27
|
10 |
|
$this->setClient($client); |
28
|
23 |
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return Type\EmailAddressType |
33
|
15 |
|
*/ |
34
|
|
|
public function getPrimarySmtpMailbox() |
35
|
15 |
|
{ |
36
|
|
|
return $this->getClient()->getPrimarySmtpMailbox(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private $fieldUris = array(); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Storing the API client |
43
|
|
|
* @var ExchangeWebServices |
44
|
|
|
*/ |
45
|
|
|
private $client; |
46
|
4 |
|
|
47
|
|
|
public function setupFieldUris() |
48
|
|
|
{ |
49
|
4 |
|
//So, since we have to pass in URI's of everything we update, we need to fetch them |
50
|
4 |
|
$reflection = new \ReflectionClass('jamesiarmes\PEWS\API\Enumeration\UnindexedFieldURIType'); |
51
|
4 |
|
$constants = $reflection->getConstants(); |
52
|
|
|
$constantsFound = array(); |
53
|
|
|
|
54
|
4 |
|
//Loop through all URI's to list them in an array |
55
|
4 |
|
foreach ($constants as $constant) { |
56
|
4 |
|
$exploded = explode(":", $constant); |
57
|
4 |
|
if (count($exploded) == 1) { |
58
|
4 |
|
$exploded = ['item', $exploded[0]]; |
59
|
|
|
} |
60
|
4 |
|
|
61
|
4 |
|
$name = strtolower($exploded[1]); |
62
|
|
|
$category = strtolower($exploded[0]); |
63
|
4 |
|
|
64
|
4 |
|
if (!isset($constantsFound[$name])) { |
65
|
4 |
|
$constantsFound[$name] = array(); |
66
|
4 |
|
} |
67
|
4 |
|
$constantsFound[$name][$category] = $constant; |
68
|
|
|
} |
69
|
4 |
|
|
70
|
4 |
|
$this->fieldUris = $constantsFound; |
71
|
|
|
} |
72
|
4 |
|
|
73
|
|
|
public function getFieldUriByName($fieldName, $preference = 'item') |
74
|
4 |
|
{ |
75
|
4 |
|
$fieldName = strtolower($fieldName); |
76
|
|
|
$preference = strtolower($preference); |
77
|
4 |
|
|
78
|
4 |
|
if (empty($this->fieldUris)) { |
79
|
4 |
|
$this->setupFieldUris(); |
80
|
|
|
} |
81
|
4 |
|
|
82
|
1 |
|
if (!isset($this->fieldUris[$fieldName])) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
4 |
|
|
86
|
1 |
|
if (!isset($this->fieldUris[$fieldName][$preference])) { |
87
|
1 |
|
$preference = 'item'; |
88
|
|
|
} |
89
|
4 |
|
|
90
|
|
|
if (!isset($this->fieldUris[$fieldName][$preference])) { |
91
|
|
|
throw new \Exception("Could not find uri $preference:$fieldName"); |
92
|
|
|
} |
93
|
4 |
|
|
94
|
|
|
return $this->fieldUris[$fieldName][$preference]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get a calendar item |
99
|
|
|
* |
100
|
|
|
* @param string $name |
101
|
|
|
* @return CalendarAPI |
102
|
6 |
|
*/ |
103
|
|
|
public function getCalendar($name = null) |
104
|
6 |
|
{ |
105
|
6 |
|
$calendar = new CalendarAPI(); |
106
|
6 |
|
$calendar->setClient($this->getClient()); |
107
|
|
|
$calendar->pickCalendar($name); |
108
|
6 |
|
|
109
|
|
|
return $calendar; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $folderName |
114
|
|
|
* @return MailAPI |
115
|
8 |
|
*/ |
116
|
|
|
public function getMailbox($folderName = null) |
117
|
8 |
|
{ |
118
|
6 |
|
$mailApi = new MailAPI(); |
119
|
6 |
|
$mailApi->setClient($this->getClient()); |
120
|
|
|
$mailApi->pickMailFolder($folderName); |
121
|
6 |
|
|
122
|
|
|
return $mailApi; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Set the API client |
127
|
|
|
* |
128
|
|
|
* @param ExchangeWebServices $client |
129
|
|
|
* @return $this |
130
|
23 |
|
*/ |
131
|
|
|
public function setClient($client) |
132
|
23 |
|
{ |
133
|
23 |
|
$this->client = $client; |
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get the API client |
139
|
|
|
* |
140
|
|
|
* @return ExchangeWebServices |
141
|
22 |
|
*/ |
142
|
|
|
public function getClient() |
143
|
22 |
|
{ |
144
|
|
|
return $this->client; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Instantiate and set a client (ExchangeWebServices) based on the parameters given |
149
|
|
|
* |
150
|
|
|
* @deprecated Since 0.6.3 |
151
|
|
|
* @param $server |
152
|
|
|
* @param $username |
153
|
|
|
* @param $password |
154
|
|
|
* @param array $options |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function buildClient( |
158
|
|
|
$server, |
159
|
|
|
$username, |
160
|
|
|
$password, |
161
|
|
|
$options = [ ] |
162
|
|
|
) { |
163
|
|
|
$this->setClient(ExchangeWebServices::fromUsernameAndPassword( |
164
|
|
|
$server, |
165
|
|
|
$username, |
166
|
|
|
$password, |
167
|
|
|
array_replace_recursive(self::$defaultClientOptions, $options) |
168
|
|
|
)); |
169
|
|
|
} |
170
|
9 |
|
|
171
|
|
|
public static function withUsernameAndPassword($server, $username, $password, $options = [ ]) |
172
|
9 |
|
{ |
173
|
9 |
|
return new static(ExchangeWebServices::fromUsernameAndPassword( |
174
|
9 |
|
$server, |
175
|
9 |
|
$username, |
176
|
9 |
|
$password, |
177
|
9 |
|
array_replace_recursive(self::$defaultClientOptions, $options) |
178
|
|
|
)); |
179
|
|
|
} |
180
|
1 |
|
|
181
|
|
|
public static function withCallbackToken($server, $token, $options = [ ]) |
182
|
1 |
|
{ |
183
|
1 |
|
return new static(ExchangeWebServices::fromCallbackToken( |
184
|
1 |
|
$server, |
185
|
1 |
|
$token, |
186
|
1 |
|
array_replace_recursive(self::$defaultClientOptions, $options) |
187
|
|
|
)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getPrimarySmptEmailAddress() |
191
|
|
|
{ |
192
|
|
|
if ($this->getPrimarySmtpMailbox() == null) { |
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->getPrimarySmtpMailbox()->getEmailAddress(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function setPrimarySmtpEmailAddress($emailAddress) |
200
|
|
|
{ |
201
|
|
|
$this->getClient()->setPrimarySmtpEmailAddress($emailAddress); |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Create items through the API client |
208
|
|
|
* |
209
|
|
|
* @param $items |
210
|
|
|
* @param array $options |
211
|
|
|
* @return API\CreateItemResponseType |
212
|
9 |
|
*/ |
213
|
|
|
public function createItems($items, $options = array()) |
214
|
9 |
|
{ |
215
|
|
|
if (!is_array($items)) { |
216
|
|
|
$items = array($items); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$request = array( |
220
|
9 |
|
'Items' => $items |
221
|
|
|
); |
222
|
9 |
|
|
223
|
9 |
|
$request = array_replace_recursive($request, $options); |
224
|
|
|
$request = Type::buildFromArray($request); |
225
|
9 |
|
|
226
|
|
|
$response = $this->getClient()->CreateItem($request); |
227
|
9 |
|
|
228
|
|
|
return $response; |
229
|
|
|
} |
230
|
2 |
|
|
231
|
|
|
public function updateItems($items, $options = array()) |
232
|
|
|
{ |
233
|
2 |
|
$request = array( |
234
|
2 |
|
'ItemChanges' => $items, |
235
|
|
|
'MessageDisposition' => 'SaveOnly', |
236
|
2 |
|
'ConflictResolution' => 'AlwaysOverwrite' |
237
|
|
|
); |
238
|
2 |
|
|
239
|
|
|
$request = array_replace_recursive($request, $options); |
240
|
2 |
|
|
241
|
|
|
$request = Type::buildFromArray($request); |
242
|
2 |
|
|
243
|
|
|
return $this->getClient()->UpdateItem($request)->getItems(); |
|
|
|
|
244
|
|
|
} |
245
|
2 |
|
|
246
|
|
|
protected function buildUpdateItemChanges($itemType, $uriType, $changes) |
247
|
2 |
|
{ |
248
|
|
|
$setItemFields = array(); |
249
|
|
|
|
250
|
2 |
|
//Add each property to a setItemField |
251
|
2 |
|
foreach ($changes as $key => $value) { |
252
|
|
|
$fullName = $this->getFieldUriByName($key, $uriType); |
253
|
2 |
|
|
254
|
2 |
|
$setItemFields[] = array( |
255
|
2 |
|
'FieldURI' => array('FieldURI' => $fullName), |
256
|
2 |
|
$itemType => array($key => $value) |
257
|
2 |
|
); |
258
|
|
|
} |
259
|
2 |
|
|
260
|
|
|
return $setItemFields; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
public function createFolders($names, Type\FolderIdType $parentFolder, $options = array()) |
264
|
|
|
{ |
265
|
|
|
$request = array('Folders' => array('Folder' => array())); |
266
|
|
|
if (!empty($parentFolder)) { |
267
|
|
|
$request['ParentFolderId'] = array('FolderId' => $parentFolder->toArray()); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
if (!is_array($names)) { |
271
|
|
|
$names = array($names); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
foreach ($names as $name) { |
275
|
|
|
$request['Folders']['Folder'][] = array( |
276
|
|
|
'DisplayName' => $name |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$request = array_merge_recursive($request, $options); |
281
|
|
|
|
282
|
|
|
$this->client->CreateFolder($request); |
|
|
|
|
283
|
|
|
|
284
|
|
|
return true; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
View Code Duplication |
public function deleteFolder(Type\FolderIdType $folderId, $options = array()) |
|
|
|
|
288
|
|
|
{ |
289
|
|
|
$request = array( |
290
|
|
|
'DeleteType' => 'HardDelete', |
291
|
|
|
'FolderIds' => array( |
292
|
|
|
'FolderId' => $folderId->toArray() |
293
|
|
|
) |
294
|
|
|
); |
295
|
|
|
|
296
|
|
|
$request = array_merge_recursive($request, $options); |
297
|
|
|
return $this->client->DeleteFolder($request); |
|
|
|
|
298
|
|
|
} |
299
|
|
|
|
300
|
|
View Code Duplication |
public function moveItem(Type\ItemIdType $itemId, Type\FolderIdType $folderId, $options = array()) |
|
|
|
|
301
|
|
|
{ |
302
|
|
|
$request = array( |
303
|
9 |
|
'ToFolderId' => array('FolderId' => $folderId->toArray()), |
304
|
|
|
'ItemIds' => array('ItemId' => $itemId->toArray()) |
305
|
9 |
|
); |
306
|
9 |
|
|
307
|
9 |
|
$request = array_merge_recursive($request, $options); |
308
|
|
|
|
309
|
9 |
|
return $this->client->MoveItem($request); |
|
|
|
|
310
|
9 |
|
} |
311
|
9 |
|
|
312
|
8 |
|
/** |
313
|
8 |
|
* @param $items Type\ItemIdType|Type\ItemIdType[] |
314
|
9 |
|
* @param array $options |
315
|
9 |
|
* @return bool |
316
|
9 |
|
*/ |
317
|
9 |
|
public function deleteItems($items, $options = array()) |
318
|
9 |
|
{ |
319
|
9 |
|
if (!is_array($items) || Type::arrayIsAssoc($items)) { |
320
|
|
|
$items = array($items); |
321
|
|
|
} |
322
|
9 |
|
|
323
|
|
|
$itemIds = array(); |
324
|
9 |
|
foreach ($items as $item) { |
325
|
|
|
if ($item instanceof Type\ItemIdType) { |
|
|
|
|
326
|
9 |
|
$item = $item->toArray(); |
327
|
9 |
|
} |
328
|
9 |
|
$item = (array) $item; |
329
|
|
|
$itemIds[] = array( |
330
|
|
|
'Id' => $item['Id'], |
331
|
9 |
|
'ChangeKey' => $item['ChangeKey'] |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$request = array( |
336
|
|
|
'ItemIds' => array('ItemId' => $itemIds), |
337
|
|
|
'DeleteType' => 'MoveToDeletedItems' |
338
|
15 |
|
); |
339
|
|
|
|
340
|
|
|
$request = array_replace_recursive($request, $options); |
341
|
|
|
$request = Type::buildFromArray($request); |
342
|
15 |
|
$this->getClient()->DeleteItem($request); |
343
|
15 |
|
|
344
|
|
|
//If the delete fails, an Exception will be thrown in processResponse before it gets here |
345
|
15 |
|
return true; |
346
|
15 |
|
} |
347
|
|
|
|
348
|
15 |
|
/** |
349
|
15 |
|
* @param $identifier |
350
|
|
|
* @return Type\BaseFolderType |
351
|
|
|
*/ |
352
|
|
|
public function getFolder($identifier) |
353
|
|
|
{ |
354
|
|
|
$request = array( |
355
|
|
|
'FolderShape' => array( |
356
|
|
|
'BaseShape' => array('_' => 'Default') |
357
|
|
|
), |
358
|
15 |
|
'FolderIds' => $identifier |
359
|
|
|
); |
360
|
15 |
|
$request = Type::buildFromArray($request); |
361
|
|
|
|
362
|
15 |
|
$response = $this->getClient()->GetFolder($request); |
363
|
15 |
|
return $response; |
364
|
15 |
|
} |
365
|
15 |
|
|
366
|
|
|
/** |
367
|
|
|
* Get a folder by it's distinguishedId |
368
|
|
|
* |
369
|
|
|
* @param string $distinguishedId |
370
|
|
|
* @return Type\BaseFolderType |
371
|
|
|
*/ |
372
|
4 |
|
public function getFolderByDistinguishedId($distinguishedId) |
373
|
|
|
{ |
374
|
4 |
|
return $this->getFolder(array( |
375
|
4 |
|
'DistinguishedFolderId' => array( |
376
|
4 |
|
'Id' => $distinguishedId, |
377
|
|
|
'Mailbox' => $this->getPrimarySmtpMailbox() |
378
|
|
|
) |
379
|
|
|
)); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @param $folderId |
384
|
14 |
|
* @return Type\BaseFolderType |
385
|
|
|
*/ |
386
|
14 |
|
public function getFolderByFolderId($folderId) |
387
|
14 |
|
{ |
388
|
14 |
|
return $this->getFolder(array( |
389
|
|
|
'FolderId' => array('Id'=>$folderId, 'Mailbox' => $this->getPrimarySmtpMailbox()) |
390
|
|
|
)); |
391
|
14 |
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
14 |
|
* @param string|Type\FolderIdType $parentFolderId |
395
|
|
|
* @param array $options |
396
|
14 |
|
* @return bool|Type\BaseFolderType |
397
|
14 |
|
*/ |
398
|
14 |
|
public function getChildrenFolders($parentFolderId = 'root', $options = array()) |
399
|
|
|
{ |
400
|
14 |
|
if (is_string($parentFolderId)) { |
401
|
|
|
$parentFolderId = $this->getFolderByDistinguishedId($parentFolderId)->getFolderId(); |
|
|
|
|
402
|
14 |
|
} |
403
|
|
|
|
404
|
|
|
$request = array( |
405
|
14 |
|
'Traversal' => 'Shallow', |
406
|
|
|
'FolderShape' => array( |
407
|
|
|
'BaseShape' => 'AllProperties' |
408
|
|
|
), |
409
|
|
|
'ParentFolderIds' => array( |
410
|
|
|
'FolderId' => $parentFolderId->toArray() |
411
|
|
|
) |
412
|
|
|
); |
413
|
|
|
|
414
|
|
|
$request = array_replace_recursive($request, $options); |
415
|
14 |
|
|
416
|
|
|
$request = Type::buildFromArray($request); |
417
|
14 |
|
|
418
|
|
|
/** @var \jamesiarmes\PEWS\API\Message\FindFolderResponseMessageType $folders */ |
419
|
14 |
|
return $this->getClient()->FindFolder($request); |
|
|
|
|
420
|
14 |
|
return $folders->getFolders(); |
|
|
|
|
421
|
14 |
|
} |
422
|
|
|
|
423
|
6 |
|
/** |
424
|
|
|
* @param $folderName |
425
|
|
|
* @param string|Type\FolderIdType $parentFolderId |
426
|
|
|
* @param array $options |
427
|
|
|
* @return bool|Type\BaseFolderType |
428
|
|
|
*/ |
429
|
|
|
public function getFolderByDisplayName($folderName, $parentFolderId = 'root', $options = array()) |
430
|
|
|
{ |
431
|
|
|
$folders = $this->getChildrenFolders($parentFolderId, $options); |
432
|
|
|
|
433
|
1 |
|
foreach ($folders as $folder) { |
|
|
|
|
434
|
|
|
if ($folder->getDisplayName() == $folderName) { |
435
|
1 |
|
return $folder; |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
return false; |
440
|
1 |
|
} |
441
|
1 |
|
|
442
|
1 |
|
/** |
443
|
|
|
* @param $itemId array|Type\ItemIdType |
444
|
1 |
|
* @param array $options |
445
|
|
|
* @return Type |
446
|
1 |
|
*/ |
447
|
|
|
public function getItem($itemId, $options = array()) |
448
|
|
|
{ |
449
|
|
|
if ($itemId instanceof Type\ItemIdType) { |
|
|
|
|
450
|
|
|
$itemId = $itemId->toArray(); |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
$request = array( |
454
|
|
|
'ItemShape' => array('BaseShape' => 'AllProperties'), |
455
|
|
|
'ItemIds' => array('ItemId' => $itemId) |
456
|
|
|
); |
457
|
2 |
|
|
458
|
|
|
$request = array_replace_recursive($request, $options); |
459
|
|
|
|
460
|
2 |
|
return $this->getClient()->GetItem($request); |
461
|
2 |
|
} |
462
|
2 |
|
|
463
|
|
|
/** |
464
|
2 |
|
* Get a list of sync changes on a folder |
465
|
|
|
* |
466
|
2 |
|
* @param Type\FolderIdType $folderId |
467
|
|
|
* @param null $syncState |
468
|
|
|
* @param array $options |
469
|
|
|
* @return SyncFolderItemsResponseMessageType |
470
|
|
|
*/ |
471
|
2 |
|
public function listItemChanges($folderId, $syncState = null, $options = array()) |
472
|
|
|
{ |
473
|
2 |
|
$request = array( |
474
|
2 |
|
'ItemShape' => array('BaseShape' => 'IdOnly'), |
475
|
2 |
|
'SyncFolderId' => array('FolderId' => $folderId->toXmlObject()), |
476
|
|
|
'SyncScope' => 'NormalItems', |
477
|
|
|
'MaxChangesReturned' => '10' |
478
|
|
|
); |
479
|
|
|
|
480
|
|
|
if ($syncState != null) { |
481
|
|
|
$request['SyncState'] = $syncState; |
482
|
|
|
$request['ItemShape']['BaseShape'] = 'AllProperties'; |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
$request = array_replace_recursive($request, $options); |
486
|
|
|
|
487
|
|
|
$request = Type::buildFromArray($request); |
488
|
|
|
$response = $this->getClient()->SyncFolderItems($request); |
489
|
|
|
return $response; |
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: