1<?php
2
3namespace Sabre\CalDAV;
4
5use Sabre\DAV\Sharing\Plugin as SPlugin;
6
7/**
8 * This object represents a CalDAV calendar that is shared by a different user.
9 *
10 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
11 * @author Evert Pot (http://evertpot.com/)
12 * @license http://sabre.io/license/ Modified BSD License
13 */
14class SharedCalendar extends Calendar implements ISharedCalendar {
15
16    /**
17     * Returns the 'access level' for the instance of this shared resource.
18     *
19     * The value should be one of the Sabre\DAV\Sharing\Plugin::ACCESS_
20     * constants.
21     *
22     * @return int
23     */
24    function getShareAccess() {
25
26        return isset($this->calendarInfo['share-access']) ? $this->calendarInfo['share-access'] : SPlugin::ACCESS_NOTSHARED;
27
28    }
29
30    /**
31     * This function must return a URI that uniquely identifies the shared
32     * resource. This URI should be identical across instances, and is
33     * also used in several other XML bodies to connect invites to
34     * resources.
35     *
36     * This may simply be a relative reference to the original shared instance,
37     * but it could also be a urn. As long as it's a valid URI and unique.
38     *
39     * @return string
40     */
41    function getShareResourceUri() {
42
43        return $this->calendarInfo['share-resource-uri'];
44
45    }
46
47    /**
48     * Updates the list of sharees.
49     *
50     * Every item must be a Sharee object.
51     *
52     * @param \Sabre\DAV\Xml\Element\Sharee[] $sharees
53     * @return void
54     */
55    function updateInvites(array $sharees) {
56
57        $this->caldavBackend->updateInvites($this->calendarInfo['id'], $sharees);
58
59    }
60
61    /**
62     * Returns the list of people whom this resource is shared with.
63     *
64     * Every item in the returned array must be a Sharee object with
65     * at least the following properties set:
66     *
67     * * $href
68     * * $shareAccess
69     * * $inviteStatus
70     *
71     * and optionally:
72     *
73     * * $properties
74     *
75     * @return \Sabre\DAV\Xml\Element\Sharee[]
76     */
77    function getInvites() {
78
79        return $this->caldavBackend->getInvites($this->calendarInfo['id']);
80
81    }
82
83    /**
84     * Marks this calendar as published.
85     *
86     * Publishing a calendar should automatically create a read-only, public,
87     * subscribable calendar.
88     *
89     * @param bool $value
90     * @return void
91     */
92    function setPublishStatus($value) {
93
94        $this->caldavBackend->setPublishStatus($this->calendarInfo['id'], $value);
95
96    }
97
98    /**
99     * Returns a list of ACE's for this node.
100     *
101     * Each ACE has the following properties:
102     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
103     *     currently the only supported privileges
104     *   * 'principal', a url to the principal who owns the node
105     *   * 'protected' (optional), indicating that this ACE is not allowed to
106     *      be updated.
107     *
108     * @return array
109     */
110    function getACL() {
111
112        $acl = [];
113
114        switch ($this->getShareAccess()) {
115            case SPlugin::ACCESS_NOTSHARED :
116            case SPlugin::ACCESS_SHAREDOWNER :
117                $acl[] = [
118                    'privilege' => '{DAV:}share',
119                    'principal' => $this->calendarInfo['principaluri'],
120                    'protected' => true,
121                ];
122                $acl[] = [
123                    'privilege' => '{DAV:}share',
124                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
125                    'protected' => true,
126                ];
127                // No break intentional!
128            case SPlugin::ACCESS_READWRITE :
129                $acl[] = [
130                    'privilege' => '{DAV:}write',
131                    'principal' => $this->calendarInfo['principaluri'],
132                    'protected' => true,
133                ];
134                $acl[] = [
135                    'privilege' => '{DAV:}write',
136                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
137                    'protected' => true,
138                ];
139                // No break intentional!
140            case SPlugin::ACCESS_READ :
141                $acl[] = [
142                    'privilege' => '{DAV:}write-properties',
143                    'principal' => $this->calendarInfo['principaluri'],
144                    'protected' => true,
145                ];
146                $acl[] = [
147                    'privilege' => '{DAV:}write-properties',
148                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
149                    'protected' => true,
150                ];
151                $acl[] = [
152                    'privilege' => '{DAV:}read',
153                    'principal' => $this->calendarInfo['principaluri'],
154                    'protected' => true,
155                ];
156                $acl[] = [
157                    'privilege' => '{DAV:}read',
158                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-read',
159                    'protected' => true,
160                ];
161                $acl[] = [
162                    'privilege' => '{DAV:}read',
163                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
164                    'protected' => true,
165                ];
166                $acl[] = [
167                    'privilege' => '{' . Plugin::NS_CALDAV . '}read-free-busy',
168                    'principal' => '{DAV:}authenticated',
169                    'protected' => true,
170                ];
171                break;
172        }
173        return $acl;
174
175    }
176
177
178    /**
179     * This method returns the ACL's for calendar objects in this calendar.
180     * The result of this method automatically gets passed to the
181     * calendar-object nodes in the calendar.
182     *
183     * @return array
184     */
185    function getChildACL() {
186
187        $acl = [];
188
189        switch ($this->getShareAccess()) {
190            case SPlugin::ACCESS_NOTSHARED :
191                // No break intentional
192            case SPlugin::ACCESS_SHAREDOWNER :
193                // No break intentional
194            case SPlugin::ACCESS_READWRITE:
195                $acl[] = [
196                    'privilege' => '{DAV:}write',
197                    'principal' => $this->calendarInfo['principaluri'],
198                    'protected' => true,
199                ];
200                $acl[] = [
201                    'privilege' => '{DAV:}write',
202                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
203                    'protected' => true,
204                ];
205                // No break intentional
206            case SPlugin::ACCESS_READ:
207                $acl[] = [
208                    'privilege' => '{DAV:}read',
209                    'principal' => $this->calendarInfo['principaluri'],
210                    'protected' => true,
211                ];
212                $acl[] = [
213                    'privilege' => '{DAV:}read',
214                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-write',
215                    'protected' => true,
216                ];
217                $acl[] = [
218                    'privilege' => '{DAV:}read',
219                    'principal' => $this->calendarInfo['principaluri'] . '/calendar-proxy-read',
220                    'protected' => true,
221                ];
222                break;
223        }
224
225        return $acl;
226
227    }
228
229}
230