Part-DB.Part-DB-server/src/Controller/RedirectController.php

93 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
2020-02-22 18:14:36 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
namespace App\Controller;
use App\Entity\UserSystem\User;
2020-01-05 22:49:00 +01:00
use function function_exists;
use function in_array;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2020-01-05 22:49:00 +01:00
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
class RedirectController extends AbstractController
{
2022-09-18 22:59:31 +02:00
protected string $default_locale;
protected TranslatorInterface $translator;
protected bool $enforce_index_php;
public function __construct(string $default_locale, TranslatorInterface $translator, bool $enforce_index_php)
{
$this->default_locale = $default_locale;
$this->translator = $translator;
$this->enforce_index_php = $enforce_index_php;
}
/**
* This function is called whenever a route was not matching the localized routes.
* The purpose is to redirect the user to the localized version of the page.
*/
2020-02-02 14:05:36 +01:00
public function addLocalePart(Request $request): RedirectResponse
{
2023-04-15 23:14:53 +02:00
//By default, we use the global default locale
$locale = $this->default_locale;
//Check if a user has set a preferred language setting:
$user = $this->getUser();
2020-08-21 21:36:22 +02:00
if (($user instanceof User) && !empty($user->getLanguage())) {
2019-09-16 22:04:59 +02:00
$locale = $user->getLanguage();
}
$new_url = $request->getUriForPath('/'.$locale.$request->getPathInfo());
//If either mod_rewrite is not enabled or the index.php version is enforced, add index.php to the string
2020-08-21 21:36:22 +02:00
if (($this->enforce_index_php || !$this->checkIfModRewriteAvailable())
&& !str_contains($new_url, 'index.php')) {
//Like Request::getUriForPath only with index.php
$new_url = $request->getSchemeAndHttpHost().$request->getBaseUrl().'/index.php/'.$locale.$request->getPathInfo();
}
//Add the query string
$new_url .= $request->getQueryString() ? '?'.$request->getQueryString() : '';
return $this->redirect($new_url);
}
/**
2019-11-10 14:00:56 +01:00
* Check if mod_rewrite is available (URL rewriting is possible).
* If this is true, we can redirect to /en, otherwise we have to redirect to index.php/en.
* When the PHP is not used via Apache SAPI, we just assume that URL rewriting is available.
*/
2020-02-02 14:05:36 +01:00
public function checkIfModRewriteAvailable(): bool
{
2020-08-21 21:36:22 +02:00
if (!function_exists('apache_get_modules')) {
//If we can not check for apache modules, we just hope for the best and assume url rewriting is available
//If you want to enforce index.php versions of the url, you can override this via ENV vars.
return true;
}
//Check if the mod_rewrite module is loaded
2020-01-05 22:49:00 +01:00
return in_array('mod_rewrite', apache_get_modules(), false);
}
}