hxp
2025-06-04 f4a514d5ac952110da846636ecbb9de951eaf3d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?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 && $serverInfo['MainServer'] != $serverInfo['ServerID']) {
            # 有主服的代表是合服的,只允许主服发送, 除非标明了只往某个服发送
            # 单服发送消息,如混(合)服情况下只给其中一个服务器玩家发邮件
            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;
        $post['toServerID'] = $serverInfo['ServerID'];
 
        $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;
    }
    $toServerID = $_POST['toServerID']; // 有指定serverID直接取指定的,没有则取默认
    if ($toServerID) {
        \Logging\LogInfo("get toServerID from post.");
    } else {
        $toServerID = $_SESSION['serverID'];
        \Logging\LogInfo("get toServerID from session.");
    }
    if (!isset($toServerID) || $toServerID <= 0) {
        \Logging\LogError("toServerID is null.");
        ProcessRet(-1, "Error. " . \Lang\gettext("没有目标服务器ID"));
        return;
    }
 
    //有配置面审权限的,玩家没有对应面审权限时,无法直接发送
    if (array_key_exists("NOCheck", $logPerInfo) && !$user->HavePermission($logPerInfo["NOCheck"], false)) {
        $_POST["tool_page"] = $tool_page;
        $_POST["toServerID"] = $toServerID; // 防止和旧参数可能已经存在serverID了,这里直接用 toServerID
        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;
    $post['toServerID'] = $toServerID;
 
    \Logging\LogInfo("post tool_page : " . $tool_page . " toServerID:" . $toServerID);
    \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 "&nbsp;&nbsp;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 "&nbsp;&nbsp;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 "&nbsp;&nbsp;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;
}