1<?php 2 3declare(strict_types=1); 4 5namespace Sabre\CardDAV; 6 7use Sabre\DAV; 8use Sabre\DAV\MkCol; 9use Sabre\DAVACL; 10use Sabre\Uri; 11 12/** 13 * AddressBook Home class. 14 * 15 * This collection contains a list of addressbooks associated with one user. 16 * 17 * @copyright Copyright (C) fruux GmbH (https://fruux.com/) 18 * @author Evert Pot (http://evertpot.com/) 19 * @license http://sabre.io/license/ Modified BSD License 20 */ 21class AddressBookHome extends DAV\Collection implements DAV\IExtendedCollection, DAVACL\IACL 22{ 23 use DAVACL\ACLTrait; 24 25 /** 26 * Principal uri. 27 * 28 * @var array 29 */ 30 protected $principalUri; 31 32 /** 33 * carddavBackend. 34 * 35 * @var Backend\BackendInterface 36 */ 37 protected $carddavBackend; 38 39 /** 40 * Constructor. 41 * 42 * @param string $principalUri 43 */ 44 public function __construct(Backend\BackendInterface $carddavBackend, $principalUri) 45 { 46 $this->carddavBackend = $carddavBackend; 47 $this->principalUri = $principalUri; 48 } 49 50 /** 51 * Returns the name of this object. 52 * 53 * @return string 54 */ 55 public function getName() 56 { 57 list(, $name) = Uri\split($this->principalUri); 58 59 return $name; 60 } 61 62 /** 63 * Updates the name of this object. 64 * 65 * @param string $name 66 */ 67 public function setName($name) 68 { 69 throw new DAV\Exception\MethodNotAllowed(); 70 } 71 72 /** 73 * Deletes this object. 74 */ 75 public function delete() 76 { 77 throw new DAV\Exception\MethodNotAllowed(); 78 } 79 80 /** 81 * Returns the last modification date. 82 * 83 * @return int 84 */ 85 public function getLastModified() 86 { 87 return null; 88 } 89 90 /** 91 * Creates a new file under this object. 92 * 93 * This is currently not allowed 94 * 95 * @param string $filename 96 * @param resource $data 97 */ 98 public function createFile($filename, $data = null) 99 { 100 throw new DAV\Exception\MethodNotAllowed('Creating new files in this collection is not supported'); 101 } 102 103 /** 104 * Creates a new directory under this object. 105 * 106 * This is currently not allowed. 107 * 108 * @param string $filename 109 */ 110 public function createDirectory($filename) 111 { 112 throw new DAV\Exception\MethodNotAllowed('Creating new collections in this collection is not supported'); 113 } 114 115 /** 116 * Returns a single addressbook, by name. 117 * 118 * @param string $name 119 * 120 * @todo needs optimizing 121 * 122 * @return AddressBook 123 */ 124 public function getChild($name) 125 { 126 foreach ($this->getChildren() as $child) { 127 if ($name == $child->getName()) { 128 return $child; 129 } 130 } 131 throw new DAV\Exception\NotFound('Addressbook with name \''.$name.'\' could not be found'); 132 } 133 134 /** 135 * Returns a list of addressbooks. 136 * 137 * @return array 138 */ 139 public function getChildren() 140 { 141 $addressbooks = $this->carddavBackend->getAddressBooksForUser($this->principalUri); 142 $objs = []; 143 foreach ($addressbooks as $addressbook) { 144 $objs[] = new AddressBook($this->carddavBackend, $addressbook); 145 } 146 147 return $objs; 148 } 149 150 /** 151 * Creates a new address book. 152 * 153 * @param string $name 154 * 155 * @throws DAV\Exception\InvalidResourceType 156 */ 157 public function createExtendedCollection($name, MkCol $mkCol) 158 { 159 if (!$mkCol->hasResourceType('{'.Plugin::NS_CARDDAV.'}addressbook')) { 160 throw new DAV\Exception\InvalidResourceType('Unknown resourceType for this collection'); 161 } 162 $properties = $mkCol->getRemainingValues(); 163 $mkCol->setRemainingResultCode(201); 164 $this->carddavBackend->createAddressBook($this->principalUri, $name, $properties); 165 } 166 167 /** 168 * Returns the owner principal. 169 * 170 * This must be a url to a principal, or null if there's no owner 171 * 172 * @return string|null 173 */ 174 public function getOwner() 175 { 176 return $this->principalUri; 177 } 178} 179