-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.php
327 lines (297 loc) · 8.3 KB
/
index.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
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
<?php
/**
* @param $key
* @param null $default
* @return mixed|null
*/
function input($key, $default = null)
{
if (isset($_GET[$key])) {
return $_GET[$key];
}
if (isset($_POST[$key])) {
return $_POST[$key];
}
return $default;
}
/**
* @param $array
* @param $key
* @param null $default
* @return mixed|null
*/
function value($array, $key, $default = null)
{
if (!is_array($array)) {
return $default;
}
if (isset($array[$key])) {
return $array[$key];
}
return $default;
}
/**
* @param $string
* @param $find
* @return bool
*/
function strExists($string, $find)
{
if (!is_string($string) || !is_string($find)) {
return false;
}
return !(strpos($string, $find) === FALSE);
}
/**
* @param null $path
* @return string
*/
function url($path = null)
{
$protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
if (substr($path, 0, 1) === '/') {
return $protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $path;
}
$php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
$req_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self . (isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : $path_info);
$curUrl = $protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '') . $req_url;
if ($path === null) {
return $curUrl;
}
return dirname($curUrl) . '/' . $path;
}
/**
* @param $srcurl
* @param string $baseurl
* @return mixed|string
*/
function formatUrl($srcurl, $baseurl = '')
{
if (empty($srcurl)) {
return "";
}
if (empty($baseurl)) {
$baseurl = url('index.php');
}
$srcinfo = parse_url($srcurl);
if (isset($srcinfo['scheme'])) {
return $srcurl;
}
$baseinfo = parse_url($baseurl);
$url = $baseinfo['scheme'] . '://' . $baseinfo['host'];
if (substr($srcinfo['path'], 0, 1) == '/') {
$path = $srcinfo['path'];
} else {
$path = dirname($baseinfo['path']) . '/' . $srcinfo['path'];
}
$rst = [];
$path_array = explode('/', $path);
if (!$path_array[0]) {
$rst[] = '';
}
foreach ($path_array AS $key => $dir) {
if ($dir == '..') {
if (end($rst) == '..') {
$rst[] = '..';
} elseif (!array_pop($rst)) {
$rst[] = '..';
}
} elseif ($dir && $dir != '.') {
$rst[] = $dir;
}
}
if (!end($path_array)) {
$rst[] = '';
}
$url .= implode('/', $rst);
return str_replace('\\', '/', $url);
}
/**
* @return array|mixed
*/
function getConfig()
{
$array = [];
$file = __DIR__ . '/config.json';
if (is_file($file)) {
$array = json_decode(file_get_contents($file), true);
}
$array['appkey'] = value($array, 'appkey', 'test');
$array['welcome_image'] = formatUrl(value($array, 'welcome_image', ''));
$array['welcome_wait'] = intval(value($array, 'welcome_wait', 2000));
$array['welcome_skip'] = intval(value($array, 'welcome_skip', 1));
$array['welcome_jump'] = formatUrl(value($array, 'welcome_jump', ''));
$array['welcome_limit_s'] = intval(value($array, 'welcome_limit_s', 0));
$array['welcome_limit_e'] = intval(value($array, 'welcome_limit_e', 0));
return $array;
}
/**
* @param $file
* @return array
*/
function getJson($file)
{
$array = [
'platform' => '',
'debug' => 0,
'valid' => 1,
'reboot' => 2,
'reboot_info' => [],
'clear_cache' => 0,
];
$jsonFile = substr($file, 0, strlen($file) - 4) . '.json';
if (is_file($jsonFile)) {
$jsonArray = json_decode(file_get_contents($jsonFile), true);
$array = array_merge($array, $jsonArray);
}
if (empty($array['platform'])) {
$array['platform'] = 'android,ios';
}
$array['platform'] = ',' . strtolower($array['platform']) . ',';
$array['debug'] = intval($array['debug']);
$array['valid'] = intval($array['valid']);
$array['reboot'] = intval($array['reboot']);
$array['clear_cache'] = intval($array['clear_cache']);
if (!is_array($array['reboot_info'])) {
$array['reboot_info'] = [];
}
$array['reboot_info']['title'] = value($array['reboot_info'], 'title', '温馨提示');
$array['reboot_info']['message'] = value($array['reboot_info'], 'message', '已为您更新至最新版本');
$array['reboot_info']['confirm_reboot'] = 1;
return $array;
}
/**
* @param $str
* @return mixed
*/
function idEncode($str) {
$str = str_replace('/', '--2-2f-f--', $str);
return $str;
}
/**
* @param $str
* @return mixed
*/
function idDecode($str) {
$str = str_replace('--2-2f-f--', '/', $str);
return $str;
}
/**
* @param $version
* @param $platform
* @param $debug
* @return array
*/
function getUplists($version, $platform, $debug)
{
$dir = 'zip/' . $version . '/';
$lists = glob(__DIR__ . '/' . $dir . '*.zip', GLOB_BRACE);
sort($lists);
$uplists = [];
foreach ($lists AS $key => $file) {
if (is_dir($file)) {
continue;
}
$jsonArray = getJson($file);
if (!strExists($jsonArray['platform'], ',' . $platform . ',')) {
continue;
}
if ($debug && $jsonArray['debug'] !== 1) {
continue;
}
if ($jsonArray['valid'] === 0) {
continue;
}
$fileName = basename($file);
$filePath = $dir . $fileName;
$uplists[] = [
'id' => idEncode(substr($filePath, 0, strlen($filePath) - 4)),
'size' => sprintf("%.2f", filesize($file) / 1024),
'path' => url($filePath),
'valid' => $jsonArray['valid'],
'clear_cache' => $jsonArray['clear_cache'],
'reboot' => $jsonArray['reboot'],
'reboot_info' => $jsonArray['reboot_info'],
];
}
return $uplists;
}
/**
* @param $msg
* @param array $data
* @param int $ret
*/
function error($msg, $data = [], $ret = 0)
{
$param = [
'ret' => $ret,
'msg' => $msg,
'data' => $data
];
header('Content-Type: application/json');
echo json_encode($param);
die();
}
/**
* @param $msg
* @param array $data
* @param int $ret
*/
function success($msg, $data = [], $ret = 1)
{
$param = [
'ret' => $ret,
'msg' => $msg,
'data' => $data
];
header('Content-Type: application/json');
echo json_encode($param);
die();
}
/** ************************************************************************************************/
/** ************************************************************************************************/
/** ************************************************************************************************/
$act = input('act', 'app');
switch ($act) {
case "update-success":
{
$id = idDecode(input('id'));
$path = $file = __DIR__ . '/' . $id . '.success.log';
file_put_contents($path, date("Y-m-d H:i:s") . "\n", FILE_APPEND);
success('success');
break;
}
case "update-delete":
{
$id = idDecode(input('id'));
$path = $file = __DIR__ . '/' . $id . '.delete.log';
file_put_contents($path, date("Y-m-d H:i:s") . "\n", FILE_APPEND);
success('success');
break;
}
case "app":
{
$appkey = input('appkey');
$package = input('package');
$version = input('version');
$platform = strtolower(input('platform'));
$debug = intval(input('debug'));
if (empty($appkey)) {
error("appkey is empty!");
}
if (empty($version)) {
error("version is empty!");
}
if (empty($platform)) {
error("version is platform!");
}
$config = getConfig();
if ($config['appkey'] !== $appkey) {
error("appkey is error!");
}
$config['uplists'] = getUplists($version, $platform, $debug);
success('success', $config);
break;
}
}