AddressBookHome.php 4.0 KB

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