Plugin.php 5.2 KB

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