AddressBook.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\CardDAV;
  28. use OCA\DAV\DAV\Sharing\IShareable;
  29. use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException;
  30. use OCP\DB\Exception;
  31. use OCP\IL10N;
  32. use OCP\Server;
  33. use Psr\Log\LoggerInterface;
  34. use Sabre\CardDAV\Backend\BackendInterface;
  35. use Sabre\DAV\Exception\Forbidden;
  36. use Sabre\DAV\Exception\NotFound;
  37. use Sabre\DAV\IMoveTarget;
  38. use Sabre\DAV\INode;
  39. use Sabre\DAV\PropPatch;
  40. /**
  41. * Class AddressBook
  42. *
  43. * @package OCA\DAV\CardDAV
  44. * @property CardDavBackend $carddavBackend
  45. */
  46. class AddressBook extends \Sabre\CardDAV\AddressBook implements IShareable, IMoveTarget {
  47. /**
  48. * AddressBook constructor.
  49. *
  50. * @param BackendInterface $carddavBackend
  51. * @param array $addressBookInfo
  52. * @param IL10N $l10n
  53. */
  54. public function __construct(BackendInterface $carddavBackend, array $addressBookInfo, IL10N $l10n) {
  55. parent::__construct($carddavBackend, $addressBookInfo);
  56. if ($this->addressBookInfo['{DAV:}displayname'] === CardDavBackend::PERSONAL_ADDRESSBOOK_NAME &&
  57. $this->getName() === CardDavBackend::PERSONAL_ADDRESSBOOK_URI) {
  58. $this->addressBookInfo['{DAV:}displayname'] = $l10n->t('Contacts');
  59. }
  60. }
  61. /**
  62. * Updates the list of shares.
  63. *
  64. * The first array is a list of people that are to be added to the
  65. * addressbook.
  66. *
  67. * Every element in the add array has the following properties:
  68. * * href - A url. Usually a mailto: address
  69. * * commonName - Usually a first and last name, or false
  70. * * readOnly - A boolean value
  71. *
  72. * Every element in the remove array is just the address string.
  73. *
  74. * @param list<array{href: string, commonName: string, readOnly: bool}> $add
  75. * @param list<string> $remove
  76. * @throws Forbidden
  77. */
  78. public function updateShares(array $add, array $remove): void {
  79. if ($this->isShared()) {
  80. throw new Forbidden();
  81. }
  82. $this->carddavBackend->updateShares($this, $add, $remove);
  83. }
  84. /**
  85. * Returns the list of people whom this addressbook is shared with.
  86. *
  87. * Every element in this array should have the following properties:
  88. * * href - Often a mailto: address
  89. * * commonName - Optional, for example a first + last name
  90. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  91. * * readOnly - boolean
  92. *
  93. * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}>
  94. */
  95. public function getShares(): array {
  96. if ($this->isShared()) {
  97. return [];
  98. }
  99. return $this->carddavBackend->getShares($this->getResourceId());
  100. }
  101. public function getACL() {
  102. $acl = [
  103. [
  104. 'privilege' => '{DAV:}read',
  105. 'principal' => $this->getOwner(),
  106. 'protected' => true,
  107. ],[
  108. 'privilege' => '{DAV:}write',
  109. 'principal' => $this->getOwner(),
  110. 'protected' => true,
  111. ],
  112. [
  113. 'privilege' => '{DAV:}write-properties',
  114. 'principal' => '{DAV:}authenticated',
  115. 'protected' => true,
  116. ],
  117. ];
  118. if ($this->getOwner() === 'principals/system/system') {
  119. $acl[] = [
  120. 'privilege' => '{DAV:}read',
  121. 'principal' => '{DAV:}authenticated',
  122. 'protected' => true,
  123. ];
  124. }
  125. if (!$this->isShared()) {
  126. return $acl;
  127. }
  128. if ($this->getOwner() !== parent::getOwner()) {
  129. $acl[] = [
  130. 'privilege' => '{DAV:}read',
  131. 'principal' => parent::getOwner(),
  132. 'protected' => true,
  133. ];
  134. if ($this->canWrite()) {
  135. $acl[] = [
  136. 'privilege' => '{DAV:}write',
  137. 'principal' => parent::getOwner(),
  138. 'protected' => true,
  139. ];
  140. }
  141. }
  142. $acl = $this->carddavBackend->applyShareAcl($this->getResourceId(), $acl);
  143. $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/system', '{DAV:}authenticated'];
  144. return array_filter($acl, function ($rule) use ($allowedPrincipals) {
  145. return \in_array($rule['principal'], $allowedPrincipals, true);
  146. });
  147. }
  148. public function getChildACL() {
  149. return $this->getACL();
  150. }
  151. public function getChild($name) {
  152. $obj = $this->carddavBackend->getCard($this->addressBookInfo['id'], $name);
  153. if (!$obj) {
  154. throw new NotFound('Card not found');
  155. }
  156. $obj['acl'] = $this->getChildACL();
  157. return new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  158. }
  159. public function getChildren() {
  160. $objs = $this->carddavBackend->getCards($this->addressBookInfo['id']);
  161. $children = [];
  162. foreach ($objs as $obj) {
  163. $obj['acl'] = $this->getChildACL();
  164. $children[] = new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  165. }
  166. return $children;
  167. }
  168. public function getMultipleChildren(array $paths) {
  169. $objs = $this->carddavBackend->getMultipleCards($this->addressBookInfo['id'], $paths);
  170. $children = [];
  171. foreach ($objs as $obj) {
  172. $obj['acl'] = $this->getChildACL();
  173. $children[] = new Card($this->carddavBackend, $this->addressBookInfo, $obj);
  174. }
  175. return $children;
  176. }
  177. public function getResourceId(): int {
  178. return $this->addressBookInfo['id'];
  179. }
  180. public function getOwner(): ?string {
  181. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  182. return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'];
  183. }
  184. return parent::getOwner();
  185. }
  186. public function delete() {
  187. if (isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  188. $principal = 'principal:' . parent::getOwner();
  189. $shares = $this->carddavBackend->getShares($this->getResourceId());
  190. $shares = array_filter($shares, function ($share) use ($principal) {
  191. return $share['href'] === $principal;
  192. });
  193. if (empty($shares)) {
  194. throw new Forbidden();
  195. }
  196. $this->carddavBackend->updateShares($this, [], [
  197. $principal
  198. ]);
  199. return;
  200. }
  201. parent::delete();
  202. }
  203. public function propPatch(PropPatch $propPatch) {
  204. if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  205. parent::propPatch($propPatch);
  206. }
  207. }
  208. public function getContactsGroups() {
  209. return $this->carddavBackend->collectCardProperties($this->getResourceId(), 'CATEGORIES');
  210. }
  211. private function isShared(): bool {
  212. if (!isset($this->addressBookInfo['{http://owncloud.org/ns}owner-principal'])) {
  213. return false;
  214. }
  215. return $this->addressBookInfo['{http://owncloud.org/ns}owner-principal'] !== $this->addressBookInfo['principaluri'];
  216. }
  217. private function canWrite(): bool {
  218. if (isset($this->addressBookInfo['{http://owncloud.org/ns}read-only'])) {
  219. return !$this->addressBookInfo['{http://owncloud.org/ns}read-only'];
  220. }
  221. return true;
  222. }
  223. public function getChanges($syncToken, $syncLevel, $limit = null) {
  224. if (!$syncToken && $limit) {
  225. throw new UnsupportedLimitOnInitialSyncException();
  226. }
  227. return parent::getChanges($syncToken, $syncLevel, $limit);
  228. }
  229. /**
  230. * @inheritDoc
  231. */
  232. public function moveInto($targetName, $sourcePath, INode $sourceNode) {
  233. if (!($sourceNode instanceof Card)) {
  234. return false;
  235. }
  236. try {
  237. return $this->carddavBackend->moveCard($sourceNode->getAddressbookId(), (int)$this->addressBookInfo['id'], $sourceNode->getUri(), $sourceNode->getOwner());
  238. } catch (Exception $e) {
  239. // Avoid injecting LoggerInterface everywhere
  240. Server::get(LoggerInterface::class)->error('Could not move calendar object: ' . $e->getMessage(), ['exception' => $e]);
  241. return false;
  242. }
  243. }
  244. }