1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* 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 |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Controller; |
23
|
|
|
|
24
|
|
|
use App\Entity\UserSystem\User; |
25
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
26
|
|
|
use Symfony\Component\HttpFoundation\Request; |
27
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
28
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
29
|
|
|
|
30
|
|
|
class RedirectController extends AbstractController |
31
|
|
|
{ |
32
|
|
|
protected $default_locale; |
33
|
|
|
protected $translator; |
34
|
|
|
protected $session; |
35
|
|
|
protected $enforce_index_php; |
36
|
|
|
|
37
|
|
|
public function __construct(string $default_locale, TranslatorInterface $translator, SessionInterface $session, bool $enforce_index_php) |
38
|
|
|
{ |
39
|
|
|
$this->default_locale = $default_locale; |
40
|
|
|
$this->session = $session; |
41
|
|
|
$this->translator = $translator; |
42
|
|
|
$this->enforce_index_php = $enforce_index_php; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* This function is called whenever a route was not matching the localized routes. |
47
|
|
|
* The purpose is to redirect the user to the localized version of the page. |
48
|
|
|
* |
49
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
50
|
|
|
*/ |
51
|
|
|
public function addLocalePart(Request $request) |
52
|
|
|
{ |
53
|
|
|
//By default we use the global default locale |
54
|
|
|
$locale = $this->default_locale; |
55
|
|
|
|
56
|
|
|
//Check if a user has set a preferred language setting: |
57
|
|
|
$user = $this->getUser(); |
58
|
|
|
if (($user instanceof User) && !empty($user->getLanguage())) { |
59
|
|
|
$locale = $user->getLanguage(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
//$new_url = str_replace($request->getPathInfo(), '/' . $locale . $request->getPathInfo(), $request->getUri()); |
63
|
|
|
$new_url = $request->getUriForPath('/'.$locale.$request->getPathInfo()); |
64
|
|
|
|
65
|
|
|
//If either mod_rewrite is not enabled or the index.php version is enforced, add index.php to the string |
66
|
|
|
if (($this->enforce_index_php || !$this->checkIfModRewriteAvailable()) |
67
|
|
|
&& false === strpos($new_url, 'index.php')) { |
68
|
|
|
//Like Request::getUriForPath only with index.php |
69
|
|
|
$new_url = $request->getSchemeAndHttpHost().$request->getBaseUrl().'/index.php/'.$locale.$request->getPathInfo(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->redirect($new_url); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if mod_rewrite is available (URL rewriting is possible). |
77
|
|
|
* If this is true, we can redirect to /en, otherwise we have to redirect to index.php/en. |
78
|
|
|
* When the PHP is not used via Apache SAPI, we just assume that URL rewriting is available. |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function checkIfModRewriteAvailable() |
83
|
|
|
{ |
84
|
|
|
if (!\function_exists('apache_get_modules')) { |
85
|
|
|
//If we can not check for apache modules, we just hope for the best and assume url rewriting is available |
86
|
|
|
//If you want to enforce index.php versions of the url, you can override this via ENV vars. |
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
//Check if the mod_rewrite module is loaded |
91
|
|
|
return \in_array('mod_rewrite', apache_get_modules(), false); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|