]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/CalDAV/CalendarObject.php
Merge remote branch 'friendica/master'
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / CalDAV / CalendarObject.php
1 <?php
2
3 /**
4  * The CalendarObject represents a single VEVENT or VTODO within a Calendar.
5  *
6  * @package Sabre
7  * @subpackage CalDAV
8  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
9  * @author Evert Pot (http://www.rooftopsolutions.nl/)
10  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
11  */
12 class Sabre_CalDAV_CalendarObject extends Sabre_DAV_File implements Sabre_CalDAV_ICalendarObject, Sabre_DAVACL_IACL {
13
14     /**
15      * Sabre_CalDAV_Backend_Abstract
16      *
17      * @var array
18      */
19     protected $caldavBackend;
20
21     /**
22      * Array with information about this CalendarObject
23      *
24      * @var array
25      */
26     protected $objectData;
27
28     /**
29      * Array with information about the containing calendar
30      *
31      * @var array
32      */
33     protected $calendarInfo;
34
35     /**
36      * Constructor
37      *
38      * @param Sabre_CalDAV_Backend_Abstract $caldavBackend
39      * @param array $calendarInfo
40      * @param array $objectData
41      */
42     public function __construct(Sabre_CalDAV_Backend_Abstract $caldavBackend,array $calendarInfo,array $objectData) {
43
44         $this->caldavBackend = $caldavBackend;
45
46         if (!isset($objectData['calendarid'])) {
47             throw new InvalidArgumentException('The objectData argument must contain a \'calendarid\' property');
48         }
49         if (!isset($objectData['uri'])) {
50             throw new InvalidArgumentException('The objectData argument must contain an \'uri\' property');
51         }
52
53         $this->calendarInfo = $calendarInfo;
54         $this->objectData = $objectData;
55
56     }
57
58     /**
59      * Returns the uri for this object
60      *
61      * @return string
62      */
63     public function getName() {
64
65         return $this->objectData['uri'];
66
67     }
68
69     /**
70      * Returns the ICalendar-formatted object
71      *
72      * @return string
73      */
74     public function get() {
75
76         // Pre-populating the 'calendardata' is optional, if we don't have it
77         // already we fetch it from the backend.
78         if (!isset($this->objectData['calendardata'])) {
79             $this->objectData = $this->caldavBackend->getCalendarObject($this->objectData['calendarid'], $this->objectData['uri']);
80         }
81         return $this->objectData['calendardata'];
82
83     }
84
85     /**
86      * Updates the ICalendar-formatted object
87      *
88      * @param string|resource $calendarData
89      * @return string
90      */
91     public function put($calendarData) {
92
93         if (is_resource($calendarData)) {
94             $calendarData = stream_get_contents($calendarData);
95         }
96         $etag = $this->caldavBackend->updateCalendarObject($this->calendarInfo['id'],$this->objectData['uri'],$calendarData);
97         $this->objectData['calendardata'] = $calendarData;
98         $this->objectData['etag'] = $etag;
99
100         return $etag;
101
102     }
103
104     /**
105      * Deletes the calendar object
106      *
107      * @return void
108      */
109     public function delete() {
110
111         $this->caldavBackend->deleteCalendarObject($this->calendarInfo['id'],$this->objectData['uri']);
112
113     }
114
115     /**
116      * Returns the mime content-type
117      *
118      * @return string
119      */
120     public function getContentType() {
121
122         return 'text/calendar; charset=utf-8';
123
124     }
125
126     /**
127      * Returns an ETag for this object.
128      *
129      * The ETag is an arbitrary string, but MUST be surrounded by double-quotes.
130      *
131      * @return string
132      */
133     public function getETag() {
134
135         if (isset($this->objectData['etag'])) {
136             return $this->objectData['etag'];
137         } else {
138             return '"' . md5($this->get()). '"';
139         }
140
141     }
142
143     /**
144      * Returns the last modification date as a unix timestamp
145      *
146      * @return int
147      */
148     public function getLastModified() {
149
150         return $this->objectData['lastmodified'];
151
152     }
153
154     /**
155      * Returns the size of this object in bytes
156      *
157      * @return int
158      */
159     public function getSize() {
160
161         if (array_key_exists('size',$this->objectData)) {
162             return $this->objectData['size'];
163         } else {
164             return strlen($this->get());
165         }
166
167     }
168
169     /**
170      * Returns the owner principal
171      *
172      * This must be a url to a principal, or null if there's no owner
173      *
174      * @return string|null
175      */
176     public function getOwner() {
177
178         return $this->calendarInfo['principaluri'];
179
180     }
181
182     /**
183      * Returns a group principal
184      *
185      * This must be a url to a principal, or null if there's no owner
186      *
187      * @return string|null
188      */
189     public function getGroup() {
190
191         return null;
192
193     }
194
195     /**
196      * Returns a list of ACE's for this node.
197      *
198      * Each ACE has the following properties:
199      *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
200      *     currently the only supported privileges
201      *   * 'principal', a url to the principal who owns the node
202      *   * 'protected' (optional), indicating that this ACE is not allowed to
203      *      be updated.
204      *
205      * @return array
206      */
207     public function getACL() {
208
209         return array(
210             array(
211                 'privilege' => '{DAV:}read',
212                 'principal' => $this->calendarInfo['principaluri'],
213                 'protected' => true,
214             ),
215             array(
216                 'privilege' => '{DAV:}write',
217                 'principal' => $this->calendarInfo['principaluri'],
218                 'protected' => true,
219             ),
220             array(
221                 'privilege' => '{DAV:}read',
222                 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
223                 'protected' => true,
224             ),
225             array(
226                 'privilege' => '{DAV:}write',
227                 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
228                 'protected' => true,
229             ),
230             array(
231                 'privilege' => '{DAV:}read',
232                 'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-read',
233                 'protected' => true,
234             ),
235
236         );
237
238     }
239
240     /**
241      * Updates the ACL
242      *
243      * This method will receive a list of new ACE's.
244      *
245      * @param array $acl
246      * @return void
247      */
248     public function setACL(array $acl) {
249
250         throw new Sabre_DAV_Exception_MethodNotAllowed('Changing ACL is not yet supported');
251
252     }
253
254     /**
255      * Returns the list of supported privileges for this node.
256      *
257      * The returned data structure is a list of nested privileges.
258      * See Sabre_DAVACL_Plugin::getDefaultSupportedPrivilegeSet for a simple
259      * standard structure.
260      *
261      * If null is returned from this method, the default privilege set is used,
262      * which is fine for most common usecases.
263      *
264      * @return array|null
265      */
266     public function getSupportedPrivilegeSet() {
267
268         return null;
269
270     }
271
272 }
273