diff --git a/controller/Controller.class.php b/controller/Controller.class.php index e07e7422..dcd77942 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -58,6 +58,7 @@ class Controller public static function execute($method, $uri) { $router = static::getRouterWithRoutes(); + static::performSubdomainRedirects(); try { $dispatcher = new Routing\Dispatcher($router->getData()); @@ -75,6 +76,17 @@ class Controller } } + protected static function performSubdomainRedirects() + { + $subDomain = Request::getSubDomain(); + + switch($subDomain) { + case 'chat': + case 'slack': + return static::redirect('https://discordapp.com/invite/U5aRyN6'); + } + } + protected static function getRouterWithRoutes(): \Routing\RouteCollector { $router = new Routing\RouteCollector(); diff --git a/controller/Request.class.php b/controller/Request.class.php index e0ad6a3c..f2badcd4 100644 --- a/controller/Request.class.php +++ b/controller/Request.class.php @@ -92,6 +92,30 @@ class Request return static::getHttpHeader('Host') ? rtrim(static::getHttpHeader('Host'), '.') : ''; } + public static function getSubDomain(): string + { + $urlParts = parse_url(static::getHost()); + $host = $urlParts['host'] ?? ''; + $domainParts = explode('.', $host); + $domainPartCount = count($domainParts); + + if (count($domainParts) < 1) + { + return ''; + } + + $isLocalhost = $domainParts[$domainPartCount - 1] === 'localhost'; + + if (!$isLocalhost && count($domainParts) < 2) + { + return ''; + } + + return $isLocalhost ? + $domainParts[$domainPartCount - 2] : + $domainParts[$domainPartCount - 3]; + } + public static function getHostAndProto(): string { return (static::isSSL() ? 'https' : 'http') . '://' . static::getHost();