1<?php
2
3namespace Sabre\CalDAV;
4
5/**
6 * This object represents a CalDAV calendar that is shared by a different user.
7 *
8 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
9 * @author Evert Pot (http://evertpot.com/)
10 * @license http://sabre.io/license/ Modified BSD License
11 */
12class SharedCalendar extends Calendar implements ISharedCalendar {
13
14    /**
15     * Constructor
16     *
17     * @param Backend\BackendInterface $caldavBackend
18     * @param array $calendarInfo
19     */
20    function __construct(Backend\BackendInterface $caldavBackend, $calendarInfo) {
21
22        $required = [
23            '{http://calendarserver.org/ns/}shared-url',
24            '{http://sabredav.org/ns}owner-principal',
25            '{http://sabredav.org/ns}read-only',
26        ];
27        foreach ($required as $r) {
28            if (!isset($calendarInfo[$r])) {
29                throw new \InvalidArgumentException('The ' . $r . ' property must be specified for SharedCalendar(s)');
30            }
31        }
32
33        parent::__construct($caldavBackend, $calendarInfo);
34
35    }
36
37    /**
38     * This method should return the url of the owners' copy of the shared
39     * calendar.
40     *
41     * @return string
42     */
43    function getSharedUrl() {
44
45        return $this->calendarInfo['{http://calendarserver.org/ns/}shared-url'];
46
47    }
48
49    /**
50     * Returns the owner principal
51     *
52     * This must be a url to a principal, or null if there's no owner
53     *
54     * @return string|null
55     */
56    function getOwner() {
57
58        return $this->calendarInfo['{http://sabredav.org/ns}owner-principal'];
59
60    }
61
62    /**
63     * Returns a list of ACE's for this node.
64     *
65     * Each ACE has the following properties:
66     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
67     *     currently the only supported privileges
68     *   * 'principal', a url to the principal who owns the node
69     *   * 'protected' (optional), indicating that this ACE is not allowed to
70     *      be updated.
71     *
72     * @return array
73     */
74    function getACL() {
75
76        // The top-level ACL only contains access information for the true
77        // owner of the calendar, so we need to add the information for the
78        // sharee.
79        $acl = parent::getACL();
80        $acl[] = [
81            'privilege' => '{DAV:}read',
82            'principal' => $this->calendarInfo['principaluri'],
83            'protected' => true,
84        ];
85        if ($this->calendarInfo['{http://sabredav.org/ns}read-only']) {
86            $acl[] = [
87                'privilege' => '{DAV:}write-properties',
88                'principal' => $this->calendarInfo['principaluri'],
89                'protected' => true,
90            ];
91        } else {
92            $acl[] = [
93                'privilege' => '{DAV:}write',
94                'principal' => $this->calendarInfo['principaluri'],
95                'protected' => true,
96            ];
97        }
98        return $acl;
99
100    }
101
102    /**
103     * This method returns the ACL's for calendar objects in this calendar.
104     * The result of this method automatically gets passed to the
105     * calendar-object nodes in the calendar.
106     *
107     * @return array
108     */
109    function getChildACL() {
110
111        $acl = parent::getChildACL();
112        $acl[] = [
113            'privilege' => '{DAV:}read',
114            'principal' => $this->calendarInfo['principaluri'],
115            'protected' => true,
116        ];
117
118        if (!$this->calendarInfo['{http://sabredav.org/ns}read-only']) {
119            $acl[] = [
120                'privilege' => '{DAV:}write',
121                'principal' => $this->calendarInfo['principaluri'],
122                'protected' => true,
123            ];
124        }
125        return $acl;
126
127    }
128
129
130    /**
131     * Returns the list of people whom this calendar is shared with.
132     *
133     * Every element in this array should have the following properties:
134     *   * href - Often a mailto: address
135     *   * commonName - Optional, for example a first + last name
136     *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
137     *   * readOnly - boolean
138     *   * summary - Optional, a description for the share
139     *
140     * @return array
141     */
142    function getShares() {
143
144        return $this->caldavBackend->getShares($this->calendarInfo['id']);
145
146    }
147
148}
149