1
0

PublishPlugin.php 6.2 KB

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