aboutsummaryrefslogtreecommitdiff
path: root/public/plugins.php
blob: 9f30028a7f58a37c603fe19318872465ee6a65a2 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/*
 * Copyright (C) 2007 - Marcus Lunzenauer <mlunzena@uos.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App;
use Slim\Factory\AppFactory;
use Psr\Http\Server\RequestHandlerInterface;

require '../lib/bootstrap.php';

// prepare environment
URLHelper::setBaseUrl($GLOBALS['ABSOLUTE_URI_STUDIP']);

// Build PHP_DI Container
$container = app();

// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
$container->set(App::class, $app);
$app->setBasePath($GLOBALS['CANONICAL_RELATIVE_PATH_STUDIP'] . 'plugins.php');
$plugin_dispatch = function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($app) {
    $responseFactory = app(ResponseFactoryInterface::class);
    try {
        // get plugin class from request
        $dispatch_to = Request::pathInfo();
        list($plugin_class, $unconsumed) = PluginEngine::routeRequest($dispatch_to);

        // handle legacy forum plugin URLs
        if ($plugin_class === 'coreforum') {
            $response = $responseFactory->createResponse(302);
            return $response->withHeader('Location', URLHelper::getURL('dispatch.php/course/forum/' . $unconsumed));
        }

        // create an instance of the queried plugin
        $plugin = PluginEngine::getPlugin($plugin_class);

        // user is not permitted, show login screen
        if (is_null($plugin)) {
            // TODO (mlunzena) should not getPlugin throw this exception?
            throw new AccessDeniedException(_('Sie besitzen keine Rechte zum Aufruf dieses Plugins.'));
        }

        // set default page title
        PageLayout::setTitle($plugin->getPluginName());
        $still_not_consumed = $plugin->registerSlimRoutes($unconsumed, $app);
        if ($still_not_consumed !== false) {
            $app->any('{path_info:.*}', $plugin->getRouteCallable($still_not_consumed));
        }
    } catch (AccessDeniedException $ade) {
        $_SESSION['redirect_after_login'] = Request::url();
        $response = $responseFactory->createResponse(302);
        return $response->withHeader('Location', URLHelper::getURL('dispatch.php/login'));
    }
    return $handler->handle($request);
};

$app->add($plugin_dispatch);
$app->add(app(Studip\Middleware\SeminarOpenMiddleware::class));
$app->add(app(Studip\Middleware\AuthenticationMiddleware::class));
auth()->setNobody(true);
$app->add(app(Studip\Middleware\SessionMiddleware::class));

NotificationCenter::postNotification('SLIM_BEFORE_RUN', $app);
$app->run();
NotificationCenter::postNotification('SLIM_AFTER_RUN', $app);