-
Notifications
You must be signed in to change notification settings - Fork 60
/
SimpleCalDAVClient.php
415 lines (358 loc) · 15.1 KB
/
SimpleCalDAVClient.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
* SimpleCalDAVClient
*
* Copyright 2014 Michael Palm <[email protected]>
*
* simpleCalDAV is a php library that allows you to connect to a calDAV-server to get event-, todo-
* and free/busy-calendar resources from the server, to change them, to delete them, to create new ones, etc.
* simpleCalDAV was made and tested for connections to the CalDAV-server Baikal 0.2.7. But it should work
* with any other CalDAV-server too.
*
* It contains the following functions:
* - connect()
* - findCalendars()
* - setCalendar()
* - create()
* - change()
* - delete()
* - getEvents()
* - getTODOs()
* - getCustomReport()
*
* All of those functions - except the last one - are realy easy to use, self-explanatory and are
* deliverd with a big innitial comment, which explains all needed arguments and the return values.
*
* This library is heavily based on AgenDAV caldav-client-v2.php by Jorge López Pérez <[email protected]> which
* again is heavily based on DAViCal caldav-client-v2.php by Andrew McMillan <[email protected]>.
* Actually, I hardly added any features. The main point of my work is to make everything straight
* forward and easy to use. You can use simpleCalDAV whithout a deeper understanding of the
* calDAV-protocol.
*
* Requirements of this library are
* - The php extension cURL ( http://www.php.net/manual/en/book.curl.php )
* - From Andrew’s Web Libraries: ( https://github.com/andrews-web-libraries/awl )
* - XMLDocument.php
* - XMLElement.php
* - AWLUtilities.php
*
* @package simpleCalDAV
*/
require_once('CalDAVClient.php');
require_once('CalDAVException.php');
require_once('CalDAVFilter.php');
require_once('CalDAVObject.php');
class SimpleCalDAVClient {
private $client;
private $url;
/**
* function connect()
* Connects to a CalDAV-Server.
*
* Arguments:
* @param $url URL to the CalDAV-server. E.g. http://exam.pl/baikal/cal.php/username/calendername/
* @param $user Username to login with
* @param $pass Password to login with
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); }
*/
function connect ( $url, $user, $pass )
{
// Connect to CalDAV-Server and log in
$client = new CalDAVClient($url, $user, $pass);
// Valid CalDAV-Server? Or is it just a WebDAV-Server?
if( ! $client->isValidCalDAVServer() )
{
if( $client->GetHttpResultCode() == '401' ) // unauthorisized
{
throw new CalDAVException('Login failed', $client);
}
elseif( $client->GetHttpResultCode() == '' ) // can't reach server
{
throw new CalDAVException('Can\'t reach server', $client);
}
else throw new CalDAVException('Could\'n find a CalDAV-collection under the url', $client);
}
// Check for errors
if( $client->GetHttpResultCode() != '200' ) {
if( $client->GetHttpResultCode() == '401' ) // unauthorisized
{
throw new CalDAVException('Login failed', $client);
}
elseif( $client->GetHttpResultCode() == '' ) // can't reach server
{
throw new CalDAVException('Can\'t reach server', $client);
}
else // Unknown status
{
throw new CalDAVException('Recieved unknown HTTP status while checking the connection after establishing it', $client);
}
}
$this->client = $client;
}
/**
* function findCalendars()
*
* Requests a list of all accessible calendars on the server
*
* Return value:
* @return an array of CalDAVCalendar-Objects (see CalDAVCalendar.php), representing all calendars accessible by the current principal (user).
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function findCalendars()
{
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
return $this->client->FindCalendars(true);
}
/**
* function setCalendar()
*
* Sets the actual calendar to work with
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function setCalendar ( CalDAVCalendar $calendar )
{
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
$this->client->SetCalendar($this->client->first_url_part.$calendar->getURL());
// Is there a '/' at the end of the calendar_url?
if ( ! preg_match( '#^.*?/$#', $this->client->calendar_url, $matches ) ) { $this->url = $this->client->calendar_url.'/'; }
else { $this->url = $this->client->calendar_url; }
}
/**
* function create()
* Creates a new calendar resource on the CalDAV-Server (event, todo, etc.).
*
* Arguments:
* @param $cal iCalendar-data of the resource you want to create.
* Notice: The iCalendar-data contains the unique ID which specifies where the event is being saved.
*
* Return value:
* @return An CalDAVObject-representation (see CalDAVObject.php) of your created resource
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function create ( $cal )
{
// Connection and calendar set?
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
if(!isset($this->client->calendar_url)) throw new Exception('No calendar selected. Try findCalendars() and setCalendar().');
// Parse $cal for UID
if (! preg_match( '#^UID:(.*?)\r?\n?$#m', $cal, $matches ) ) { throw new Exception('Can\'t find UID in $cal'); }
else { $uid = $matches[1]; }
// Does $this->url.$uid.'.ics' already exist?
$result = $this->client->GetEntryByHref( $this->url.$uid.'.ics' );
if ( $this->client->GetHttpResultCode() == '200' ) { throw new CalDAVException($this->url.$uid.'.ics already exists. UID not unique?', $this->client); }
else if ( $this->client->GetHttpResultCode() == '404' );
else throw new CalDAVException('Recieved unknown HTTP status', $this->client);
// Put it!
$newEtag = $this->client->DoPUTRequest( $this->url.$uid.'.ics', $cal );
// PUT-request successfull?
if ( $this->client->GetHttpResultCode() != '201' )
{
if ( $this->client->GetHttpResultCode() == '204' ) // $url.$uid.'.ics' already existed on server
{
throw new CalDAVException( $this->url.$uid.'.ics already existed. Entry has been overwritten.', $this->client);
}
else // Unknown status
{
throw new CalDAVException('Recieved unknown HTTP status', $this->client);
}
}
return new CalDAVObject($this->url.$uid.'.ics', $cal, $newEtag);
}
/**
* function change()
* Changes a calendar resource (event, todo, etc.) on the CalDAV-Server.
*
* Arguments:
* @param $href See CalDAVObject.php
* @param $cal The new iCalendar-data that should be used to overwrite the old one.
* @param $etag See CalDAVObject.php
*
* Return value:
* @return An CalDAVObject-representation (see CalDAVObject.php) of your changed resource
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function change ( $href, $new_data, $etag )
{
// Connection and calendar set?
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
if(!isset($this->client->calendar_url)) throw new Exception('No calendar selected. Try findCalendars() and setCalendar().');
// Does $href exist?
$result = $this->client->GetEntryByHref($href);
if ( $this->client->GetHttpResultCode() == '200' );
else if ( $this->client->GetHttpResultCode() == '404' ) throw new CalDAVException('Can\'t find '.$href.' on the server', $this->client);
else throw new CalDAVException('Recieved unknown HTTP status', $this->client);
// $etag correct?
if($result[0]['etag'] != $etag) { throw new CalDAVException('Wrong entity tag. The entity seems to have changed.', $this->client); }
// Put it!
$newEtag = $this->client->DoPUTRequest( $href, $new_data, $etag );
// PUT-request successfull?
if ( $this->client->GetHttpResultCode() != '204' && $this->client->GetHttpResultCode() != '200' )
{
throw new CalDAVException('Recieved unknown HTTP status', $this->client);
}
return new CalDAVObject($href, $new_data, $newEtag);
}
/**
* function delete()
* Delets an event or a TODO from the CalDAV-Server.
*
* Arguments:
* @param $href See CalDAVObject.php
* @param $etag See CalDAVObject.php
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function delete ( $href, $etag )
{
// Connection and calendar set?
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
if(!isset($this->client->calendar_url)) throw new Exception('No calendar selected. Try findCalendars() and setCalendar().');
// Does $href exist?
$result = $this->client->GetEntryByHref($href);
if(count($result) == 0) throw new CalDAVException('Can\'t find '.$href.'on server', $this->client);
// $etag correct?
if($result[0]['etag'] != $etag) { throw new CalDAVException('Wrong entity tag. The entity seems to have changed.', $this->client); }
// Do the deletion
$this->client->DoDELETERequest($href, $etag);
// Deletion successfull?
if ( $this->client->GetHttpResultCode() != '200' and $this->client->GetHttpResultCode() != '204' )
{
throw new CalDAVException('Recieved unknown HTTP status', $this->client);
}
}
/**
* function getEvents()
* Gets a all events from the CalDAV-Server which lie in a defined time interval.
*
* Arguments:
* @param $start The starting point of the time interval. Must be in the format yyyymmddThhmmssZ and should be in
* GMT. If omitted the value is set to -infinity.
* @param $end The end point of the time interval. Must be in the format yyyymmddThhmmssZ and should be in
* GMT. If omitted the value is set to +infinity.
*
* Return value:
* @return an array of CalDAVObjects (See CalDAVObject.php), representing the found events.
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function getEvents ( $start = null, $end = null )
{
// Connection and calendar set?
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
if(!isset($this->client->calendar_url)) throw new Exception('No calendar selected. Try findCalendars() and setCalendar().');
// Are $start and $end in the correct format?
if ( ( isset($start) and ! preg_match( '#^\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ$#', $start, $matches ) )
or ( isset($end) and ! preg_match( '#^\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ$#', $end, $matches ) ) )
{ trigger_error('$start or $end are in the wrong format. They must have the format yyyymmddThhmmssZ and should be in GMT', E_USER_ERROR); }
// Get it!
$results = $this->client->GetEvents( $start, $end );
// GET-request successfull?
if ( $this->client->GetHttpResultCode() != '207' )
{
throw new CalDAVException('Recieved unknown HTTP status', $this->client);
}
// Reformat
$report = array();
foreach($results as $event) $report[] = new CalDAVObject($this->url.$event['href'], $event['data'], $event['etag']);
return $report;
}
/**
* function getTODOs()
* Gets a all TODOs from the CalDAV-Server which lie in a defined time interval and match the
* given criteria.
*
* Arguments:
* @param $start The starting point of the time interval. Must be in the format yyyymmddThhmmssZ and should be in
* GMT. If omitted the value is set to -infinity.
* @param $end The end point of the time interval. Must be in the format yyyymmddThhmmssZ and should be in
* GMT. If omitted the value is set to +infinity.
* @param $complete Filter for completed tasks (true) or for uncompleted tasks (false). If omitted, the function will return both.
* @param $cancelled Filter for cancelled tasks (true) or for uncancelled tasks (false). If omitted, the function will return both.
*
* Return value:
* @return an array of CalDAVObjects (See CalDAVObject.php), representing the found TODOs.
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function getTODOs ( $start = null, $end = null, $completed = null, $cancelled = null )
{
// Connection and calendar set?
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
if(!isset($this->client->calendar_url)) throw new Exception('No calendar selected. Try findCalendars() and setCalendar().');
// Are $start and $end in the correct format?
if ( ( isset($start) and ! preg_match( '#^\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ$#', $start, $matches ) )
or ( isset($end) and ! preg_match( '#^\d\d\d\d\d\d\d\dT\d\d\d\d\d\dZ$#', $end, $matches ) ) )
{ trigger_error('$start or $end are in the wrong format. They must have the format yyyymmddThhmmssZ and should be in GMT', E_USER_ERROR); }
// Get it!
$results = $this->client->GetTodos( $start, $end, $completed, $cancelled );
// GET-request successfull?
if ( $this->client->GetHttpResultCode() != '207' )
{
throw new CalDAVException('Recieved unknown HTTP status', $this->client);
}
// Reformat
$report = array();
foreach($results as $event) $report[] = new CalDAVObject($this->url.$event['href'], $event['data'], $event['etag']);
return $report;
}
/**
* function getCustomReport()
* Sends a custom request to the server
* (Sends a REPORT-request with a custom <C:filter>-tag)
*
* You can either write the filterXML yourself or build an CalDAVFilter-object (see CalDAVFilter.php).
*
* See http://www.rfcreader.com/#rfc4791_line1524 for more information about how to write filters on your own.
*
* Arguments:
* @param $filterXML The stuff, you want to send encapsulated in the <C:filter>-tag.
*
* Return value:
* @return an array of CalDAVObjects (See CalDAVObject.php), representing the found calendar resources.
*
* Debugging:
* @throws CalDAVException
* For debugging purposes, just sorround everything with try { ... } catch (Exception $e) { echo $e->__toString(); exit(-1); }
*/
function getCustomReport ( $filterXML )
{
// Connection and calendar set?
if(!isset($this->client)) throw new Exception('No connection. Try connect().');
if(!isset($this->client->calendar_url)) throw new Exception('No calendar selected. Try findCalendars() and setCalendar().');
// Get report!
$this->client->SetDepth('1');
// Get it!
$results = $this->client->DoCalendarQuery('<C:filter>'.$filterXML.'</C:filter>');
// GET-request successfull?
if ( $this->client->GetHttpResultCode() != '207' )
{
throw new CalDAVException('Recieved unknown HTTP status', $this->client);
}
// Reformat
$report = array();
foreach($results as $event) $report[] = new CalDAVObject($this->url.$event['href'], $event['data'], $event['etag']);
return $report;
}
}
?>