add subdomain based redirection; redirect chat.lbry.io

This commit is contained in:
Jeremy Kauffman 2017-10-24 14:08:24 -04:00
parent bccdf1277e
commit abe242e86e
2 changed files with 36 additions and 0 deletions

View file

@ -58,6 +58,7 @@ class Controller
public static function execute($method, $uri) public static function execute($method, $uri)
{ {
$router = static::getRouterWithRoutes(); $router = static::getRouterWithRoutes();
static::performSubdomainRedirects();
try try
{ {
$dispatcher = new Routing\Dispatcher($router->getData()); $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 protected static function getRouterWithRoutes(): \Routing\RouteCollector
{ {
$router = new Routing\RouteCollector(); $router = new Routing\RouteCollector();

View file

@ -92,6 +92,30 @@ class Request
return static::getHttpHeader('Host') ? rtrim(static::getHttpHeader('Host'), '.') : ''; 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 public static function getHostAndProto(): string
{ {
return (static::isSSL() ? 'https' : 'http') . '://' . static::getHost(); return (static::isSSL() ? 'https' : 'http') . '://' . static::getHost();