|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Common\Http; |
|
11
|
|
|
|
|
12
|
|
|
use fkooman\SeCookie\Cookie; |
|
13
|
|
|
use SURFnet\VPN\Common\Http\Exception\HttpException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This hook is used to be able to switch the language without requiring to be |
|
17
|
|
|
* authenticated. As the language switcher is only a user preference stored in |
|
18
|
|
|
* a cookie this is not a problem. This way, even the authentication page can |
|
19
|
|
|
* use the language switcher. |
|
20
|
|
|
*/ |
|
21
|
|
|
class LanguageSwitcherHook implements BeforeHookInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var \fkooman\SeCookie\Cookie */ |
|
24
|
|
|
private $cookie; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
private $supportedLanguages; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(array $supportedLanguages, Cookie $cookie) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->supportedLanguages = $supportedLanguages; |
|
32
|
|
|
$this->cookie = $cookie; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function executeBefore(Request $request, array $hookData) |
|
36
|
|
|
{ |
|
37
|
|
|
if ('POST' !== $request->getRequestMethod()) { |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ('/setLanguage' !== $request->getPathInfo()) { |
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$language = $request->getPostParameter('setLanguage', false, 'en_US'); |
|
46
|
|
|
if (!in_array($language, $this->supportedLanguages)) { |
|
47
|
|
|
throw new HttpException('invalid language', 400); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$this->cookie->set('uiLanguage', $language); |
|
51
|
|
|
|
|
52
|
|
|
return new RedirectResponse($request->getHeader('HTTP_REFERER'), 302); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|