From e536e2d4cae937e7ed132cea758ee39ce7b7e6b2 Mon Sep 17 00:00:00 2001 From: hide_d Date: Sat, 3 Feb 2018 03:36:34 +0900 Subject: [PATCH] =?UTF-8?q?=EB=82=B4=EB=B6=80=20=EC=A0=84=EC=97=AD=20?= =?UTF-8?q?=EB=B3=80=EC=88=98=EA=B0=92=20=EC=B2=98=EB=A6=AC=EB=A5=BC=20?= =?UTF-8?q?=EC=9C=84=ED=95=B4=20=ED=95=A8=EC=88=98=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit conf.php에 getServPrefix 추가. 이후 같은 db에서 다른 서버를 운용할때 사용 가능할 것으로 예상. $_SESSION 값을 직접 받아서 처리하지 않도록 하기위한 용도로 getUserID(), getGeneralID(), getGeneralName() 을 설정 $_SESSION['p_id']를 p_no로 착각하여 구현한 부분 수정 int|null 변환을 위한 toInt 함수 추가 func_string.php에서 null이 입력될 경우 일부 처리 각 서버에 계정이 생성되었는지 확인하는 isSigned()함수 추가 --- i_banner/banner.php | 55 ------------------ twe/d_setting/conf.orig.php | 4 ++ twe/func.php | 112 +++++++++++++++++++++++++++++------- twe/func_converter.php | 2 +- twe/func_map.php | 22 +++---- twe/func_message.php | 17 ++++-- twe/func_string.php | 9 +++ twe/j_map.php | 4 +- twe/j_msgsubmit.php | 2 +- twe/j_turn.php | 9 +++ twe/lib.php | 53 ++++++++++++++--- twe/login_process.php | 39 +++++++------ twe/logout_process.php | 36 ++---------- twe/npc_login_process.php | 3 +- twe/turn.php | 2 + 15 files changed, 215 insertions(+), 154 deletions(-) create mode 100644 twe/j_turn.php diff --git a/i_banner/banner.php b/i_banner/banner.php index 5f8da1ca..b3d9bbc7 100644 --- a/i_banner/banner.php +++ b/i_banner/banner.php @@ -1,56 +1 @@ - - - - - - - - - - - - - - - - -G���� - - - -queryFirstField('select no from general where user_id = %s', $username); + if(!$generalID && $forceExit){ + header('Location:..'); + die(); + } + + if($generalIDid){ + $_SESSION[$id_key] = $generalID; + } + + return $generalID; +} + +/** + * 로그인한 유저의 장수명을 받아옴 + * + * @return string|null + */ +function getGeneralName($forceExit=false) +{ + $generalID = getGeneralID(); + if(!$generalID){ + if($forceExit){ + header('Location:..'); + die(); + } + + return null; + } + + $id_key = getServPrefix().'p_name'; + $generalName = util::array_get($_SESSION[$id_key], null); + + if($generalName){ + return $generalName; + } + + //흠? + $generalName = getDB()->queryFirstField('select name from general where no = %i', $generalID); +} function GetImageURL($imgsvr) { global $image, $image1; @@ -44,9 +109,14 @@ function GetImageURL($imgsvr) { } } -function CheckLoginEx(){ - //TODO: 서버 별로 p_id를 다르게 설정할 수 있어야함. - if(!isset($_SESSION['p_id'])) { +/** + * generalID를 이용해 각 서버 등록 여부를 확인함 + * + * @return bool + */ +function isSigned(){ + $p_id = getGeneralID(); + if(!$_pid){ return false; } return true; @@ -70,7 +140,7 @@ function checkLimit($userlevel, $con, $conlimit) { } function getBlockLevel() { - return getDB()->queryFirstField('select block from general where user_id= %i', getGeneralID()); + return getDB()->queryFirstField('select block from general where no = %i', getGeneralID()); } function getRandGenName() { @@ -1801,7 +1871,9 @@ function getOnlineNum() { function onlinegen($connect) { $onlinegen = ""; - if($_SESSION['p_nation'] == 0) { + $generalID = getGeneralID(); + $nationID = getDB()->queryFirstField('select `nation` from `general` where `no` = %i', $generalID); + if($nationID !== null || toInt($nationID) === 0) { $query = "select onlinegen from game where no='1'"; $result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),""); $game = MYDB_fetch_array($result); @@ -2140,14 +2212,14 @@ function increateRefreshEx($type, $cnt=1){ )); $date = date('Y-m-d H:i:s'); - $p_id = getGeneralID(); + $generalID = getGeneralID(); if($p_id !== NULL){ $db->query("update `general` set `lastrefresh`= %s_date, `con` = `con`+%d_cnt, `connect`= `connect`+ %d_cnt, '\ - '`refcnt` = `refcnt` + %d_cnt, `refresh` = `refresh` + %d_cnt where `user_id` =%s_p_id",array( + '`refcnt` = `refcnt` + %d_cnt, `refresh` = `refresh` + %d_cnt where `no` =%i_generalID",array( 'date'=>$date, 'cnt'=>$cnt, - 'p_id'=>$p_id + 'p_id'=>$generalID )); } @@ -2155,7 +2227,7 @@ function increateRefreshEx($type, $cnt=1){ $date2 = substr($date, 0, 10); $online = getOnlineNum(); $fp = fopen("logs/_{$date2}_refresh.txt", "a"); - $msg = _String::Fill2($date,20," ")._String::Fill2($_SESSION['p_id'],13," ")._String::Fill2($_SESSION['p_name'],13," ")._String::Fill2($_SESSION['p_ip'],16," ")._String::Fill2($type, 10, " ")." 동접자: {$online}"; + $msg = _String::Fill2($date,20," ")._String::Fill2(getUserID(),13," ")._String::Fill2(getGeneralName(),13," ")._String::Fill2($_SESSION['p_ip'],16," ")._String::Fill2($type, 10, " ")." 동접자: {$online}"; fwrite($fp, $msg."\n"); fclose($fp); @@ -2184,7 +2256,7 @@ function increateRefreshEx($type, $cnt=1){ if($str != "") { file_put_contents("logs/_{$date2}_ipcheck.txt", sprintf("ID:%s//name:%s//REMOTE_ADDR:%s%s\n", - $_SESSION['p_id'],$_SESSION['p_name'],$_SERVER['REMOTE_ADDR'],$str), FILE_APPEND); + getUserID(), getGeneralName(),$_SERVER['REMOTE_ADDR'],$str), FILE_APPEND); } diff --git a/twe/func_converter.php b/twe/func_converter.php index d0c37e05..c801fbf5 100644 --- a/twe/func_converter.php +++ b/twe/func_converter.php @@ -629,7 +629,7 @@ function getBill($dedication) { function getCost($armtype) { //FIXME: 정말로 side effect가 없으려면 query는 밖으로 이동해야함. //TODO: 병종 값이 column으로 들어있는건 전혀 옳지 않음. key->value 형태로 바꿔야함 - return getDB()->queryFirstColumn('select cst%l from game where no=1', intval($armtype)); + return getDB()->queryFirstColumn('select %b from game where no=1', sprintf('cst%d', $armtype)); } function TechLimit($startyear, $year, $tech) { diff --git a/twe/func_map.php b/twe/func_map.php index b14f1ac3..2aa49146 100644 --- a/twe/func_map.php +++ b/twe/func_map.php @@ -50,22 +50,22 @@ function getWorldMap($req){ return getHistoryMap($req->year, $req->month); } - $generalID = getGeneralID(); + $generalID = getGeneralID(false); $db = getDB(); $game = $db->queryFirstRow('select `startyear`, `year`, `month` from `game` where `no` = 1'); - $startYear = intval($game['startyear']); - $year = intval($game['year']); - $month = intval($game['month']); + $startYear = toInt($game['startyear']); + $year = toInt($game['year']); + $month = toInt($game['month']); if($generalID && ($req->showMe || $req->neutralView)){ $city = $db->queryFirstRow( 'select `city`, `nation` from `general` where `user_id`=%i', $generalID); - $myCity = intval($city['city']); - $myNation = intval($city['nation']); + $myCity = toInt($city['city']); + $myNation = toInt($city['nation']); if(!$req->showMe){ $myCity = null; @@ -82,7 +82,7 @@ function getWorldMap($req){ if($myNation){ $spyList = $db->queryFirstField('select `spy` from `nation` where `nation`=%i', $myNation); - $spyList = array_map('intval', explode("|", $spyList)); + $spyList = array_map('toInt', explode("|", $spyList)); } else{ $spyList = []; @@ -91,17 +91,17 @@ function getWorldMap($req){ $nationList = []; foreach($db->query('select `nation`, `name`, `color`, `capital` from `nation`') as $row){ $nationList[] = [ - intval($row['nation']), + toInt($row['nation']), $row['name'], $row['color'], - intval($row['capital']) + toInt($row['capital']) ]; } if($myNation){ //굳이 타국 도시에 있는 아국 장수 리스트를 뽑을 이유가 없음. 일단 다 뽑자. $shownByGeneralList = - array_map('intval', + array_map('toInt', $db->queryFirstColumn('select distinct `city` from `general` where `nation` = %i', $myNation)); } @@ -112,7 +112,7 @@ function getWorldMap($req){ $cityList = []; foreach($db->query('select `city`, `level`, `state`, `nation`, `region`, `supply` from `city`') as $r){ $cityList[] = - array_map('intval', [$r['city'], $r['level'], $r['state'], $r['nation'], $r['region'], $r['supply']]); + array_map('toInt', [$r['city'], $r['level'], $r['state'], $r['nation'], $r['region'], $r['supply']]); } return [ diff --git a/twe/func_message.php b/twe/func_message.php index 303b8a71..271dc7e9 100644 --- a/twe/func_message.php +++ b/twe/func_message.php @@ -82,7 +82,7 @@ function getRawMessage($mailbox, $limit=30, $fromTime=NULL){ } function getMessage($msgType, $limit=30, $fromTime=NULL){ - $generalID = getGeneralID(); + $generalID = getGeneralID(false); if($generalID === NULL){ return []; } @@ -91,12 +91,12 @@ function getMessage($msgType, $limit=30, $fromTime=NULL){ return getRawMessage(9999, $limit, $fromTime); } else if($msgType === 'private'){ - return getRawMessage($genID, $limit, $fromTime); + return getRawMessage($generalID, $limit, $fromTime); } else if($msgType === 'national'){ $nationID = getDB()->queryFirstField( - 'select `nation` from `general` where user_id = %i', - $genID + 'select `nation` from `general` where no = %i', + $generalID ); return getRawMessage(9000 + $nationID, $limit, $fromTime); } @@ -164,9 +164,14 @@ function sendMessage($msgType, $src, $dest, $msg, $date = null){ function getMailboxList(){ $result = []; - $generalID = getGeneralID(); + $generalID = getGeneralID(false); + + if(!$generalID){ + + } + $db = getDB(); - $me = $db->queryFirstRow('select no,nation,level from general where user_id=%i', $generalID); + $me = $db->queryFirstRow('select no,nation,level from general where no=%i', $generalID); //가장 최근에 주고 받은 사람. $latestMessage = util::array_get(getMessage('private', 1)[0], null); diff --git a/twe/func_string.php b/twe/func_string.php index e804e52b..5543bfe5 100644 --- a/twe/func_string.php +++ b/twe/func_string.php @@ -224,6 +224,9 @@ class _String { //중간정렬 public static function staticFill($str, $maxsize, $ch) { + if(!$str){ + $str = ''; + } $size = strlen($str); $count = ($maxsize - $size) / 2; @@ -239,6 +242,9 @@ class _String { } public static function Fill($str, $maxsize, $ch) { + if(!$str){ + $str = ''; + } $size = strlen($str); $count = ($maxsize - $size) / 2; @@ -256,6 +262,9 @@ class _String { //우측정렬 public static function Fill2($str, $maxsize, $ch='0') { + if(!$str){ + $str = ''; + } $size = strlen($str); $count = ($maxsize - $size); diff --git a/twe/j_map.php b/twe/j_map.php index 7f9c8496..7bcf8f53 100644 --- a/twe/j_map.php +++ b/twe/j_map.php @@ -25,8 +25,8 @@ if($post['year']){ ]); } - $post['year'] = intval($post['year']); - $post['month'] = intval($post['month']); + $post['year'] = toInt($post['year']); + $post['month'] = toInt($post['month']); } else{ $post['year'] = null; diff --git a/twe/j_msgsubmit.php b/twe/j_msgsubmit.php index 9ef9fb6e..c64b97ee 100644 --- a/twe/j_msgsubmit.php +++ b/twe/j_msgsubmit.php @@ -24,7 +24,7 @@ $datetime = new DateTime(); $date = $datetime->format('Y-m-d H:i:s'); //로그인 검사 -if(!CheckLoginEx($db)){ +if(!isSigned()){ returnJson([ 'result' => false, 'reason' => '로그인되지 않았습니다.', diff --git a/twe/j_turn.php b/twe/j_turn.php new file mode 100644 index 00000000..8ddfb695 --- /dev/null +++ b/twe/j_turn.php @@ -0,0 +1,9 @@ +경과시간 : {$_endTime}초"; } +/** + * '비교적' 안전한 int 변환 + * null -> null + * int -> int + * float -> int + * numeric(int, float) 포함 -> int + * 기타 -> 예외처리 + * + * @return int|null + */ +function toInt($val){ + if($val === null){ + return null; + } + if(is_int($val)){ + return $val; + } + if(is_numeric($val)){ + return intval($val);// + } + + throw new InvalidArgumentException('올바르지 않은 타입형 :'.$val); +} + function LogText($prefix, $variable){ $fp = fopen('logs/dbg_logs.txt', 'a+'); if($fp == false){ @@ -217,10 +243,19 @@ function LogText($prefix, $variable){ $fp = fopen('logs/dbg_logs.txt', 'a+'); } } - fwrite($fp, sprintf('%s : %s\n', $prefix, var_export($_POST, true))); + fwrite($fp, sprintf('%s : %s'."\n", $prefix, var_export($_POST, true))); fclose($fp); } +function dictToArray($dict, $keys){ + $result = []; + + foreach($keys as $key){ + $result[] = util::array_get($dict[$key], null); + } + return $result; +} + function parseJsonPost(){ // http://thisinterestsme.com/receiving-json-post-data-via-php/ // http://thisinterestsme.com/php-json-error-handling/ @@ -279,7 +314,9 @@ function parseJsonPost(){ return $decoded; } -LogText($_SERVER['REQUEST_URI'], $_POST); +if(isset($_POST) && count($_POST) > 0){ + LogText($_SERVER['REQUEST_URI'], $_POST); +} extract($_POST, EXTR_SKIP); //XXX: $_POST를 추출 없이 그냥 쓰는 경우가 많아서 일단 디버깅을 위해 씀!!!! 절대 production 서버에서 사용 금지! //todo: $_POST로 제공되는 데이터를 각 페이지마다 분석할것. \ No newline at end of file diff --git a/twe/login_process.php b/twe/login_process.php index 61806117..795e7642 100644 --- a/twe/login_process.php +++ b/twe/login_process.php @@ -1,11 +1,10 @@ queryFirstRow('select no,name from MEMBER where id=%s and pw=%s', $id, $pw); -$connect = dbConn(); +if(!$member) { + MessageBox("아이디나 암호가 올바르지 않습니다!!!"); + //TODO:login_process를 rest 형태로 처리 + //header ("Location: index.php"); + exit(0); +} + +//NOTE: 왜 Session을 지우는가? DeleteSession(); +$db = getDB(); + //회원 테이블에서 정보확인 -$query="select no,name,nation,block,killturn from general where user_id='$id'"; +$me= $db->queryFirstRow('select no,name,nation,block,killturn from general where user_id= %s', $id); $result = MYDB_query($query, $connect) or Error(__LINE__.MYDB_error($connect),""); $me = MYDB_fetch_array($result); -if(!$member) { - //MessageBox("아이디나 암호가 올바르지 않습니다!!!"); - //TODO:login_process를 rest 형태로 처리 - header ("Location: index.php"); - exit(0); -} + if(!$me) { - //MessageBox("캐릭터가 없습니다!!!"); + MessageBox("캐릭터가 없습니다!!!"); //TODO:login_process를 rest 형태로 처리 - header ("Location: index.php"); + //header ("Location: index.php"); exit(0); } @@ -52,9 +55,9 @@ case 3: MessageBox("절대 1계정만 사용하십시오! {$me['killturn']}시간 후 재등록 가능합니다."); break; } +$_SESSION[getServPrefix().'p_no'] = toInt($me['no']); $_SESSION['p_id'] = $id; -$_SESSION['p_name'] = $me['name']; -$_SESSION['p_nation'] = $me['nation']; +$_SESSION[getServPrefix().'p_name'] = $me['name']; $_SESSION['p_time'] = time(); $date = date('Y-m-d H:i:s'); diff --git a/twe/logout_process.php b/twe/logout_process.php index 93390c01..04c9c04e 100644 --- a/twe/logout_process.php +++ b/twe/logout_process.php @@ -1,37 +1,13 @@ location.replace('start.php');"; -//echo 'start.php';//TODO:debug all and replace -header('Location:start.php'); - -/* - -a -
- - - -
- - -*/ - +header('Location:start.php'); \ No newline at end of file diff --git a/twe/npc_login_process.php b/twe/npc_login_process.php index b45a79f7..c6421961 100644 --- a/twe/npc_login_process.php +++ b/twe/npc_login_process.php @@ -40,8 +40,7 @@ if(!$me2) { } $_SESSION['p_id'] = $id; - $_SESSION['p_name'] = $me['name']; - $_SESSION['p_nation'] = $me['nation']; + $_SESSION['p_ip'] = getenv("REMOTE_ADDR"); $_SESSION['p_time'] = time(); $date = date('Y-m-d H:i:s'); diff --git a/twe/turn.php b/twe/turn.php index ad9e5410..3703ecb8 100644 --- a/twe/turn.php +++ b/twe/turn.php @@ -2,6 +2,8 @@ include "lib.php"; include "func.php"; //로그인 검사 + +use utilphp\util as util; CheckLogin(1); $connect = dbConn(); increaseRefresh($connect, "턴반복", 1);