You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Calendar.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Gary Kim <gary@garykim.dev>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Citharel <nextcloud@tcit.fr>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\CalDAV;
  29. use OCA\DAV\DAV\Sharing\IShareable;
  30. use OCA\DAV\Exception\UnsupportedLimitOnInitialSyncException;
  31. use OCP\IConfig;
  32. use OCP\IL10N;
  33. use Sabre\CalDAV\Backend\BackendInterface;
  34. use Sabre\DAV\Exception\Forbidden;
  35. use Sabre\DAV\Exception\NotFound;
  36. use Sabre\DAV\PropPatch;
  37. /**
  38. * Class Calendar
  39. *
  40. * @package OCA\DAV\CalDAV
  41. * @property BackendInterface|CalDavBackend $caldavBackend
  42. */
  43. class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
  44. /** @var IConfig */
  45. private $config;
  46. /** @var IL10N */
  47. protected $l10n;
  48. /**
  49. * Calendar constructor.
  50. *
  51. * @param BackendInterface $caldavBackend
  52. * @param $calendarInfo
  53. * @param IL10N $l10n
  54. * @param IConfig $config
  55. */
  56. public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n, IConfig $config) {
  57. parent::__construct($caldavBackend, $calendarInfo);
  58. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  59. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
  60. }
  61. if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI &&
  62. $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  63. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal');
  64. }
  65. $this->config = $config;
  66. $this->l10n = $l10n;
  67. }
  68. /**
  69. * Updates the list of shares.
  70. *
  71. * The first array is a list of people that are to be added to the
  72. * resource.
  73. *
  74. * Every element in the add array has the following properties:
  75. * * href - A url. Usually a mailto: address
  76. * * commonName - Usually a first and last name, or false
  77. * * summary - A description of the share, can also be false
  78. * * readOnly - A boolean value
  79. *
  80. * Every element in the remove array is just the address string.
  81. *
  82. * @param array $add
  83. * @param array $remove
  84. * @return void
  85. * @throws Forbidden
  86. */
  87. public function updateShares(array $add, array $remove) {
  88. if ($this->isShared()) {
  89. throw new Forbidden();
  90. }
  91. $this->caldavBackend->updateShares($this, $add, $remove);
  92. }
  93. /**
  94. * Returns the list of people whom this resource is shared with.
  95. *
  96. * Every element in this array should have the following properties:
  97. * * href - Often a mailto: address
  98. * * commonName - Optional, for example a first + last name
  99. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  100. * * readOnly - boolean
  101. * * summary - Optional, a description for the share
  102. *
  103. * @return array
  104. */
  105. public function getShares() {
  106. if ($this->isShared()) {
  107. return [];
  108. }
  109. return $this->caldavBackend->getShares($this->getResourceId());
  110. }
  111. /**
  112. * @return int
  113. */
  114. public function getResourceId() {
  115. return $this->calendarInfo['id'];
  116. }
  117. /**
  118. * @return string
  119. */
  120. public function getPrincipalURI() {
  121. return $this->calendarInfo['principaluri'];
  122. }
  123. /**
  124. * @return array
  125. */
  126. public function getACL() {
  127. $acl = [
  128. [
  129. 'privilege' => '{DAV:}read',
  130. 'principal' => $this->getOwner(),
  131. 'protected' => true,
  132. ],
  133. [
  134. 'privilege' => '{DAV:}read',
  135. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  136. 'protected' => true,
  137. ],
  138. [
  139. 'privilege' => '{DAV:}read',
  140. 'principal' => $this->getOwner() . '/calendar-proxy-read',
  141. 'protected' => true,
  142. ],
  143. ];
  144. if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
  145. $acl[] = [
  146. 'privilege' => '{DAV:}write',
  147. 'principal' => $this->getOwner(),
  148. 'protected' => true,
  149. ];
  150. $acl[] = [
  151. 'privilege' => '{DAV:}write',
  152. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  153. 'protected' => true,
  154. ];
  155. } else {
  156. $acl[] = [
  157. 'privilege' => '{DAV:}write-properties',
  158. 'principal' => $this->getOwner(),
  159. 'protected' => true,
  160. ];
  161. $acl[] = [
  162. 'privilege' => '{DAV:}write-properties',
  163. 'principal' => $this->getOwner() . '/calendar-proxy-write',
  164. 'protected' => true,
  165. ];
  166. }
  167. $acl[] = [
  168. 'privilege' => '{DAV:}write-properties',
  169. 'principal' => $this->getOwner() . '/calendar-proxy-read',
  170. 'protected' => true,
  171. ];
  172. if (!$this->isShared()) {
  173. return $acl;
  174. }
  175. if ($this->getOwner() !== parent::getOwner()) {
  176. $acl[] = [
  177. 'privilege' => '{DAV:}read',
  178. 'principal' => parent::getOwner(),
  179. 'protected' => true,
  180. ];
  181. if ($this->canWrite()) {
  182. $acl[] = [
  183. 'privilege' => '{DAV:}write',
  184. 'principal' => parent::getOwner(),
  185. 'protected' => true,
  186. ];
  187. } else {
  188. $acl[] = [
  189. 'privilege' => '{DAV:}write-properties',
  190. 'principal' => parent::getOwner(),
  191. 'protected' => true,
  192. ];
  193. }
  194. }
  195. if ($this->isPublic()) {
  196. $acl[] = [
  197. 'privilege' => '{DAV:}read',
  198. 'principal' => 'principals/system/public',
  199. 'protected' => true,
  200. ];
  201. }
  202. $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
  203. $allowedPrincipals = [
  204. $this->getOwner(),
  205. $this->getOwner(). '/calendar-proxy-read',
  206. $this->getOwner(). '/calendar-proxy-write',
  207. parent::getOwner(),
  208. 'principals/system/public'
  209. ];
  210. return array_filter($acl, function($rule) use ($allowedPrincipals) {
  211. return \in_array($rule['principal'], $allowedPrincipals, true);
  212. });
  213. }
  214. public function getChildACL() {
  215. return $this->getACL();
  216. }
  217. public function getOwner() {
  218. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  219. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  220. }
  221. return parent::getOwner();
  222. }
  223. public function delete() {
  224. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) &&
  225. $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) {
  226. $principal = 'principal:' . parent::getOwner();
  227. $shares = $this->caldavBackend->getShares($this->getResourceId());
  228. $shares = array_filter($shares, function($share) use ($principal){
  229. return $share['href'] === $principal;
  230. });
  231. if (empty($shares)) {
  232. throw new Forbidden();
  233. }
  234. $this->caldavBackend->updateShares($this, [], [
  235. $principal
  236. ]);
  237. return;
  238. }
  239. // Remember when a user deleted their birthday calendar
  240. // in order to not regenerate it on the next contacts change
  241. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  242. $principalURI = $this->getPrincipalURI();
  243. $userId = substr($principalURI, 17);
  244. $this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'no');
  245. }
  246. parent::delete();
  247. }
  248. public function propPatch(PropPatch $propPatch) {
  249. // parent::propPatch will only update calendars table
  250. // if calendar is shared, changes have to be made to the properties table
  251. if (!$this->isShared()) {
  252. parent::propPatch($propPatch);
  253. }
  254. }
  255. public function getChild($name) {
  256. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  257. if (!$obj) {
  258. throw new NotFound('Calendar object not found');
  259. }
  260. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  261. throw new NotFound('Calendar object not found');
  262. }
  263. $obj['acl'] = $this->getChildACL();
  264. return new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj);
  265. }
  266. public function getChildren() {
  267. $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
  268. $children = [];
  269. foreach ($objs as $obj) {
  270. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  271. continue;
  272. }
  273. $obj['acl'] = $this->getChildACL();
  274. $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj);
  275. }
  276. return $children;
  277. }
  278. public function getMultipleChildren(array $paths) {
  279. $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
  280. $children = [];
  281. foreach ($objs as $obj) {
  282. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  283. continue;
  284. }
  285. $obj['acl'] = $this->getChildACL();
  286. $children[] = new CalendarObject($this->caldavBackend, $this->l10n, $this->calendarInfo, $obj);
  287. }
  288. return $children;
  289. }
  290. public function childExists($name) {
  291. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  292. if (!$obj) {
  293. return false;
  294. }
  295. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  296. return false;
  297. }
  298. return true;
  299. }
  300. public function calendarQuery(array $filters) {
  301. $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
  302. if ($this->isShared()) {
  303. return array_filter($uris, function ($uri) {
  304. return $this->childExists($uri);
  305. });
  306. }
  307. return $uris;
  308. }
  309. /**
  310. * @param boolean $value
  311. * @return string|null
  312. */
  313. public function setPublishStatus($value) {
  314. $publicUri = $this->caldavBackend->setPublishStatus($value, $this);
  315. $this->calendarInfo['publicuri'] = $publicUri;
  316. return $publicUri;
  317. }
  318. /**
  319. * @return mixed $value
  320. */
  321. public function getPublishStatus() {
  322. return $this->caldavBackend->getPublishStatus($this);
  323. }
  324. public function canWrite() {
  325. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  326. return false;
  327. }
  328. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  329. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  330. }
  331. return true;
  332. }
  333. private function isPublic() {
  334. return isset($this->calendarInfo['{http://owncloud.org/ns}public']);
  335. }
  336. protected function isShared() {
  337. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  338. return false;
  339. }
  340. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  341. }
  342. public function isSubscription() {
  343. return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']);
  344. }
  345. /**
  346. * @inheritDoc
  347. */
  348. public function getChanges($syncToken, $syncLevel, $limit = null) {
  349. if (!$syncToken && $limit) {
  350. throw new UnsupportedLimitOnInitialSyncException();
  351. }
  352. return parent::getChanges($syncToken, $syncLevel, $limit);
  353. }
  354. }