1
0

InvitationResponseController.php 6.7 KB

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