1
0

Calendar.php 11 KB

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