1
0

Calendar.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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\DAV\Sharing\IShareable;
  29. use OCP\IConfig;
  30. use OCP\IL10N;
  31. use Sabre\CalDAV\Backend\BackendInterface;
  32. use Sabre\DAV\Exception\Forbidden;
  33. use Sabre\DAV\Exception\NotFound;
  34. use Sabre\DAV\PropPatch;
  35. /**
  36. * Class Calendar
  37. *
  38. * @package OCA\DAV\CalDAV
  39. * @property BackendInterface|CalDavBackend $caldavBackend
  40. */
  41. class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
  42. /** @var IConfig */
  43. private $config;
  44. public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n, IConfig $config) {
  45. parent::__construct($caldavBackend, $calendarInfo);
  46. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  47. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
  48. }
  49. if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI &&
  50. $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  51. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal');
  52. }
  53. $this->config = $config;
  54. }
  55. /**
  56. * Updates the list of shares.
  57. *
  58. * The first array is a list of people that are to be added to the
  59. * resource.
  60. *
  61. * Every element in the add array has the following properties:
  62. * * href - A url. Usually a mailto: address
  63. * * commonName - Usually a first and last name, or false
  64. * * summary - A description of the share, can also be false
  65. * * readOnly - A boolean value
  66. *
  67. * Every element in the remove array is just the address string.
  68. *
  69. * @param array $add
  70. * @param array $remove
  71. * @return void
  72. * @throws Forbidden
  73. */
  74. public function updateShares(array $add, array $remove) {
  75. if ($this->isShared()) {
  76. throw new Forbidden();
  77. }
  78. $this->caldavBackend->updateShares($this, $add, $remove);
  79. }
  80. /**
  81. * Returns the list of people whom this resource is shared with.
  82. *
  83. * Every element in this array should have the following properties:
  84. * * href - Often a mailto: address
  85. * * commonName - Optional, for example a first + last name
  86. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  87. * * readOnly - boolean
  88. * * summary - Optional, a description for the share
  89. *
  90. * @return array
  91. */
  92. public function getShares() {
  93. if ($this->isShared()) {
  94. return [];
  95. }
  96. return $this->caldavBackend->getShares($this->getResourceId());
  97. }
  98. /**
  99. * @return int
  100. */
  101. public function getResourceId() {
  102. return $this->calendarInfo['id'];
  103. }
  104. /**
  105. * @return string
  106. */
  107. public function getPrincipalURI() {
  108. return $this->calendarInfo['principaluri'];
  109. }
  110. public function getACL() {
  111. $acl = [
  112. [
  113. 'privilege' => '{DAV:}read',
  114. 'principal' => $this->getOwner(),
  115. 'protected' => true,
  116. ]];
  117. if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
  118. $acl[] = [
  119. 'privilege' => '{DAV:}write',
  120. 'principal' => $this->getOwner(),
  121. 'protected' => true,
  122. ];
  123. } else {
  124. $acl[] = [
  125. 'privilege' => '{DAV:}write-properties',
  126. 'principal' => $this->getOwner(),
  127. 'protected' => true,
  128. ];
  129. }
  130. if (!$this->isShared()) {
  131. return $acl;
  132. }
  133. if ($this->getOwner() !== parent::getOwner()) {
  134. $acl[] = [
  135. 'privilege' => '{DAV:}read',
  136. 'principal' => parent::getOwner(),
  137. 'protected' => true,
  138. ];
  139. if ($this->canWrite()) {
  140. $acl[] = [
  141. 'privilege' => '{DAV:}write',
  142. 'principal' => parent::getOwner(),
  143. 'protected' => true,
  144. ];
  145. } else {
  146. $acl[] = [
  147. 'privilege' => '{DAV:}write-properties',
  148. 'principal' => parent::getOwner(),
  149. 'protected' => true,
  150. ];
  151. }
  152. }
  153. if ($this->isPublic()) {
  154. $acl[] = [
  155. 'privilege' => '{DAV:}read',
  156. 'principal' => 'principals/system/public',
  157. 'protected' => true,
  158. ];
  159. }
  160. $acl = $this->caldavBackend->applyShareAcl($this->getResourceId(), $acl);
  161. $allowedPrincipals = [$this->getOwner(), parent::getOwner(), 'principals/system/public'];
  162. return array_filter($acl, function($rule) use ($allowedPrincipals) {
  163. return \in_array($rule['principal'], $allowedPrincipals, true);
  164. });
  165. }
  166. public function getChildACL() {
  167. return $this->getACL();
  168. }
  169. public function getOwner() {
  170. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  171. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  172. }
  173. return parent::getOwner();
  174. }
  175. public function delete() {
  176. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) &&
  177. $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) {
  178. $principal = 'principal:' . parent::getOwner();
  179. $shares = $this->caldavBackend->getShares($this->getResourceId());
  180. $shares = array_filter($shares, function($share) use ($principal){
  181. return $share['href'] === $principal;
  182. });
  183. if (empty($shares)) {
  184. throw new Forbidden();
  185. }
  186. $this->caldavBackend->updateShares($this, [], [
  187. $principal
  188. ]);
  189. return;
  190. }
  191. // Remember when a user deleted their birthday calendar
  192. // in order to not regenerate it on the next contacts change
  193. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  194. $principalURI = $this->getPrincipalURI();
  195. $userId = substr($principalURI, 17);
  196. $this->config->setUserValue($userId, 'dav', 'generateBirthdayCalendar', 'no');
  197. }
  198. parent::delete();
  199. }
  200. public function propPatch(PropPatch $propPatch) {
  201. // parent::propPatch will only update calendars table
  202. // if calendar is shared, changes have to be made to the properties table
  203. if (!$this->isShared()) {
  204. parent::propPatch($propPatch);
  205. }
  206. }
  207. public function getChild($name) {
  208. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  209. if (!$obj) {
  210. throw new NotFound('Calendar object not found');
  211. }
  212. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  213. throw new NotFound('Calendar object not found');
  214. }
  215. $obj['acl'] = $this->getChildACL();
  216. return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  217. }
  218. public function getChildren() {
  219. $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
  220. $children = [];
  221. foreach ($objs as $obj) {
  222. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  223. continue;
  224. }
  225. $obj['acl'] = $this->getChildACL();
  226. $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  227. }
  228. return $children;
  229. }
  230. public function getMultipleChildren(array $paths) {
  231. $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
  232. $children = [];
  233. foreach ($objs as $obj) {
  234. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  235. continue;
  236. }
  237. $obj['acl'] = $this->getChildACL();
  238. $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  239. }
  240. return $children;
  241. }
  242. public function childExists($name) {
  243. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  244. if (!$obj) {
  245. return false;
  246. }
  247. if ($obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE && $this->isShared()) {
  248. return false;
  249. }
  250. return true;
  251. }
  252. public function calendarQuery(array $filters) {
  253. $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
  254. if ($this->isShared()) {
  255. return array_filter($uris, function ($uri) {
  256. return $this->childExists($uri);
  257. });
  258. }
  259. return $uris;
  260. }
  261. /**
  262. * @param boolean $value
  263. * @return string|null
  264. */
  265. public function setPublishStatus($value) {
  266. $publicUri = $this->caldavBackend->setPublishStatus($value, $this);
  267. $this->calendarInfo['publicuri'] = $publicUri;
  268. return $publicUri;
  269. }
  270. /**
  271. * @return mixed $value
  272. */
  273. public function getPublishStatus() {
  274. return $this->caldavBackend->getPublishStatus($this);
  275. }
  276. private function canWrite() {
  277. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  278. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  279. }
  280. return true;
  281. }
  282. private function isPublic() {
  283. return isset($this->calendarInfo['{http://owncloud.org/ns}public']);
  284. }
  285. protected function isShared() {
  286. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  287. return false;
  288. }
  289. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  290. }
  291. public function isSubscription() {
  292. return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']);
  293. }
  294. }