1<?php
2
3namespace Sabre\CalDAV\Schedule;
4
5use Sabre\CalDAV;
6use Sabre\CalDAV\Backend;
7use Sabre\DAV;
8use Sabre\DAVACL;
9use Sabre\VObject;
10
11/**
12 * The CalDAV scheduling inbox
13 *
14 * @copyright Copyright (C) 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    use DAVACL\ACLTrait;
21
22    /**
23     * CalDAV backend
24     *
25     * @var Backend\BackendInterface
26     */
27    protected $caldavBackend;
28
29    /**
30     * The principal Uri
31     *
32     * @var string
33     */
34    protected $principalUri;
35
36    /**
37     * Constructor
38     *
39     * @param Backend\SchedulingSupport $caldavBackend
40     * @param string $principalUri
41     */
42    function __construct(Backend\SchedulingSupport $caldavBackend, $principalUri) {
43
44        $this->caldavBackend = $caldavBackend;
45        $this->principalUri = $principalUri;
46
47    }
48
49    /**
50     * Returns the name of the node.
51     *
52     * This is used to generate the url.
53     *
54     * @return string
55     */
56    function getName() {
57
58        return 'inbox';
59
60    }
61
62    /**
63     * Returns an array with all the child nodes
64     *
65     * @return \Sabre\DAV\INode[]
66     */
67    function getChildren() {
68
69        $objs = $this->caldavBackend->getSchedulingObjects($this->principalUri);
70        $children = [];
71        foreach ($objs as $obj) {
72            //$obj['acl'] = $this->getACL();
73            $obj['principaluri'] = $this->principalUri;
74            $children[] = new SchedulingObject($this->caldavBackend, $obj);
75        }
76        return $children;
77
78    }
79
80    /**
81     * Creates a new file in the directory
82     *
83     * Data will either be supplied as a stream resource, or in certain cases
84     * as a string. Keep in mind that you may have to support either.
85     *
86     * After successful creation of the file, you may choose to return the ETag
87     * of the new file here.
88     *
89     * The returned ETag must be surrounded by double-quotes (The quotes should
90     * be part of the actual string).
91     *
92     * If you cannot accurately determine the ETag, you should not return it.
93     * If you don't store the file exactly as-is (you're transforming it
94     * somehow) you should also not return an ETag.
95     *
96     * This means that if a subsequent GET to this new file does not exactly
97     * return the same contents of what was submitted here, you are strongly
98     * recommended to omit the ETag.
99     *
100     * @param string $name Name of the file
101     * @param resource|string $data Initial payload
102     * @return null|string
103     */
104    function createFile($name, $data = null) {
105
106        $this->caldavBackend->createSchedulingObject($this->principalUri, $name, $data);
107
108    }
109
110    /**
111     * Returns the owner principal
112     *
113     * This must be a url to a principal, or null if there's no owner
114     *
115     * @return string|null
116     */
117    function getOwner() {
118
119        return $this->principalUri;
120
121    }
122
123    /**
124     * Returns a list of ACE's for this node.
125     *
126     * Each ACE has the following properties:
127     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
128     *     currently the only supported privileges
129     *   * 'principal', a url to the principal who owns the node
130     *   * 'protected' (optional), indicating that this ACE is not allowed to
131     *      be updated.
132     *
133     * @return array
134     */
135    function getACL() {
136
137        return [
138            [
139                'privilege' => '{DAV:}read',
140                'principal' => '{DAV:}authenticated',
141                'protected' => true,
142            ],
143            [
144                'privilege' => '{DAV:}write-properties',
145                'principal' => $this->getOwner(),
146                'protected' => true,
147            ],
148            [
149                'privilege' => '{DAV:}unbind',
150                'principal' => $this->getOwner(),
151                'protected' => true,
152            ],
153            [
154                'privilege' => '{DAV:}unbind',
155                'principal' => $this->getOwner() . '/calendar-proxy-write',
156                'protected' => true,
157            ],
158            [
159                'privilege' => '{' . CalDAV\Plugin::NS_CALDAV . '}schedule-deliver',
160                'principal' => '{DAV:}authenticated',
161                'protected' => true,
162            ],
163        ];
164
165    }
166
167    /**
168     * Performs a calendar-query on the contents of this calendar.
169     *
170     * The calendar-query is defined in RFC4791 : CalDAV. Using the
171     * calendar-query it is possible for a client to request a specific set of
172     * object, based on contents of iCalendar properties, date-ranges and
173     * iCalendar component types (VTODO, VEVENT).
174     *
175     * This method should just return a list of (relative) urls that match this
176     * query.
177     *
178     * The list of filters are specified as an array. The exact array is
179     * documented by \Sabre\CalDAV\CalendarQueryParser.
180     *
181     * @param array $filters
182     * @return array
183     */
184    function calendarQuery(array $filters) {
185
186        $result = [];
187        $validator = new CalDAV\CalendarQueryValidator();
188
189        $objects = $this->caldavBackend->getSchedulingObjects($this->principalUri);
190        foreach ($objects as $object) {
191            $vObject = VObject\Reader::read($object['calendardata']);
192            if ($validator->validate($vObject, $filters)) {
193                $result[] = $object['uri'];
194            }
195
196            // Destroy circular references to PHP will GC the object.
197            $vObject->destroy();
198        }
199        return $result;
200
201    }
202
203}
204