AddressBook.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CardDAV;
  4. use Sabre\DAV;
  5. use Sabre\DAVACL;
  6. /**
  7. * The AddressBook class represents a CardDAV addressbook, owned by a specific user.
  8. *
  9. * The AddressBook can contain multiple vcards
  10. *
  11. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  12. * @author Evert Pot (http://evertpot.com/)
  13. * @license http://sabre.io/license/ Modified BSD License
  14. */
  15. class AddressBook extends DAV\Collection implements IAddressBook, DAV\IProperties, DAVACL\IACL, DAV\Sync\ISyncCollection, DAV\IMultiGet
  16. {
  17. use DAVACL\ACLTrait;
  18. /**
  19. * This is an array with addressbook information.
  20. *
  21. * @var array
  22. */
  23. protected $addressBookInfo;
  24. /**
  25. * CardDAV backend.
  26. *
  27. * @var Backend\BackendInterface
  28. */
  29. protected $carddavBackend;
  30. /**
  31. * Constructor.
  32. */
  33. public function __construct(Backend\BackendInterface $carddavBackend, array $addressBookInfo)
  34. {
  35. $this->carddavBackend = $carddavBackend;
  36. $this->addressBookInfo = $addressBookInfo;
  37. }
  38. /**
  39. * Returns the name of the addressbook.
  40. *
  41. * @return string
  42. */
  43. public function getName()
  44. {
  45. return $this->addressBookInfo['uri'];
  46. }
  47. /**
  48. * Returns a card.
  49. *
  50. * @param string $name
  51. *
  52. * @return Card
  53. */
  54. public function getChild($name)
  55. {
  56. $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name);
  57. if (!$obj) {
  58. throw new DAV\Exception\NotFound('Card not found');
  59. }
  60. return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  61. }
  62. /**
  63. * Returns the full list of cards.
  64. *
  65. * @return array
  66. */
  67. public function getChildren()
  68. {
  69. $objs = $this->carddavBackend->getCards($this->addressBookInfo['id']);
  70. $children = [];
  71. foreach ($objs as $obj) {
  72. $obj['acl'] = $this->getChildACL();
  73. $children[] = new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  74. }
  75. return $children;
  76. }
  77. /**
  78. * This method receives a list of paths in it's first argument.
  79. * It must return an array with Node objects.
  80. *
  81. * If any children are not found, you do not have to return them.
  82. *
  83. * @param string[] $paths
  84. *
  85. * @return array
  86. */
  87. public function getMultipleChildren(array $paths)
  88. {
  89. $objs = $this->carddavBackend->getMultipleCards($this->addressBookInfo['id'], $paths);
  90. $children = [];
  91. foreach ($objs as $obj) {
  92. $obj['acl'] = $this->getChildACL();
  93. $children[] = new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  94. }
  95. return $children;
  96. }
  97. /**
  98. * Creates a new directory.
  99. *
  100. * We actually block this, as subdirectories are not allowed in addressbooks.
  101. *
  102. * @param string $name
  103. */
  104. public function createDirectory($name)
  105. {
  106. throw new DAV\Exception\MethodNotAllowed('Creating collections in addressbooks is not allowed');
  107. }
  108. /**
  109. * Creates a new file.
  110. *
  111. * The contents of the new file must be a valid VCARD.
  112. *
  113. * This method may return an ETag.
  114. *
  115. * @param string $name
  116. * @param resource $data
  117. *
  118. * @return string|null
  119. */
  120. public function createFile($name, $data = null)
  121. {
  122. if (is_resource($data)) {
  123. $data = stream_get_contents($data);
  124. }
  125. // Converting to UTF-8, if needed
  126. $data = DAV\StringUtil::ensureUTF8($data);
  127. return $this->carddavBackend->createCard($this->addressBookInfo['id'], $name, $data);
  128. }
  129. /**
  130. * Deletes the entire addressbook.
  131. */
  132. public function delete()
  133. {
  134. $this->carddavBackend->deleteAddressBook($this->addressBookInfo['id']);
  135. }
  136. /**
  137. * Renames the addressbook.
  138. *
  139. * @param string $newName
  140. */
  141. public function setName($newName)
  142. {
  143. throw new DAV\Exception\MethodNotAllowed('Renaming addressbooks is not yet supported');
  144. }
  145. /**
  146. * Returns the last modification date as a unix timestamp.
  147. */
  148. public function getLastModified()
  149. {
  150. return null;
  151. }
  152. /**
  153. * Updates properties on this node.
  154. *
  155. * This method received a PropPatch object, which contains all the
  156. * information about the update.
  157. *
  158. * To update specific properties, call the 'handle' method on this object.
  159. * Read the PropPatch documentation for more information.
  160. */
  161. public function propPatch(DAV\PropPatch $propPatch)
  162. {
  163. return $this->carddavBackend->updateAddressBook($this->addressBookInfo['id'], $propPatch);
  164. }
  165. /**
  166. * Returns a list of properties for this nodes.
  167. *
  168. * The properties list is a list of propertynames the client requested,
  169. * encoded in clark-notation {xmlnamespace}tagname
  170. *
  171. * If the array is empty, it means 'all properties' were requested.
  172. *
  173. * @param array $properties
  174. *
  175. * @return array
  176. */
  177. public function getProperties($properties)
  178. {
  179. $response = [];
  180. foreach ($properties as $propertyName) {
  181. if (isset($this->addressBookInfo[$propertyName])) {
  182. $response[$propertyName] = $this->addressBookInfo[$propertyName];
  183. }
  184. }
  185. return $response;
  186. }
  187. /**
  188. * Returns the owner principal.
  189. *
  190. * This must be a url to a principal, or null if there's no owner
  191. *
  192. * @return string|null
  193. */
  194. public function getOwner()
  195. {
  196. return $this->addressBookInfo['principaluri'];
  197. }
  198. /**
  199. * This method returns the ACL's for card nodes in this address book.
  200. * The result of this method automatically gets passed to the
  201. * card nodes in this address book.
  202. *
  203. * @return array
  204. */
  205. public function getChildACL()
  206. {
  207. return [
  208. [
  209. 'privilege' => '{DAV:}all',
  210. 'principal' => $this->getOwner(),
  211. 'protected' => true,
  212. ],
  213. ];
  214. }
  215. /**
  216. * This method returns the current sync-token for this collection.
  217. * This can be any string.
  218. *
  219. * If null is returned from this function, the plugin assumes there's no
  220. * sync information available.
  221. *
  222. * @return string|null
  223. */
  224. public function getSyncToken()
  225. {
  226. if (
  227. $this->carddavBackend instanceof Backend\SyncSupport &&
  228. isset($this->addressBookInfo['{DAV:}sync-token'])
  229. ) {
  230. return $this->addressBookInfo['{DAV:}sync-token'];
  231. }
  232. if (
  233. $this->carddavBackend instanceof Backend\SyncSupport &&
  234. isset($this->addressBookInfo['{http://sabredav.org/ns}sync-token'])
  235. ) {
  236. return $this->addressBookInfo['{http://sabredav.org/ns}sync-token'];
  237. }
  238. }
  239. /**
  240. * The getChanges method returns all the changes that have happened, since
  241. * the specified syncToken and the current collection.
  242. *
  243. * This function should return an array, such as the following:
  244. *
  245. * [
  246. * 'syncToken' => 'The current synctoken',
  247. * 'added' => [
  248. * 'new.txt',
  249. * ],
  250. * 'modified' => [
  251. * 'modified.txt',
  252. * ],
  253. * 'deleted' => [
  254. * 'foo.php.bak',
  255. * 'old.txt'
  256. * ]
  257. * ];
  258. *
  259. * The syncToken property should reflect the *current* syncToken of the
  260. * collection, as reported getSyncToken(). This is needed here too, to
  261. * ensure the operation is atomic.
  262. *
  263. * If the syncToken is specified as null, this is an initial sync, and all
  264. * members should be reported.
  265. *
  266. * The modified property is an array of nodenames that have changed since
  267. * the last token.
  268. *
  269. * The deleted property is an array with nodenames, that have been deleted
  270. * from collection.
  271. *
  272. * The second argument is basically the 'depth' of the report. If it's 1,
  273. * you only have to report changes that happened only directly in immediate
  274. * descendants. If it's 2, it should also include changes from the nodes
  275. * below the child collections. (grandchildren)
  276. *
  277. * The third (optional) argument allows a client to specify how many
  278. * results should be returned at most. If the limit is not specified, it
  279. * should be treated as infinite.
  280. *
  281. * If the limit (infinite or not) is higher than you're willing to return,
  282. * you should throw a Sabre\DAV\Exception\TooMuchMatches() exception.
  283. *
  284. * If the syncToken is expired (due to data cleanup) or unknown, you must
  285. * return null.
  286. *
  287. * The limit is 'suggestive'. You are free to ignore it.
  288. *
  289. * @param string $syncToken
  290. * @param int $syncLevel
  291. * @param int $limit
  292. *
  293. * @return array|null
  294. */
  295. public function getChanges($syncToken, $syncLevel, $limit = null)
  296. {
  297. if (!$this->carddavBackend instanceof Backend\SyncSupport) {
  298. return null;
  299. }
  300. return $this->carddavBackend->getChangesForAddressBook(
  301. $this->addressBookInfo['id'],
  302. $syncToken,
  303. $syncLevel,
  304. $limit
  305. );
  306. }
  307. }