1: <?php
2: /*
3: * SimpleID
4: *
5: * Copyright (C) Kelvin Mo 2014-2025
6: *
7: * This program is free software; you can redistribute it and/or
8: * modify it under the terms of the GNU General Public
9: * License as published by the Free Software Foundation; either
10: * version 2 of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program; if not, write to the Free
19: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20: *
21: */
22:
23: namespace SimpleID\Base;
24:
25: use Psr\Log\LogLevel;
26: use SimpleID\Module;
27: use SimpleID\ModuleManager;
28: use SimpleID\Auth\AuthManager;
29: use SimpleID\Crypt\SecurityToken;
30: use SimpleID\Util\UI\Template;
31:
32: /**
33: * This module contains generic routes for SimpleID.
34: */
35: class IndexModule extends Module {
36: static function init($f3) {
37: $f3->route('GET|POST @index: /', 'SimpleID\Base\IndexModule->index');
38: $f3->route('GET|POST /continue/@token', 'SimpleID\Base\IndexModule->continueRequest');
39: }
40:
41: /**
42: * The default route, called when the q parameter is missing or is invalid.
43: *
44: * This function performs the following:
45: *
46: * - This calls the index hook to determine whether other modules would handle this
47: * request
48: * - Otherwise, if MyModule is loaded, the dashboard is displayed
49: * - If MyModule is not loaded, a blank page is displayed
50: *
51: * @return void
52: */
53: public function index() {
54: $mgr = ModuleManager::instance();
55:
56: $this->logger->log(LogLevel::DEBUG, 'SimpleID\Base\IndexModule->index');
57: header('Vary: Accept');
58:
59: $event = new RouteContentNegotiationEvent($this->f3->get('ALIAS'), $this->f3->get('REQUEST'), $this->f3->get('SERVER.HTTP_ACCEPT'));
60: $dispatcher = \Events::instance();
61: $dispatcher->dispatch($event);
62: if ($event->isPropagationStopped()) return;
63:
64: $auth = AuthManager::instance();
65:
66: if (!$auth->isLoggedIn()) {
67: /** @var \SimpleID\Auth\AuthModule $auth_module */
68: $auth_module = $mgr->getModule('SimpleID\Auth\AuthModule');
69: $auth_module->loginForm();
70: } elseif ($mgr->isModuleLoaded('SimpleID\Base\MyModule')) {
71: $this->f3->mock('GET /my/dashboard');
72: } else {
73: $tpl = Template::instance();
74: $this->f3->set('user_header', true);
75: $this->f3->set('title', 'SimpleID');
76: print $tpl->render('page.html');
77: }
78: }
79:
80: /**
81: * Continues a previously saved request.
82: *
83: * The request is saved as a {@link RequestState}, encoded
84: * in a {@link SecurityToken}, which is passed through
85: * the <code>token</code> path parameter.
86: *
87: * @param \Base $f3
88: * @param array<string, mixed> $params
89: * @return void
90: */
91: public function continueRequest($f3, $params) {
92: $token = new SecurityToken();
93: $payload = $token->getPayload($params['token']);
94:
95: if ($payload === null) {
96: $this->fatalError($this->f3->get('intl.common.invalid_request'), 400);
97: return;
98: }
99:
100: $request_state = new RequestState($payload);
101: $this->f3->mock($request_state->toF3RoutePattern(), $request_state->getParams());
102: }
103:
104: }
105:
106: ?>