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 7.5KB

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