4 * The AddressBook class represents a CardDAV addressbook, owned by a specific user
6 * The AddressBook can contain multiple vcards
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
14 class Sabre_CardDAV_AddressBook extends Sabre_DAV_Collection implements Sabre_CardDAV_IAddressBook, Sabre_DAV_IProperties, Sabre_DAVACL_IACL {
17 * This is an array with addressbook information
21 private $addressBookInfo;
26 * @var Sabre_CardDAV_Backend_Abstract
28 private $carddavBackend;
33 * @param Sabre_CardDAV_Backend_Abstract $carddavBackend
34 * @param array $addressBookInfo
36 public function __construct(Sabre_CardDAV_Backend_Abstract $carddavBackend, array $addressBookInfo) {
38 $this->carddavBackend = $carddavBackend;
39 $this->addressBookInfo = $addressBookInfo;
44 * Returns the name of the addressbook
48 public function getName() {
50 return $this->addressBookInfo['uri'];
58 * @return Sabre_CardDAV_ICard
60 public function getChild($name) {
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);
69 * Returns the full list of cards
73 public function getChildren() {
75 $objs = $this->carddavBackend->getCards($this->addressBookInfo['id']);
77 foreach($objs as $obj) {
78 $children[] = new Sabre_CardDAV_Card($this->carddavBackend,$this->addressBookInfo,$obj);
85 * Creates a new directory
87 * We actually block this, as subdirectories are not allowed in addressbooks.
92 public function createDirectory($name) {
94 throw new Sabre_DAV_Exception_MethodNotAllowed('Creating collections in addressbooks is not allowed');
101 * The contents of the new file must be a valid VCARD.
103 * This method may return an ETag.
105 * @param string $name
106 * @param resource $vcardData
107 * @return string|null
109 public function createFile($name,$vcardData = null) {
111 if (is_resource($vcardData)) {
112 $vcardData = stream_get_contents($vcardData);
114 // Converting to UTF-8, if needed
115 $vcardData = Sabre_DAV_StringUtil::ensureUTF8($vcardData);
117 return $this->carddavBackend->createCard($this->addressBookInfo['id'],$name,$vcardData);
122 * Deletes the entire addressbook.
126 public function delete() {
128 $this->carddavBackend->deleteAddressBook($this->addressBookInfo['id']);
133 * Renames the addressbook
135 * @param string $newName
138 public function setName($newName) {
140 throw new Sabre_DAV_Exception_MethodNotAllowed('Renaming addressbooks is not yet supported');
145 * Returns the last modification date as a unix timestamp.
149 public function getLastModified() {
156 * Updates properties on this node,
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.
162 * This method must be atomic. If one property cannot be changed, the
163 * entire operation must fail.
165 * If the operation was successful, true can be returned.
166 * If the operation failed, false can be returned.
168 * Deletion of a non-existent property is always successful.
170 * Lastly, it is optional to return detailed information about any
171 * failures. In this case an array should be returned with the following
176 * '{DAV:}displayname' => null,
179 * '{DAV:}owner' => null,
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.
187 * @param array $mutations
190 public function updateProperties($mutations) {
192 return $this->carddavBackend->updateAddressBook($this->addressBookInfo['id'], $mutations);
197 * Returns a list of properties for this nodes.
199 * The properties list is a list of propertynames the client requested,
200 * encoded in clark-notation {xmlnamespace}tagname
202 * If the array is empty, it means 'all properties' were requested.
204 * @param array $properties
207 public function getProperties($properties) {
210 foreach($properties as $propertyName) {
212 if (isset($this->addressBookInfo[$propertyName])) {
214 $response[$propertyName] = $this->addressBookInfo[$propertyName];
225 * Returns the owner principal
227 * This must be a url to a principal, or null if there's no owner
229 * @return string|null
231 public function getOwner() {
233 return $this->addressBookInfo['principaluri'];
238 * Returns a group principal
240 * This must be a url to a principal, or null if there's no owner
242 * @return string|null
244 public function getGroup() {
251 * Returns a list of ACE's for this node.
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
262 public function getACL() {
266 'privilege' => '{DAV:}read',
267 'principal' => $this->addressBookInfo['principaluri'],
271 'privilege' => '{DAV:}write',
272 'principal' => $this->addressBookInfo['principaluri'],
283 * This method will receive a list of new ACE's.
288 public function setACL(array $acl) {
290 throw new Sabre_DAV_Exception_MethodNotAllowed('Changing ACL is not yet supported');
295 * Returns the list of supported privileges for this node.
297 * The returned data structure is a list of nested privileges.
298 * See Sabre_DAVACL_Plugin::getDefaultSupportedPrivilegeSet for a simple
299 * standard structure.
301 * If null is returned from this method, the default privilege set is used,
302 * which is fine for most common usecases.
306 public function getSupportedPrivilegeSet() {