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/lc-hydraulics.komma.nl/app/Http/Middleware/SitesResolver.php
<?php


namespace App\Http\Middleware;


use App\KommaApp\Sites\Models\Site;
use Illuminate\Support\Facades\URL;

class SitesResolver
{

    /**
     * Set site on Application
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {

        //Skip this when it is an Kms route
        if ($request->segment(1) == 'kms') {
            return $next($request);
        }

        if ( ! \Config::get('app.multipleSites')) {

            \App::setSite(Site::find(1));
            // If there is only one site, the redirect should be handled by the web.config
        }else {

            $root = $request->root();
            $path = $request->path();

            $siteDomains = \Config::get('domains.' . \App::environment() );
            $activeSiteSlug = '';

            foreach ($siteDomains as $siteKey => $domains) {
                foreach ($domains as $domain) {
                    if (str_contains($root, $domain)) {
                        if ($activeSiteSlug == '') {
                            $activeSiteSlug = $siteKey;
                        }else {
                            throw new \InvalidArgumentException("Domain isn't specific enough because active site key is overruled by other site, best practice is to define domains with there top-level domain in the site config");
                        }
                    }
                }
            }

            // Throw error if no site is found
            if ($activeSiteSlug === '') {
                throw new \InvalidArgumentException("No site found for the domain: " . $root);
            }

            // Check if the set site is on the main domain and has https
            $secureMainRoot = 'https://' . \Config::get('domains.' . \App::environment() . '.' . $activeSiteSlug)[0];
            if (\App::environment() == 'production' && $root !== $secureMainRoot) {

                // If there is a path defined, append it to the redirect string
                $redirectPath = $secureMainRoot;
                if (isset($path) && ! in_array($path, ['', '/', 'index.php'])) {
                    $redirectPath .= '/' . $path;
                }

                return redirect($redirectPath);
            }

            // Find the site by the active site slug
            $site = Site::where('slug', $activeSiteSlug)->first();
            \App::setSite($site);

            // 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("Defined app language doesn't exist in this site, so check how this could happen.");
            }
        }

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

    }

}