Plugin.php 5.2 KB

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