PublishPlugin.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Thomas Citharel <tcit@tcit.fr>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\CalDAV\Publishing;
  28. use OCA\DAV\CalDAV\Calendar;
  29. use OCA\DAV\CalDAV\Publishing\Xml\Publisher;
  30. use OCP\IConfig;
  31. use OCP\IURLGenerator;
  32. use Sabre\CalDAV\Xml\Property\AllowedSharingModes;
  33. use Sabre\DAV\Exception\NotFound;
  34. use Sabre\DAV\INode;
  35. use Sabre\DAV\PropFind;
  36. use Sabre\DAV\Server;
  37. use Sabre\DAV\ServerPlugin;
  38. use Sabre\HTTP\RequestInterface;
  39. use Sabre\HTTP\ResponseInterface;
  40. class PublishPlugin extends ServerPlugin {
  41. public const NS_CALENDARSERVER = 'http://calendarserver.org/ns/';
  42. /**
  43. * Reference to SabreDAV server object.
  44. *
  45. * @var \Sabre\DAV\Server
  46. */
  47. protected $server;
  48. /**
  49. * Config instance to get instance secret.
  50. *
  51. * @var IConfig
  52. */
  53. protected $config;
  54. /**
  55. * URL Generator for absolute URLs.
  56. *
  57. * @var IURLGenerator
  58. */
  59. protected $urlGenerator;
  60. /**
  61. * PublishPlugin constructor.
  62. *
  63. * @param IConfig $config
  64. * @param IURLGenerator $urlGenerator
  65. */
  66. public function __construct(IConfig $config, IURLGenerator $urlGenerator) {
  67. $this->config = $config;
  68. $this->urlGenerator = $urlGenerator;
  69. }
  70. /**
  71. * This method should return a list of server-features.
  72. *
  73. * This is for example 'versioning' and is added to the DAV: header
  74. * in an OPTIONS response.
  75. *
  76. * @return string[]
  77. */
  78. public function getFeatures() {
  79. // May have to be changed to be detected
  80. return ['oc-calendar-publishing', 'calendarserver-sharing'];
  81. }
  82. /**
  83. * Returns a plugin name.
  84. *
  85. * Using this name other plugins will be able to access other plugins
  86. * using Sabre\DAV\Server::getPlugin
  87. *
  88. * @return string
  89. */
  90. public function getPluginName() {
  91. return 'oc-calendar-publishing';
  92. }
  93. /**
  94. * This initializes the plugin.
  95. *
  96. * This function is called by Sabre\DAV\Server, after
  97. * addPlugin is called.
  98. *
  99. * This method should set up the required event subscriptions.
  100. *
  101. * @param Server $server
  102. */
  103. public function initialize(Server $server) {
  104. $this->server = $server;
  105. $this->server->on('method:POST', [$this, 'httpPost']);
  106. $this->server->on('propFind', [$this, 'propFind']);
  107. }
  108. public function propFind(PropFind $propFind, INode $node) {
  109. if ($node instanceof Calendar) {
  110. $propFind->handle('{'.self::NS_CALENDARSERVER.'}publish-url', function () use ($node) {
  111. if ($node->getPublishStatus()) {
  112. // We return the publish-url only if the calendar is published.
  113. $token = $node->getPublishStatus();
  114. $publishUrl = $this->urlGenerator->getAbsoluteURL($this->server->getBaseUri().'public-calendars/').$token;
  115. return new Publisher($publishUrl, true);
  116. }
  117. });
  118. $propFind->handle('{'.self::NS_CALENDARSERVER.'}allowed-sharing-modes', function () use ($node) {
  119. $canShare = (!$node->isSubscription() && $node->canWrite());
  120. $canPublish = (!$node->isSubscription() && $node->canWrite());
  121. return new AllowedSharingModes($canShare, $canPublish);
  122. });
  123. }
  124. }
  125. /**
  126. * We intercept this to handle POST requests on calendars.
  127. *
  128. * @param RequestInterface $request
  129. * @param ResponseInterface $response
  130. *
  131. * @return void|bool
  132. */
  133. public function httpPost(RequestInterface $request, ResponseInterface $response) {
  134. $path = $request->getPath();
  135. // Only handling xml
  136. $contentType = $request->getHeader('Content-Type');
  137. if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) {
  138. return;
  139. }
  140. // Making sure the node exists
  141. try {
  142. $node = $this->server->tree->getNodeForPath($path);
  143. } catch (NotFound $e) {
  144. return;
  145. }
  146. $requestBody = $request->getBodyAsString();
  147. // If this request handler could not deal with this POST request, it
  148. // will return 'null' and other plugins get a chance to handle the
  149. // request.
  150. //
  151. // However, we already requested the full body. This is a problem,
  152. // because a body can only be read once. This is why we preemptively
  153. // re-populated the request body with the existing data.
  154. $request->setBody($requestBody);
  155. $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
  156. switch ($documentType) {
  157. case '{'.self::NS_CALENDARSERVER.'}publish-calendar':
  158. // We can only deal with IShareableCalendar objects
  159. if (!$node instanceof Calendar) {
  160. return;
  161. }
  162. $this->server->transactionType = 'post-publish-calendar';
  163. // Getting ACL info
  164. $acl = $this->server->getPlugin('acl');
  165. // If there's no ACL support, we allow everything
  166. if ($acl) {
  167. $acl->checkPrivileges($path, '{DAV:}write');
  168. }
  169. $node->setPublishStatus(true);
  170. // iCloud sends back the 202, so we will too.
  171. $response->setStatus(202);
  172. // Adding this because sending a response body may cause issues,
  173. // and I wanted some type of indicator the response was handled.
  174. $response->setHeader('X-Sabre-Status', 'everything-went-well');
  175. // Breaking the event chain
  176. return false;
  177. case '{'.self::NS_CALENDARSERVER.'}unpublish-calendar':
  178. // We can only deal with IShareableCalendar objects
  179. if (!$node instanceof Calendar) {
  180. return;
  181. }
  182. $this->server->transactionType = 'post-unpublish-calendar';
  183. // Getting ACL info
  184. $acl = $this->server->getPlugin('acl');
  185. // If there's no ACL support, we allow everything
  186. if ($acl) {
  187. $acl->checkPrivileges($path, '{DAV:}write');
  188. }
  189. $node->setPublishStatus(false);
  190. $response->setStatus(200);
  191. // Adding this because sending a response body may cause issues,
  192. // and I wanted some type of indicator the response was handled.
  193. $response->setHeader('X-Sabre-Status', 'everything-went-well');
  194. // Breaking the event chain
  195. return false;
  196. }
  197. }
  198. }