diff --git a/src/Http/Handlers/SockChatHandler.php b/src/Http/Handlers/SockChatHandler.php index 9104a465..6533957e 100644 --- a/src/Http/Handlers/SockChatHandler.php +++ b/src/Http/Handlers/SockChatHandler.php @@ -271,47 +271,15 @@ final class SockChatHandler extends Handler { public function login(HttpResponse $response, HttpRequest $request) { $currentUser = User::getCurrent(); - - if($currentUser === null) { - $response->redirect(url('auth-login')); - return; - } - $params = $request->getQueryParams(); - - try { - $token = UserChatToken::create($currentUser); - } catch(UserChatTokenCreationFailedException $ex) { - return 500; - } - - if(MSZ_DEBUG && isset($params['dump'])) { - $ipAddr = $request->getRemoteAddress(); - $hash = hash_hmac('sha256', implode('#', [$token->getUserId(), $token->getToken(), $ipAddr]), $this->hashKey); - - $response->setText(sprintf( - '/_sockchat.php?user_id=%d&token=%s&ip=%s&hash=%s', - $token->getUserId(), - $token->getToken(), - urlencode($ipAddr), - $hash - )); - return; - } - - $cookieName = Config::get('sockChat.cookie', Config::TYPE_STR, 'sockchat_auth'); - $cookieData = implode('_', [$token->getUserId(), $token->getToken()]); - $cookieDomain = '.' . $request->getHeaderLine('Host'); - setcookie($cookieName, $cookieData, $token->getExpirationTime(), '/', $cookieDomain); - $configKey = isset($params['legacy']) ? 'sockChat.chatPath.legacy' : 'sockChat.chatPath.normal'; $chatPath = Config::get($configKey, Config::TYPE_STR, '/'); - if(MSZ_DEBUG) { - $response->setText(sprintf('Umi.Cookies.Set(\'%s\', \'%s\');', $cookieName, $cookieData)); - } else { - $response->redirect($chatPath); - } + $response->redirect( + $currentUser === null + ? url('auth-login', ['redirect' => $chatPath]) + : $chatPath + ); } public function bump(HttpResponse $response, HttpRequest $request): void { diff --git a/src/url.php b/src/url.php index 15d347c8..80bd4db7 100644 --- a/src/url.php +++ b/src/url.php @@ -265,13 +265,14 @@ function url_prefix(bool $trailingSlash = true): string { function is_local_url(string $url): bool { $length = mb_strlen($url); - if($length < 1) { + if($length < 1) return false; - } - if($url[0] === '/' && ($length > 1 ? $url[1] !== '/' : true)) { + if($url[0] === '/' && ($length > 1 ? $url[1] !== '/' : true)) return true; - } - return starts_with($url, url_prefix()); + if(starts_with($url, url_prefix())) + return true; + + return ends_with(parse_url($url, PHP_URL_HOST), '.' . $_SERVER['HTTP_HOST']); }