Calendar.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV;
  25. use OCA\DAV\DAV\Sharing\IShareable;
  26. use OCP\IL10N;
  27. use Sabre\CalDAV\Backend\BackendInterface;
  28. use Sabre\DAV\Exception\Forbidden;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\DAV\PropPatch;
  31. class Calendar extends \Sabre\CalDAV\Calendar implements IShareable {
  32. public function __construct(BackendInterface $caldavBackend, $calendarInfo, IL10N $l10n) {
  33. parent::__construct($caldavBackend, $calendarInfo);
  34. if ($this->getName() === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  35. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Contact birthdays');
  36. }
  37. if ($this->getName() === CalDavBackend::PERSONAL_CALENDAR_URI &&
  38. $this->calendarInfo['{DAV:}displayname'] === CalDavBackend::PERSONAL_CALENDAR_NAME) {
  39. $this->calendarInfo['{DAV:}displayname'] = $l10n->t('Personal');
  40. }
  41. }
  42. /**
  43. * Updates the list of shares.
  44. *
  45. * The first array is a list of people that are to be added to the
  46. * resource.
  47. *
  48. * Every element in the add array has the following properties:
  49. * * href - A url. Usually a mailto: address
  50. * * commonName - Usually a first and last name, or false
  51. * * summary - A description of the share, can also be false
  52. * * readOnly - A boolean value
  53. *
  54. * Every element in the remove array is just the address string.
  55. *
  56. * @param array $add
  57. * @param array $remove
  58. * @return void
  59. */
  60. function updateShares(array $add, array $remove) {
  61. /** @var CalDavBackend $calDavBackend */
  62. $calDavBackend = $this->caldavBackend;
  63. $calDavBackend->updateShares($this, $add, $remove);
  64. }
  65. /**
  66. * Returns the list of people whom this resource is shared with.
  67. *
  68. * Every element in this array should have the following properties:
  69. * * href - Often a mailto: address
  70. * * commonName - Optional, for example a first + last name
  71. * * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
  72. * * readOnly - boolean
  73. * * summary - Optional, a description for the share
  74. *
  75. * @return array
  76. */
  77. function getShares() {
  78. /** @var CalDavBackend $calDavBackend */
  79. $calDavBackend = $this->caldavBackend;
  80. return $calDavBackend->getShares($this->getResourceId());
  81. }
  82. /**
  83. * @return int
  84. */
  85. public function getResourceId() {
  86. return $this->calendarInfo['id'];
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getPrincipalURI() {
  92. return $this->calendarInfo['principaluri'];
  93. }
  94. function getACL() {
  95. $acl = [
  96. [
  97. 'privilege' => '{DAV:}read',
  98. 'principal' => $this->getOwner(),
  99. 'protected' => true,
  100. ]];
  101. if ($this->getName() !== BirthdayService::BIRTHDAY_CALENDAR_URI) {
  102. $acl[] = [
  103. 'privilege' => '{DAV:}write',
  104. 'principal' => $this->getOwner(),
  105. 'protected' => true,
  106. ];
  107. }
  108. if ($this->getOwner() !== parent::getOwner()) {
  109. $acl[] = [
  110. 'privilege' => '{DAV:}read',
  111. 'principal' => parent::getOwner(),
  112. 'protected' => true,
  113. ];
  114. if ($this->canWrite()) {
  115. $acl[] = [
  116. 'privilege' => '{DAV:}write',
  117. 'principal' => parent::getOwner(),
  118. 'protected' => true,
  119. ];
  120. }
  121. }
  122. if ($this->isPublic()) {
  123. $acl[] = [
  124. 'privilege' => '{DAV:}read',
  125. 'principal' => 'principals/system/public',
  126. 'protected' => true,
  127. ];
  128. }
  129. /** @var CalDavBackend $calDavBackend */
  130. $calDavBackend = $this->caldavBackend;
  131. return $calDavBackend->applyShareAcl($this->getResourceId(), $acl);
  132. }
  133. function getChildACL() {
  134. return $this->getACL();
  135. }
  136. function getOwner() {
  137. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  138. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  139. }
  140. return parent::getOwner();
  141. }
  142. function delete() {
  143. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal']) &&
  144. $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri']) {
  145. $principal = 'principal:' . parent::getOwner();
  146. $shares = $this->getShares();
  147. $shares = array_filter($shares, function($share) use ($principal){
  148. return $share['href'] === $principal;
  149. });
  150. if (empty($shares)) {
  151. throw new Forbidden();
  152. }
  153. /** @var CalDavBackend $calDavBackend */
  154. $calDavBackend = $this->caldavBackend;
  155. $calDavBackend->updateShares($this, [], [
  156. 'href' => $principal
  157. ]);
  158. return;
  159. }
  160. parent::delete();
  161. }
  162. function propPatch(PropPatch $propPatch) {
  163. $mutations = $propPatch->getMutations();
  164. // If this is a shared calendar, the user can only change the enabled property, to hide it.
  165. if ($this->isShared() && (sizeof($mutations) !== 1 || !isset($mutations['{http://owncloud.org/ns}calendar-enabled']))) {
  166. throw new Forbidden();
  167. }
  168. parent::propPatch($propPatch);
  169. }
  170. function getChild($name) {
  171. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  172. if (!$obj) {
  173. throw new NotFound('Calendar object not found');
  174. }
  175. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  176. throw new NotFound('Calendar object not found');
  177. }
  178. $obj['acl'] = $this->getChildACL();
  179. return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  180. }
  181. function getChildren() {
  182. $objs = $this->caldavBackend->getCalendarObjects($this->calendarInfo['id']);
  183. $children = [];
  184. foreach ($objs as $obj) {
  185. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  186. continue;
  187. }
  188. $obj['acl'] = $this->getChildACL();
  189. $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  190. }
  191. return $children;
  192. }
  193. function getMultipleChildren(array $paths) {
  194. $objs = $this->caldavBackend->getMultipleCalendarObjects($this->calendarInfo['id'], $paths);
  195. $children = [];
  196. foreach ($objs as $obj) {
  197. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  198. continue;
  199. }
  200. $obj['acl'] = $this->getChildACL();
  201. $children[] = new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj);
  202. }
  203. return $children;
  204. }
  205. function childExists($name) {
  206. $obj = $this->caldavBackend->getCalendarObject($this->calendarInfo['id'], $name);
  207. if (!$obj) {
  208. return false;
  209. }
  210. if ($this->isShared() && $obj['classification'] === CalDavBackend::CLASSIFICATION_PRIVATE) {
  211. return false;
  212. }
  213. return true;
  214. }
  215. function calendarQuery(array $filters) {
  216. $uris = $this->caldavBackend->calendarQuery($this->calendarInfo['id'], $filters);
  217. if ($this->isShared()) {
  218. return array_filter($uris, function ($uri) {
  219. return $this->childExists($uri);
  220. });
  221. }
  222. return $uris;
  223. }
  224. /**
  225. * @param boolean $value
  226. * @return string|null
  227. */
  228. function setPublishStatus($value) {
  229. $publicUri = $this->caldavBackend->setPublishStatus($value, $this);
  230. $this->calendarInfo['publicuri'] = $publicUri;
  231. return $publicUri;
  232. }
  233. /**
  234. * @return mixed $value
  235. */
  236. function getPublishStatus() {
  237. return $this->caldavBackend->getPublishStatus($this);
  238. }
  239. private function canWrite() {
  240. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  241. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  242. }
  243. return true;
  244. }
  245. private function isPublic() {
  246. return isset($this->calendarInfo['{http://owncloud.org/ns}public']);
  247. }
  248. private function isShared() {
  249. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  250. return false;
  251. }
  252. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  253. }
  254. public function isSubscription() {
  255. return isset($this->calendarInfo['{http://calendarserver.org/ns/}source']);
  256. }
  257. }