CalendarHome.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\CalDAV;
  4. use Sabre\DAV;
  5. use Sabre\DAV\Exception\NotFound;
  6. use Sabre\DAV\MkCol;
  7. use Sabre\DAVACL;
  8. use Sabre\Uri;
  9. /**
  10. * The CalendarHome represents a node that is usually in a users'
  11. * calendar-homeset.
  12. *
  13. * It contains all the users' calendars, and can optionally contain a
  14. * notifications collection, calendar subscriptions, a users' inbox, and a
  15. * users' outbox.
  16. *
  17. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  18. * @author Evert Pot (http://evertpot.com/)
  19. * @license http://sabre.io/license/ Modified BSD License
  20. */
  21. class CalendarHome implements DAV\IExtendedCollection, DAVACL\IACL
  22. {
  23. use DAVACL\ACLTrait;
  24. /**
  25. * CalDAV backend.
  26. *
  27. * @var Backend\BackendInterface
  28. */
  29. protected $caldavBackend;
  30. /**
  31. * Principal information.
  32. *
  33. * @var array
  34. */
  35. protected $principalInfo;
  36. /**
  37. * Constructor.
  38. *
  39. * @param array $principalInfo
  40. */
  41. public function __construct(Backend\BackendInterface $caldavBackend, $principalInfo)
  42. {
  43. $this->caldavBackend = $caldavBackend;
  44. $this->principalInfo = $principalInfo;
  45. }
  46. /**
  47. * Returns the name of this object.
  48. *
  49. * @return string
  50. */
  51. public function getName()
  52. {
  53. list(, $name) = Uri\split($this->principalInfo['uri']);
  54. return $name;
  55. }
  56. /**
  57. * Updates the name of this object.
  58. *
  59. * @param string $name
  60. */
  61. public function setName($name)
  62. {
  63. throw new DAV\Exception\Forbidden();
  64. }
  65. /**
  66. * Deletes this object.
  67. */
  68. public function delete()
  69. {
  70. throw new DAV\Exception\Forbidden();
  71. }
  72. /**
  73. * Returns the last modification date.
  74. *
  75. * @return int
  76. */
  77. public function getLastModified()
  78. {
  79. return null;
  80. }
  81. /**
  82. * Creates a new file under this object.
  83. *
  84. * This is currently not allowed
  85. *
  86. * @param string $name
  87. * @param resource $data
  88. */
  89. public function createFile($name, $data = null)
  90. {
  91. throw new DAV\Exception\MethodNotAllowed('Creating new files in this collection is not supported');
  92. }
  93. /**
  94. * Creates a new directory under this object.
  95. *
  96. * This is currently not allowed.
  97. *
  98. * @param string $filename
  99. */
  100. public function createDirectory($filename)
  101. {
  102. throw new DAV\Exception\MethodNotAllowed('Creating new collections in this collection is not supported');
  103. }
  104. /**
  105. * Returns a single calendar, by name.
  106. *
  107. * @param string $name
  108. *
  109. * @return Calendar
  110. */
  111. public function getChild($name)
  112. {
  113. // Special nodes
  114. if ('inbox' === $name && $this->caldavBackend instanceof Backend\SchedulingSupport) {
  115. return new Schedule\Inbox($this->caldavBackend, $this->principalInfo['uri']);
  116. }
  117. if ('outbox' === $name && $this->caldavBackend instanceof Backend\SchedulingSupport) {
  118. return new Schedule\Outbox($this->principalInfo['uri']);
  119. }
  120. if ('notifications' === $name && $this->caldavBackend instanceof Backend\NotificationSupport) {
  121. return new Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  122. }
  123. // Calendars
  124. foreach ($this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']) as $calendar) {
  125. if ($calendar['uri'] === $name) {
  126. if ($this->caldavBackend instanceof Backend\SharingSupport) {
  127. return new SharedCalendar($this->caldavBackend, $calendar);
  128. } else {
  129. return new Calendar($this->caldavBackend, $calendar);
  130. }
  131. }
  132. }
  133. if ($this->caldavBackend instanceof Backend\SubscriptionSupport) {
  134. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  135. if ($subscription['uri'] === $name) {
  136. return new Subscriptions\Subscription($this->caldavBackend, $subscription);
  137. }
  138. }
  139. }
  140. throw new NotFound('Node with name \''.$name.'\' could not be found');
  141. }
  142. /**
  143. * Checks if a calendar exists.
  144. *
  145. * @param string $name
  146. *
  147. * @return bool
  148. */
  149. public function childExists($name)
  150. {
  151. try {
  152. return (bool) $this->getChild($name);
  153. } catch (NotFound $e) {
  154. return false;
  155. }
  156. }
  157. /**
  158. * Returns a list of calendars.
  159. *
  160. * @return array
  161. */
  162. public function getChildren()
  163. {
  164. $calendars = $this->caldavBackend->getCalendarsForUser($this->principalInfo['uri']);
  165. $objs = [];
  166. foreach ($calendars as $calendar) {
  167. if ($this->caldavBackend instanceof Backend\SharingSupport) {
  168. $objs[] = new SharedCalendar($this->caldavBackend, $calendar);
  169. } else {
  170. $objs[] = new Calendar($this->caldavBackend, $calendar);
  171. }
  172. }
  173. if ($this->caldavBackend instanceof Backend\SchedulingSupport) {
  174. $objs[] = new Schedule\Inbox($this->caldavBackend, $this->principalInfo['uri']);
  175. $objs[] = new Schedule\Outbox($this->principalInfo['uri']);
  176. }
  177. // We're adding a notifications node, if it's supported by the backend.
  178. if ($this->caldavBackend instanceof Backend\NotificationSupport) {
  179. $objs[] = new Notifications\Collection($this->caldavBackend, $this->principalInfo['uri']);
  180. }
  181. // If the backend supports subscriptions, we'll add those as well,
  182. if ($this->caldavBackend instanceof Backend\SubscriptionSupport) {
  183. foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) {
  184. $objs[] = new Subscriptions\Subscription($this->caldavBackend, $subscription);
  185. }
  186. }
  187. return $objs;
  188. }
  189. /**
  190. * Creates a new calendar or subscription.
  191. *
  192. * @param string $name
  193. *
  194. * @throws DAV\Exception\InvalidResourceType
  195. */
  196. public function createExtendedCollection($name, MkCol $mkCol)
  197. {
  198. $isCalendar = false;
  199. $isSubscription = false;
  200. foreach ($mkCol->getResourceType() as $rt) {
  201. switch ($rt) {
  202. case '{DAV:}collection':
  203. case '{http://calendarserver.org/ns/}shared-owner':
  204. // ignore
  205. break;
  206. case '{urn:ietf:params:xml:ns:caldav}calendar':
  207. $isCalendar = true;
  208. break;
  209. case '{http://calendarserver.org/ns/}subscribed':
  210. $isSubscription = true;
  211. break;
  212. default:
  213. throw new DAV\Exception\InvalidResourceType('Unknown resourceType: '.$rt);
  214. }
  215. }
  216. $properties = $mkCol->getRemainingValues();
  217. $mkCol->setRemainingResultCode(201);
  218. if ($isSubscription) {
  219. if (!$this->caldavBackend instanceof Backend\SubscriptionSupport) {
  220. throw new DAV\Exception\InvalidResourceType('This backend does not support subscriptions');
  221. }
  222. $this->caldavBackend->createSubscription($this->principalInfo['uri'], $name, $properties);
  223. } elseif ($isCalendar) {
  224. $this->caldavBackend->createCalendar($this->principalInfo['uri'], $name, $properties);
  225. } else {
  226. throw new DAV\Exception\InvalidResourceType('You can only create calendars and subscriptions in this collection');
  227. }
  228. }
  229. /**
  230. * Returns the owner of the calendar home.
  231. *
  232. * @return string
  233. */
  234. public function getOwner()
  235. {
  236. return $this->principalInfo['uri'];
  237. }
  238. /**
  239. * Returns a list of ACE's for this node.
  240. *
  241. * Each ACE has the following properties:
  242. * * 'privilege', a string such as {DAV:}read or {DAV:}write. These are
  243. * currently the only supported privileges
  244. * * 'principal', a url to the principal who owns the node
  245. * * 'protected' (optional), indicating that this ACE is not allowed to
  246. * be updated.
  247. *
  248. * @return array
  249. */
  250. public function getACL()
  251. {
  252. return [
  253. [
  254. 'privilege' => '{DAV:}read',
  255. 'principal' => $this->principalInfo['uri'],
  256. 'protected' => true,
  257. ],
  258. [
  259. 'privilege' => '{DAV:}write',
  260. 'principal' => $this->principalInfo['uri'],
  261. 'protected' => true,
  262. ],
  263. [
  264. 'privilege' => '{DAV:}read',
  265. 'principal' => $this->principalInfo['uri'].'/calendar-proxy-write',
  266. 'protected' => true,
  267. ],
  268. [
  269. 'privilege' => '{DAV:}write',
  270. 'principal' => $this->principalInfo['uri'].'/calendar-proxy-write',
  271. 'protected' => true,
  272. ],
  273. [
  274. 'privilege' => '{DAV:}read',
  275. 'principal' => $this->principalInfo['uri'].'/calendar-proxy-read',
  276. 'protected' => true,
  277. ],
  278. ];
  279. }
  280. /**
  281. * This method is called when a user replied to a request to share.
  282. *
  283. * This method should return the url of the newly created calendar if the
  284. * share was accepted.
  285. *
  286. * @param string $href The sharee who is replying (often a mailto: address)
  287. * @param int $status One of the SharingPlugin::STATUS_* constants
  288. * @param string $calendarUri The url to the calendar thats being shared
  289. * @param string $inReplyTo The unique id this message is a response to
  290. * @param string $summary A description of the reply
  291. *
  292. * @return string|null
  293. */
  294. public function shareReply($href, $status, $calendarUri, $inReplyTo, $summary = null)
  295. {
  296. if (!$this->caldavBackend instanceof Backend\SharingSupport) {
  297. throw new DAV\Exception\NotImplemented('Sharing support is not implemented by this backend.');
  298. }
  299. return $this->caldavBackend->shareReply($href, $status, $calendarUri, $inReplyTo, $summary);
  300. }
  301. /**
  302. * Searches through all of a users calendars and calendar objects to find
  303. * an object with a specific UID.
  304. *
  305. * This method should return the path to this object, relative to the
  306. * calendar home, so this path usually only contains two parts:
  307. *
  308. * calendarpath/objectpath.ics
  309. *
  310. * If the uid is not found, return null.
  311. *
  312. * This method should only consider * objects that the principal owns, so
  313. * any calendars owned by other principals that also appear in this
  314. * collection should be ignored.
  315. *
  316. * @param string $uid
  317. *
  318. * @return string|null
  319. */
  320. public function getCalendarObjectByUID($uid)
  321. {
  322. return $this->caldavBackend->getCalendarObjectByUID($this->principalInfo['uri'], $uid);
  323. }
  324. }