HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Http/Middleware/SiteResolver.php
<?php

namespace App\Http\Middleware;

use Illuminate\Support\Str;

/**
 * Class SiteResolver
 *
 * Does set the current site depending on the host / domain name using the site config file
 */
class SiteResolver extends AbstractResolver
{
    /**
     * Set site on Application.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        // Check if route should be excluded from resolving
        if ($this->isExcludedFromResolving($request)) {
            return $next($request);
        }

        $site = $this->siteService->getCurrentSite();
        if (!$site->exists && !config('app.multipleSites')) {
            throw new \RuntimeException("SiteResolver: This site doesn't exists. Make a new Site with this slug:'" . $siteSlug . "'  or rename the desired site to this slug.");
        }


        $redirectPath = $this->getHttpsRedirectPathIfNeeded($request->root(), $request->path());
        if (! empty($redirectPath)) {
            return redirect($redirectPath);
        }


        // Check if defined language is one of the languages of this site
        // if not throw error, because it shouldn't happen...
        if (!$site->languages->contains(\App::getLanguage())) {
            throw new \InvalidArgumentException('SiteResolver: Defined app language with iso_2 (' . \App::getLanguage()->iso_2 . ") doesn't exist in this site (id: " . $site->id . '), so check how this could happen.');
        }

        //Return the Request, and check the other routes
        return $next($request);
    }


    /**
     * Get Https redirect path if needed
     *
     * @param string $siteSlug
     * @param string $root
     * @param string $path
     * @return string|null
     */
    private function getHttpsRedirectPathIfNeeded(string $root, string $path)
    {
        // If we aren't on production we don't want to redirect
        if (\App::environment() !== 'production') {
            return null;
        }

        if (Str::contains($root, 'farmfun.nl')) {
            if ($root !== 'https://farmfun.nl') {

                $redirectPath = 'https://farmfun.nl';
                if (isset($path) && !in_array($path, ['', '/', 'index.php'])) {
                    $redirectPath .= '/' . $path;
                }
                
                return $redirectPath;
            }
        }
        else {
            if ($root !== 'https://farmfun.be') {

                $redirectPath = 'https://farmfun.be';
                if (isset($path) && !in_array($path, ['', '/', 'index.php'])) {
                    $redirectPath .= '/' . $path;
                }

                return $redirectPath;
            }
        }
    }
}