1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Controller; |
33
|
|
|
|
34
|
|
|
use App\Entity\UserSystem\User; |
35
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
36
|
|
|
use Symfony\Component\HttpFoundation\Request; |
37
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
38
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
39
|
|
|
|
40
|
|
|
class RedirectController extends AbstractController |
41
|
|
|
{ |
42
|
|
|
protected $default_locale; |
43
|
|
|
protected $translator; |
44
|
|
|
protected $session; |
45
|
|
|
protected $enforce_index_php; |
46
|
|
|
|
47
|
|
|
public function __construct(string $default_locale, TranslatorInterface $translator, SessionInterface $session, bool $enforce_index_php) |
48
|
|
|
{ |
49
|
|
|
$this->default_locale = $default_locale; |
50
|
|
|
$this->session = $session; |
51
|
|
|
$this->translator = $translator; |
52
|
|
|
$this->enforce_index_php = $enforce_index_php; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* This function is called whenever a route was not matching the localized routes. |
57
|
|
|
* The purpose is to redirect the user to the localized version of the page. |
58
|
|
|
* @param Request $request |
59
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
60
|
|
|
*/ |
61
|
|
|
public function addLocalePart(Request $request) |
62
|
|
|
{ |
63
|
|
|
//By default we use the global default locale |
64
|
|
|
$locale = $this->default_locale; |
65
|
|
|
|
66
|
|
|
//Check if a user has set a preferred language setting: |
67
|
|
|
$user = $this->getUser(); |
68
|
|
|
if (($user instanceof User) && !empty($user->getLanguage())) { |
69
|
|
|
$locale = $user->getLanguage(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
//Check if the user needs to change the password. In that case redirect him to settings_page |
73
|
|
|
if ($user instanceof User && $user->isNeedPwChange()) { |
74
|
|
|
$this->session->getFlashBag()->add('warning', $this->translator->trans('flash.password_change_needed')); |
75
|
|
|
return $this->redirectToRoute('user_settings', ['_locale' => $locale]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//$new_url = str_replace($request->getPathInfo(), '/' . $locale . $request->getPathInfo(), $request->getUri()); |
79
|
|
|
$new_url = $request->getUriForPath('/' . $locale . $request->getPathInfo()); |
80
|
|
|
|
81
|
|
|
//If either mod_rewrite is not enabled or the index.php version is enforced, add index.php to the string |
82
|
|
|
if (($this->enforce_index_php || !$this->checkIfModRewriteAvailable()) |
83
|
|
|
&& strpos($new_url, 'index.php') === false) { |
84
|
|
|
//Like Request::getUriForPath only with index.php |
85
|
|
|
$new_url = $request->getSchemeAndHttpHost(). $request->getBaseUrl().'/index.php/' . $locale . $request->getPathInfo(); |
86
|
|
|
} |
87
|
|
|
return $this->redirect($new_url); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check if mod_rewrite is availabe (URL rewriting is possible). |
92
|
|
|
* If this is true, we can redirect to /en, otherwise we have to redirect to index.php/en. |
93
|
|
|
* When the PHP is not used via Apache SAPI, we just assume that URL rewriting is available |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function checkIfModRewriteAvailable() |
97
|
|
|
{ |
98
|
|
|
if (!function_exists('apache_get_modules')) { |
99
|
|
|
//If we can not check for apache modules, we just hope for the best and assume url rewriting is available |
100
|
|
|
//If you want to enforce index.php versions of the url, you can override this via ENV vars. |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
//Check if the mod_rewrite module is loaded |
105
|
|
|
return in_array('mod_rewrite', apache_get_modules(), false); |
106
|
|
|
} |
107
|
|
|
} |