]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/CardDAV/AddressBook.php
8d545114d97c61e9ce395d85be29ce19c4f8c144
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / CardDAV / AddressBook.php
1 <?php
2
3 /**
4  * The AddressBook class represents a CardDAV addressbook, owned by a specific user
5  *
6  * The AddressBook can contain multiple vcards
7  *
8  * @package Sabre
9  * @subpackage CardDAV
10  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
11  * @author Evert Pot (http://www.rooftopsolutions.nl/)
12  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
13  */
14 class Sabre_CardDAV_AddressBook extends Sabre_DAV_Collection implements Sabre_CardDAV_IAddressBook, Sabre_DAV_IProperties, Sabre_DAVACL_IACL {
15
16     /**
17      * This is an array with addressbook information
18      *
19      * @var array
20      */
21     private $addressBookInfo;
22
23     /**
24      * CardDAV backend
25      *
26      * @var Sabre_CardDAV_Backend_Abstract
27      */
28     private $carddavBackend;
29
30     /**
31      * Constructor
32      *
33      * @param Sabre_CardDAV_Backend_Abstract $carddavBackend
34      * @param array $addressBookInfo
35      */
36     public function __construct(Sabre_CardDAV_Backend_Abstract $carddavBackend, array $addressBookInfo) {
37
38         $this->carddavBackend = $carddavBackend;
39         $this->addressBookInfo = $addressBookInfo;
40
41     }
42
43     /**
44      * Returns the name of the addressbook
45      *
46      * @return string
47      */
48     public function getName() {
49
50         return $this->addressBookInfo['uri'];
51
52     }
53
54     /**
55      * Returns a card
56      *
57      * @param string $name
58      * @return Sabre_CardDAV_ICard
59      */
60     public function getChild($name) {
61
62         $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'],$name);
63         if (!$obj) throw new Sabre_DAV_Exception_NotFound('Card not found');
64         return new Sabre_CardDAV_Card($this->carddavBackend,$this->addressBookInfo,$obj);
65
66     }
67
68     /**
69      * Returns the full list of cards
70      *
71      * @return array
72      */
73     public function getChildren() {
74
75         $objs = $this->carddavBackend->getCards($this->addressBookInfo['id']);
76         $children = array();
77         foreach($objs as $obj) {
78             $children[] = new Sabre_CardDAV_Card($this->carddavBackend,$this->addressBookInfo,$obj);
79         }
80         return $children;
81
82     }
83
84     /**
85      * Creates a new directory
86      *
87      * We actually block this, as subdirectories are not allowed in addressbooks.
88      *
89      * @param string $name
90      * @return void
91      */
92     public function createDirectory($name) {
93
94         throw new Sabre_DAV_Exception_MethodNotAllowed('Creating collections in addressbooks is not allowed');
95
96     }
97
98     /**
99      * Creates a new file
100      *
101      * The contents of the new file must be a valid VCARD.
102      *
103      * This method may return an ETag.
104      *
105      * @param string $name
106      * @param resource $vcardData
107      * @return string|null
108      */
109     public function createFile($name,$vcardData = null) {
110
111         if (is_resource($vcardData)) {
112             $vcardData = stream_get_contents($vcardData);
113         }
114         // Converting to UTF-8, if needed
115         $vcardData = Sabre_DAV_StringUtil::ensureUTF8($vcardData);
116
117         return $this->carddavBackend->createCard($this->addressBookInfo['id'],$name,$vcardData);
118
119     }
120
121     /**
122      * Deletes the entire addressbook.
123      *
124      * @return void
125      */
126     public function delete() {
127
128         $this->carddavBackend->deleteAddressBook($this->addressBookInfo['id']);
129
130     }
131
132     /**
133      * Renames the addressbook
134      *
135      * @param string $newName
136      * @return void
137      */
138     public function setName($newName) {
139
140         throw new Sabre_DAV_Exception_MethodNotAllowed('Renaming addressbooks is not yet supported');
141
142     }
143
144     /**
145      * Returns the last modification date as a unix timestamp.
146      *
147      * @return void
148      */
149     public function getLastModified() {
150
151         return null;
152
153     }
154
155     /**
156      * Updates properties on this node,
157      *
158      * The properties array uses the propertyName in clark-notation as key,
159      * and the array value for the property value. In the case a property
160      * should be deleted, the property value will be null.
161      *
162      * This method must be atomic. If one property cannot be changed, the
163      * entire operation must fail.
164      *
165      * If the operation was successful, true can be returned.
166      * If the operation failed, false can be returned.
167      *
168      * Deletion of a non-existent property is always successful.
169      *
170      * Lastly, it is optional to return detailed information about any
171      * failures. In this case an array should be returned with the following
172      * structure:
173      *
174      * array(
175      *   403 => array(
176      *      '{DAV:}displayname' => null,
177      *   ),
178      *   424 => array(
179      *      '{DAV:}owner' => null,
180      *   )
181      * )
182      *
183      * In this example it was forbidden to update {DAV:}displayname.
184      * (403 Forbidden), which in turn also caused {DAV:}owner to fail
185      * (424 Failed Dependency) because the request needs to be atomic.
186      *
187      * @param array $mutations
188      * @return bool|array
189      */
190     public function updateProperties($mutations) {
191
192         return $this->carddavBackend->updateAddressBook($this->addressBookInfo['id'], $mutations);
193
194     }
195
196     /**
197      * Returns a list of properties for this nodes.
198      *
199      * The properties list is a list of propertynames the client requested,
200      * encoded in clark-notation {xmlnamespace}tagname
201      *
202      * If the array is empty, it means 'all properties' were requested.
203      *
204      * @param array $properties
205      * @return array
206      */
207     public function getProperties($properties) {
208
209         $response = array();
210         foreach($properties as $propertyName) {
211
212             if (isset($this->addressBookInfo[$propertyName])) {
213
214                 $response[$propertyName] = $this->addressBookInfo[$propertyName];
215
216             }
217
218         }
219
220         return $response;
221
222     }
223
224     /**
225      * Returns the owner principal
226      *
227      * This must be a url to a principal, or null if there's no owner
228      *
229      * @return string|null
230      */
231     public function getOwner() {
232
233         return $this->addressBookInfo['principaluri'];
234
235     }
236
237     /**
238      * Returns a group principal
239      *
240      * This must be a url to a principal, or null if there's no owner
241      *
242      * @return string|null
243      */
244     public function getGroup() {
245
246         return null;
247
248     }
249
250     /**
251      * Returns a list of ACE's for this node.
252      *
253      * Each ACE has the following properties:
254      *   * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
255      *     currently the only supported privileges
256      *   * 'principal', a url to the principal who owns the node
257      *   * 'protected' (optional), indicating that this ACE is not allowed to
258      *      be updated.
259      *
260      * @return array
261      */
262     public function getACL() {
263
264         return array(
265             array(
266                 'privilege' => '{DAV:}read',
267                 'principal' => $this->addressBookInfo['principaluri'],
268                 'protected' => true,
269             ),
270             array(
271                 'privilege' => '{DAV:}write',
272                 'principal' => $this->addressBookInfo['principaluri'],
273                 'protected' => true,
274             ),
275
276         );
277
278     }
279
280     /**
281      * Updates the ACL
282      *
283      * This method will receive a list of new ACE's.
284      *
285      * @param array $acl
286      * @return void
287      */
288     public function setACL(array $acl) {
289
290         throw new Sabre_DAV_Exception_MethodNotAllowed('Changing ACL is not yet supported');
291
292     }
293
294     /**
295      * Returns the list of supported privileges for this node.
296      *
297      * The returned data structure is a list of nested privileges.
298      * See Sabre_DAVACL_Plugin::getDefaultSupportedPrivilegeSet for a simple
299      * standard structure.
300      *
301      * If null is returned from this method, the default privilege set is used,
302      * which is fine for most common usecases.
303      *
304      * @return array|null
305      */
306     public function getSupportedPrivilegeSet() {
307
308         return null;
309
310     }
311
312 }