InvitationResponseController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\Controller;
  8. use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  11. use OCP\AppFramework\Http\Attribute\OpenAPI;
  12. use OCP\AppFramework\Http\Attribute\PublicPage;
  13. use OCP\AppFramework\Http\TemplateResponse;
  14. use OCP\AppFramework\Utility\ITimeFactory;
  15. use OCP\IDBConnection;
  16. use OCP\IRequest;
  17. use Sabre\VObject\ITip\Message;
  18. use Sabre\VObject\Reader;
  19. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  20. class InvitationResponseController extends Controller {
  21. /** @var IDBConnection */
  22. private $db;
  23. /** @var ITimeFactory */
  24. private $timeFactory;
  25. /** @var InvitationResponseServer */
  26. private $responseServer;
  27. /**
  28. * InvitationResponseController constructor.
  29. *
  30. * @param string $appName
  31. * @param IRequest $request
  32. * @param IDBConnection $db
  33. * @param ITimeFactory $timeFactory
  34. * @param InvitationResponseServer $responseServer
  35. */
  36. public function __construct(string $appName, IRequest $request,
  37. IDBConnection $db, ITimeFactory $timeFactory,
  38. InvitationResponseServer $responseServer) {
  39. parent::__construct($appName, $request);
  40. $this->db = $db;
  41. $this->timeFactory = $timeFactory;
  42. $this->responseServer = $responseServer;
  43. // Don't run `$server->exec()`, because we just need access to the
  44. // fully initialized schedule plugin, but we don't want Sabre/DAV
  45. // to actually handle and reply to the request
  46. }
  47. /**
  48. * @param string $token
  49. * @return TemplateResponse
  50. */
  51. #[PublicPage]
  52. #[NoCSRFRequired]
  53. public function accept(string $token):TemplateResponse {
  54. $row = $this->getTokenInformation($token);
  55. if (!$row) {
  56. return new TemplateResponse($this->appName, 'schedule-response-error', [], 'guest');
  57. }
  58. $iTipMessage = $this->buildITipResponse($row, 'ACCEPTED');
  59. $this->responseServer->handleITipMessage($iTipMessage);
  60. if ($iTipMessage->getScheduleStatus() === '1.2') {
  61. return new TemplateResponse($this->appName, 'schedule-response-success', [], 'guest');
  62. }
  63. return new TemplateResponse($this->appName, 'schedule-response-error', [
  64. 'organizer' => $row['organizer'],
  65. ], 'guest');
  66. }
  67. /**
  68. * @param string $token
  69. * @return TemplateResponse
  70. */
  71. #[PublicPage]
  72. #[NoCSRFRequired]
  73. public function decline(string $token):TemplateResponse {
  74. $row = $this->getTokenInformation($token);
  75. if (!$row) {
  76. return new TemplateResponse($this->appName, 'schedule-response-error', [], 'guest');
  77. }
  78. $iTipMessage = $this->buildITipResponse($row, 'DECLINED');
  79. $this->responseServer->handleITipMessage($iTipMessage);
  80. if ($iTipMessage->getScheduleStatus() === '1.2') {
  81. return new TemplateResponse($this->appName, 'schedule-response-success', [], 'guest');
  82. }
  83. return new TemplateResponse($this->appName, 'schedule-response-error', [
  84. 'organizer' => $row['organizer'],
  85. ], 'guest');
  86. }
  87. /**
  88. * @param string $token
  89. * @return TemplateResponse
  90. */
  91. #[PublicPage]
  92. #[NoCSRFRequired]
  93. public function options(string $token):TemplateResponse {
  94. return new TemplateResponse($this->appName, 'schedule-response-options', [
  95. 'token' => $token
  96. ], 'guest');
  97. }
  98. /**
  99. * @param string $token
  100. *
  101. * @return TemplateResponse
  102. */
  103. #[PublicPage]
  104. #[NoCSRFRequired]
  105. public function processMoreOptionsResult(string $token):TemplateResponse {
  106. $partstat = $this->request->getParam('partStat');
  107. $row = $this->getTokenInformation($token);
  108. if (!$row || !\in_array($partstat, ['ACCEPTED', 'DECLINED', 'TENTATIVE'])) {
  109. return new TemplateResponse($this->appName, 'schedule-response-error', [], 'guest');
  110. }
  111. $iTipMessage = $this->buildITipResponse($row, $partstat);
  112. $this->responseServer->handleITipMessage($iTipMessage);
  113. if ($iTipMessage->getScheduleStatus() === '1.2') {
  114. return new TemplateResponse($this->appName, 'schedule-response-success', [], 'guest');
  115. }
  116. return new TemplateResponse($this->appName, 'schedule-response-error', [
  117. 'organizer' => $row['organizer'],
  118. ], 'guest');
  119. }
  120. /**
  121. * @param string $token
  122. * @return array|null
  123. */
  124. private function getTokenInformation(string $token) {
  125. $query = $this->db->getQueryBuilder();
  126. $query->select('*')
  127. ->from('calendar_invitations')
  128. ->where($query->expr()->eq('token', $query->createNamedParameter($token)));
  129. $stmt = $query->executeQuery();
  130. $row = $stmt->fetch(\PDO::FETCH_ASSOC);
  131. $stmt->closeCursor();
  132. if (!$row) {
  133. return null;
  134. }
  135. $currentTime = $this->timeFactory->getTime();
  136. if (((int)$row['expiration']) < $currentTime) {
  137. return null;
  138. }
  139. return $row;
  140. }
  141. /**
  142. * @param array $row
  143. * @param string $partStat participation status of attendee - SEE RFC 5545
  144. * @param int|null $guests
  145. * @param string|null $comment
  146. * @return Message
  147. */
  148. private function buildITipResponse(array $row, string $partStat):Message {
  149. $iTipMessage = new Message();
  150. $iTipMessage->uid = $row['uid'];
  151. $iTipMessage->component = 'VEVENT';
  152. $iTipMessage->method = 'REPLY';
  153. $iTipMessage->sequence = $row['sequence'];
  154. $iTipMessage->sender = $row['attendee'];
  155. if ($this->responseServer->isExternalAttendee($row['attendee'])) {
  156. $iTipMessage->recipient = $row['organizer'];
  157. } else {
  158. $iTipMessage->recipient = $row['attendee'];
  159. }
  160. $message = <<<EOF
  161. BEGIN:VCALENDAR
  162. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  163. METHOD:REPLY
  164. VERSION:2.0
  165. BEGIN:VEVENT
  166. ATTENDEE;PARTSTAT=%s:%s
  167. ORGANIZER:%s
  168. UID:%s
  169. SEQUENCE:%s
  170. REQUEST-STATUS:2.0;Success
  171. %sEND:VEVENT
  172. END:VCALENDAR
  173. EOF;
  174. $vObject = Reader::read(vsprintf($message, [
  175. $partStat, $row['attendee'], $row['organizer'],
  176. $row['uid'], $row['sequence'] ?? 0, $row['recurrenceid'] ?? ''
  177. ]));
  178. $vEvent = $vObject->{'VEVENT'};
  179. $vEvent->DTSTAMP = date('Ymd\\THis\\Z', $this->timeFactory->getTime());
  180. $iTipMessage->message = $vObject;
  181. return $iTipMessage;
  182. }
  183. }