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.

PublishPlugin.php 6.4KB

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