aboutsummaryrefslogtreecommitdiff
path: root/lib/middleware/LegacyRedirectorMiddleware.php
blob: d072e27157fe03b42bb1a81e78adfb0148466398 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Studip\Middleware;

use Forum\Topic;
use NotificationCenter;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\{ResponseInterface as Response,
    ServerRequestInterface as Request,
    UriInterface
};
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\App;

final class LegacyRedirectorMiddleware implements MiddlewareInterface
{
    private array $legacyRoutes = [];

    public function __construct(
        private readonly App $app,
        private readonly ResponseFactoryInterface $response_factory
    ) {
        $this->addLegacyRoute(
            '#^course/forum/index/index#',
            [$this, 'redirectForumPost']
        );

        NotificationCenter::postNotification('LEGACY_REDIRECTOR_DID_CREATE', $this);
    }

    public function addLegacyRoute(string $route, callable $handler): void
    {
        $this->legacyRoutes[$route] = $handler;
    }

    public function process(Request $request, RequestHandlerInterface $handler): Response
    {
        $path = $this->getRelativePath($request);

        foreach ($this->legacyRoutes as $legacyRoute => $routeHandler) {
            if ($this->routeMatches($path, $legacyRoute)) {
                /** @var UriInterface $newUri */
                $newUri = $routeHandler($request);

                $response = $this->response_factory->createResponse(301);
                $response->getBody()->write('');
                return $response->withHeader('Location', (string) $newUri);
            }
        }

        return $handler->handle($request);
    }

    private function getRelativePath(Request $request): string
    {
        $basePath = $this->app->getBasePath();
        $fullPath = $request->getUri()->getPath();

        return ltrim(substr($fullPath, strlen($basePath)), '/');
    }

    private function routeMatches(string $path, string $route): bool
    {
        if (str_starts_with($route, '#') && str_ends_with($route, '#')) {
            return preg_match($route, $path);
        }

        return str_starts_with($path, $route);
    }

    private function redirectForumPost(Request $request): UriInterface
    {
        $uri = $request->getUri();
        $range_id = $request->getQueryParams()['cid'];
        $forum_id = explode('/', $this->getRelativePath($request))[4] ?? null;

        if ($forum_id === $range_id) {
            $redirectUri = $uri->withPath(
                str_replace(
                    'course/forum/index/index/' . $forum_id,
                    'course/forum/topics',
                    $uri->getPath()
                )
            );
        } elseif (Topic::exists($forum_id)) {
            $redirectUri = $uri->withPath(
                str_replace(
                    'course/forum/index/index',
                    'course/forum/topics/show',
                    $uri->getPath()
                )
            );
        } else {
            $redirectUri = $uri->withPath(
                str_replace(
                    'course/forum/index/index',
                    'course/forum/discussions/show',
                    $uri->getPath()
                )
            );
        }

        return $redirectUri;
    }
}