Plugin.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\DAV\Sharing;
  8. use OCA\DAV\CalDAV\CalDavBackend;
  9. use OCA\DAV\CalDAV\CalendarHome;
  10. use OCA\DAV\Connector\Sabre\Auth;
  11. use OCA\DAV\DAV\Sharing\Xml\Invite;
  12. use OCA\DAV\DAV\Sharing\Xml\ShareRequest;
  13. use OCP\IConfig;
  14. use OCP\IRequest;
  15. use Sabre\DAV\Exception\NotFound;
  16. use Sabre\DAV\INode;
  17. use Sabre\DAV\PropFind;
  18. use Sabre\DAV\Server;
  19. use Sabre\DAV\ServerPlugin;
  20. use Sabre\HTTP\RequestInterface;
  21. use Sabre\HTTP\ResponseInterface;
  22. class Plugin extends ServerPlugin {
  23. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  24. public const NS_NEXTCLOUD = 'http://nextcloud.com/ns';
  25. /**
  26. * Plugin constructor.
  27. *
  28. * @param Auth $auth
  29. * @param IRequest $request
  30. * @param IConfig $config
  31. */
  32. public function __construct(
  33. private Auth $auth,
  34. private IRequest $request,
  35. private IConfig $config,
  36. ) {
  37. }
  38. /**
  39. * Reference to SabreDAV server object.
  40. *
  41. * @var \Sabre\DAV\Server
  42. */
  43. protected $server;
  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. return ['oc-resource-sharing'];
  54. }
  55. /**
  56. * Returns a plugin name.
  57. *
  58. * Using this name other plugins will be able to access other plugins
  59. * using Sabre\DAV\Server::getPlugin
  60. *
  61. * @return string
  62. */
  63. public function getPluginName() {
  64. return 'oc-resource-sharing';
  65. }
  66. /**
  67. * This initializes the plugin.
  68. *
  69. * This function is called by Sabre\DAV\Server, after
  70. * addPlugin is called.
  71. *
  72. * This method should set up the required event subscriptions.
  73. *
  74. * @param Server $server
  75. * @return void
  76. */
  77. public function initialize(Server $server) {
  78. $this->server = $server;
  79. $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = ShareRequest::class;
  80. $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = Invite::class;
  81. $this->server->on('method:POST', [$this, 'httpPost']);
  82. $this->server->on('propFind', [$this, 'propFind']);
  83. }
  84. /**
  85. * We intercept this to handle POST requests on a dav resource.
  86. *
  87. * @param RequestInterface $request
  88. * @param ResponseInterface $response
  89. * @return null|false
  90. */
  91. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  92. $path = $request->getPath();
  93. // Only handling xml
  94. $contentType = (string)$request->getHeader('Content-Type');
  95. if (!str_contains($contentType, 'application/xml') && !str_contains($contentType, 'text/xml')) {
  96. return;
  97. }
  98. // Making sure the node exists
  99. try {
  100. $node = $this->server->tree->getNodeForPath($path);
  101. } catch (NotFound $e) {
  102. return;
  103. }
  104. $requestBody = $request->getBodyAsString();
  105. // If this request handler could not deal with this POST request, it
  106. // will return 'null' and other plugins get a chance to handle the
  107. // request.
  108. //
  109. // However, we already requested the full body. This is a problem,
  110. // because a body can only be read once. This is why we preemptively
  111. // re-populated the request body with the existing data.
  112. $request->setBody($requestBody);
  113. $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  114. switch ($documentType) {
  115. // Dealing with the 'share' document, which modified invitees on a
  116. // calendar.
  117. case '{' . self::NS_OWNCLOUD . '}share':
  118. // We can only deal with IShareableCalendar objects
  119. if (!$node instanceof IShareable) {
  120. return;
  121. }
  122. $this->server->transactionType = 'post-oc-resource-share';
  123. // Getting ACL info
  124. $acl = $this->server->getPlugin('acl');
  125. // If there's no ACL support, we allow everything
  126. if ($acl) {
  127. /** @var \Sabre\DAVACL\Plugin $acl */
  128. $acl->checkPrivileges($path, '{DAV:}write');
  129. $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
  130. $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
  131. if ($limitSharingToOwner && !$isOwner) {
  132. return;
  133. }
  134. }
  135. $node->updateShares($message->set, $message->remove);
  136. $response->setStatus(200);
  137. // Adding this because sending a response body may cause issues,
  138. // and I wanted some type of indicator the response was handled.
  139. $response->setHeader('X-Sabre-Status', 'everything-went-well');
  140. // Breaking the event chain
  141. return false;
  142. }
  143. }
  144. /**
  145. * This event is triggered when properties are requested for a certain
  146. * node.
  147. *
  148. * This allows us to inject any properties early.
  149. *
  150. * @param PropFind $propFind
  151. * @param INode $node
  152. * @return void
  153. */
  154. public function propFind(PropFind $propFind, INode $node) {
  155. if ($node instanceof CalendarHome && $propFind->getDepth() === 1) {
  156. $backend = $node->getCalDAVBackend();
  157. if ($backend instanceof CalDavBackend) {
  158. $calendars = $node->getChildren();
  159. $calendars = array_filter($calendars, function (INode $node) {
  160. return $node instanceof IShareable;
  161. });
  162. /** @var int[] $resourceIds */
  163. $resourceIds = array_map(function (IShareable $node) {
  164. return $node->getResourceId();
  165. }, $calendars);
  166. $backend->preloadShares($resourceIds);
  167. }
  168. }
  169. if ($node instanceof IShareable) {
  170. $propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function () use ($node) {
  171. return new Invite(
  172. $node->getShares()
  173. );
  174. });
  175. }
  176. }
  177. }