From aaa3f1aa88817c28464d00eaff2d5ffcb24a5f26 Mon Sep 17 00:00:00 2001 From: hide_d Date: Sat, 26 Dec 2020 15:48:05 +0900 Subject: [PATCH] =?UTF-8?q?DummySession=20=EC=A4=80=EB=B9=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hwe/j_server_basic_info.php | 2 +- src/sammo/DummySession.php | 267 ++++++++++++++++++++++++++++++++++++ src/sammo/Session.php | 4 +- src/sammo/Setting.php | 11 ++ 4 files changed, 281 insertions(+), 3 deletions(-) create mode 100644 src/sammo/DummySession.php diff --git a/hwe/j_server_basic_info.php b/hwe/j_server_basic_info.php index 9214cb68..7e04d937 100644 --- a/hwe/j_server_basic_info.php +++ b/hwe/j_server_basic_info.php @@ -43,7 +43,7 @@ $session = Session::requireLogin([ 'game'=>null, 'me'=>null ])->setReadOnly(); -$userID = Session::getUserID(); +$userID = $session->userID; if(!class_exists('\\sammo\\DB')){ Json::die([ diff --git a/src/sammo/DummySession.php b/src/sammo/DummySession.php new file mode 100644 index 00000000..320acc13 --- /dev/null +++ b/src/sammo/DummySession.php @@ -0,0 +1,267 @@ +sessionInfo = []; + return $this; + } + + + public function __construct() + { + $this->set('userID', -1); + $this->set('userName', 'Dummy'); + $this->set('ip', '127.0.0.1'); + $this->set('time', time()); + $this->set('userGrade', '-1'); + $this->set('acl', '[]'); + $this->set('reqOTP', false); + $this->set('tokenValidUntil', null); + + } + + public function setReadOnly(): Session + { + $this->writeClosed = true; + return $this; + } + + private function set(string $name, $value) + { + $this->sessionInfo[$name] = $value; + } + + public function __get(string $name) + { + if ($name == 'generalID') { + if (!class_exists('\\sammo\\UniqueConst')){ + return null; + } + return $this->get(UniqueConst::$serverID.static::GAME_KEY_GENERAL_ID); + } + if ($name == 'generalName') { + if (!class_exists('\\sammo\\UniqueConst')){ + return null; + } + return $this->get(UniqueConst::$serverID.static::GAME_KEY_GENERAL_NAME); + } + return $this->get($name); + } + + private function get(string $name) + { + return Util::array_get($_SESSION[$name]); + } + + public function login(int $userID, string $userName, int $grade, bool $reqOTP, ?string $tokenValidUntil, array $acl): Session + { + $this->set('userID', $userID); + $this->set('userName', $userName); + $this->set('ip', Util::get_client_ip(true)); + $this->set('time', time()); + $this->set('userGrade', $grade); + $this->set('acl', $acl); + $this->set('reqOTP', $reqOTP); + $this->set('tokenValidUntil', $tokenValidUntil); + return $this; + } + + public function setReqOTP(bool $reqOTP=false, string $tokenValidUntil){ + $this->set('reqOTP', $reqOTP); + $this->set('tokenValidUntil', $tokenValidUntil); + } + + + public function logout(): Session + { + if ($this->writeClosed) { + $this->restart(); + } + if (class_exists('\\sammo\\UniqueConst')) { + $this->logoutGame(); + } + + $this->set('userID', null); + $this->set('userName', null); + $this->set('userGrade', null); + $this->set('acl', null); + $this->set('reqOTP', null); + $this->set('time', time()); + $this->set('lastMsgGet', null); + return $this; + } + + public function loginGame(&$result = null): Session + { + $userID = $this->userID; + if (!$userID) { + if ($result !== null) { + $result = false; + } + return $this; + } + + if (!class_exists('\\sammo\\UniqueConst')) { + if ($result !== null) { + $result = false; + } + return $this; + } + + $serverID = UniqueConst::$serverID; + + $loginDate = $this->get($serverID.static::GAME_KEY_DATE); + $generalID = $this->get($serverID.static::GAME_KEY_GENERAL_ID); + $generalName = $this->get($serverID.static::GAME_KEY_GENERAL_NAME); + $deadTime = $this->get($serverID.static::GAME_KEY_EXPECTED_DEADTIME); + + $now = time(); + if ( + $generalID && $generalName && $loginDate && $deadTime + && $loginDate + 1800 > $now && $deadTime > $now + ) { + //로그인 정보는 30분간 유지한다. + if ($result !== null) { + $result = true; + } + return $this; + } + + if ($generalID || $generalName || $loginDate || $deadTime) { + $this->logoutGame(); + } + + $generalID = -1; + $generalName = 'DummyGeneral'; + $deadTime = $now+60*60*24; + + $this->set($serverID.static::GAME_KEY_DATE, $now); + $this->set($serverID.static::GAME_KEY_GENERAL_ID, $generalID); + $this->set($serverID.static::GAME_KEY_GENERAL_NAME, $generalName); + $this->set($serverID.static::GAME_KEY_EXPECTED_DEADTIME, $deadTime); + return $this; + } + + public function logoutGame(): Session + { + if ($this->writeClosed) { + $this->restart(); + } + $serverID = UniqueConst::$serverID; + $this->set($serverID.static::GAME_KEY_DATE, null); + $this->set($serverID.static::GAME_KEY_GENERAL_ID, null); + $this->set($serverID.static::GAME_KEY_GENERAL_NAME, null); + $this->set($serverID.static::GAME_KEY_EXPECTED_DEADTIME, null); + return $this; + } + + + /** + * 로그인 유저의 전역 grade를 받아옴 + * @return int|null + */ + public static function getUserGrade(bool $requireLogin = false, string $exitPath = '..') + { + if ($requireLogin) { + $obj = self::requireLogin($exitPath); + } else { + $obj = self::getInstance(); + } + + return $obj->userGrade; + } + + /** + * 로그인한 유저의 전역 id(숫자)를 받아옴 + * + * @return int|null + */ + public static function getUserID(bool $requireLogin = false, string $exitPath = '..') + { + if ($requireLogin) { + $obj = self::requireLogin($exitPath); + } else { + $obj = self::getInstance(); + } + + return $obj->userID; + } + + /** + * + */ + public static function getGeneralID(bool $requireLogin = false, string $exitPath = '..') + { + if ($requireLogin) { + $obj = self::requireLogin($exitPath); + } else { + $obj = self::getInstance(); + } + + return $obj->generalID??0; + } + + public function isLoggedIn(bool $ignoreOTP = false): bool + { + if(!$ignoreOTP){ + if($this->reqOTP){ + return false; + } + if(!$this->tokenValidUntil){ + return false; + } + $now = TimeUtil::now(); + if($this->tokenValidUntil < $now){ + return false; + } + } + + if ($this->userID) { + return true; + } else { + return false; + } + } + + public function isGameLoggedIn(): bool + { + if ($this->generalID) { + return true; + } else { + return false; + } + } + + public function __destruct() + { + } +} diff --git a/src/sammo/Session.php b/src/sammo/Session.php index c0689e49..a9b198dc 100644 --- a/src/sammo/Session.php +++ b/src/sammo/Session.php @@ -83,7 +83,7 @@ class Session public static function requireLogin($actionOnError = '..'): Session { - $session = Session::getInstance(); + $session = static::getInstance(); if ($session->isLoggedIn()) { return $session; } @@ -93,7 +93,7 @@ class Session public static function requireGameLogin($actionOnError = '..'): Session { - $session = Session::requireLogin($actionOnError)->loginGame(); + $session = static::requireLogin($actionOnError)->loginGame(); if ($session->generalID) { return $session; diff --git a/src/sammo/Setting.php b/src/sammo/Setting.php index 9a8210f4..af9d29ae 100644 --- a/src/sammo/Setting.php +++ b/src/sammo/Setting.php @@ -132,4 +132,15 @@ class Setting } return true; } + + public function getDetailStatus() + { + if(!$this->isRunning()){ + return [ + 'game'=>null, + 'me'=>null + ]; + } + throw new NotImplementedException(); + } }