1<?php
2
3namespace Sabre\CalDAV\Subscriptions;
4
5use Sabre\CalDAV\Backend\SubscriptionSupport;
6use Sabre\DAV\Collection;
7use Sabre\DAV\PropPatch;
8use Sabre\DAV\Xml\Property\Href;
9use Sabre\DAVACL\ACLTrait;
10use Sabre\DAVACL\IACL;
11
12/**
13 * Subscription Node
14 *
15 * This node represents a subscription.
16 *
17 * @copyright Copyright (C) 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    use ACLTrait;
24
25    /**
26     * caldavBackend
27     *
28     * @var SubscriptionSupport
29     */
30    protected $caldavBackend;
31
32    /**
33     * subscriptionInfo
34     *
35     * @var array
36     */
37    protected $subscriptionInfo;
38
39    /**
40     * Constructor
41     *
42     * @param SubscriptionSupport $caldavBackend
43     * @param array $subscriptionInfo
44     */
45    function __construct(SubscriptionSupport $caldavBackend, array $subscriptionInfo) {
46
47        $this->caldavBackend = $caldavBackend;
48        $this->subscriptionInfo = $subscriptionInfo;
49
50        $required = [
51            'id',
52            'uri',
53            'principaluri',
54            'source',
55            ];
56
57        foreach ($required as $r) {
58            if (!isset($subscriptionInfo[$r])) {
59                throw new \InvalidArgumentException('The ' . $r . ' field is required when creating a subscription node');
60            }
61        }
62
63    }
64
65    /**
66     * Returns the name of the node.
67     *
68     * This is used to generate the url.
69     *
70     * @return string
71     */
72    function getName() {
73
74        return $this->subscriptionInfo['uri'];
75
76    }
77
78    /**
79     * Returns the last modification time
80     *
81     * @return int
82     */
83    function getLastModified() {
84
85        if (isset($this->subscriptionInfo['lastmodified'])) {
86            return $this->subscriptionInfo['lastmodified'];
87        }
88
89    }
90
91    /**
92     * Deletes the current node
93     *
94     * @return void
95     */
96    function delete() {
97
98        $this->caldavBackend->deleteSubscription(
99            $this->subscriptionInfo['id']
100        );
101
102    }
103
104    /**
105     * Returns an array with all the child nodes
106     *
107     * @return \Sabre\DAV\INode[]
108     */
109    function getChildren() {
110
111        return [];
112
113    }
114
115    /**
116     * Updates properties on this node.
117     *
118     * This method received a PropPatch object, which contains all the
119     * information about the update.
120     *
121     * To update specific properties, call the 'handle' method on this object.
122     * Read the PropPatch documentation for more information.
123     *
124     * @param PropPatch $propPatch
125     * @return void
126     */
127    function propPatch(PropPatch $propPatch) {
128
129        return $this->caldavBackend->updateSubscription(
130            $this->subscriptionInfo['id'],
131            $propPatch
132        );
133
134    }
135
136    /**
137     * Returns a list of properties for this nodes.
138     *
139     * The properties list is a list of propertynames the client requested,
140     * encoded in clark-notation {xmlnamespace}tagname.
141     *
142     * If the array is empty, it means 'all properties' were requested.
143     *
144     * Note that it's fine to liberally give properties back, instead of
145     * conforming to the list of requested properties.
146     * The Server class will filter out the extra.
147     *
148     * @param array $properties
149     * @return array
150     */
151    function getProperties($properties) {
152
153        $r = [];
154
155        foreach ($properties as $prop) {
156
157            switch ($prop) {
158                case '{http://calendarserver.org/ns/}source' :
159                    $r[$prop] = new Href($this->subscriptionInfo['source']);
160                    break;
161                default :
162                    if (array_key_exists($prop, $this->subscriptionInfo)) {
163                        $r[$prop] = $this->subscriptionInfo[$prop];
164                    }
165                    break;
166            }
167
168        }
169
170        return $r;
171
172    }
173
174    /**
175     * Returns the owner principal.
176     *
177     * This must be a url to a principal, or null if there's no owner
178     *
179     * @return string|null
180     */
181    function getOwner() {
182
183        return $this->subscriptionInfo['principaluri'];
184
185    }
186
187    /**
188     * Returns a list of ACE's for this node.
189     *
190     * Each ACE has the following properties:
191     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
192     *     currently the only supported privileges
193     *   * 'principal', a url to the principal who owns the node
194     *   * 'protected' (optional), indicating that this ACE is not allowed to
195     *      be updated.
196     *
197     * @return array
198     */
199    function getACL() {
200
201        return [
202            [
203                'privilege' => '{DAV:}all',
204                'principal' => $this->getOwner(),
205                'protected' => true,
206            ],
207            [
208                'privilege' => '{DAV:}all',
209                'principal' => $this->getOwner() . '/calendar-proxy-write',
210                'protected' => true,
211            ],
212            [
213                'privilege' => '{DAV:}read',
214                'principal' => $this->getOwner() . '/calendar-proxy-read',
215                'protected' => true,
216            ]
217        ];
218
219    }
220
221}
222