src/Controller/RedirectController.php line 73

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