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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
<?php
/**
* PSR 15 middleware Stud.IP initialization
*
* 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.
*
* @author André Noack <noack@data-quest.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 6.0
*/
namespace Studip\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
final class SeminarOpenMiddleware implements MiddlewareInterface
{
public function __construct(private ResponseFactoryInterface $response_factory)
{
}
/**
* @param $page_code
*
* @return ResponseInterface
*/
public function startpageRedirect($page_code): ResponseInterface
{
switch ($page_code) {
case 1:
case 2:
$jump_page = 'dispatch.php/my_courses';
break;
case 3:
$jump_page = 'dispatch.php/calendar/schedule';
break;
case 4:
$jump_page = 'dispatch.php/contact';
break;
case 5:
$jump_page = 'dispatch.php/calendar/calendar';
break;
case 6:
// redirect to global blubberstream
// or no redirection if blubber isn't active
if (\Config::get()->BLUBBER_GLOBAL_MESSENGER_ACTIVATE) {
$jump_page = 'dispatch.php/blubber';
}
break;
case 7:
$jump_page = 'dispatch.php/contents/overview';
break;
}
$response = $this->response_factory->createResponse(302);
return $response->withHeader('Location', \URLHelper::getURL($jump_page));
}
/**
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
global $user, $perm, $_language_path;
//INITS
$seminar_open_redirected = false;
$user_did_login = false;
// session init starts here
if (empty($_SESSION['SessionStart'])) {
$_SESSION['SessionStart'] = time();
$_SESSION['object_cache'] = [];
// try to get accepted languages from browser
if (!isset($_SESSION['_language'])) {
$_SESSION['_language'] = get_accepted_languages();
}
if (!$_SESSION['_language']) {
$_SESSION['_language'] = \Config::get()->DEFAULT_LANGUAGE;
}
}
// user init starts here
if (is_object($user) && $user->id != "nobody") {
if ($_SESSION['SessionStart'] > \UserConfig::get($user->id)->CURRENT_LOGIN_TIMESTAMP) { // just logged in
// store old CURRENT_LOGIN in LAST_LOGIN and set CURRENT_LOGIN to start of session
\UserConfig::get($user->id)->store(
'LAST_LOGIN_TIMESTAMP', \UserConfig::get($user->id)->CURRENT_LOGIN_TIMESTAMP
);
\UserConfig::get($user->id)->store('CURRENT_LOGIN_TIMESTAMP', $_SESSION['SessionStart']);
//find current semester and store it in $_SESSION['_default_sem']
$current_sem = \Semester::findDefault();
$_SESSION['_default_sem'] = $current_sem->semester_id ?? null;
//redirect user to another page if he want to, redirect is deferred to allow plugins to catch the UserDidLogin notification
if (
\UserConfig::get($user->id)->PERSONAL_STARTPAGE > 0
&& !isset($_SESSION['redirect_after_login'])
&& !$perm->have_perm('root')
) {
$seminar_open_redirected = true;
}
unset($_SESSION['redirect_after_login']);
if (isset($_SESSION['contrast'])) {
\UserConfig::get($GLOBALS['user']->id)->store('USER_HIGH_CONTRAST', $_SESSION['contrast']);
unset($_SESSION['contrast']);
}
// store last language click
if (!empty($_SESSION['forced_language'])) {
\User::findCurrent()->preferred_language = $_SESSION['forced_language'];
\User::findCurrent()->store();
$_SESSION['_language'] = $_SESSION['forced_language'];
}
$_SESSION['forced_language'] = null;
$user_did_login = true;
}
\TwoFactorAuth::get()->secureSession();
}
if (!empty($_SESSION['contrast']) || \UserConfig::get($GLOBALS['user']->id)->USER_HIGH_CONTRAST) {
\PageLayout::addStylesheet('accessibility.css');
}
// init of output via I18N
$_language_path = init_i18n($_SESSION['_language']);
//force reload of config to get translated data
include $GLOBALS['STUDIP_BASE_PATH'] . '/config/config.inc.php';
// Try to select the course or institute given by the parameter 'cid'
// in the current request.
$course_id = (\Request::int('cancel_login') && (!is_object($user) || $user->id === 'nobody'))
? null
: \Request::option('cid');
// Select the current course or institute if we got one from 'cid' or session.
// This also binds Context::getId()
// to the URL parameter 'cid' for all generated links.
if (isset($course_id)) {
\Context::set($course_id);
unset($course_id);
}
if (\Request::int('disable_plugins') !== null && ($user->id === 'nobody' || $perm->have_perm('root'))) {
// deactivate non-core plugins
\PluginManager::getInstance()->setPluginsDisabled(\Request::int('disable_plugins'));
}
// load the default set of plugins
\PluginEngine::loadPlugins();
// add navigation item for profile: add modules
if (\Navigation::hasItem('/profile/edit')) {
$plus_nav = new \Navigation(_('Mehr …'), 'dispatch.php/profilemodules/index');
$plus_nav->setDescription(_('Mehr Stud.IP-Funktionen für Ihr Profil'));
\Navigation::addItem('/profile/modules', $plus_nav);
}
if ($user_did_login) {
if (isset($_SESSION[\StudipAuthOAuth2::class]['redirect'])) {
$redirect = $_SESSION[\StudipAuthOAuth2::class]['redirect'];
unset($_SESSION[\StudipAuthOAuth2::class]);
$response = $this->response_factory->createResponse(302);
return $response->withHeader('Location', \URLHelper::getURL($redirect));
}
\NotificationCenter::postNotification('UserDidLogin', $user->id);
}
if (!\Request::isXhr() && $perm->have_perm('root')) {
if (!isset($_SESSION['migration-check']) || $_SESSION['migration-check']['timestamp'] < time() - 5 * 60) {
$migrator = new \Migrator(
"{$GLOBALS['STUDIP_BASE_PATH']}/db/migrations",
new \DBSchemaVersion('studip')
);
$_SESSION['migration-check'] = [
'disabled' => $_SESSION['migration-check']['disabled'] ?? false,
'timestamp' => time(),
'count' => $migrator->pendingMigrations()
];
}
if (\Request::option('stop-migration-nag')) {
$_SESSION['migration-check']['disabled'] = true;
}
if (empty($_SESSION['migration-check']['disabled'])
&& $_SESSION['migration-check']['count'] > 0
) {
$info = sprintf(
_('Es gibt %u noch nicht ausgeführte Migration(en).'),
$_SESSION['migration-check']['count']
);
$message = \MessageBox::info($info, [
sprintf(
_('Zur %sMigrationsseite%s'),
'<a class="link-intern" href="' . \URLHelper::getLink('web_migrate.php') . '">',
'</a>'
),
sprintf(
'<small><a href="%s">%s</a></small>',
\URLHelper::getLink('', ['stop-migration-nag' => true]),
_('Diese Nachricht bis zum nächsten Login nicht mehr anzeigen')
)
]
);
\PageLayout::postMessage($message, 'migration-info');
}
}
if (
$GLOBALS['perm']->have_perm('root')
&& \Config::get()->MIGRATION_START_VERSION
&& \Config::get()->MIGRATION_START_VERSION < \StudipVersion::getStudipVersion(true)
&& !\Config::get()->UPDATE_NEWS_SEEN
) {
$message = \MessageBox::info(
_('Sie haben ein Stud.IP-Update durchgeführt.'),
[
sprintf(
_('Zu den %sRelease-Notes%s'),
'<a class="link-intern" href="' . \URLHelper::getLink('dispatch.php/root_assistant') . '" data-dialog>',
'</a>'
),
]
);
\PageLayout::postMessage($message, 'release-notes');
}
if ($seminar_open_redirected) {
return $this->startpageRedirect(\UserConfig::get($user->id)->PERSONAL_STARTPAGE);
}
// Show terms on first login
if (
is_object($GLOBALS['user'])
&& $GLOBALS['user']->needsToAcceptTerms()
&& !match_route('dispatch.php/terms')
&& !match_route('dispatch.php/siteinfo/*')
) {
if (!\Request::isXhr()) {
$response = $this->response_factory->createResponse(302);
return $response->withHeader(
'Location', \URLHelper::getURL(
'dispatch.php/terms',
[
'return_to' => $_SERVER['REQUEST_URI'],
'redirect_token' => \Token::create(600)
],
true
)
);
} else {
throw new \Trails\Exception(400);
}
}
if (
\Config::get()->USER_VISIBILITY_CHECK
&& is_object($GLOBALS['user'])
&& $GLOBALS['user']->id !== 'nobody'
&& !(
\Config::get()->DOZENT_ALWAYS_VISIBLE
&& $perm->get_perm() === 'dozent'
)
&& !match_route('dispatch.php/siteinfo/*')
&& $GLOBALS['user']->visible === 'unknown'
) {
require_once('lib/user_visible.inc.php');
$response = $this->response_factory->createResponse(200);
$response->getBody()->write((string)first_decision($GLOBALS['user']->id));
return $response;
}
return $handler->handle($request);
}
}
|