InvitationResponseController.php 6.8 KB

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