diff --git a/hwe/_admin7.php b/hwe/_admin7.php index e44f1ec7..8623f9c2 100644 --- a/hwe/_admin7.php +++ b/hwe/_admin7.php @@ -31,7 +31,7 @@ $queryMap = [ ]; if ($reqQueryType === null || !key_exists($reqQueryType, $queryMap)) { - $reqQueryType = Util::array_first_key($queryMap); + $reqQueryType = array_key_first($queryMap); } //로그인 검사 diff --git a/hwe/func_gamerule.php b/hwe/func_gamerule.php index 6bb0b224..56883c7f 100644 --- a/hwe/func_gamerule.php +++ b/hwe/func_gamerule.php @@ -559,7 +559,7 @@ function checkStatistic() $nationHist = ''; foreach (GameConst::$availableNationType as $nationType) { - if (!Util::array_get($nationHists[$nationType])) { + if (!($nationHists[$nationType] ?? null)) { $nationHists[$nationType] = '-'; } $nationHist .= getNationType($nationType) . "({$nationHists[$nationType]}), "; diff --git a/hwe/func_legacy.php b/hwe/func_legacy.php index ffe23de9..deaf55c3 100644 --- a/hwe/func_legacy.php +++ b/hwe/func_legacy.php @@ -138,7 +138,7 @@ function JSCitiesBasedOnDistance(int $cityNo, int $maxDistance = 1): array{ $distanceList = searchDistance($cityNo, $maxDistance, true); $result = []; for ($dist = 1; $dist <= $maxDistance; $dist++) { - $result[$dist] = Util::array_get($distanceList[$dist], []); + $result[$dist] = $distanceList[$dist] ?? []; } return $result; } @@ -152,7 +152,7 @@ function printCitiesBasedOnDistance(int $cityNo, int $maxDistance = 1): string for ($dist = 1; $dist <= $maxDistance; $dist++) { $cityList = array_map(function ($cityID) { return CityConst::byID($cityID)->name; - }, Util::array_get($distanceList[$dist], [])); + }, $distanceList[$dist] ?? []); $cityStr = join(', ', $cityList); diff --git a/hwe/process_war.php b/hwe/process_war.php index 1c8f7f01..c7c84701 100644 --- a/hwe/process_war.php +++ b/hwe/process_war.php @@ -526,7 +526,7 @@ function DeleteConflict($nation) function getConquerNation($city): int { $conflict = Json::decode($city['conflict']); - return Util::array_first_key($conflict); + return array_key_first($conflict); } function ConquerCity(array $admin, General $general, array $city, array $defenderCityGeneralList) diff --git a/hwe/sammo/ActionItem/event_전투특기_공성.php b/hwe/sammo/ActionItem/event_전투특기_공성.php index a26a47e2..ad9cc8f9 100644 --- a/hwe/sammo/ActionItem/event_전투특기_공성.php +++ b/hwe/sammo/ActionItem/event_전투특기_공성.php @@ -33,7 +33,7 @@ class event_전투특기_공성 extends \sammo\BaseItem{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_SIEGE; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionItem/event_전투특기_궁병.php b/hwe/sammo/ActionItem/event_전투특기_궁병.php index 81381a69..62dacf2c 100644 --- a/hwe/sammo/ActionItem/event_전투특기_궁병.php +++ b/hwe/sammo/ActionItem/event_전투특기_궁병.php @@ -28,7 +28,7 @@ class event_전투특기_궁병 extends \sammo\BaseItem{ if($statName === 'warAvoidRatio'){ return $value + 0.2; } - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_ARCHER; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionItem/event_전투특기_귀병.php b/hwe/sammo/ActionItem/event_전투특기_귀병.php index e11ea251..ca181504 100644 --- a/hwe/sammo/ActionItem/event_전투특기_귀병.php +++ b/hwe/sammo/ActionItem/event_전투특기_귀병.php @@ -28,7 +28,7 @@ class event_전투특기_귀병 extends \sammo\BaseItem{ if($statName === 'warMagicSuccessProb'){ return $value + 0.2; } - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_WIZARD; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionItem/event_전투특기_기병.php b/hwe/sammo/ActionItem/event_전투특기_기병.php index e19ad59b..ba4457b4 100644 --- a/hwe/sammo/ActionItem/event_전투특기_기병.php +++ b/hwe/sammo/ActionItem/event_전투특기_기병.php @@ -32,7 +32,7 @@ class event_전투특기_기병 extends \sammo\BaseItem{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_CAVALRY; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionItem/event_전투특기_보병.php b/hwe/sammo/ActionItem/event_전투특기_보병.php index 36af41ad..b9f0ebaa 100644 --- a/hwe/sammo/ActionItem/event_전투특기_보병.php +++ b/hwe/sammo/ActionItem/event_전투특기_보병.php @@ -33,7 +33,7 @@ class event_전투특기_보병 extends \sammo\BaseItem{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_FOOTMAN; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialDomestic/che_event_공성.php b/hwe/sammo/ActionSpecialDomestic/che_event_공성.php index 52da3cd4..23f62508 100644 --- a/hwe/sammo/ActionSpecialDomestic/che_event_공성.php +++ b/hwe/sammo/ActionSpecialDomestic/che_event_공성.php @@ -35,7 +35,7 @@ class che_event_공성 extends \sammo\BaseSpecial{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_SIEGE; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialDomestic/che_event_궁병.php b/hwe/sammo/ActionSpecialDomestic/che_event_궁병.php index 43862278..ee243c6d 100644 --- a/hwe/sammo/ActionSpecialDomestic/che_event_궁병.php +++ b/hwe/sammo/ActionSpecialDomestic/che_event_궁병.php @@ -31,7 +31,7 @@ class che_event_궁병 extends \sammo\BaseSpecial{ if($statName === 'warAvoidRatio'){ return $value + 0.2; } - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_ARCHER; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialDomestic/che_event_귀병.php b/hwe/sammo/ActionSpecialDomestic/che_event_귀병.php index ba9863f4..6267e8d8 100644 --- a/hwe/sammo/ActionSpecialDomestic/che_event_귀병.php +++ b/hwe/sammo/ActionSpecialDomestic/che_event_귀병.php @@ -30,7 +30,7 @@ class che_event_귀병 extends \sammo\BaseSpecial{ if($statName === 'warMagicSuccessProb'){ return $value + 0.2; } - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_WIZARD; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialDomestic/che_event_기병.php b/hwe/sammo/ActionSpecialDomestic/che_event_기병.php index 6a5e92f4..b40ce270 100644 --- a/hwe/sammo/ActionSpecialDomestic/che_event_기병.php +++ b/hwe/sammo/ActionSpecialDomestic/che_event_기병.php @@ -35,7 +35,7 @@ class che_event_기병 extends \sammo\BaseSpecial{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_CAVALRY; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialDomestic/che_event_보병.php b/hwe/sammo/ActionSpecialDomestic/che_event_보병.php index 1e99d048..08bc934f 100644 --- a/hwe/sammo/ActionSpecialDomestic/che_event_보병.php +++ b/hwe/sammo/ActionSpecialDomestic/che_event_보병.php @@ -35,7 +35,7 @@ class che_event_보병 extends \sammo\BaseSpecial{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_FOOTMAN; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialWar/che_공성.php b/hwe/sammo/ActionSpecialWar/che_공성.php index ca866324..6df70402 100644 --- a/hwe/sammo/ActionSpecialWar/che_공성.php +++ b/hwe/sammo/ActionSpecialWar/che_공성.php @@ -35,7 +35,7 @@ class che_공성 extends \sammo\BaseSpecial{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_SIEGE; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialWar/che_궁병.php b/hwe/sammo/ActionSpecialWar/che_궁병.php index c64b0645..47813188 100644 --- a/hwe/sammo/ActionSpecialWar/che_궁병.php +++ b/hwe/sammo/ActionSpecialWar/che_궁병.php @@ -31,7 +31,7 @@ class che_궁병 extends \sammo\BaseSpecial{ if($statName === 'warAvoidRatio'){ return $value + 0.2; } - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_ARCHER; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialWar/che_귀병.php b/hwe/sammo/ActionSpecialWar/che_귀병.php index aa99c869..83160ebf 100644 --- a/hwe/sammo/ActionSpecialWar/che_귀병.php +++ b/hwe/sammo/ActionSpecialWar/che_귀병.php @@ -30,7 +30,7 @@ class che_귀병 extends \sammo\BaseSpecial{ if($statName === 'warMagicSuccessProb'){ return $value + 0.2; } - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_WIZARD; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialWar/che_기병.php b/hwe/sammo/ActionSpecialWar/che_기병.php index 683db233..3ab31d7e 100644 --- a/hwe/sammo/ActionSpecialWar/che_기병.php +++ b/hwe/sammo/ActionSpecialWar/che_기병.php @@ -35,7 +35,7 @@ class che_기병 extends \sammo\BaseSpecial{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_CAVALRY; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/ActionSpecialWar/che_보병.php b/hwe/sammo/ActionSpecialWar/che_보병.php index 4bedeb93..1a89d48f 100644 --- a/hwe/sammo/ActionSpecialWar/che_보병.php +++ b/hwe/sammo/ActionSpecialWar/che_보병.php @@ -35,7 +35,7 @@ class che_보병 extends \sammo\BaseSpecial{ } public function onCalcStat(General $general, string $statName, $value, $aux=null){ - if(\sammo\Util::starts_with($statName, 'dex')){ + if(str_starts_with($statName, 'dex')){ $myArmType = 'dex'.GameUnitConst::T_FOOTMAN; $opposeArmType = 'dex'.$aux['opposeType']->armType;; if($aux['isAttacker'] && $opposeArmType === $statName){ diff --git a/hwe/sammo/Command/General/che_랜덤임관.php b/hwe/sammo/Command/General/che_랜덤임관.php index be17946c..715dfa43 100644 --- a/hwe/sammo/Command/General/che_랜덤임관.php +++ b/hwe/sammo/Command/General/che_랜덤임관.php @@ -208,7 +208,7 @@ class che_랜덤임관 extends Command\GeneralCommand foreach ($rawGeneralsCnt as $nation) { $calcCnt = $nation['warpower'] + $nation['develpower']; - if ($general->getNPCType() < 2 && Util::starts_with($nation['name'], 'ⓤ')) { + if ($general->getNPCType() < 2 && str_starts_with($nation['name'], 'ⓤ')) { $calcCnt *= 100; } diff --git a/hwe/sammo/Command/General/che_출병.php b/hwe/sammo/Command/General/che_출병.php index da28b875..7477937d 100644 --- a/hwe/sammo/Command/General/che_출병.php +++ b/hwe/sammo/Command/General/che_출병.php @@ -164,7 +164,7 @@ class che_출병 extends Command\GeneralCommand $currDist = 999; - $minDist = Util::array_first_key($distanceList); + $minDist = array_key_first($distanceList); do { //1: 최단 거리 도시 중 공격 대상이 있는가 확인 //2: 최단 거리 + 1 도시 중 공격 대상이 있는가 확인 diff --git a/hwe/sammo/Event/Action/ChangeCity.php b/hwe/sammo/Event/Action/ChangeCity.php index b9ef9475..c4a05507 100644 --- a/hwe/sammo/Event/Action/ChangeCity.php +++ b/hwe/sammo/Event/Action/ChangeCity.php @@ -127,7 +127,7 @@ class ChangeCity extends \sammo\Event\Action{ return DB::db()->sqleval('ROUND(%b * %d, 0)', $keyMax, $value/100); } - if(Util::ends_with($key, '_max') && preg_match(self::REGEXP_MATH, $value, $matches)){ + if(str_ends_with($key, '_max') && preg_match(self::REGEXP_MATH, $value, $matches)){ //key가 아예 최대치에 대한 값이면 연산은 결과값이 0보다만 높게. $op = $matches[1]; $value = $matches[2]; diff --git a/hwe/sammo/GeneralAI.php b/hwe/sammo/GeneralAI.php index 1c7458f1..5b89f246 100644 --- a/hwe/sammo/GeneralAI.php +++ b/hwe/sammo/GeneralAI.php @@ -2084,7 +2084,7 @@ class GeneralAI } } - $finalCityID = Util::array_first_key($cityScoreList); + $finalCityID = array_key_first($cityScoreList); $dist = $distanceList[$capital][$finalCityID]; $targetCityID = $finalCityID; if ($dist > 1) { diff --git a/hwe/sammo/TriggerCaller.php b/hwe/sammo/TriggerCaller.php index 0f3d36b3..31378a79 100644 --- a/hwe/sammo/TriggerCaller.php +++ b/hwe/sammo/TriggerCaller.php @@ -62,7 +62,7 @@ abstract class TriggerCaller{ return $this; } - $lastKey = Util::array_last_key($this->triggerListByPriority); + $lastKey = array_key_last($this->triggerListByPriority); if($lastKey < $priority){ $this->triggerListByPriority[$priority] = [$uniqueID=>$trigger]; return $this; diff --git a/j_updateServer.php b/j_updateServer.php index 3d0ad1c6..7e8abf4b 100644 --- a/j_updateServer.php +++ b/j_updateServer.php @@ -174,7 +174,7 @@ function tryNpmInstall() tryComposerInstall(); if (tryNpmInstall()) { - genJS(Util::array_last_key(ServConfig::getServerList())); + genJS(array_key_last(ServConfig::getServerList())); } $session = Session::requireLogin(null)->setReadOnly(); @@ -227,7 +227,7 @@ if (!$allowFullUpdate || !$target) { $target = $request['target']; } -$baseServerName = Util::array_last_key(ServConfig::getServerList()); +$baseServerName = array_key_last(ServConfig::getServerList()); if (!$target && $server != $baseServerName) { Json::die([ diff --git a/oauth_kakao/j_join_process.php b/oauth_kakao/j_join_process.php index d902c3cf..087ca8bd 100644 --- a/oauth_kakao/j_join_process.php +++ b/oauth_kakao/j_join_process.php @@ -109,16 +109,16 @@ if($expires < $nowDate){ $access_token = $result['access_token']; $expires = TimeUtil::nowAddSeconds($result['expires_in']); if(isset($result['refresh_token'])){ - $refresh_token = Util::array_get($result['refresh_token']); + $refresh_token = $result['refresh_token'] ?? null; $refresh_token_expires = TimeUtil::nowAddSeconds($result['refresh_token_expires_in']); } } $signupResult = $restAPI->signup(); -$kakaoID = Util::array_get($signupResult['id']); +$kakaoID = $signupResult['id'] ?? null; -if(!$kakaoID && Util::array_get($signupResult['msg'])!='already registered'){ +if(!$kakaoID && ($signupResult['msg'] ?? null) !='already registered'){ Json::die([ 'result'=>false, 'reason'=>'카카오 로그인 과정 중 앱 연결 절차를 실패했습니다'.Json::encode($signupResult) @@ -126,7 +126,7 @@ if(!$kakaoID && Util::array_get($signupResult['msg'])!='already registered'){ } $me = $restAPI->meWithEmail(); -$me['code'] = Util::array_get($me['code'], 0); +$me['code'] = $me['code'] ?? 0; $kakao_account = $me['kakao_account']??[]; if ($me['code']< 0) { $restAPI->unlink(); diff --git a/src/sammo/Setting.php b/src/sammo/Setting.php index af9d29ae..c24dc2b5 100644 --- a/src/sammo/Setting.php +++ b/src/sammo/Setting.php @@ -88,9 +88,9 @@ class Setting $tail = new FileTail($this->versionFile); $version = 'noVersionJson'; foreach ($tail->smart(5, 100, true) as $line) { - if (Util::starts_with($line, '//{')) { + if (str_starts_with($line, '//{')) { $version = Json::decode(substr($line, 2)); - $version = Util::array_get($version['version'], 'noVersionValue'); + $version = $version['version'] ?? 'noVersionValue'; break; } } @@ -106,8 +106,8 @@ class Setting $templates = new \League\Plates\Engine(__DIR__.'/templates'); //TODO: .htaccess가 서버 오픈에도 사용될 수 있으니 별도의 방법이 필요함 $allow_ip = Util::get_client_ip(false); - if (Util::starts_with($allow_ip, '192.168.') || - Util::starts_with($allow_ip, '10.')) { + if (str_starts_with($allow_ip, '192.168.') || + str_starts_with($allow_ip, '10.')) { //172.16~172.31은 코딩하기 귀찮으니까 안할거다 $allow_ip = Util::get_client_ip(true); } diff --git a/src/sammo/Util.php b/src/sammo/Util.php index c618b6af..dd9227f6 100644 --- a/src/sammo/Util.php +++ b/src/sammo/Util.php @@ -3,7 +3,7 @@ namespace sammo; use Iterator; -class Util extends \utilphp\util +class Util { /** @@ -889,4 +889,110 @@ class Util extends \utilphp\util } return join('|', $result); } + + +/** + * http://github.com/brandonwamboldt/utilphp/ 의 구버전 코드에서 이식함 + * 일부 코드는 수정 + * 해당 라이선스: MIT + */ + + /** + * Returns the first element in an array. + * PHP 8.5부터 array_first 사용 가능 + * + * @param array $array + * @return mixed + */ + public static function array_first(array $array) + { + return $array[array_key_first($array)]; + } + + /** + * Returns the last element in an array. + * PHP 8.5부터 array_last 사용 가능 + * + * @param array $array + * @return mixed + */ + public static function array_last(array $array) + { + return $array[array_key_last($array)]; + } + + /** + * Flatten a multi-dimensional array into a one dimensional array. + * + * Contributed by Theodore R. Smith of PHP Experts, Inc. + * + * @param array $array The array to flatten + * @param boolean $preserve_keys Whether or not to preserve array keys. + * Keys from deeply nested arrays will + * overwrite keys from shallowy nested arrays + * @return array + */ + public static function array_flatten(array $array, $preserve_keys = true) + { + $flattened = array(); + + array_walk_recursive($array, function($value, $key) use (&$flattened, $preserve_keys) { + if ($preserve_keys && !is_int($key)) { + $flattened[$key] = $value; + } else { + $flattened[] = $value; + } + }); + + return $flattened; + } + + /** + * Returns the IP address of the client. + * + * @param boolean $trust_proxy_headers Whether or not to trust the + * proxy headers HTTP_CLIENT_IP + * and HTTP_X_FORWARDED_FOR. ONLY + * use if your server is behind a + * proxy that sets these values + * @return string + */ + public static function get_client_ip($trust_proxy_headers = false) + { + if (!$trust_proxy_headers) { + return $_SERVER['REMOTE_ADDR']; + } + + if (!empty($_SERVER['HTTP_CLIENT_IP'])) { + $ip = $_SERVER['HTTP_CLIENT_IP']; + } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + } else { + $ip = $_SERVER['REMOTE_ADDR']; + } + + return $ip; + } + + /** + * Access an array index, retrieving the value stored there if it + * exists or a default if it does not. This function allows you to + * concisely access an index which may or may not exist without + * raising a warning. + * + * @deprecated PHP 7.0 이상에서는 null 병합 연산자 사용을 권장 + * + * @param array $var Array value to access + * @param mixed $default Default value to return if the key is not + * present in the array + * @return mixed + */ + public static function array_get(&$var, $default = null) + { + if (isset($var)) { + return $var; + } + + return $default; + } };