aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Routes/Events/UserEventsIcal.php
blob: c8080c46803add729bb99fc62535f270da4b4de9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

namespace JsonApi\Routes\Events;

use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\InternalServerError;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\NonJsonApiController;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class UserEventsIcal extends NonJsonApiController
{
    public function __invoke(Request $request, Response $response, array $args)
    {
        if (!$observedUser = \User::find($args['id'])) {
            throw new RecordNotFoundException();
        }

        $user = $this->getUser($request);
        if ($user->id !== $observedUser->id) {
            // absichtlich keine AuthorizationFailedException
            // damit unsichtbare Nutzer nicht ermittelt werden können
            throw new RecordNotFoundException();
        }

        $writer = new \CalendarWriterICalendar();
        $export = new \CalendarExport($writer);
        $export->exportFromDatabase($observedUser->id, 0, 2114377200, ['CalendarEvent', 'CourseEvent', 'CourseCancelledEvent']);
        if ($GLOBALS['_calendar_error']->getMaxStatus(\ErrorHandler::ERROR_CRITICAL)) {
            throw new InternalServerError();
        }

        $content = implode($export->getExport());
        $response->getBody()->write($content);

        return $response->withHeader('Content-Type', 'text/calendar')
            ->withHeader('Content-Disposition', 'attachment; ' . encode_header_parameter('filename', 'studip.ics'));
    }
}