|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Common\Http; |
|
20
|
|
|
|
|
21
|
|
|
use SURFnet\VPN\Common\Http\Exception\HttpException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This hook is used to be able to switch the language without requiring to be |
|
25
|
|
|
* authenticated. As the language switcher is only a user preference stored in |
|
26
|
|
|
* a cookie this is not a problem. This way, even the authentication page can |
|
27
|
|
|
* use the language switcher. |
|
28
|
|
|
*/ |
|
29
|
|
|
class LanguageSwitcherHook implements BeforeHookInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** @var Cookie */ |
|
32
|
|
|
private $cookie; |
|
33
|
|
|
|
|
34
|
|
|
/** @var array */ |
|
35
|
|
|
private $supportedLanguages; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(array $supportedLanguages, Cookie $cookie) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->supportedLanguages = $supportedLanguages; |
|
40
|
|
|
$this->cookie = $cookie; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function executeBefore(Request $request, array $hookData) |
|
44
|
|
|
{ |
|
45
|
|
|
if ('POST' !== $request->getRequestMethod()) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ('/setLanguage' !== $request->getPathInfo()) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$language = $request->getPostParameter('setLanguage', false, 'en_US'); |
|
54
|
|
|
if (!in_array($language, $this->supportedLanguages)) { |
|
55
|
|
|
throw new HttpException('invalid language', 400); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$this->cookie->set('uiLanguage', $language); |
|
59
|
|
|
|
|
60
|
|
|
return new RedirectResponse($request->getHeader('HTTP_REFERER'), 302); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|