1
0

InvitationResponseController.php 6.6 KB

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