Plugin.php 5.3 KB

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