1
0

Plugin.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\AppFramework\Http;
  14. use OCP\IConfig;
  15. use OCP\IRequest;
  16. use Sabre\DAV\Exception\NotFound;
  17. use Sabre\DAV\INode;
  18. use Sabre\DAV\PropFind;
  19. use Sabre\DAV\Server;
  20. use Sabre\DAV\ServerPlugin;
  21. use Sabre\HTTP\RequestInterface;
  22. use Sabre\HTTP\ResponseInterface;
  23. class Plugin extends ServerPlugin {
  24. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  25. public const NS_NEXTCLOUD = 'http://nextcloud.com/ns';
  26. /**
  27. * Plugin constructor.
  28. *
  29. * @param Auth $auth
  30. * @param IRequest $request
  31. * @param IConfig $config
  32. */
  33. public function __construct(
  34. private Auth $auth,
  35. private IRequest $request,
  36. private IConfig $config,
  37. ) {
  38. }
  39. /**
  40. * Reference to SabreDAV server object.
  41. *
  42. * @var \Sabre\DAV\Server
  43. */
  44. protected $server;
  45. /**
  46. * This method should return a list of server-features.
  47. *
  48. * This is for example 'versioning' and is added to the DAV: header
  49. * in an OPTIONS response.
  50. *
  51. * @return string[]
  52. */
  53. public function getFeatures() {
  54. return ['oc-resource-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-resource-sharing';
  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. * @return void
  77. */
  78. public function initialize(Server $server) {
  79. $this->server = $server;
  80. $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = ShareRequest::class;
  81. $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = Invite::class;
  82. $this->server->on('method:POST', [$this, 'httpPost']);
  83. $this->server->on('propFind', [$this, 'propFind']);
  84. }
  85. /**
  86. * We intercept this to handle POST requests on a dav resource.
  87. *
  88. * @param RequestInterface $request
  89. * @param ResponseInterface $response
  90. * @return null|false
  91. */
  92. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  93. $path = $request->getPath();
  94. // Only handling xml
  95. $contentType = (string)$request->getHeader('Content-Type');
  96. if (!str_contains($contentType, 'application/xml') && !str_contains($contentType, 'text/xml')) {
  97. return;
  98. }
  99. // Making sure the node exists
  100. try {
  101. $node = $this->server->tree->getNodeForPath($path);
  102. } catch (NotFound $e) {
  103. return;
  104. }
  105. $requestBody = $request->getBodyAsString();
  106. // If this request handler could not deal with this POST request, it
  107. // will return 'null' and other plugins get a chance to handle the
  108. // request.
  109. //
  110. // However, we already requested the full body. This is a problem,
  111. // because a body can only be read once. This is why we preemptively
  112. // re-populated the request body with the existing data.
  113. $request->setBody($requestBody);
  114. $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  115. switch ($documentType) {
  116. // Dealing with the 'share' document, which modified invitees on a
  117. // calendar.
  118. case '{' . self::NS_OWNCLOUD . '}share':
  119. // We can only deal with IShareableCalendar objects
  120. if (!$node instanceof IShareable) {
  121. return;
  122. }
  123. $this->server->transactionType = 'post-oc-resource-share';
  124. // Getting ACL info
  125. $acl = $this->server->getPlugin('acl');
  126. // If there's no ACL support, we allow everything
  127. if ($acl) {
  128. /** @var \Sabre\DAVACL\Plugin $acl */
  129. $acl->checkPrivileges($path, '{DAV:}write');
  130. $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
  131. $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
  132. if ($limitSharingToOwner && !$isOwner) {
  133. return;
  134. }
  135. }
  136. $node->updateShares($message->set, $message->remove);
  137. $response->setStatus(Http::STATUS_OK);
  138. // Adding this because sending a response body may cause issues,
  139. // and I wanted some type of indicator the response was handled.
  140. $response->setHeader('X-Sabre-Status', 'everything-went-well');
  141. // Breaking the event chain
  142. return false;
  143. }
  144. }
  145. /**
  146. * This event is triggered when properties are requested for a certain
  147. * node.
  148. *
  149. * This allows us to inject any properties early.
  150. *
  151. * @param PropFind $propFind
  152. * @param INode $node
  153. * @return void
  154. */
  155. public function propFind(PropFind $propFind, INode $node) {
  156. if ($node instanceof CalendarHome && $propFind->getDepth() === 1) {
  157. $backend = $node->getCalDAVBackend();
  158. if ($backend instanceof CalDavBackend) {
  159. $calendars = $node->getChildren();
  160. $calendars = array_filter($calendars, function (INode $node) {
  161. return $node instanceof IShareable;
  162. });
  163. /** @var int[] $resourceIds */
  164. $resourceIds = array_map(function (IShareable $node) {
  165. return $node->getResourceId();
  166. }, $calendars);
  167. $backend->preloadShares($resourceIds);
  168. }
  169. }
  170. if ($node instanceof IShareable) {
  171. $propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function () use ($node) {
  172. return new Invite(
  173. $node->getShares()
  174. );
  175. });
  176. }
  177. }
  178. }