1<?php
2
3namespace Sabre\CardDAV;
4
5use Sabre\DAV;
6use Sabre\DAV\MkCol;
7use Sabre\DAVACL;
8use Sabre\Uri;
9
10/**
11 * AddressBook Home class
12 *
13 * This collection contains a list of addressbooks associated with one user.
14 *
15 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
16 * @author Evert Pot (http://evertpot.com/)
17 * @license http://sabre.io/license/ Modified BSD License
18 */
19class AddressBookHome extends DAV\Collection implements DAV\IExtendedCollection, DAVACL\IACL {
20
21    /**
22     * Principal uri
23     *
24     * @var array
25     */
26    protected $principalUri;
27
28    /**
29     * carddavBackend
30     *
31     * @var Backend\BackendInterface
32     */
33    protected $carddavBackend;
34
35    /**
36     * Constructor
37     *
38     * @param Backend\BackendInterface $carddavBackend
39     * @param string $principalUri
40     */
41    function __construct(Backend\BackendInterface $carddavBackend, $principalUri) {
42
43        $this->carddavBackend = $carddavBackend;
44        $this->principalUri = $principalUri;
45
46    }
47
48    /**
49     * Returns the name of this object
50     *
51     * @return string
52     */
53    function getName() {
54
55        list(, $name) = Uri\split($this->principalUri);
56        return $name;
57
58    }
59
60    /**
61     * Updates the name of this object
62     *
63     * @param string $name
64     * @return void
65     */
66    function setName($name) {
67
68        throw new DAV\Exception\MethodNotAllowed();
69
70    }
71
72    /**
73     * Deletes this object
74     *
75     * @return void
76     */
77    function delete() {
78
79        throw new DAV\Exception\MethodNotAllowed();
80
81    }
82
83    /**
84     * Returns the last modification date
85     *
86     * @return int
87     */
88    function getLastModified() {
89
90        return null;
91
92    }
93
94    /**
95     * Creates a new file under this object.
96     *
97     * This is currently not allowed
98     *
99     * @param string $filename
100     * @param resource $data
101     * @return void
102     */
103    function createFile($filename, $data = null) {
104
105        throw new DAV\Exception\MethodNotAllowed('Creating new files in this collection is not supported');
106
107    }
108
109    /**
110     * Creates a new directory under this object.
111     *
112     * This is currently not allowed.
113     *
114     * @param string $filename
115     * @return void
116     */
117    function createDirectory($filename) {
118
119        throw new DAV\Exception\MethodNotAllowed('Creating new collections in this collection is not supported');
120
121    }
122
123    /**
124     * Returns a single addressbook, by name
125     *
126     * @param string $name
127     * @todo needs optimizing
128     * @return \AddressBook
129     */
130    function getChild($name) {
131
132        foreach ($this->getChildren() as $child) {
133            if ($name == $child->getName())
134                return $child;
135
136        }
137        throw new DAV\Exception\NotFound('Addressbook with name \'' . $name . '\' could not be found');
138
139    }
140
141    /**
142     * Returns a list of addressbooks
143     *
144     * @return array
145     */
146    function getChildren() {
147
148        $addressbooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri);
149        $objs = [];
150        foreach ($addressbooks as $addressbook) {
151            $objs[] = new AddressBook($this->carddavBackend, $addressbook);
152        }
153        return $objs;
154
155    }
156
157    /**
158     * Creates a new address book.
159     *
160     * @param string $name
161     * @param MkCol $mkCol
162     * @throws DAV\Exception\InvalidResourceType
163     * @return void
164     */
165    function createExtendedCollection($name, MkCol $mkCol) {
166
167        if (!$mkCol->hasResourceType('{' . Plugin::NS_CARDDAV . '}addressbook')) {
168            throw new DAV\Exception\InvalidResourceType('Unknown resourceType for this collection');
169        }
170        $properties = $mkCol->getRemainingValues();
171        $mkCol->setRemainingResultCode(201);
172        $this->carddavBackend->createAddressBook($this->principalUri, $name, $properties);
173
174    }
175
176    /**
177     * Returns the owner principal
178     *
179     * This must be a url to a principal, or null if there's no owner
180     *
181     * @return string|null
182     */
183    function getOwner() {
184
185        return $this->principalUri;
186
187    }
188
189    /**
190     * Returns a group principal
191     *
192     * This must be a url to a principal, or null if there's no owner
193     *
194     * @return string|null
195     */
196    function getGroup() {
197
198        return null;
199
200    }
201
202    /**
203     * Returns a list of ACE's for this node.
204     *
205     * Each ACE has the following properties:
206     *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
207     *     currently the only supported privileges
208     *   * 'principal', a url to the principal who owns the node
209     *   * 'protected' (optional), indicating that this ACE is not allowed to
210     *      be updated.
211     *
212     * @return array
213     */
214    function getACL() {
215
216        return [
217            [
218                'privilege' => '{DAV:}read',
219                'principal' => $this->principalUri,
220                'protected' => true,
221            ],
222            [
223                'privilege' => '{DAV:}write',
224                'principal' => $this->principalUri,
225                'protected' => true,
226            ],
227        ];
228
229    }
230
231    /**
232     * Updates the ACL
233     *
234     * This method will receive a list of new ACE's.
235     *
236     * @param array $acl
237     * @return void
238     */
239    function setACL(array $acl) {
240
241        throw new DAV\Exception\MethodNotAllowed('Changing ACL is not yet supported');
242
243    }
244
245    /**
246     * Returns the list of supported privileges for this node.
247     *
248     * The returned data structure is a list of nested privileges.
249     * See Sabre\DAVACL\Plugin::getDefaultSupportedPrivilegeSet for a simple
250     * standard structure.
251     *
252     * If null is returned from this method, the default privilege set is used,
253     * which is fine for most common usecases.
254     *
255     * @return array|null
256     */
257    function getSupportedPrivilegeSet() {
258
259        return null;
260
261    }
262
263}
264