]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/CalDAV/Calendar.php
Update function names
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / CalDAV / Calendar.php
1 <?php
2
3 /**
4  * This object represents a CalDAV calendar.
5  *
6  * A calendar can contain multiple TODO and or Events. These are represented
7  * as Sabre_CalDAV_CalendarObject objects.
8  *
9  * @package Sabre
10  * @subpackage CalDAV
11  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
12  * @author Evert Pot (http://www.rooftopsolutions.nl/)
13  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
14  */
15 class Sabre_CalDAV_Calendar implements Sabre_CalDAV_ICalendar, Sabre_DAV_IProperties, Sabre_DAVACL_IACL {
16
17     /**
18      * This is an array with calendar information
19      *
20      * @var array
21      */
22     protected $calendarInfo;
23
24     /**
25      * CalDAV backend
26      *
27      * @var Sabre_CalDAV_Backend_BackendInterface
28      */
29     protected $caldavBackend;
30
31     /**
32      * Principal backend
33      *
34      * @var Sabre_DAVACL_IPrincipalBackend
35      */
36     protected $principalBackend;
37
38     /**
39      * Constructor
40      *
41      * @param Sabre_DAVACL_IPrincipalBackend $principalBackend
42      * @param Sabre_CalDAV_Backend_BackendInterface $caldavBackend
43      * @param array $calendarInfo
44      */
45     public function __construct(Sabre_DAVACL_IPrincipalBackend $principalBackend, Sabre_CalDAV_Backend_BackendInterface $caldavBackend, $calendarInfo) {
46
47         $this->caldavBackend = $caldavBackend;
48         $this->principalBackend = $principalBackend;
49         $this->calendarInfo = $calendarInfo;
50
51
52     }
53
54     /**
55      * Returns the name of the calendar
56      *
57      * @return string
58      */
59     public function getName() {
60
61         return $this->calendarInfo['uri'];
62
63     }
64
65     /**
66      * Updates properties such as the display name and description
67      *
68      * @param array $mutations
69      * @return array
70      */
71     public function updateProperties($mutations) {
72
73         return $this->caldavBackend->updateCalendar($this->calendarInfo['id'],$mutations);
74
75     }
76
77     /**
78      * Returns the list of properties
79      *
80      * @param array $requestedProperties
81      * @return array
82      */
83     public function getProperties($requestedProperties) {
84
85         $response = array();
86
87         foreach($requestedProperties as $prop) switch($prop) {
88
89             case '{urn:ietf:params:xml:ns:caldav}supported-calendar-data' :
90                 $response[$prop] = new Sabre_CalDAV_Property_SupportedCalendarData();
91                 break;
92             case '{urn:ietf:params:xml:ns:caldav}supported-collation-set' :
93                 $response[$prop] =  new Sabre_CalDAV_Property_SupportedCollationSet();
94                 break;
95             case '{DAV:}owner' :
96                 $response[$prop] = new Sabre_DAVACL_Property_Principal(Sabre_DAVACL_Property_Principal::HREF,$this->calendarInfo['principaluri']);
97                 break;
98             default :
99                 if (isset($this->calendarInfo[$prop])) $response[$prop] = $this->calendarInfo[$prop];
100                 break;
101
102         }
103         return $response;
104
105     }
106
107     /**
108      * Returns a calendar object
109      *
110      * The contained calendar objects are for example Events or Todo's.
111      *
112      * @param string $name
113      * @return Sabre_CalDAV_ICalendarObject
114      */
115     public function getChild($name) {
116
117         $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
118         if (!$obj) throw new Sabre_DAV_Exception_NotFound('Calendar object not found');
119         return new Sabre_CalDAV_CalendarObject($this->caldavBackend,$this->calendarInfo,$obj);
120
121     }
122
123     /**
124      * Returns the full list of calendar objects
125      *
126      * @return array
127      */
128     public function getChildren() {
129
130         $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
131         $children = array();
132         foreach($objs as $obj) {
133             $children[] = new Sabre_CalDAV_CalendarObject($this->caldavBackend,$this->calendarInfo,$obj);
134         }
135         return $children;
136
137     }
138
139     /**
140      * Checks if a child-node exists.
141      *
142      * @param string $name
143      * @return bool
144      */
145     public function childExists($name) {
146
147         $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'],$name);
148         if (!$obj)
149             return false;
150         else
151             return true;
152
153     }
154
155     /**
156      * Creates a new directory
157      *
158      * We actually block this, as subdirectories are not allowed in calendars.
159      *
160      * @param string $name
161      * @return void
162      */
163     public function createDirectory($name) {
164
165         throw new Sabre_DAV_Exception_MethodNotAllowed('Creating collections in calendar objects is not allowed');
166
167     }
168
169     /**
170      * Creates a new file
171      *
172      * The contents of the new file must be a valid ICalendar string.
173      *
174      * @param string $name
175      * @param resource $calendarData
176      * @return string|null
177      */
178     public function createFile($name,$calendarData = null) {
179
180         if (is_resource($calendarData)) {
181             $calendarData = stream_get_contents($calendarData);
182         }
183         return $this->caldavBackend->createCalendarObject($this->calendarInfo['id'],$name,$calendarData);
184
185     }
186
187     /**
188      * Deletes the calendar.
189      *
190      * @return void
191      */
192     public function delete() {
193
194         $this->caldavBackend->deleteCalendar($this->calendarInfo['id']);
195
196     }
197
198     /**
199      * Renames the calendar. Note that most calendars use the
200      * {DAV:}displayname to display a name to display a name.
201      *
202      * @param string $newName
203      * @return void
204      */
205     public function setName($newName) {
206
207         throw new Sabre_DAV_Exception_MethodNotAllowed('Renaming calendars is not yet supported');
208
209     }
210
211     /**
212      * Returns the last modification date as a unix timestamp.
213      *
214      * @return void
215      */
216     public function getLastModified() {
217
218         return null;
219
220     }
221
222     /**
223      * Returns the owner principal
224      *
225      * This must be a url to a principal, or null if there's no owner
226      *
227      * @return string|null
228      */
229     public function getOwner() {
230
231         return $this->calendarInfo['principaluri'];
232
233     }
234
235     /**
236      * Returns a group principal
237      *
238      * This must be a url to a principal, or null if there's no owner
239      *
240      * @return string|null
241      */
242     public function getGroup() {
243
244         return null;
245
246     }
247
248     /**
249      * Returns a list of ACE's for this node.
250      *
251      * Each ACE has the following properties:
252      *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
253      *     currently the only supported privileges
254      *   * 'principal', a url to the principal who owns the node
255      *   * 'protected' (optional), indicating that this ACE is not allowed to
256      *      be updated.
257      *
258      * @return array
259      */
260     public function getACL() {
261
262         return array(
263             array(
264                 'privilege' => '{DAV:}read',
265                 'principal' => $this->calendarInfo['principaluri'],
266                 'protected' => true,
267             ),
268             array(
269                 'privilege' => '{DAV:}write',
270                 'principal' => $this->calendarInfo['principaluri'],
271                 'protected' => true,
272             ),
273             array(
274                 'privilege' => '{DAV:}read',
275                 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
276                 'protected' => true,
277             ),
278             array(
279                 'privilege' => '{DAV:}write',
280                 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
281                 'protected' => true,
282             ),
283             array(
284                 'privilege' => '{DAV:}read',
285                 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-read',
286                 'protected' => true,
287             ),
288             array(
289                 'privilege' => '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}read-free-busy',
290                 'principal' => '{DAV:}authenticated',
291                 'protected' => true,
292             ),
293
294         );
295
296     }
297
298     /**
299      * Updates the ACL
300      *
301      * This method will receive a list of new ACE's.
302      *
303      * @param array $acl
304      * @return void
305      */
306     public function setACL(array $acl) {
307
308         throw new Sabre_DAV_Exception_MethodNotAllowed('Changing ACL is not yet supported');
309
310     }
311
312     /**
313      * Returns the list of supported privileges for this node.
314      *
315      * The returned data structure is a list of nested privileges.
316      * See Sabre_DAVACL_Plugin::getDefaultSupportedPrivilegeSet for a simple
317      * standard structure.
318      *
319      * If null is returned from this method, the default privilege set is used,
320      * which is fine for most common usecases.
321      *
322      * @return array|null
323      */
324     public function getSupportedPrivilegeSet() {
325
326         $default = Sabre_DAVACL_Plugin::getDefaultSupportedPrivilegeSet();
327
328         // We need to inject 'read-free-busy' in the tree, aggregated under
329         // {DAV:}read.
330         foreach($default['aggregates'] as &$agg) {
331
332             if ($agg['privilege'] !== '{DAV:}read') continue;
333
334             $agg['aggregates'][] = array(
335                 'privilege' => '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}read-free-busy',
336             );
337
338         }
339         return $default;
340
341     }
342
343     /**
344      * Performs a calendar-query on the contents of this calendar.
345      *
346      * The calendar-query is defined in RFC4791 : CalDAV. Using the
347      * calendar-query it is possible for a client to request a specific set of
348      * object, based on contents of iCalendar properties, date-ranges and
349      * iCalendar component types (VTODO, VEVENT).
350      *
351      * This method should just return a list of (relative) urls that match this
352      * query.
353      *
354      * The list of filters are specified as an array. The exact array is
355      * documented by Sabre_CalDAV_CalendarQueryParser.
356      *
357      * @param array $filters
358      * @return array
359      */
360     public function calendarQuery(array $filters) {
361
362         return $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
363
364     }
365
366 }