InvitationResponseController.php 6.8 KB

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