Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LegacyMiddleware
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
2 / 2
31
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
10
 loadPage
n/a
0 / 0
n/a
0 / 0
16
 renderPage
n/a
0 / 0
n/a
0 / 0
4
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Middleware;
6
7use Engelsystem\Helpers\Authenticator;
8use Engelsystem\Helpers\Translation\Translator;
9use Engelsystem\Http\Request;
10use Psr\Container\ContainerInterface;
11use Psr\Http\Message\ResponseInterface;
12use Psr\Http\Message\ServerRequestInterface;
13use Psr\Http\Server\MiddlewareInterface;
14use Psr\Http\Server\RequestHandlerInterface;
15
16/**
17 * Middleware to support the old routing / pages from includes
18 */
19class LegacyMiddleware implements MiddlewareInterface
20{
21    /** @var array<string> */
22    protected array $free_pages = [
23        'angeltypes',
24        'public_dashboard',
25        'locations',
26        'shift_entries',
27        'shifts',
28        'users',
29    ];
30
31    public function __construct(protected ContainerInterface $container, protected Authenticator $auth)
32    {
33    }
34
35    /**
36     * Handle the request the old way
37     *
38     * Should be used before a 404 is sent
39     */
40    public function process(
41        ServerRequestInterface $request,
42        RequestHandlerInterface $handler
43    ): ResponseInterface {
44        /** @var Request $appRequest */
45        $appRequest = $this->container->get('request');
46        $page = $appRequest->query->get('p');
47        // Support old URL/permission scheme
48        if (empty($page)) {
49            $page = $appRequest->path();
50            $page = str_replace('-', '_', $page);
51        }
52
53        $allowPage = false;
54        if ($page === 'admin_arrive') {
55            $allowPage = $this->auth->can('users.arrive.list');
56        }
57
58        $title = $content = '';
59        if (
60            preg_match('~^\w+$~i', $page)
61            && (in_array($page, $this->free_pages) || $this->auth->can($page) || $allowPage)
62        ) {
63            list($title, $content) = $this->loadPage($page);
64        }
65
66        if ($content instanceof ResponseInterface) {
67            return $content;
68        }
69
70        if (empty($title) and empty($content)) {
71            /** @var Translator $translator */
72            $translator = $this->container->get('translator');
73
74            $page = 404;
75            $title = $translator->translate('page.404.title');
76            $content = $translator->translate('page.404.text');
77        }
78
79        return $this->renderPage($page, $title, $content);
80    }
81
82    /**
83     * Get the legacy page content and title
84     *
85     * @return array ['title', 'content']
86     * @codeCoverageIgnore
87     */
88    protected function loadPage(string $page): array
89    {
90        switch ($page) {
91            case 'public_dashboard':
92                return public_dashboard_controller();
93            case 'angeltypes':
94                return angeltypes_controller();
95            case 'shift_entries':
96                return shift_entries_controller();
97            case 'shifts':
98                return shifts_controller();
99            case 'users':
100                return users_controller();
101            case 'user_angeltypes':
102                return user_angeltypes_controller();
103            case 'locations':
104                return locations_controller();
105            case 'user_myshifts':
106                $title = myshifts_title();
107                $content = user_myshifts();
108                return [$title, $content];
109            case 'user_shifts':
110                $title = shifts_title();
111                $content = user_shifts();
112                return [$title, $content];
113            case 'admin_user':
114                $title = admin_user_title();
115                $content = admin_user();
116                return [$title, $content];
117            case 'admin_arrive':
118                $title = admin_arrive_title();
119                $content = admin_arrive();
120                return [$title, $content];
121            case 'admin_active':
122                $title = admin_active_title();
123                $content = admin_active();
124                return [$title, $content];
125            case 'admin_free':
126                $title = admin_free_title();
127                $content = admin_free();
128                return [$title, $content];
129            case 'admin_groups':
130                $title = admin_groups_title();
131                $content = admin_groups();
132                return [$title, $content];
133            case 'admin_shifts':
134                $title = admin_shifts_title();
135                $content = admin_shifts();
136                return [$title, $content];
137        }
138
139        throw_redirect(url('/login'));
140
141        return [];
142    }
143
144    /**
145     * Render the template
146     *
147     * @codeCoverageIgnore
148     */
149    protected function renderPage(string | int $page, string $title, string $content): ResponseInterface
150    {
151        if (!empty($page) && is_int($page)) {
152            return response($content, $page);
153        }
154
155        if (str_contains($content, '<html')) {
156            return response($content);
157        }
158
159        return response(
160            view(
161                'layouts/app',
162                [
163                    'title'   => $title,
164                    'content' => msg() . $content,
165                ]
166            )
167        );
168    }
169}