-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
225 lines (173 loc) · 7.44 KB
/
api.php
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
<?php
require_once "minimal_init.php";
require_once "common.php";
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');
// Get data from db
$input = GS_get_common_input();
$db = DB::getInstance();
$servers = GS_list_servers($input["server"], $input["password"], GS_REQTYPE_WEBSITE, 0);
$mods = GS_list_mods($servers["mods"], array_keys($input["modver"]), $input["modver"], $input["password"], GS_REQTYPE_WEBSITE, $servers["lastmodified"]);
$servers_keys = array_keys($servers["info"]);
$mod_keys = array_keys($mods["info"]);
$all_user_id = [];
$to_remove = ["id", "ip", "password", "access", "type", "starttime", "timezone", "duration", "modified1", "modified2"];
// Edit server columns
foreach($servers_keys as $id) {
$servers["info"][$id]["admin"] = "";
$servers["info"][$id]["voice"] = "";
$servers["info"][$id]["logo"] = $servers["info"][$id]["logo"]!="" ? (GS_get_current_url().GS_LOGO_FOLDER."/".$servers["info"][$id]["logo"]) : "";
$servers["info"][$id]["equalmodreq"] = $servers["info"][$id]["equalmodreq"]=="1" ? true : false;
$servers["info"][$id]["maxcustomfilesize"] = intval($servers["info"][$id]["maxcustomfilesize"]);
$servers["info"][$id]["version"] = floatval($servers["info"][$id]["version"]);
$servers["info"][$id]["modified"] = strtotime($servers["info"][$id]["modified1"]);
$servers["info"][$id]["created"] = strtotime($servers["info"][$id]["created"]);
// Replace mod id with mod unique id
foreach($servers["info"][$id]["mods"] as $index=>$mod_id)
$servers["info"][$id]["mods"][$index] = $mods["info"][$mod_id]["uniqueid"];
// Replace voice ip with program name
foreach(GS_VOICE as $program_name=>$program_info)
if (substr($servers["info"][$id]["voice"],0,strlen($program_info["url"])) == $program_info["url"])
$servers["info"][$id]["voice"] = $program_name;
// Add user id to the list
if (!in_array($servers["info"][$id]["createdby"],$all_user_id))
$all_user_id[] = $servers["info"][$id]["createdby"];
if (!in_array($servers["info"][$id]["modifiedby"],$all_user_id))
$all_user_id[] = $servers["info"][$id]["modifiedby"];
}
// Edit mod columns
foreach($mod_keys as $id) {
$mods["info"][$id]["admin"] = "";
$mods["info"][$id]["forcename"] = $mods["info"][$id]["forcename"]=="true" ? true : false;
$mods["info"][$id]["is_mp"] = $mods["info"][$id]["is_mp"]=="1" ? true : false;
$mods["info"][$id]["type"] = intval($mods["info"][$id]["type"]);
$mods["info"][$id]["version"] = floatval($mods["info"][$id]["version"]);
$mods["info"][$id]["modified"] = strtotime($mods["info"][$id]["modified"]);
$mods["info"][$id]["created"] = strtotime($mods["info"][$id]["created"]);
$temp_array = explode(",", substr($mods["info"][$id]["sizearray"], 1, -1));
foreach($temp_array as $index=>$item)
$temp_array[$index] = intval($item);
$mods["info"][$id]["sizearray"] = ["GB"=>$temp_array[0], "MB"=>$temp_array[1], "KB"=>$temp_array[2]];
foreach($mods["info"][$id]["updates"] as $id2=>$update) {
$mods["info"][$id]["updates"][$id2]["version"] = floatval($update["version"]);
$mods["info"][$id]["updates"][$id2]["date"] = strtotime($update["date"]);
foreach($update["note_date"] as $id3=>$notedate)
$mods["info"][$id]["updates"][$id2]["note_date"][$id3] = strtotime($notedate);
foreach($update["note_version"] as $id3=>$noteversion)
$mods["info"][$id]["updates"][$id2]["note_version"][$id3] = floatval($noteversion);
}
// Add user id to the list
if (!in_array($mods["info"][$id]["createdby"],$all_user_id))
$all_user_id[] = $mods["info"][$id]["createdby"];
if (!in_array($mods["info"][$id]["modifiedby"],$all_user_id))
$all_user_id[] = $mods["info"][$id]["modifiedby"];
}
// Convert user id numbers to usernames
if (!empty($all_user_id)) {
$sql = "
SELECT
users.id,
users.username
FROM
users
WHERE
users.id IN (". substr( str_repeat(",?",count($all_user_id)), 1) . ")
";
if (!$db->query($sql,$all_user_id)->error()) {
$all_user_names = [];
foreach($db->results(true) as $row)
$all_user_names[$row["id"]] = $row["username"];
foreach($servers_keys as $id) {
$servers["info"][$id]["createdby"] = $all_user_names[ $servers["info"][$id]["createdby"] ];
$servers["info"][$id]["modifiedby"] = $all_user_names[ $servers["info"][$id]["modifiedby"] ];
}
foreach($mod_keys as $id) {
$mods["info"][$id]["createdby"] = $all_user_names[ $mods["info"][$id]["createdby"] ];
$mods["info"][$id]["modifiedby"] = $all_user_names[ $mods["info"][$id]["modifiedby"] ];
}
} else {
$to_remove[] = "createdby";
$to_remove[] = "modifiedby";
}
}
// Get server/mod administrator names
foreach(["serv","mods"] as $type) {
$column_name = "";
$input_array_ptr = "";
$output_array_ptr = "";
switch($type) {
case "serv" : $column_name="Server"; $input_array_ptr="servers_keys"; $output_array_ptr="servers"; break;
case "mods" : $column_name="Mod"; $input_array_ptr="mod_keys"; $output_array_ptr="mods"; break;
}
$sql = "
SELECT
gs_{$type}.id,
users.username
FROM
gs_{$type} JOIN gs_{$type}_admins
ON gs_{$type}.id = gs_{$type}_admins.{$column_name}ID
JOIN users
ON gs_{$type}_admins.userid = users.id
WHERE
gs_{$type}.id IN (". substr( str_repeat(",?",count($$input_array_ptr)), 1) .") AND
gs_{$type}_admins.isowner=1
";
if (!$db->query($sql,$$input_array_ptr)->error()) {
foreach($db->results(true) as $row)
$$output_array_ptr["info"][$row["id"]]["admin"] = $row["username"];
}
}
// Remove server columns
foreach($to_remove as $column_name)
foreach($servers_keys as $id)
unset($servers["info"][$id][$column_name]);
// Change server array keys from id to unique id
foreach($servers_keys as $id) {
$uniqueid = $servers["info"][$id]["uniqueid"];
unset($servers["info"][$id]["uniqueid"]);
$servers["info"][$uniqueid] = $servers["info"][$id];
unset($servers["info"][$id]);
}
// Change mod array keys from id to unique id
foreach($mod_keys as $id) {
$uniqueid = $mods["info"][$id]["uniqueid"];
unset($mods["info"][$id]["uniqueid"]);
$mods["info"][$uniqueid] = $mods["info"][$id];
unset($mods["info"][$id]);
}
/*
// List all ID if requested
$servers["listid"] = [];
$mods["listid"] = [];
foreach($input["listid"] as $type) {
$table_name = "";
$array_ptr = "";
switch($type) {
case "servers" : $table_name="serv"; $array_ptr="servers"; break;
case "mods" : $table_name="mods"; $array_ptr="mods"; break;
}
if (empty($$array_ptr["listid"])) {
if (!$db->query("SELECT gs_{$table_name}.uniqueid FROM gs_{$table_name}")->error()) {
foreach($db->results(true) as $row)
$$array_ptr["listid"][] = $row["uniqueid"];
}
}
}*/
$mod_types = [];
for($i=0; $i<4; $i++)
$mod_types[] = lang("GS_STR_MOD_TYPE$i") ." ". lang("GS_STR_MOD_TYPE$i_DESC");
// Output
echo json_encode([
"info" => [
"api_version" => 1,
"gs_version" => GS_VERSION,
"fwatch_lastmodified" => GS_fwatch_date_to_timestamp(GS_FWATCH_LAST_UPDATE),
"gs_lastmodified" => $mods["lastmodified"],
"voice" => GS_VOICE,
"mod_types" => $mod_types
],
"servers" => $servers["info"],
"mods" => $mods["info"]
]);
?>