1
|
|
|
<?php namespace CalDAVClient\Facade; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2017 OpenStack Foundation |
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
6
|
|
|
* you may not use this file except in compliance with the License. |
7
|
|
|
* You may obtain a copy of the License at |
8
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
* Unless required by applicable law or agreed to in writing, software |
10
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
11
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12
|
|
|
* See the License for the specific language governing permissions and |
13
|
|
|
* limitations under the License. |
14
|
|
|
**/ |
15
|
|
|
|
16
|
|
|
use CalDAVClient\Facade\Exceptions\ForbiddenException; |
17
|
|
|
use CalDAVClient\Facade\Requests\CalDAVRequestFactory; |
18
|
|
|
use CalDAVClient\Facade\Requests\CalendarQueryFilter; |
19
|
|
|
use CalDAVClient\Facade\Requests\EventRequestVO; |
20
|
|
|
use CalDAVClient\Facade\Requests\MakeCalendarRequestVO; |
21
|
|
|
use CalDAVClient\Facade\Responses\CalendarDeletedResponse; |
22
|
|
|
use CalDAVClient\Facade\Responses\CalendarHomesResponse; |
23
|
|
|
use CalDAVClient\Facade\Responses\CalendarSyncInfoResponse; |
24
|
|
|
use CalDAVClient\Facade\Responses\EventCreatedResponse; |
25
|
|
|
use CalDAVClient\Facade\Responses\EventDeletedResponse; |
26
|
|
|
use CalDAVClient\Facade\Responses\EventUpdatedResponse; |
27
|
|
|
use CalDAVClient\Facade\Responses\GetCalendarResponse; |
28
|
|
|
use CalDAVClient\Facade\Responses\GetCalendarsResponse; |
29
|
|
|
use CalDAVClient\Facade\Responses\ResourceCollectionResponse; |
30
|
|
|
use CalDAVClient\Facade\Responses\UserPrincipalResponse; |
31
|
|
|
use CalDAVClient\Facade\Utils\RequestFactory; |
32
|
|
|
use CalDAVClient\ICalDavClient; |
33
|
|
|
use CalDAVClient\Facade\Exceptions\NotFoundResourceException; |
34
|
|
|
use CalDAVClient\Facade\Exceptions\ServerErrorException; |
35
|
|
|
use CalDAVClient\Facade\Exceptions\UserUnAuthorizedException; |
36
|
|
|
use GuzzleHttp\Client; |
37
|
|
|
use GuzzleHttp\Exception\ClientException; |
38
|
|
|
use GuzzleHttp\Psr7\Request; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class CalDavClient |
42
|
|
|
* @package CalDAVClient\Facade |
43
|
|
|
*/ |
44
|
|
|
final class CalDavClient implements ICalDavClient |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* As indicated in Section 3.10 of [RFC2445], the URL of calendar object |
49
|
|
|
* resources containing (an arbitrary set of) calendaring and scheduling |
50
|
|
|
* information may be suffixed by ".ics", and the URL of calendar object |
51
|
|
|
* resources containing free or busy time information may be suffixed by |
52
|
|
|
* ".ifb". |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
const SchedulingInformationSuffix = '.ics'; |
56
|
|
|
|
57
|
|
|
const FreeBusyTimeInformationSuffix = '.ics'; |
58
|
|
|
|
59
|
|
|
const ETagHeader = 'ETag'; |
60
|
|
|
|
61
|
|
|
const DAVHeader = 'DAV'; |
62
|
|
|
|
63
|
|
|
const CalendarAccessOption = 'calendar-access'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
private $server_url; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $user; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string |
77
|
|
|
*/ |
78
|
|
|
private $password; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
private $authtype = "basic"; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var Client |
87
|
|
|
*/ |
88
|
|
|
private $client; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var int |
92
|
|
|
*/ |
93
|
|
|
private $timeout = 60; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* CalDavClient constructor. |
97
|
|
|
* @param string $server_url |
98
|
|
|
* @param string|null $user |
99
|
|
|
* @param string|null $password |
100
|
|
|
* @param string|"basic" $authtype |
101
|
|
|
*/ |
|
|
|
|
102
|
|
|
public function __construct($server_url, $user = null, $password = null, $authtype = "basic") |
103
|
|
|
{ |
104
|
|
|
$this->server_url = $server_url; |
105
|
|
|
$this->user = $user; |
106
|
|
|
$this->password = $password; |
107
|
|
|
$this->authtype = $authtype; |
108
|
|
|
|
109
|
|
|
$this->client = new Client(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $server_url |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function setServerUrl($server_url) |
117
|
|
|
{ |
118
|
|
|
$this->server_url = $server_url; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $username |
123
|
|
|
* @param string $password |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function setCredentials($username, $password) |
127
|
|
|
{ |
128
|
|
|
$this->user = $username; |
129
|
|
|
$this->password = $password; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function setAuthenticationType($authtype) { |
133
|
|
|
$this->authtype = $authtype; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Request $http_request |
138
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
139
|
|
|
*/ |
140
|
|
|
private function makeRequest(Request $http_request){ |
141
|
|
|
try{ |
142
|
|
|
return $this->client->send($http_request, [ |
143
|
|
|
'auth' => [$this->user, $this->password, $this->authtype], |
144
|
|
|
'timeout' => $this->timeout |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
catch (ClientException $ex){ |
148
|
|
|
switch($ex->getCode()){ |
149
|
|
|
case 401: |
150
|
|
|
throw new UserUnAuthorizedException(); |
151
|
|
|
break; |
152
|
|
|
case 404: |
153
|
|
|
throw new NotFoundResourceException(); |
154
|
|
|
break; |
155
|
|
|
case 403: |
156
|
|
|
throw new ForbiddenException(); |
157
|
|
|
default: |
158
|
|
|
throw new ServerErrorException($ex->getMessage(), $ex->getCode()); |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
|
|
public function isValidServer() |
168
|
|
|
{ |
169
|
|
|
|
170
|
|
|
$http_response = $this->makeRequest( |
171
|
|
|
RequestFactory::createOptionsRequest($this->server_url) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$res = $http_response->hasHeader(self::DAVHeader); |
175
|
|
|
$options = []; |
176
|
|
|
if($res){ |
177
|
|
|
$val = $http_response->getHeaderLine(self::DAVHeader); |
178
|
|
|
if(!empty($val)){ |
179
|
|
|
$options = explode(', ', $val); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $res && count($options) > 0 && in_array(self::CalendarAccessOption, $options); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return UserPrincipalResponse |
188
|
|
|
*/ |
189
|
|
|
public function getUserPrincipal() |
190
|
|
|
{ |
191
|
|
|
$http_response = $this->makeRequest( |
192
|
|
|
RequestFactory::createPropFindRequest |
193
|
|
|
( |
194
|
|
|
$this->server_url, |
195
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::PrincipalRequestType)->getContent() |
196
|
|
|
) |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
return new UserPrincipalResponse($this->server_url, (string)$http_response->getBody(), $http_response->getStatusCode()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $principal_url |
204
|
|
|
* @return CalendarHomesResponse |
205
|
|
|
*/ |
206
|
|
|
public function getCalendarHome($principal_url) |
207
|
|
|
{ |
208
|
|
|
$http_response = $this->makeRequest( |
209
|
|
|
RequestFactory::createPropFindRequest |
210
|
|
|
( |
211
|
|
|
$principal_url, |
212
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::CalendarHomeRequestType)->getContent() |
213
|
|
|
) |
214
|
|
|
); |
215
|
|
|
|
216
|
|
|
return new CalendarHomesResponse($this->server_url, (string)$http_response->getBody(), $http_response->getStatusCode()); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param string $calendar_home_set |
221
|
|
|
* @param MakeCalendarRequestVO $vo |
222
|
|
|
* @see https://tools.ietf.org/html/rfc4791#section-5.3.1 |
223
|
|
|
* @return string|boolean |
224
|
|
|
*/ |
225
|
|
|
public function createCalendar($calendar_home_set, MakeCalendarRequestVO $vo) |
226
|
|
|
{ |
227
|
|
|
$uid = $vo->getUID(); |
228
|
|
|
$resource_url = $calendar_home_set.$uid; |
229
|
|
|
$http_response = $this->makeRequest( |
230
|
|
|
RequestFactory::createMakeCalendarRequest |
231
|
|
|
( |
232
|
|
|
$resource_url, |
233
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::CalendarCreateRequestType, [$vo])->getContent() |
234
|
|
|
) |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
return $http_response->getStatusCode() == 201 ? $resource_url : false; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param string $calendar_home_set_url |
242
|
|
|
* @return GetCalendarsResponse |
243
|
|
|
*/ |
244
|
|
|
public function getCalendars($calendar_home_set_url) |
245
|
|
|
{ |
246
|
|
|
$http_response = $this->makeRequest( |
247
|
|
|
RequestFactory::createPropFindRequest |
248
|
|
|
( |
249
|
|
|
$calendar_home_set_url, |
250
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::CalendarsRequestType)->getContent() |
251
|
|
|
) |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
return new GetCalendarsResponse($this->server_url, (string)$http_response->getBody(), $http_response->getStatusCode()); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param string $calendar_url |
259
|
|
|
* @return GetCalendarResponse |
260
|
|
|
*/ |
261
|
|
|
public function getCalendar($calendar_url) |
262
|
|
|
{ |
263
|
|
|
$http_response = $this->makeRequest( |
264
|
|
|
RequestFactory::createPropFindRequest |
265
|
|
|
( |
266
|
|
|
$calendar_url, |
267
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::CalendarRequestType)->getContent(), |
268
|
|
|
0 |
269
|
|
|
) |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
return new GetCalendarResponse($this->server_url, (string)$http_response->getBody(), $http_response->getStatusCode()); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param string $calendar_url |
278
|
|
|
* @param string $sync_token |
279
|
|
|
* @return CalendarSyncInfoResponse |
280
|
|
|
*/ |
281
|
|
|
public function getCalendarSyncInfo($calendar_url, $sync_token) |
282
|
|
|
{ |
283
|
|
|
|
284
|
|
|
$http_response = $this->makeRequest( |
285
|
|
|
RequestFactory::createReportRequest |
286
|
|
|
( |
287
|
|
|
$calendar_url, |
288
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::CalendarSyncRequestType, [$sync_token])->getContent() |
289
|
|
|
) |
290
|
|
|
); |
291
|
|
|
|
292
|
|
|
return new CalendarSyncInfoResponse($this->server_url, (string)$http_response->getBody(), $http_response->getStatusCode()); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param string $calendar_url |
297
|
|
|
* @param EventRequestVO $vo |
298
|
|
|
* @return EventCreatedResponse |
299
|
|
|
*/ |
300
|
|
|
public function createEvent($calendar_url, EventRequestVO $vo) |
301
|
|
|
{ |
302
|
|
|
$uid = $vo->getUID(); |
303
|
|
|
$resource_url = $calendar_url.$uid.self::SchedulingInformationSuffix; |
304
|
|
|
$http_response = $this->makeRequest( |
305
|
|
|
RequestFactory::createPutRequest |
306
|
|
|
( |
307
|
|
|
$resource_url, |
308
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::EventCreateRequestType, [$vo])->getContent() |
309
|
|
|
) |
310
|
|
|
); |
311
|
|
|
$etag = $http_response->hasHeader(self::ETagHeader) ? $http_response->getHeaderLine(self::ETagHeader) : null; |
312
|
|
|
return new EventCreatedResponse |
313
|
|
|
( |
314
|
|
|
$uid, |
315
|
|
|
$etag, |
316
|
|
|
$resource_url, |
317
|
|
|
(string)$http_response->getBody(), |
318
|
|
|
$http_response->getStatusCode() |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param string $calendar_url |
324
|
|
|
* @param EventRequestVO $vo |
325
|
|
|
* @param string $etag |
326
|
|
|
* @return EventUpdatedResponse |
327
|
|
|
*/ |
328
|
|
|
public function updateEvent($calendar_url, EventRequestVO $vo, $etag = null) |
329
|
|
|
{ |
330
|
|
|
$uid = $vo->getUID(); |
331
|
|
|
$resource_url = $calendar_url.$uid.self::SchedulingInformationSuffix; |
332
|
|
|
$http_response = $this->makeRequest( |
333
|
|
|
RequestFactory::createPutRequest |
334
|
|
|
( |
335
|
|
|
$resource_url, |
336
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::EventUpdateRequestType, [$vo])->getContent(), |
337
|
|
|
$etag |
338
|
|
|
) |
339
|
|
|
); |
340
|
|
|
$etag = $http_response->hasHeader(self::ETagHeader) ? $http_response->getHeaderLine(self::ETagHeader) : null; |
341
|
|
|
return new EventUpdatedResponse |
342
|
|
|
( |
343
|
|
|
$uid, |
344
|
|
|
$etag, |
345
|
|
|
$resource_url, |
346
|
|
|
(string)$http_response->getBody(), |
347
|
|
|
$http_response->getStatusCode() |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param string $calendar_url |
353
|
|
|
* @param string $uid |
354
|
|
|
* @param string $etag |
355
|
|
|
* @return EventDeletedResponse |
356
|
|
|
*/ |
357
|
|
|
public function deleteEvent($calendar_url, $uid, $etag = null) |
358
|
|
|
{ |
359
|
|
|
$http_response = $this->makeRequest( |
360
|
|
|
RequestFactory::createDeleteRequest |
361
|
|
|
( |
362
|
|
|
$calendar_url.$uid.self::SchedulingInformationSuffix, |
363
|
|
|
$etag |
364
|
|
|
) |
365
|
|
|
); |
366
|
|
|
|
367
|
|
|
return new EventDeletedResponse |
368
|
|
|
( |
369
|
|
|
(string)$http_response->getBody(), $http_response->getStatusCode() |
370
|
|
|
); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @param string $event_url |
375
|
|
|
* @return string |
376
|
|
|
*/ |
377
|
|
|
public function getEventVCardBy($event_url){ |
378
|
|
|
$http_response = $this->makeRequest( |
379
|
|
|
RequestFactory::createGetRequest |
380
|
|
|
( |
381
|
|
|
$event_url |
382
|
|
|
) |
383
|
|
|
); |
384
|
|
|
|
385
|
|
|
$ical = (string)$http_response->getBody(); |
386
|
|
|
return $ical; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param string $calendar_url |
391
|
|
|
* @param array $events_urls |
392
|
|
|
* @return ResourceCollectionResponse |
393
|
|
|
*/ |
394
|
|
|
public function getEventsBy($calendar_url, array $events_urls) |
395
|
|
|
{ |
396
|
|
|
$http_response = $this->makeRequest( |
397
|
|
|
RequestFactory::createReportRequest |
398
|
|
|
( |
399
|
|
|
$calendar_url, |
400
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::CalendarMultiGetRequestType, [$events_urls])->getContent() |
401
|
|
|
) |
402
|
|
|
); |
403
|
|
|
|
404
|
|
|
return new ResourceCollectionResponse |
405
|
|
|
( |
406
|
|
|
$this->server_url, |
407
|
|
|
(string)$http_response->getBody(), |
408
|
|
|
$http_response->getStatusCode() |
409
|
|
|
); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @param string $calendar_url |
414
|
|
|
* @param CalendarQueryFilter $filter |
415
|
|
|
* @return ResourceCollectionResponse |
416
|
|
|
*/ |
417
|
|
|
public function getEventsByQuery($calendar_url, CalendarQueryFilter $filter) |
418
|
|
|
{ |
419
|
|
|
|
420
|
|
|
$http_response = $this->makeRequest( |
421
|
|
|
RequestFactory::createReportRequest |
422
|
|
|
( |
423
|
|
|
$calendar_url, |
424
|
|
|
CalDAVRequestFactory::getInstance()->build(CalDAVRequestFactory::CalendarQueryRequestType, [$filter])->getContent() |
425
|
|
|
) |
426
|
|
|
); |
427
|
|
|
|
428
|
|
|
return new ResourceCollectionResponse |
429
|
|
|
( |
430
|
|
|
$this->server_url, |
431
|
|
|
(string)$http_response->getBody(), |
432
|
|
|
$http_response->getStatusCode() |
433
|
|
|
); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @param string $calendar_url |
438
|
|
|
* @param string|null $etag |
439
|
|
|
* @return CalendarDeletedResponse |
440
|
|
|
*/ |
441
|
|
|
public function deleteCalendar($calendar_url, $etag = null) |
442
|
|
|
{ |
443
|
|
|
$http_response = $this->makeRequest( |
444
|
|
|
RequestFactory::createDeleteRequest |
445
|
|
|
( |
446
|
|
|
$calendar_url, |
447
|
|
|
$etag |
448
|
|
|
) |
449
|
|
|
); |
450
|
|
|
|
451
|
|
|
return new CalendarDeletedResponse |
452
|
|
|
( |
453
|
|
|
$this->server_url, (string)$http_response->getBody(), $http_response->getStatusCode() |
|
|
|
|
454
|
|
|
); |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|