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/SBogers95/rentman.io/app/Http/Middleware/SiteResolver.php
<?php

namespace App\Http\Middleware;

use App\Komma\Sites\SiteServiceInterface;

/**
 * Class SiteResolver
 *
 * Does set the current site depending on the host / domain name using the site config file
 */
final class SiteResolver extends AbstractResolver
{
    /** @var SiteServiceInterface */
    private $siteService;

    public function __construct()
    {
        parent::__construct();
        $this->siteService = \App::make(SiteServiceInterface::class);
    }

    /**
     * Set site on Application.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        // Check if middleware could be skipped
        if ($this->isKmsRoute($request)) {
            return $next($request);
        }

        if (! \Config::get('app.multipleSites')) {
            $this->siteService->setCurrentSiteToDefault();
        // 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('site.domains');
            $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");
                        }
                    }
                }
            }

            // Find the site by the active site slug
            $this->siteService->setCurrentSiteBySlug($activeSiteSlug);

            // Check if the set site is on the main domain and has https
            $secureMainRoot = 'https://'.\Config::get('site.domains.'.$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);
            }
        }

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

        // 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);
    }
}