feat: APIHelper에서 recovery 반환 타입 경로 추가

This commit is contained in:
2023-03-17 01:18:20 +09:00
parent 1d4707fb20
commit 6b9cfe46a0
+91 -52
View File
@@ -2,16 +2,48 @@
namespace sammo; namespace sammo;
use sammo\Enums\APIRecoveryType;
class APIHelper class APIHelper
{ {
private function __construct() private function __construct()
{ {
//static only //static only
} }
public static function getSession(int $sessionMode): Session | APIRecoveryType
{
if ($sessionMode === BaseAPI::NO_SESSION) {
return DummySession::getInstance();
}
$session = Session::getInstance();
if($sessionMode & BaseAPI::REQ_LOGIN || $sessionMode & BaseAPI::REQ_GAME_LOGIN){
if(!$session->isLoggedIn()){
return APIRecoveryType::Login;
}
}
if($sessionMode & BaseAPI::REQ_GAME_LOGIN){
if(!$session->isGameLoggedIn()){
$tmpResult = false;
$session->loginGame($tmpResult);
if(!$tmpResult){
return APIRecoveryType::Gateway;
}
}
}
if ($sessionMode & BaseAPI::REQ_READ_ONLY) {
$session->setReadOnly();
}
return $session;
}
public static function launch(string $rootPath, string $actionPath, array $eParams = [], bool $loadRawInput = true) public static function launch(string $rootPath, string $actionPath, array $eParams = [], bool $loadRawInput = true)
{ {
if($loadRawInput){ if ($loadRawInput) {
try { try {
$rawInput = file_get_contents('php://input'); $rawInput = file_get_contents('php://input');
$input = Json::decode($rawInput); $input = Json::decode($rawInput);
@@ -19,12 +51,11 @@ class APIHelper
Json::dieWithReason($e->getMessage()); Json::dieWithReason($e->getMessage());
$input = null; $input = null;
} }
} } else {
else{
$input = null; $input = null;
} }
if(!$actionPath){ if (!$actionPath) {
Json::dieWithReason('path가 지정되지 않았습니다.'); Json::dieWithReason('path가 지정되지 않았습니다.');
} }
if ($input && !is_array($input)) { if ($input && !is_array($input)) {
@@ -34,15 +65,14 @@ class APIHelper
$input = []; $input = [];
} }
if(class_exists('\\sammo\\UniqueConst')){ if (class_exists('\\sammo\\UniqueConst')) {
$serverID = UniqueConst::$serverID; $serverID = UniqueConst::$serverID;
$logPath = "{$rootPath}/logs/{$serverID}/api_log.db"; $logPath = "{$rootPath}/logs/{$serverID}/api_log.db";
} } else {
else{
$realYearMonth = date('Y_m'); $realYearMonth = date('Y_m');
$logPath = "{$rootPath}/d_log/{$realYearMonth}_api_log.db"; $logPath = "{$rootPath}/d_log/{$realYearMonth}_api_log.db";
} }
$logDB = FileDB::db($logPath, __DIR__.'/../../f_install/sql/api_log.sql'); $logDB = FileDB::db($logPath, __DIR__ . '/../../f_install/sql/api_log.sql');
$ip = $_SERVER['REMOTE_ADDR'] ?? 'local'; $ip = $_SERVER['REMOTE_ADDR'] ?? 'local';
$date = date('Y-m-d H:i:s'); $date = date('Y-m-d H:i:s');
@@ -53,7 +83,7 @@ class APIHelper
try { try {
$obj = buildAPIExecutorClass($actionPath, $rootPath, $actionArgs); $obj = buildAPIExecutorClass($actionPath, $rootPath, $actionArgs);
if(!$obj::$allowExternalAPI){ if (!$obj::$allowExternalAPI) {
Json::dieWithReason('외부에서는 호출할 수 없습니다.'); Json::dieWithReason('외부에서는 호출할 수 없습니다.');
} }
$validateResult = $obj->validateArgs(); $validateResult = $obj->validateArgs();
@@ -65,9 +95,9 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>false, 'result' => false,
'state'=>'validate', 'state' => 'validate',
'reason'=>$validateResult 'reason' => $validateResult
]), ]),
]); ]);
Json::dieWithReason($validateResult); Json::dieWithReason($validateResult);
@@ -75,21 +105,13 @@ class APIHelper
$filteredArgs = $obj->getFilteredArgs(); $filteredArgs = $obj->getFilteredArgs();
$sessionMode = $obj->getRequiredSessionMode(); $sessionMode = $obj->getRequiredSessionMode();
if ($sessionMode === BaseAPI::NO_SESSION) { $sessionOrFail = static::getSession($sessionMode);
$session = DummySession::getInstance();
} else {
if ($sessionMode & BaseAPI::REQ_GAME_LOGIN) {
$session = Session::requireGameLogin(null);
} else if ($sessionMode & BaseAPI::REQ_LOGIN) {
$session = Session::requireLogin(null);
} else {
$session = Session::getInstance();
}
if ($sessionMode & BaseAPI::REQ_READ_ONLY) { if(!($sessionOrFail instanceof Session)){
$session->setReadOnly(); $recoveryType = $sessionOrFail;
} Json::dieWithReason($recoveryType->info(), $recoveryType);
} }
$session = $sessionOrFail;
$modifiedSince = WebUtil::parseLastModified(); $modifiedSince = WebUtil::parseLastModified();
$reqEtags = WebUtil::parseETag(); $reqEtags = WebUtil::parseETag();
@@ -103,27 +125,44 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>false, 'result' => false,
'state'=>'launch', 'state' => 'launch',
'reason'=>$validateResult 'reason' => $result
]), ]),
]); ]);
Json::dieWithReason($result); Json::dieWithReason($result);
} }
else if($result instanceof APIRecoveryType){
$recoveryType = $result;
$logDB->insert('api_log', [
'user_id' => $session->userID ?? 0,
'ip' => $ip,
'date' => $date,
'path' => $actionPath,
'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([
'result' => false,
'state' => 'launch',
'reason' => $recoveryType->info(),
'recovery' => $recoveryType->value,
]),
]);
Json::dieWithReason($recoveryType->info(), $recoveryType);
}
$cache = $obj->tryCache(); $cache = $obj->tryCache();
$setCache = false; $setCache = false;
if ($cache !== null) { if ($cache !== null) {
if($cache->lastModified !== null || $cache->etag !== null){ if ($cache->lastModified !== null || $cache->etag !== null) {
$setCache = true; $setCache = true;
WebUtil::setCacheHeader($cache); WebUtil::setCacheHeader($cache);
} }
if ($modifiedSince !== null && $cache->lastModified !== null){ if ($modifiedSince !== null && $cache->lastModified !== null) {
$lastModifiedUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($cache->lastModified, true)); $lastModifiedUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($cache->lastModified, true));
$modifiedSinceUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($modifiedSince)); $modifiedSinceUnixTime = Util::toInt(TimeUtil::DateTimeToSeconds($modifiedSince));
if($lastModifiedUnixTime === $modifiedSinceUnixTime){ if ($lastModifiedUnixTime === $modifiedSinceUnixTime) {
$logDB->insert('api_log', [ $logDB->insert('api_log', [
'user_id' => $session->userID ?? 0, 'user_id' => $session->userID ?? 0,
'ip' => $ip, 'ip' => $ip,
@@ -131,8 +170,8 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>true, 'result' => true,
'state'=>'cache_not_modified', 'state' => 'cache_not_modified',
]), ]),
]); ]);
WebUtil::dieWithNotModified(); WebUtil::dieWithNotModified();
@@ -146,8 +185,8 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>true, 'result' => true,
'state'=>'cache_not_modified', 'state' => 'cache_not_modified',
]), ]),
]); ]);
WebUtil::dieWithNotModified(); WebUtil::dieWithNotModified();
@@ -162,9 +201,9 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>true, 'result' => true,
'state'=>'success_simple', 'state' => 'success_simple',
'set_cache'=>$setCache, 'set_cache' => $setCache,
]), ]),
]); ]);
Json::die([ Json::die([
@@ -179,9 +218,9 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>true, 'result' => true,
'state'=>'success_complex', 'state' => 'success_complex',
'set_cache'=>$setCache, 'set_cache' => $setCache,
]), ]),
]); ]);
$result['result'] = $result['result'] ?? true; $result['result'] = $result['result'] ?? true;
@@ -196,10 +235,10 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>false, 'result' => false,
'state'=>'error_exception', 'state' => 'error_exception',
'errMsg'=>$errMsg, 'errMsg' => $errMsg,
'errTrace'=>$errTrace, 'errTrace' => $errTrace,
]), ]),
]); ]);
Json::dieWithReason("{$errMsg}\n{$errTrace}"); Json::dieWithReason("{$errMsg}\n{$errTrace}");
@@ -214,10 +253,10 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>false, 'result' => false,
'state'=>'error_throwable', 'state' => 'error_throwable',
'errMsg'=>$errMsg, 'errMsg' => $errMsg,
'errTrace'=>$errTrace, 'errTrace' => $errTrace,
]), ]),
]); ]);
Json::dieWithReason("{$errMsg}\n{$errTrace}"); Json::dieWithReason("{$errMsg}\n{$errTrace}");
@@ -230,9 +269,9 @@ class APIHelper
'path' => $actionPath, 'path' => $actionPath,
'arg' => JSON::encode($filteredArgs), 'arg' => JSON::encode($filteredArgs),
'aux' => JSON::encode([ 'aux' => JSON::encode([
'result'=>false, 'result' => false,
'state'=>'error_mixed', 'state' => 'error_mixed',
'errMsg'=>$errStr, 'errMsg' => $errStr,
]), ]),
]); ]);
Json::dieWithReason($errStr); Json::dieWithReason($errStr);