Plugin.php 5.6 KB

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