summaryrefslogtreecommitdiff
path: root/src/Middleware/LegacyMiddleware.php
blob: 5e08858d7f474f76536731309e08f025c67887f1 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php

namespace Engelsystem\Middleware;

use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Translation\Translator;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class LegacyMiddleware implements MiddlewareInterface
{
    protected $free_pages = [
        'admin_event_config',
        'angeltypes',
        'atom',
        'ical',
        'public_dashboard',
        'rooms',
        'shift_entries',
        'shifts',
        'shifts_json_export',
        'users',
        'user_driver_licenses',
        'user_worklog',
    ];

    /** @var ContainerInterface */
    protected $container;

    /** @var Authenticator */
    protected $auth;

    /**
     * @param ContainerInterface $container
     * @param Authenticator      $auth
     */
    public function __construct(ContainerInterface $container, Authenticator $auth)
    {
        $this->container = $container;
        $this->auth = $auth;
    }

    /**
     * Handle the request the old way
     *
     * Should be used before a 404 is send
     *
     * @param ServerRequestInterface  $request
     * @param RequestHandlerInterface $handler
     * @return ResponseInterface
     */
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        /** @var Request $appRequest */
        $appRequest = $this->container->get('request');
        $page = $appRequest->query->get('p');
        if (empty($page)) {
            $page = $appRequest->path();
            $page = str_replace('-', '_', $page);
        }

        $title = $content = '';
        if (
            preg_match('~^\w+$~i', $page)
            && (in_array($page, $this->free_pages) || $this->auth->can($page))
        ) {
            list($title, $content) = $this->loadPage($page);
        }

        if (empty($title) and empty($content)) {
            /** @var Translator $translator */
            $translator = $this->container->get('translator');

            $page = 404;
            $title = $translator->translate('Page not found');
            $content = $translator->translate('page.404.text');
        }

        return $this->renderPage($page, $title, $content);
    }

    /**
     * Get the legacy page content and title
     *
     * @param string $page
     * @return array ['title', 'content']
     * @codeCoverageIgnore
     */
    protected function loadPage($page)
    {
        $title = ucfirst($page);
        switch ($page) {
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'ical':
                require_once realpath(__DIR__ . '/../../includes/pages/user_ical.php');
                user_ical();
                break;
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'atom':
                require_once realpath(__DIR__ . '/../../includes/pages/user_atom.php');
                user_atom();
                break;
            /** @noinspection PhpMissingBreakStatementInspection */
            case 'shifts_json_export':
                require_once realpath(__DIR__ . '/../../includes/controller/shifts_controller.php');
                shifts_json_export_controller();
                break;
            case 'public_dashboard':
                return public_dashboard_controller();
            case 'angeltypes':
                return angeltypes_controller();
            case 'shift_entries':
                return shift_entries_controller();
            case 'shifts':
                return shifts_controller();
            case 'users':
                return users_controller();
            case 'user_angeltypes':
                return user_angeltypes_controller();
            case 'user_driver_licenses':
                return user_driver_licenses_controller();
            case 'shifttypes':
                list($title, $content) = shifttypes_controller();
                return [$title, $content];
            case 'admin_event_config':
                list($title, $content) = event_config_edit_controller();
                return [$title, $content];
            case 'rooms':
                return rooms_controller();
            case 'news':
                $title = news_title();
                $content = user_news();
                return [$title, $content];
            case 'news_comments':
                require_once realpath(__DIR__ . '/../../includes/pages/user_news.php');
                $title = user_news_comments_title();
                $content = user_news_comments();
                return [$title, $content];
            case 'user_meetings':
                $title = meetings_title();
                $content = user_meetings();
                return [$title, $content];
            case 'user_myshifts':
                $title = myshifts_title();
                $content = user_myshifts();
                return [$title, $content];
            case 'user_shifts':
                $title = shifts_title();
                $content = user_shifts();
                return [$title, $content];
            case 'user_worklog':
                return user_worklog_controller();
            case 'user_messages':
                $title = messages_title();
                $content = user_messages();
                return [$title, $content];
            case 'user_questions':
                $title = questions_title();
                $content = user_questions();
                return [$title, $content];
            case 'user_settings':
                $title = settings_title();
                $content = user_settings();
                return [$title, $content];
            case 'register':
                $title = register_title();
                $content = guest_register();
                return [$title, $content];
            case 'admin_questions':
                $title = admin_questions_title();
                $content = admin_questions();
                return [$title, $content];
            case 'admin_user':
                $title = admin_user_title();
                $content = admin_user();
                return [$title, $content];
            case 'admin_arrive':
                $title = admin_arrive_title();
                $content = admin_arrive();
                return [$title, $content];
            case 'admin_active':
                $title = admin_active_title();
                $content = admin_active();
                return [$title, $content];
            case 'admin_free':
                $title = admin_free_title();
                $content = admin_free();
                return [$title, $content];
            case 'admin_news':
                require_once realpath(__DIR__ . '/../../includes/pages/admin_news.php');
                $content = admin_news();
                return [$title, $content];
            case 'admin_rooms':
                $title = admin_rooms_title();
                $content = admin_rooms();
                return [$title, $content];
            case 'admin_groups':
                $title = admin_groups_title();
                $content = admin_groups();
                return [$title, $content];
            case 'admin_shifts':
                $title = admin_shifts_title();
                $content = admin_shifts();
                return [$title, $content];
            case 'admin_log':
                $title = admin_log_title();
                $content = admin_log();
                return [$title, $content];
        }

        throw_redirect(page_link_to('login'));

        return [];
    }

    /**
     * Render the template
     *
     * @param string $page
     * @param string $title
     * @param string $content
     * @return Response
     * @codeCoverageIgnore
     */
    protected function renderPage($page, $title, $content)
    {
        if (!empty($page) && is_int($page)) {
            return response($content, (int)$page);
        }

        return response(
            view(
                'layouts/app',
                [
                    'title'   => $title,
                    'content' => msg() . $content,
                ]
            ),
            200
        );
    }
}