PublishPlugin.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\CalDAV\Publishing;
  7. use OCA\DAV\CalDAV\Calendar;
  8. use OCA\DAV\CalDAV\Publishing\Xml\Publisher;
  9. use OCP\IConfig;
  10. use OCP\IURLGenerator;
  11. use Sabre\CalDAV\Xml\Property\AllowedSharingModes;
  12. use Sabre\DAV\Exception\NotFound;
  13. use Sabre\DAV\INode;
  14. use Sabre\DAV\PropFind;
  15. use Sabre\DAV\Server;
  16. use Sabre\DAV\ServerPlugin;
  17. use Sabre\HTTP\RequestInterface;
  18. use Sabre\HTTP\ResponseInterface;
  19. class PublishPlugin extends ServerPlugin {
  20. public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
  21. /**
  22. * Reference to SabreDAV server object.
  23. *
  24. * @var \Sabre\DAV\Server
  25. */
  26. protected $server;
  27. /**
  28. * PublishPlugin constructor.
  29. *
  30. * @param IConfig $config
  31. * @param IURLGenerator $urlGenerator
  32. */
  33. public function __construct(
  34. /**
  35. * Config instance to get instance secret.
  36. */
  37. protected IConfig $config,
  38. /**
  39. * URL Generator for absolute URLs.
  40. */
  41. protected IURLGenerator $urlGenerator,
  42. ) {
  43. }
  44. /**
  45. * This method should return a list of server-features.
  46. *
  47. * This is for example 'versioning' and is added to the DAV: header
  48. * in an OPTIONS response.
  49. *
  50. * @return string[]
  51. */
  52. public function getFeatures() {
  53. // May have to be changed to be detected
  54. return ['oc-calendar-publishing', 'calendarserver-sharing'];
  55. }
  56. /**
  57. * Returns a plugin name.
  58. *
  59. * Using this name other plugins will be able to access other plugins
  60. * using Sabre\DAV\Server::getPlugin
  61. *
  62. * @return string
  63. */
  64. public function getPluginName() {
  65. return 'oc-calendar-publishing';
  66. }
  67. /**
  68. * This initializes the plugin.
  69. *
  70. * This function is called by Sabre\DAV\Server, after
  71. * addPlugin is called.
  72. *
  73. * This method should set up the required event subscriptions.
  74. *
  75. * @param Server $server
  76. */
  77. public function initialize(Server $server) {
  78. $this->server = $server;
  79. $this->server->on('method:POST', [$this, 'httpPost']);
  80. $this->server->on('propFind', [$this, 'propFind']);
  81. }
  82. public function propFind(PropFind $propFind, INode $node) {
  83. if ($node instanceof Calendar) {
  84. $propFind->handle('{' . self::NS_CALENDARSERVER . '}publish-url', function () use ($node) {
  85. if ($node->getPublishStatus()) {
  86. // We return the publish-url only if the calendar is published.
  87. $token = $node->getPublishStatus();
  88. $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri() . 'public-calendars/') . $token;
  89. return new Publisher($publishUrl, true);
  90. }
  91. });
  92. $propFind->handle('{' . self::NS_CALENDARSERVER . '}allowed-sharing-modes', function () use ($node) {
  93. $canShare = (!$node->isSubscription() && $node->canWrite());
  94. $canPublish = (!$node->isSubscription() && $node->canWrite());
  95. if ($this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes') {
  96. $canShare = $canShare && ($node->getOwner() === $node->getPrincipalURI());
  97. $canPublish = $canPublish && ($node->getOwner() === $node->getPrincipalURI());
  98. }
  99. return new AllowedSharingModes($canShare, $canPublish);
  100. });
  101. }
  102. }
  103. /**
  104. * We intercept this to handle POST requests on calendars.
  105. *
  106. * @param RequestInterface $request
  107. * @param ResponseInterface $response
  108. *
  109. * @return void|bool
  110. */
  111. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  112. $path = $request->getPath();
  113. // Only handling xml
  114. $contentType = (string)$request->getHeader('Content-Type');
  115. if (!str_contains($contentType, 'application/xml') && !str_contains($contentType, 'text/xml')) {
  116. return;
  117. }
  118. // Making sure the node exists
  119. try {
  120. $node = $this->server->tree->getNodeForPath($path);
  121. } catch (NotFound $e) {
  122. return;
  123. }
  124. $requestBody = $request->getBodyAsString();
  125. // If this request handler could not deal with this POST request, it
  126. // will return 'null' and other plugins get a chance to handle the
  127. // request.
  128. //
  129. // However, we already requested the full body. This is a problem,
  130. // because a body can only be read once. This is why we preemptively
  131. // re-populated the request body with the existing data.
  132. $request->setBody($requestBody);
  133. $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  134. switch ($documentType) {
  135. case '{' . self::NS_CALENDARSERVER . '}publish-calendar':
  136. // We can only deal with IShareableCalendar objects
  137. if (!$node instanceof Calendar) {
  138. return;
  139. }
  140. $this->server->transactionType = 'post-publish-calendar';
  141. // Getting ACL info
  142. $acl = $this->server->getPlugin('acl');
  143. // If there's no ACL support, we allow everything
  144. if ($acl) {
  145. /** @var \Sabre\DAVACL\Plugin $acl */
  146. $acl->checkPrivileges($path, '{DAV:}write');
  147. $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
  148. $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
  149. if ($limitSharingToOwner && !$isOwner) {
  150. return;
  151. }
  152. }
  153. $node->setPublishStatus(true);
  154. // iCloud sends back the 202, so we will too.
  155. $response->setStatus(202);
  156. // Adding this because sending a response body may cause issues,
  157. // and I wanted some type of indicator the response was handled.
  158. $response->setHeader('X-Sabre-Status', 'everything-went-well');
  159. // Breaking the event chain
  160. return false;
  161. case '{' . self::NS_CALENDARSERVER . '}unpublish-calendar':
  162. // We can only deal with IShareableCalendar objects
  163. if (!$node instanceof Calendar) {
  164. return;
  165. }
  166. $this->server->transactionType = 'post-unpublish-calendar';
  167. // Getting ACL info
  168. $acl = $this->server->getPlugin('acl');
  169. // If there's no ACL support, we allow everything
  170. if ($acl) {
  171. /** @var \Sabre\DAVACL\Plugin $acl */
  172. $acl->checkPrivileges($path, '{DAV:}write');
  173. $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
  174. $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
  175. if ($limitSharingToOwner && !$isOwner) {
  176. return;
  177. }
  178. }
  179. $node->setPublishStatus(false);
  180. $response->setStatus(200);
  181. // Adding this because sending a response body may cause issues,
  182. // and I wanted some type of indicator the response was handled.
  183. $response->setHeader('X-Sabre-Status', 'everything-went-well');
  184. // Breaking the event chain
  185. return false;
  186. }
  187. }
  188. }