| <?php | 
| set_time_limit(180); | 
| include_once "/Common/Logging.php"; | 
| include_once "/Common/CommFunc.php"; | 
| include_once "/Account/User.php"; | 
| include_once "/language/lang.php"; | 
| include_once "/Account/userlogcomm.php"; | 
| include_once "/Common/GMTRetCode.php"; | 
|   | 
| header("Content-type: text/html; charset=utf-8"); | 
| // 本脚本用于发送GMT命令到游戏服务器,可能的行为包括 | 
| // 1. 常规页面post请求,可能是查询、插入、更新数据等,需要展示详细结果页面 | 
| // 2. 命令发送post请求,一般是插入、更新数据,不需要展示详细结果页面,但是要返回操作结果,包含结果码、结果信息数据; (如审核、补发等) | 
|   | 
| $processType = $_POST['processType']; // 请求处理类型,可不传,默认常规页面请求;  | 
| // CheckOK-日志动态审核通过; | 
| // JustRet-直接返回处理结果,由提交的页面自身决定处理逻辑 | 
| if (!$processType) { | 
|     echo "<Title>" . \Lang\gettext("执行结果") . "</Title>"; | 
| } | 
|   | 
| $pack_type = $_POST['pack_type']; | 
| if (!isset($pack_type)) { | 
|     ProcessRet(-1, "not pack_type"); | 
|     exit; | 
| } | 
| \CommFunc\SessionStart(); | 
| \Logging\CreateLogging("GMCmd_" . $pack_type); | 
| \Logging\LogInfo("_SESSION:" . print_r($_SESSION, true)); | 
| \Logging\LogInfo("_POST:" . print_r($_POST, true)); | 
|   | 
| $Permission = $_POST['Permission']; // 后台权限 | 
| $SPID = $_SESSION['spid']; | 
| $UserAccount = $_SESSION['UserAccount']; | 
| $user = new \User\User($UserAccount); | 
| if (!$user->HavePermission($Permission)) { | 
|     ProcessRet(-1, "not permission"); | 
|     exit; | 
| } | 
| $GMTLogGUID = ""; | 
| $PerInfo = \User\GetPermissionInfo($Permission); | 
| $logPermission = $Permission; | 
| $logPerInfo = $PerInfo; | 
| if ($processType == "CheckOK") { | 
|     $GMTLogGUID = $_POST['GMTLogGUID']; | 
|     if (!$GMTLogGUID) { | 
|         ProcessRet(-1, "not check GMTLogGUID"); | 
|         exit; | 
|     } | 
|     $checkPermission = $PerInfo["Check"]; | 
|     if (!$user->HavePermission($checkPermission)) { | 
|         ProcessRet(-1, "not check Permission"); | 
|         exit; | 
|     } | 
|     $logPermission = $checkPermission; | 
|     $logPerInfo = \User\GetPermissionInfo($logPermission); | 
| } | 
|   | 
| //验证 后台权限对应的 GMT_xxx 及 授权码key | 
| $superGMT = false; | 
| $keyCfg = ""; | 
| $key = $_POST['key']; | 
| if (array_key_exists("GMTKey1", $PerInfo) && in_array($pack_type, $PerInfo["GMTKey1"])) { | 
|     \CommFunc\GetConfig("Config", "GMKey", $keyCfg, ""); | 
| } elseif (array_key_exists("GMTKey2", $PerInfo) && in_array($pack_type, $PerInfo["GMTKey2"])) { | 
|     \CommFunc\GetConfig("Config", "GMKey2", $keyCfg, ""); | 
|     $superGMT = true; | 
| } else { | 
|     \Logging\LogError("GMTKey not found."); | 
|     ProcessRet(-1, \Lang\gettext("您没有该权限") . " GMTKey not found."); | 
|     exit; | 
| } | 
| if (!$key) { | 
|     if (!$processType && $user->NeedCheckKey()) { | 
|         \Logging\LogInfo("请输入命令key!" . $pack_type); | 
|         ProcessRet(-1, \Lang\gettext("请输入命令key")); | 
|         exit; | 
|     } | 
|     $key = $keyCfg; // 没有输入key,且不需要验证key的情况下,直接使用配置的key | 
| } elseif ($key == $keyCfg) { | 
|     $updCheckKeyTime = $user->UpdCheckKeyTime(); | 
|     \Logging\LogInfo("updCheckKeyTime:" . $updCheckKeyTime); | 
| } else { | 
|     \Logging\LogInfo("授权码错误 key:" . $key . " != keyCfg:" . $keyCfg); | 
|     ProcessRet(-1, \Lang\gettext("授权码错误")); | 
|     exit; | 
| } | 
| \Logging\LogInfo($Permission . " -> GMTKey:" . $pack_type . " superGMT:" . $superGMT . " key:" . $key); | 
|   | 
| // ============================== 以上验证权限结束 ===================================== | 
|   | 
| $pack_data = $_POST; | 
|   | 
| //去除多余参数 | 
| unset($pack_data['submit']); | 
| unset($pack_data['Permission']); | 
| unset($pack_data['MultiServer']); | 
|   | 
| //组合开始时间、结束时间 | 
| if ( | 
|     array_key_exists('StartDate', $pack_data) && array_key_exists('StartHour', $pack_data) | 
|     && array_key_exists('StartMinute', $pack_data) && array_key_exists('StartSecond', $pack_data) | 
|     && !$pack_data['StartDate'] == "" | 
| ) { | 
|     $pack_data['StartTime'] = $pack_data['StartDate'] . " " . $pack_data['StartHour'] . ":" . $pack_data['StartMinute'] . ":" . $pack_data['StartSecond']; | 
|     unset($pack_data['StartDate'], $pack_data['StartHour'], $pack_data['StartMinute'], $pack_data['StartSecond']); | 
| }; | 
| if ( | 
|     array_key_exists('EndDate', $pack_data) && array_key_exists('EndHour', $pack_data) | 
|     && array_key_exists('EndMinute', $pack_data) && array_key_exists('EndSecond', $pack_data) | 
|     && !$pack_data['EndDate'] == "" | 
| ) { | 
|     $pack_data['EndTime'] = $pack_data['EndDate'] . " " . $pack_data['EndHour'] . ":" . $pack_data['EndMinute'] . ":" . $pack_data['EndSecond']; | 
|     unset($pack_data['EndDate'], $pack_data['EndHour'], $pack_data['EndMinute'], $pack_data['EndSecond']); | 
| }; | 
|   | 
| //增加编码类型 | 
| $pack_data['coding'] = "utf8"; | 
| $pack_data['key'] = $key; | 
|   | 
| $SuccessMsg = array(); | 
| $ErrorMsg = array(); | 
|   | 
| // 多服选择的 | 
| if ($_POST["MultiServer"] == 1) { | 
|   | 
|     $onlyServerID = $_POST['OnlyServerID']; | 
|   | 
|     $sendServers = array(); | 
|     $serversArray = $user->GetServers(); | 
|     foreach ($serversArray as $serverName => $serverInfo) { | 
|         $postServerName = urlencode($serverName); | 
|         if (!array_key_exists($postServerName, $_POST)) { | 
|             continue; | 
|         } | 
|         if (!$_POST[$postServerName] == 'on') { | 
|             continue; | 
|         } | 
|   | 
|         if ($serverInfo['MainServer'] && !$onlyServerID) { | 
|             # 有主服的代表是合服的,只允许主服发送, 除非标明了只往某个服发送 | 
|             # 单服发送消息,如混(合)服情况下只给其中一个服务器玩家发邮件 | 
|             continue; | 
|         } | 
|   | 
|         if ($onlyServerID && $serverInfo['ServerID']) { | 
|             # 单服发送消息,如混(合)服情况下只给其中一个服务器玩家发邮件 | 
|             $pack_data["OnlyServerID"] = $serverInfo['ServerID']; | 
|         } else { | 
|             unset($pack_data['OnlyServerID']); | 
|         } | 
|   | 
|         //使用key加密 | 
|         $pack_data_dict = json_encode($pack_data); | 
|         $sign = md5('' . $pack_data_dict . $key); | 
|         $post = array(); | 
|         $post['pack'] = $pack_data_dict; | 
|         $post['sign'] = $sign; | 
|   | 
|         $sendServers[] = array($serverName, $serverInfo['Page'], $post); | 
|     } | 
|   | 
|     if (!$sendServers) { | 
|         \Logging\LogInfo("not sendServers."); | 
|         ProcessRet(-1, "Error. " . \Lang\gettext("请先选择服务器")); | 
|         return; | 
|     } | 
|   | 
|     //有配置面审权限的,玩家没有对应面审权限时,无法直接发送 | 
|     if (array_key_exists("NOCheck", $logPerInfo) && !$user->HavePermission($logPerInfo["NOCheck"], false)) { | 
|         if (AddGMTLog($SPID, $UserAccount, $logPermission, $_POST, \User\GMTCheckState::UnCheck)) { | 
|             echo \Lang\gettext("请求成功,请联系管理员进行审核"); | 
|         } else { | 
|             echo \Lang\gettext("请求失败"); | 
|         } | 
|         exit; | 
|     } | 
|   | 
|     \Logging\LogInfo("待发送的服务器信息: " . print_r($sendServers, true)); | 
|   | 
|     // 单服发送的话,由于多个子服可能是同一个主服,所以不采用多线程批量发送,还是单个单个处理,不然可能导致远程服务器链接异常 | 
|     if ($onlyServerID) { | 
|         $retList = array(); | 
|         foreach ($sendServers as $value) { | 
|             $server_url = $value[1]; | 
|             $post = $value[2]; | 
|             $retList[] = \CommFunc\DoPost($server_url, $post); | 
|         } | 
|     } | 
|     // 非单服发送的话,由于都是不同的服务器机子, | 
|     else { | 
|         $retList = \CommFunc\DoPostMulti($sendServers); | 
|     } | 
|   | 
|     //\Logging\LogInfo("结果".print_r($retList, true)); | 
|   | 
|     $retCount = count($retList); | 
|   | 
|     foreach ($retList as $i => $retStr) { | 
|         $ret = json_decode($retStr, true); | 
|         if (!isset($ret)) { | 
|             $retStr = iconv("GB2312", "UTF-8", $retStr); | 
|             \Logging\LogInfo("GB2312 to UTF-8 retStr : " . $retStr); | 
|             $ret = json_decode($retStr, true); | 
|         } | 
|         $retList[$i] = $ret; | 
|         $server_name = $sendServers[$i][0]; | 
|         $server_url = $sendServers[$i][1]; | 
|         $msg = array("ServerUrl" => $server_url); | 
|         if (isset($ret)) { | 
|             $msg = array_merge($msg, $ret); | 
|             if (array_key_exists("ResultType", $ret) && $ret["ResultType"] == 0) { | 
|                 $SuccessMsg[$server_name] = $msg; | 
|             } else if (array_key_exists("ResultType", $ret) && $ret["ResultType"] != 0) { | 
|                 $ErrorMsg[$server_name] = $msg; | 
|             } else { | 
|                 $ErrorMsg[$server_name] = $msg; | 
|             } | 
|         } else { | 
|             $ErrorMsg[$server_name] = $msg; | 
|         } | 
|     } | 
| } | 
| // 单服发送的,默认当前选择的服务器 | 
| else { | 
|   | 
|     $tool_page = $_POST['tool_page']; // 有指定地址直接取指定的,没有则取默认 | 
|     if ($tool_page) { | 
|         \Logging\LogInfo("get tool_page from post."); | 
|     } else { | 
|         $tool_page = $_SESSION['tool_page']; | 
|         \Logging\LogInfo("get tool_page from session."); | 
|     } | 
|     if ($tool_page == "") { | 
|         \Logging\LogError("tool_page is null."); | 
|         ProcessRet(-1, "Error. " . \Lang\gettext("请先选择服务器")); | 
|         return; | 
|     } | 
|   | 
|     //有配置面审权限的,玩家没有对应面审权限时,无法直接发送 | 
|     if (array_key_exists("NOCheck", $logPerInfo) && !$user->HavePermission($logPerInfo["NOCheck"], false)) { | 
|         if (AddGMTLog($SPID, $UserAccount, $logPermission, $_POST, \User\GMTCheckState::UnCheck)) { | 
|             echo \Lang\gettext("请求成功,请联系管理员进行审核"); | 
|         } else { | 
|             echo \Lang\gettext("请求失败"); | 
|         } | 
|         exit; | 
|     } | 
|   | 
|     //使用key加密 | 
|     $pack_data_dict = json_encode($pack_data); | 
|     $sign = md5('' . $pack_data_dict . $key); | 
|     $post = array(); | 
|     $post['pack'] = $pack_data_dict; | 
|     $post['sign'] = $sign; | 
|   | 
|     \Logging\LogInfo("post tool_page : " . $tool_page); | 
|     \Logging\LogInfo("post data : " . print_r($post, true)); | 
|     $retStr = \CommFunc\DoPost($tool_page, $post); | 
|     \Logging\LogInfo("retStr : " . $retStr); | 
|     $ret = json_decode($retStr, true); | 
|     if (!isset($ret)) { | 
|         $retStr = iconv("GB2312", "UTF-8", $retStr); | 
|         \Logging\LogInfo("GB2312 to UTF-8 retStr : " . $retStr); | 
|         $ret = json_decode($retStr, true); | 
|     } | 
|     $server_name = $_SESSION["server_id"]; | 
|     $msg = array("ServerUrl" => $tool_page); | 
|     if (isset($ret)) { | 
|         $msg = array_merge($msg, $ret); | 
|         if (array_key_exists("ResultType", $ret) && $ret["ResultType"] == 0) { | 
|             $SuccessMsg[$server_name] = $msg; | 
|         } else if (array_key_exists("ResultType", $ret) && $ret["ResultType"] != 0) { | 
|             $ErrorMsg[$server_name] = $msg; | 
|         } else { | 
|             $ErrorMsg[$server_name] = $msg; | 
|         } | 
|     } else { | 
|         $ErrorMsg[$server_name] = $msg; | 
|     } | 
| } | 
|   | 
| // 插入动态日志 | 
| if (array_key_exists("Log", $logPerInfo) && in_array($pack_type, $logPerInfo["Log"])) { | 
|     AddGMTLog($SPID, $UserAccount, $logPermission, $_POST, \User\GMTCheckState::OK, $SuccessMsg, $ErrorMsg); | 
| } | 
|   | 
| if ($processType) { | 
|     ProcessRet(0, "OK", $SuccessMsg, $ErrorMsg); | 
|     exit; | 
| } | 
|   | 
| // 先展示失败的 | 
| foreach ($ErrorMsg as $server_name => $ret) { | 
|     if (array_key_exists("ResultType", $ret)) { | 
|         Ret_ResultType($server_name, $ret); | 
|     } else if (array_key_exists("ErrorNo", $ret)) { | 
|         Ret_ErrorNO($server_name, $ret); | 
|     } else { | 
|         Ret_NotRet($server_name); | 
|     } | 
| } | 
|   | 
| // 再展示成功的 | 
| foreach ($SuccessMsg as $server_name => $ret) { | 
|     Ret_ResultType($server_name, $ret); | 
| } | 
|   | 
| if ($_POST["MultiServer"] == 1) { | 
|     $failCount = count($ErrorMsg); | 
|     $succCount = $retCount - $failCount; | 
|     echo \Lang\gettext("发送服务器总数") . ": " . $retCount . "<br/>" . | 
|         "  " . \Lang\gettext("成功") . ": " . $succCount . "<br/>" . | 
|         "  " . \Lang\gettext("失败") . ": " . $failCount . "<br/>"; | 
|     echo "<hr/>"; | 
|     \Logging\LogInfo($pack_type . " 发送服务器总数: " . $retCount . "  成功: " . $succCount . "  失败: " . $failCount); | 
| } | 
|   | 
| // ============================================================================= | 
|   | 
| function Ret_ResultType($server_name, $ret) | 
| { | 
|     $ResultType = $ret["ResultType"]; | 
|     if ($ResultType != 0) { | 
|         echo "<font color=\"#FF0000\">"; | 
|     } | 
|     echo \Lang\gettext("发送服务器") . "【 " . $server_name . " 】: "; | 
|     echo GetResultTypeMsg($ResultType), "<br/>"; | 
|     if ($ResultType != 0) { | 
|         if (array_key_exists("ResultMsg", $ret) && $ret["ResultMsg"] != "") { | 
|             echo "  ResultMsg: ", \CommFunc\MyJson_encode($ret["ResultMsg"]), "<br/>"; | 
|         } | 
|         echo "</font>"; | 
|     } else { | 
|         Ret_Detail($ret); | 
|     } | 
|     echo "<hr/>"; | 
| } | 
|   | 
| function Ret_Detail($ret) | 
| { | 
|     global $pack_type, $superGMT; | 
|     $fileName = $superGMT ? "SuperGMToolClient" : "GMToolClient"; | 
|     $poPath = $_SERVER['DOCUMENT_ROOT'] . "/" . $fileName . "/GMTManage/" . $pack_type . ".php"; | 
|     // 结果明细页文件不存在使用默认输出 | 
|     if (!file_exists($poPath)) { | 
|         if (array_key_exists("ResultMsg", $ret) && $ret["ResultMsg"] != "") { | 
|             echo "  ResultMsg: ", \CommFunc\MyJson_encode($ret["ResultMsg"]), "<br/>"; | 
|         } | 
|         return; | 
|     } | 
|     $retdetailUrl = 'http://' . $_SERVER['HTTP_HOST'] . "/" . $fileName . "/GMTManage/" . $pack_type . ".php"; | 
|     \Logging\LogInfo(" post to ret detail Url: " . $retdetailUrl); | 
|     echo \CommFunc\DoPost($retdetailUrl, $ret, true); //这里需要传递session | 
| } | 
|   | 
| function Ret_ErrorNO($server_name, $ret) | 
| { | 
|     $ErrorNo = $ret["ErrorNo"]; | 
|     echo "<font color=\"#FF0000\">"; | 
|     echo \Lang\gettext("发送服务器") . "【 " . $server_name . " 】: "; | 
|     echo GetErrorNoMsg($ErrorNo), "<br/>"; | 
|     if (array_key_exists("ErrorStr", $ret) && $ret["ErrorStr"] != "") { | 
|         echo "  ErrorStr: ", \CommFunc\MyJson_encode($ret["ErrorStr"]), "<br/>"; | 
|     } | 
|     echo "</font>"; | 
|     echo "<hr/>"; | 
| } | 
|   | 
| function Ret_NotRet($server_name) | 
| { | 
|     \Logging\LogError("请求异常,没有返回结果 server_name:" . $server_name); | 
|     echo "<font color=\"#FF0000\">"; | 
|     echo \Lang\gettext("发送服务器") . "【 " . $server_name . " 】: "; | 
|     echo \Lang\gettext("请求异常,没有返回结果"), "<br/>"; | 
|     echo "</font>"; | 
|     echo "<hr/>"; | 
| } | 
|   | 
| function ProcessRet($retCode, $retMsg, $SuccessMsg = array(), $ErrorMsg = array()) | 
| { | 
|     global $processType; | 
|     if (!$processType) { | 
|         echo $retMsg; | 
|         return; | 
|     } | 
|     echo \CommFunc\MyJson_encode(array( | 
|         "ProcessRetCode" => $retCode, | 
|         "ProcessRetMsg" => $retMsg, | 
|         "SuccessMsg" => $SuccessMsg, | 
|         "ErrorMsg" => $ErrorMsg, | 
|     )); | 
|     return; | 
| } |