1<?php
2
3namespace Sabre\CalDAV\Schedule;
4
5use Sabre\CalDAV\Backend;
6use Sabre\DAV\Exception\MethodNotAllowed;
7
8/**
9 * The SchedulingObject represents a scheduling object in the Inbox collection
10 *
11 * @author Brett (https://github.com/bretten)
12 * @license http://sabre.io/license/ Modified BSD License
13 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
14 */
15class SchedulingObject extends \Sabre\CalDAV\CalendarObject implements ISchedulingObject {
16
17    /**
18     /* The CalDAV backend
19     *
20     * @var Backend\SchedulingSupport
21     */
22    protected $caldavBackend;
23
24    /**
25     * Array with information about this SchedulingObject
26     *
27     * @var array
28     */
29    protected $objectData;
30
31    /**
32     * Constructor
33     *
34     * The following properties may be passed within $objectData:
35     *
36     *   * uri - A unique uri. Only the 'basename' must be passed.
37     *   * principaluri - the principal that owns the object.
38     *   * calendardata (optional) - The iCalendar data
39     *   * etag - (optional) The etag for this object, MUST be encloded with
40     *            double-quotes.
41     *   * size - (optional) The size of the data in bytes.
42     *   * lastmodified - (optional) format as a unix timestamp.
43     *   * acl - (optional) Use this to override the default ACL for the node.
44     *
45     * @param Backend\BackendInterface $caldavBackend
46     * @param array $objectData
47     */
48    function __construct(Backend\SchedulingSupport $caldavBackend, array $objectData) {
49
50        $this->caldavBackend = $caldavBackend;
51
52        if (!isset($objectData['uri'])) {
53            throw new \InvalidArgumentException('The objectData argument must contain an \'uri\' property');
54        }
55
56        $this->objectData = $objectData;
57
58    }
59
60    /**
61     * Returns the ICalendar-formatted object
62     *
63     * @return string
64     */
65    function get() {
66
67        // Pre-populating the 'calendardata' is optional, if we don't have it
68        // already we fetch it from the backend.
69        if (!isset($this->objectData['calendardata'])) {
70            $this->objectData = $this->caldavBackend->getSchedulingObject($this->objectData['principaluri'], $this->objectData['uri']);
71        }
72        return $this->objectData['calendardata'];
73
74    }
75
76    /**
77     * Updates the ICalendar-formatted object
78     *
79     * @param string|resource $calendarData
80     * @return string
81     */
82    function put($calendarData) {
83
84        throw new MethodNotAllowed('Updating scheduling objects is not supported');
85
86    }
87
88    /**
89     * Deletes the scheduling message
90     *
91     * @return void
92     */
93    function delete() {
94
95        $this->caldavBackend->deleteSchedulingObject($this->objectData['principaluri'], $this->objectData['uri']);
96
97    }
98
99    /**
100     * Returns the owner principal
101     *
102     * This must be a url to a principal, or null if there's no owner
103     *
104     * @return string|null
105     */
106    function getOwner() {
107
108        return $this->objectData['principaluri'];
109
110    }
111
112
113    /**
114     * Returns a list of ACE's for this node.
115     *
116     * Each ACE has the following properties:
117     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
118     *     currently the only supported privileges
119     *   * 'principal', a url to the principal who owns the node
120     *   * 'protected' (optional), indicating that this ACE is not allowed to
121     *      be updated.
122     *
123     * @return array
124     */
125    function getACL() {
126
127        // An alternative acl may be specified in the object data.
128        //
129
130        if (isset($this->objectData['acl'])) {
131            return $this->objectData['acl'];
132        }
133
134        // The default ACL
135        return [
136            [
137                'privilege' => '{DAV:}read',
138                'principal' => $this->objectData['principaluri'],
139                'protected' => true,
140            ],
141            [
142                'privilege' => '{DAV:}write',
143                'principal' => $this->objectData['principaluri'],
144                'protected' => true,
145            ],
146            [
147                'privilege' => '{DAV:}read',
148                'principal' => $this->objectData['principaluri'] . '/calendar-proxy-write',
149                'protected' => true,
150            ],
151            [
152                'privilege' => '{DAV:}write',
153                'principal' => $this->objectData['principaluri'] . '/calendar-proxy-write',
154                'protected' => true,
155            ],
156            [
157                'privilege' => '{DAV:}read',
158                'principal' => $this->objectData['principaluri'] . '/calendar-proxy-read',
159                'protected' => true,
160            ],
161        ];
162
163    }
164
165}
166