You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Plugin.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\DAV\Sharing;
  26. use OCA\DAV\Connector\Sabre\Auth;
  27. use OCA\DAV\DAV\Sharing\Xml\Invite;
  28. use OCA\DAV\DAV\Sharing\Xml\ShareRequest;
  29. use OCP\IConfig;
  30. use OCP\IRequest;
  31. use Sabre\DAV\Exception\NotFound;
  32. use Sabre\DAV\INode;
  33. use Sabre\DAV\PropFind;
  34. use Sabre\DAV\Server;
  35. use Sabre\DAV\ServerPlugin;
  36. use Sabre\HTTP\RequestInterface;
  37. use Sabre\HTTP\ResponseInterface;
  38. class Plugin extends ServerPlugin {
  39. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  40. public const NS_NEXTCLOUD = 'http://nextcloud.com/ns';
  41. /** @var Auth */
  42. private $auth;
  43. /** @var IRequest */
  44. private $request;
  45. /** @var IConfig */
  46. private $config;
  47. /**
  48. * Plugin constructor.
  49. *
  50. * @param Auth $authBackEnd
  51. * @param IRequest $request
  52. * @param IConfig $config
  53. */
  54. public function __construct(Auth $authBackEnd, IRequest $request, IConfig $config) {
  55. $this->auth = $authBackEnd;
  56. $this->request = $request;
  57. $this->config = $config;
  58. }
  59. /**
  60. * Reference to SabreDAV server object.
  61. *
  62. * @var \Sabre\DAV\Server
  63. */
  64. protected $server;
  65. /**
  66. * This method should return a list of server-features.
  67. *
  68. * This is for example 'versioning' and is added to the DAV: header
  69. * in an OPTIONS response.
  70. *
  71. * @return string[]
  72. */
  73. public function getFeatures() {
  74. return ['oc-resource-sharing'];
  75. }
  76. /**
  77. * Returns a plugin name.
  78. *
  79. * Using this name other plugins will be able to access other plugins
  80. * using Sabre\DAV\Server::getPlugin
  81. *
  82. * @return string
  83. */
  84. public function getPluginName() {
  85. return 'oc-resource-sharing';
  86. }
  87. /**
  88. * This initializes the plugin.
  89. *
  90. * This function is called by Sabre\DAV\Server, after
  91. * addPlugin is called.
  92. *
  93. * This method should set up the required event subscriptions.
  94. *
  95. * @param Server $server
  96. * @return void
  97. */
  98. public function initialize(Server $server) {
  99. $this->server = $server;
  100. $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = ShareRequest::class;
  101. $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = Invite::class;
  102. $this->server->on('method:POST', [$this, 'httpPost']);
  103. $this->server->on('propFind', [$this, 'propFind']);
  104. }
  105. /**
  106. * We intercept this to handle POST requests on a dav resource.
  107. *
  108. * @param RequestInterface $request
  109. * @param ResponseInterface $response
  110. * @return null|false
  111. */
  112. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  113. $path = $request->getPath();
  114. // Only handling xml
  115. $contentType = $request->getHeader('Content-Type');
  116. if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) {
  117. return;
  118. }
  119. // Making sure the node exists
  120. try {
  121. $node = $this->server->tree->getNodeForPath($path);
  122. } catch (NotFound $e) {
  123. return;
  124. }
  125. $requestBody = $request->getBodyAsString();
  126. // If this request handler could not deal with this POST request, it
  127. // will return 'null' and other plugins get a chance to handle the
  128. // request.
  129. //
  130. // However, we already requested the full body. This is a problem,
  131. // because a body can only be read once. This is why we preemptively
  132. // re-populated the request body with the existing data.
  133. $request->setBody($requestBody);
  134. $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  135. switch ($documentType) {
  136. // Dealing with the 'share' document, which modified invitees on a
  137. // calendar.
  138. case '{' . self::NS_OWNCLOUD . '}share':
  139. // We can only deal with IShareableCalendar objects
  140. if (!$node instanceof IShareable) {
  141. return;
  142. }
  143. $this->server->transactionType = 'post-oc-resource-share';
  144. // Getting ACL info
  145. $acl = $this->server->getPlugin('acl');
  146. // If there's no ACL support, we allow everything
  147. if ($acl) {
  148. /** @var \Sabre\DAVACL\Plugin $acl */
  149. $acl->checkPrivileges($path, '{DAV:}write');
  150. $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
  151. $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
  152. if ($limitSharingToOwner && !$isOwner) {
  153. return;
  154. }
  155. }
  156. $node->updateShares($message->set, $message->remove);
  157. $response->setStatus(200);
  158. // Adding this because sending a response body may cause issues,
  159. // and I wanted some type of indicator the response was handled.
  160. $response->setHeader('X-Sabre-Status', 'everything-went-well');
  161. // Breaking the event chain
  162. return false;
  163. }
  164. }
  165. /**
  166. * This event is triggered when properties are requested for a certain
  167. * node.
  168. *
  169. * This allows us to inject any properties early.
  170. *
  171. * @param PropFind $propFind
  172. * @param INode $node
  173. * @return void
  174. */
  175. public function propFind(PropFind $propFind, INode $node) {
  176. if ($node instanceof IShareable) {
  177. $propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function () use ($node) {
  178. return new Invite(
  179. $node->getShares()
  180. );
  181. });
  182. }
  183. }
  184. }