Calendar.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use DateTimeImmutable;
  9. use DateTimeInterface;
  10. use OCA\DAV\CalDAV\Trashbin\Plugin as TrashbinPlugin;
  11. use OCA\DAV\DAV\Sharing\IShareable;
  12. use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException;
  13. use OCP\DB\Exception;
  14. use OCP\IConfig;
  15. use OCP\IL10N;
  16. use Psr\Log\LoggerInterface;
  17. use Sabre\CalDAV\Backend\BackendInterface;
  18. use Sabre\DAV\Exception\Forbidden;
  19. use Sabre\DAV\Exception\NotFound;
  20. use Sabre\DAV\IMoveTarget;
  21. use Sabre\DAV\INode;
  22. use Sabre\DAV\PropPatch;
  23. /**
  24. * Class Calendar
  25. *
  26. * @package OCA\DAV\CalDAV
  27. * @property CalDavBackend $caldavBackend
  28. */
  29. class Calendar extends \Sabre\CalDAV\Calendar implements IRestorable, IShareable, IMoveTarget {
  30. private IConfig $config;
  31. protected IL10N $l10n;
  32. private bool $useTrashbin = true;
  33. private LoggerInterface $logger;
  34. public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n, IConfig $config, LoggerInterface $logger) {
  35. // Convert deletion date to ISO8601 string
  36. if (isset($calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])) {
  37. $calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT] = (new DateTimeImmutable())
  38. ->setTimestamp($calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])
  39. ->format(DateTimeInterface::ATOM);
  40. }
  41. parent::__construct($caldavBackend, $calendarInfo);
  42. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI && strcasecmp($this->calendarInfo['{DAV:}displayname'], 'Contact birthdays') === 0) {
  43. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
  44. }
  45. if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI &&
  46. $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  47. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal');
  48. }
  49. $this->config = $config;
  50. $this->l10n = $l10n;
  51. $this->logger = $logger;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. * @throws Forbidden
  56. */
  57. public function updateShares(array $add, array $remove): void {
  58. if ($this->isShared()) {
  59. throw new Forbidden();
  60. }
  61. $this->caldavBackend->updateShares($this, $add, $remove);
  62. }
  63. /**
  64. * Returns the list of people whom this resource is shared with.
  65. *
  66. * Every element in this array should have the following properties:
  67. * * href - Often a mailto: address
  68. * * commonName - Optional, for example a first + last name
  69. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  70. * * readOnly - boolean
  71. * * summary - Optional, a description for the share
  72. *
  73. * @return list<array{href: string, commonName: string, status: int, readOnly: bool, '{http://owncloud.org/ns}principal': string, '{http://owncloud.org/ns}group-share': bool}>
  74. */
  75. public function getShares(): array {
  76. if ($this->isShared()) {
  77. return [];
  78. }
  79. return $this->caldavBackend->getShares($this->getResourceId());
  80. }
  81. public function getResourceId(): int {
  82. return $this->calendarInfo['id'];
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function getPrincipalURI() {
  88. return $this->calendarInfo['principaluri'];
  89. }
  90. /**
  91. * @param int $resourceId
  92. * @param list<array{privilege: string, principal: string, protected: bool}> $acl
  93. * @return list<array{privilege: string, principal: ?string, protected: bool}>
  94. */
  95. public function getACL() {
  96. $acl = [
  97. [
  98. 'privilege' => '{DAV:}read',
  99. 'principal' => $this->getOwner(),
  100. 'protected' => true,
  101. ],
  102. [
  103. 'privilege' => '{DAV:}read',
  104. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  105. 'protected' => true,
  106. ],
  107. [
  108. 'privilege' => '{DAV:}read',
  109. 'principal' => $this->getOwner() . '/calendar-proxy-read',
  110. 'protected' => true,
  111. ],
  112. ];
  113. if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
  114. $acl[] = [
  115. 'privilege' => '{DAV:}write',
  116. 'principal' => $this->getOwner(),
  117. 'protected' => true,
  118. ];
  119. $acl[] = [
  120. 'privilege' => '{DAV:}write',
  121. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  122. 'protected' => true,
  123. ];
  124. } else {
  125. $acl[] = [
  126. 'privilege' => '{DAV:}write-properties',
  127. 'principal' => $this->getOwner(),
  128. 'protected' => true,
  129. ];
  130. $acl[] = [
  131. 'privilege' => '{DAV:}write-properties',
  132. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  133. 'protected' => true,
  134. ];
  135. }
  136. $acl[] = [
  137. 'privilege' => '{DAV:}write-properties',
  138. 'principal' => $this->getOwner() . '/calendar-proxy-read',
  139. 'protected' => true,
  140. ];
  141. if (!$this->isShared()) {
  142. return $acl;
  143. }
  144. if ($this->getOwner() !== parent::getOwner()) {
  145. $acl[] = [
  146. 'privilege' => '{DAV:}read',
  147. 'principal' => parent::getOwner(),
  148. 'protected' => true,
  149. ];
  150. if ($this->canWrite()) {
  151. $acl[] = [
  152. 'privilege' => '{DAV:}write',
  153. 'principal' => parent::getOwner(),
  154. 'protected' => true,
  155. ];
  156. } else {
  157. $acl[] = [
  158. 'privilege' => '{DAV:}write-properties',
  159. 'principal' => parent::getOwner(),
  160. 'protected' => true,
  161. ];
  162. }
  163. }
  164. if ($this->isPublic()) {
  165. $acl[] = [
  166. 'privilege' => '{DAV:}read',
  167. 'principal' => 'principals/system/public',
  168. 'protected' => true,
  169. ];
  170. }
  171. $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
  172. $allowedPrincipals = [
  173. $this->getOwner(),
  174. $this->getOwner(). '/calendar-proxy-read',
  175. $this->getOwner(). '/calendar-proxy-write',
  176. parent::getOwner(),
  177. 'principals/system/public'
  178. ];
  179. /** @var list<array{privilege: string, principal: string, protected: bool}> $acl */
  180. $acl = array_filter($acl, function (array $rule) use ($allowedPrincipals): bool {
  181. return \in_array($rule['principal'], $allowedPrincipals, true);
  182. });
  183. return $acl;
  184. }
  185. public function getChildACL() {
  186. return $this->getACL();
  187. }
  188. public function getOwner(): ?string {
  189. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  190. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  191. }
  192. return parent::getOwner();
  193. }
  194. public function delete() {
  195. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) &&
  196. $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) {
  197. $principal = 'principal:' . parent::getOwner();
  198. $this->caldavBackend->updateShares($this, [], [
  199. $principal
  200. ]);
  201. return;
  202. }
  203. // Remember when a user deleted their birthday calendar
  204. // in order to not regenerate it on the next contacts change
  205. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  206. $principalURI = $this->getPrincipalURI();
  207. $userId = substr($principalURI, 17);
  208. $this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'no');
  209. }
  210. $this->caldavBackend->deleteCalendar(
  211. $this->calendarInfo['id'],
  212. !$this->useTrashbin
  213. );
  214. }
  215. public function propPatch(PropPatch $propPatch) {
  216. // parent::propPatch will only update calendars table
  217. // if calendar is shared, changes have to be made to the properties table
  218. if (!$this->isShared()) {
  219. parent::propPatch($propPatch);
  220. }
  221. }
  222. public function getChild($name) {
  223. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  224. if (!$obj) {
  225. throw new NotFound('Calendar object not found');
  226. }
  227. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  228. throw new NotFound('Calendar object not found');
  229. }
  230. $obj['acl'] = $this->getChildACL();
  231. return new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj);
  232. }
  233. public function getChildren() {
  234. $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
  235. $children = [];
  236. foreach ($objs as $obj) {
  237. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  238. continue;
  239. }
  240. $obj['acl'] = $this->getChildACL();
  241. $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj);
  242. }
  243. return $children;
  244. }
  245. public function getMultipleChildren(array $paths) {
  246. $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
  247. $children = [];
  248. foreach ($objs as $obj) {
  249. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  250. continue;
  251. }
  252. $obj['acl'] = $this->getChildACL();
  253. $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj);
  254. }
  255. return $children;
  256. }
  257. public function childExists($name) {
  258. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  259. if (!$obj) {
  260. return false;
  261. }
  262. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  263. return false;
  264. }
  265. return true;
  266. }
  267. public function calendarQuery(array $filters) {
  268. $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
  269. if ($this->isShared()) {
  270. return array_filter($uris, function ($uri) {
  271. return $this->childExists($uri);
  272. });
  273. }
  274. return $uris;
  275. }
  276. /**
  277. * @param boolean $value
  278. * @return string|null
  279. */
  280. public function setPublishStatus($value) {
  281. $publicUri = $this->caldavBackend->setPublishStatus($value, $this);
  282. $this->calendarInfo['publicuri'] = $publicUri;
  283. return $publicUri;
  284. }
  285. /**
  286. * @return mixed $value
  287. */
  288. public function getPublishStatus() {
  289. return $this->caldavBackend->getPublishStatus($this);
  290. }
  291. public function canWrite() {
  292. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  293. return false;
  294. }
  295. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  296. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  297. }
  298. return true;
  299. }
  300. private function isPublic() {
  301. return isset($this->calendarInfo['{http://owncloud.org/ns}public']);
  302. }
  303. public function isShared() {
  304. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  305. return false;
  306. }
  307. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  308. }
  309. public function isSubscription() {
  310. return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']);
  311. }
  312. public function isDeleted(): bool {
  313. if (!isset($this->calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT])) {
  314. return false;
  315. }
  316. return $this->calendarInfo[TrashbinPlugin::PROPERTY_DELETED_AT] !== null;
  317. }
  318. /**
  319. * @inheritDoc
  320. */
  321. public function getChanges($syncToken, $syncLevel, $limit = null) {
  322. if (!$syncToken && $limit) {
  323. throw new UnsupportedLimitOnInitialSyncException();
  324. }
  325. return parent::getChanges($syncToken, $syncLevel, $limit);
  326. }
  327. /**
  328. * @inheritDoc
  329. */
  330. public function restore(): void {
  331. $this->caldavBackend->restoreCalendar((int) $this->calendarInfo['id']);
  332. }
  333. public function disableTrashbin(): void {
  334. $this->useTrashbin = false;
  335. }
  336. /**
  337. * @inheritDoc
  338. */
  339. public function moveInto($targetName, $sourcePath, INode $sourceNode) {
  340. if (!($sourceNode instanceof CalendarObject)) {
  341. return false;
  342. }
  343. try {
  344. return $this->caldavBackend->moveCalendarObject($sourceNode->getCalendarId(), (int)$this->calendarInfo['id'], $sourceNode->getId(), $sourceNode->getOwner(), $this->getOwner());
  345. } catch (Exception $e) {
  346. $this->logger->error('Could not move calendar object: ' . $e->getMessage(), ['exception' => $e]);
  347. return false;
  348. }
  349. }
  350. }