1<?php
2
3namespace Sabre\CalDAV\Schedule;
4
5use Sabre\DAV;
6use Sabre\CalDAV;
7use Sabre\DAVACL;
8use Sabre\CalDAV\Backend;
9use Sabre\VObject;
10
11/**
12 * The CalDAV scheduling inbox
13 *
14 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
15 * @author Evert Pot (http://evertpot.com/)
16 * @license http://sabre.io/license/ Modified BSD License
17 */
18class Inbox extends DAV\Collection implements IInbox {
19
20    /**
21     * CalDAV backend
22     *
23     * @var Backend\BackendInterface
24     */
25    protected $caldavBackend;
26
27    /**
28     * The principal Uri
29     *
30     * @var string
31     */
32    protected $principalUri;
33
34    /**
35     * Constructor
36     *
37     * @param Backend\SchedulingSupport $caldavBackend
38     * @param string $principalUri
39     */
40    function __construct(Backend\SchedulingSupport $caldavBackend, $principalUri) {
41
42        $this->caldavBackend = $caldavBackend;
43        $this->principalUri = $principalUri;
44
45    }
46
47    /**
48     * Returns the name of the node.
49     *
50     * This is used to generate the url.
51     *
52     * @return string
53     */
54    function getName() {
55
56        return 'inbox';
57
58    }
59
60    /**
61     * Returns an array with all the child nodes
62     *
63     * @return \Sabre\DAV\INode[]
64     */
65    function getChildren() {
66
67        $objs = $this->caldavBackend->getSchedulingObjects($this->principalUri);
68        $children = [];
69        foreach ($objs as $obj) {
70            //$obj['acl'] = $this->getACL();
71            $obj['principaluri'] = $this->principalUri;
72            $children[] = new SchedulingObject($this->caldavBackend, $obj);
73        }
74        return $children;
75
76    }
77
78    /**
79     * Creates a new file in the directory
80     *
81     * Data will either be supplied as a stream resource, or in certain cases
82     * as a string. Keep in mind that you may have to support either.
83     *
84     * After succesful creation of the file, you may choose to return the ETag
85     * of the new file here.
86     *
87     * The returned ETag must be surrounded by double-quotes (The quotes should
88     * be part of the actual string).
89     *
90     * If you cannot accurately determine the ETag, you should not return it.
91     * If you don't store the file exactly as-is (you're transforming it
92     * somehow) you should also not return an ETag.
93     *
94     * This means that if a subsequent GET to this new file does not exactly
95     * return the same contents of what was submitted here, you are strongly
96     * recommended to omit the ETag.
97     *
98     * @param string $name Name of the file
99     * @param resource|string $data Initial payload
100     * @return null|string
101     */
102    function createFile($name, $data = null) {
103
104        $this->caldavBackend->createSchedulingObject($this->principalUri, $name, $data);
105
106    }
107
108    /**
109     * Returns the owner principal
110     *
111     * This must be a url to a principal, or null if there's no owner
112     *
113     * @return string|null
114     */
115    function getOwner() {
116
117        return $this->principalUri;
118
119    }
120
121    /**
122     * Returns a group principal
123     *
124     * This must be a url to a principal, or null if there's no owner
125     *
126     * @return string|null
127     */
128    function getGroup() {
129
130        return null;
131
132    }
133
134    /**
135     * Returns a list of ACE's for this node.
136     *
137     * Each ACE has the following properties:
138     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
139     *     currently the only supported privileges
140     *   * 'principal', a url to the principal who owns the node
141     *   * 'protected' (optional), indicating that this ACE is not allowed to
142     *      be updated.
143     *
144     * @return array
145     */
146    function getACL() {
147
148        return [
149            [
150                'privilege' => '{DAV:}read',
151                'principal' => $this->getOwner(),
152                'protected' => true,
153            ],
154            [
155                'privilege' => '{DAV:}write-properties',
156                'principal' => $this->getOwner(),
157                'protected' => true,
158            ],
159            [
160                'privilege' => '{DAV:}unbind',
161                'principal' => $this->getOwner(),
162                'protected' => true,
163            ],
164            [
165                'privilege' => '{DAV:}read',
166                'principal' => $this->getOwner() . '/calendar-proxy-read',
167                'protected' => true,
168            ],
169            [
170                'privilege' => '{DAV:}read',
171                'principal' => $this->getOwner() . '/calendar-proxy-write',
172                'protected' => true,
173            ],
174            [
175                'privilege' => '{DAV:}unbind',
176                'principal' => $this->getOwner() . '/calendar-proxy-write',
177                'protected' => true,
178            ],
179            [
180                'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-deliver-invite',
181                'principal' => '{DAV:}authenticated',
182                'protected' => true,
183            ],
184            [
185                'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-deliver-reply',
186                'principal' => '{DAV:}authenticated',
187                'protected' => true,
188            ],
189        ];
190
191    }
192
193    /**
194     * Updates the ACL
195     *
196     * This method will receive a list of new ACE's.
197     *
198     * @param array $acl
199     * @return void
200     */
201    function setACL(array $acl) {
202
203        throw new DAV\Exception\MethodNotAllowed('You\'re not allowed to update the ACL');
204
205    }
206
207    /**
208     * Returns the list of supported privileges for this node.
209     *
210     * The returned data structure is a list of nested privileges.
211     * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
212     * standard structure.
213     *
214     * If null is returned from this method, the default privilege set is used,
215     * which is fine for most common usecases.
216     *
217     * @return array|null
218     */
219    function getSupportedPrivilegeSet() {
220
221        $ns = '{' . CalDAV\Plugin::NS_CALDAV . '}';
222
223        $default = DAVACL\Plugin::getDefaultSupportedPrivilegeSet();
224        $default['aggregates'][] = [
225            'privilege'  => $ns . 'schedule-deliver',
226            'aggregates' => [
227               ['privilege' => $ns . 'schedule-deliver-invite'],
228               ['privilege' => $ns . 'schedule-deliver-reply'],
229            ],
230        ];
231        return $default;
232
233    }
234
235    /**
236     * Performs a calendar-query on the contents of this calendar.
237     *
238     * The calendar-query is defined in RFC4791 : CalDAV. Using the
239     * calendar-query it is possible for a client to request a specific set of
240     * object, based on contents of iCalendar properties, date-ranges and
241     * iCalendar component types (VTODO, VEVENT).
242     *
243     * This method should just return a list of (relative) urls that match this
244     * query.
245     *
246     * The list of filters are specified as an array. The exact array is
247     * documented by \Sabre\CalDAV\CalendarQueryParser.
248     *
249     * @param array $filters
250     * @return array
251     */
252    function calendarQuery(array $filters) {
253
254        $result = [];
255        $validator = new CalDAV\CalendarQueryValidator();
256
257        $objects = $this->caldavBackend->getSchedulingObjects($this->principalUri);
258        foreach ($objects as $object) {
259            $vObject = VObject\Reader::read($object['calendardata']);
260            if ($validator->validate($vObject, $filters)) {
261                $result[] = $object['uri'];
262            }
263        }
264        return $result;
265
266    }
267
268}
269