1<?php
2
3namespace Sabre\CalDAV\Subscriptions;
4
5use Sabre\DAV\Collection;
6use Sabre\DAV\Xml\Property\Href;
7use Sabre\DAV\PropPatch;
8use Sabre\DAV\Exception\MethodNotAllowed;
9use Sabre\DAVACL\IACL;
10use Sabre\CalDAV\Backend\SubscriptionSupport;
11
12/**
13 * Subscription Node
14 *
15 * This node represents a subscription.
16 *
17 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
18 * @author Evert Pot (http://evertpot.com/)
19 * @license http://sabre.io/license/ Modified BSD License
20 */
21class Subscription extends Collection implements ISubscription, IACL {
22
23    /**
24     * caldavBackend
25     *
26     * @var SupportsSubscriptions
27     */
28    protected $caldavBackend;
29
30    /**
31     * subscriptionInfo
32     *
33     * @var array
34     */
35    protected $subscriptionInfo;
36
37    /**
38     * Constructor
39     *
40     * @param SubscriptionSupport $caldavBackend
41     * @param array $calendarInfo
42     */
43    function __construct(SubscriptionSupport $caldavBackend, array $subscriptionInfo) {
44
45        $this->caldavBackend = $caldavBackend;
46        $this->subscriptionInfo = $subscriptionInfo;
47
48        $required = [
49            'id',
50            'uri',
51            'principaluri',
52            'source',
53            ];
54
55        foreach ($required as $r) {
56            if (!isset($subscriptionInfo[$r])) {
57                throw new \InvalidArgumentException('The ' . $r . ' field is required when creating a subscription node');
58            }
59        }
60
61    }
62
63    /**
64     * Returns the name of the node.
65     *
66     * This is used to generate the url.
67     *
68     * @return string
69     */
70    function getName() {
71
72        return $this->subscriptionInfo['uri'];
73
74    }
75
76    /**
77     * Returns the last modification time
78     *
79     * @return int
80     */
81    function getLastModified() {
82
83        if (isset($this->subscriptionInfo['lastmodified'])) {
84            return $this->subscriptionInfo['lastmodified'];
85        }
86
87    }
88
89    /**
90     * Deletes the current node
91     *
92     * @return void
93     */
94    function delete() {
95
96        $this->caldavBackend->deleteSubscription(
97            $this->subscriptionInfo['id']
98        );
99
100    }
101
102    /**
103     * Returns an array with all the child nodes
104     *
105     * @return DAV\INode[]
106     */
107    function getChildren() {
108
109        return [];
110
111    }
112
113    /**
114     * Updates properties on this node.
115     *
116     * This method received a PropPatch object, which contains all the
117     * information about the update.
118     *
119     * To update specific properties, call the 'handle' method on this object.
120     * Read the PropPatch documentation for more information.
121     *
122     * @param PropPatch $propPatch
123     * @return void
124     */
125    function propPatch(PropPatch $propPatch) {
126
127        return $this->caldavBackend->updateSubscription(
128            $this->subscriptionInfo['id'],
129            $propPatch
130        );
131
132    }
133
134    /**
135     * Returns a list of properties for this nodes.
136     *
137     * The properties list is a list of propertynames the client requested,
138     * encoded in clark-notation {xmlnamespace}tagname.
139     *
140     * If the array is empty, it means 'all properties' were requested.
141     *
142     * Note that it's fine to liberally give properties back, instead of
143     * conforming to the list of requested properties.
144     * The Server class will filter out the extra.
145     *
146     * @param array $properties
147     * @return void
148     */
149    function getProperties($properties) {
150
151        $r = [];
152
153        foreach ($properties as $prop) {
154
155            switch ($prop) {
156                case '{http://calendarserver.org/ns/}source' :
157                    $r[$prop] = new Href($this->subscriptionInfo['source'], false);
158                    break;
159                default :
160                    if (array_key_exists($prop, $this->subscriptionInfo)) {
161                        $r[$prop] = $this->subscriptionInfo[$prop];
162                    }
163                    break;
164            }
165
166        }
167
168        return $r;
169
170    }
171
172    /**
173     * Returns the owner principal.
174     *
175     * This must be a url to a principal, or null if there's no owner
176     *
177     * @return string|null
178     */
179    function getOwner() {
180
181        return $this->subscriptionInfo['principaluri'];
182
183    }
184
185    /**
186     * Returns a group principal.
187     *
188     * This must be a url to a principal, or null if there's no owner
189     *
190     * @return string|null
191     */
192    function getGroup() {
193
194        return null;
195
196    }
197
198    /**
199     * Returns a list of ACE's for this node.
200     *
201     * Each ACE has the following properties:
202     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
203     *     currently the only supported privileges
204     *   * 'principal', a url to the principal who owns the node
205     *   * 'protected' (optional), indicating that this ACE is not allowed to
206     *      be updated.
207     *
208     * @return array
209     */
210    function getACL() {
211
212        return [
213            [
214                'privilege' => '{DAV:}read',
215                'principal' => $this->getOwner(),
216                'protected' => true,
217            ],
218            [
219                'privilege' => '{DAV:}write',
220                'principal' => $this->getOwner(),
221                'protected' => true,
222            ],
223            [
224                'privilege' => '{DAV:}read',
225                'principal' => $this->getOwner() . '/calendar-proxy-write',
226                'protected' => true,
227            ],
228            [
229                'privilege' => '{DAV:}write',
230                'principal' => $this->getOwner() . '/calendar-proxy-write',
231                'protected' => true,
232            ],
233            [
234                'privilege' => '{DAV:}read',
235                'principal' => $this->getOwner() . '/calendar-proxy-read',
236                'protected' => true,
237            ]
238        ];
239
240    }
241
242    /**
243     * Updates the ACL.
244     *
245     * This method will receive a list of new ACE's.
246     *
247     * @param array $acl
248     * @return void
249     */
250    function setACL(array $acl) {
251
252        throw new MethodNotAllowed('Changing ACL is not yet supported');
253
254    }
255
256    /**
257     * Returns the list of supported privileges for this node.
258     *
259     * The returned data structure is a list of nested privileges.
260     * See \Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
261     * standard structure.
262     *
263     * If null is returned from this method, the default privilege set is used,
264     * which is fine for most common usecases.
265     *
266     * @return array|null
267     */
268    function getSupportedPrivilegeSet() {
269
270        return null;
271
272    }
273
274}
275