LocalizationRedirect.php 1.7 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php

namespace App\Http\Middleware;

use Closure;
use LaravelLocalization;

class LocalizationRedirect
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
19 20 21
        $locale = LaravelLocalization::getCurrentLocale();
        $host = $request->getHttpHost();

22

23
        // We only redirect to the TLDs in the production version and exclude our onion domain
24
        if(env("APP_ENV", "") !== "production" || $host === "b7cxf4dkdsko6ah2.onion" || $request->is('metrics')){
Dominik Hebeler's avatar
Dominik Hebeler committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            return $next($request);
        }

        if($host !== "metager.de" && $locale == "de"){
            $url = $request->url();
            $url = str_replace($host, "metager.de", $url);
            $url = preg_replace("/^http:\/\//", "https://", $url);
            $url = preg_replace("/^(https:\/\/[^\/]+)\/de/", "$1", $url);
            return redirect($url);
        }

        if($host !== "metager.es" && $locale == "es"){
            $url = $request->url();
            $url = str_replace($host, "metager.es", $url);
            $url = preg_replace("/^http:\/\//", "https://", $url);
            $url = preg_replace("/^(https:\/\/[^\/]+)\/es/", "$1", $url);
            return redirect($url);
        }

        if($host !== "metager.org" && $locale == "en"){
            $url = $request->url();
            $url = str_replace($host, "metager.org", $url);
            $url = preg_replace("/^http:\/\//", "https://", $url);
            $url = preg_replace("/^(https:\/\/[^\/]+)\/en/", "$1", $url);
            return redirect($url);
        }

        return $next($request);
    }
}