This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.php
594 lines (505 loc) · 13.7 KB
/
main.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<?php
/*
* Null-Pointer Private Server
* Copyright © 2037 Dark Energy Processor Corporation
*/
/// \file main.php
ini_set('html_errors', false);
define('MAIN_INVOKED', '20161216', true);
// Fixes nginx.
// Source: http://www.php.net/manual/en/function.getallheaders.php#84262
if(!function_exists('getallheaders'))
{
function getallheaders(): array
{
$headers = [];
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
$headers[str_replace(' ', '-', ucwords(
strtolower(
str_replace('_', ' ', substr($name, 5))
)
))] = $value;
}
}
return $headers;
}
}
/// Boolean value to store maintenance flag
$MAINTENANCE_MODE = false;
/// Array of headers that client sent
$REQUEST_HEADERS = array_change_key_case(getallheaders(), CASE_LOWER);
/// Flag to indicate the request is ok or not
$REQUEST_SUCCESS = false;
/// Array of the response data. Converted to JSON later
$RESPONSE_ARRAY = [];
/// Main database variable
$DATABASE = NULL;
/// Current timestamp in seconds since January 1st, 1970
$UNIX_TIMESTAMP = time();
/// SIF-compilant current datetime
$TEXT_TIMESTAMP = date('Y-m-d H:i:s', $UNIX_TIMESTAMP);
/// Temporary variable used for npps_config() to detect current module
$CURRENT_MODULE = NULL;
/// Temporary variable used for npps_config() to detect current action
$CURRENT_ACTION = NULL;
if(!defined('WEBVIEW'))
{
set_error_handler(function($errNo, $errStr, $errFile, $errLine)
{
http_response_code(500);
throw new ErrorException("$errStr in $errFile on line $errLine", $errNo);
});
set_exception_handler(function($x)
{
http_response_code(500);
throw $x;
});
}
/// \brief Function to handle shutdown procedure
function npps_shutdown()
{
global $MAINTENANCE_MODE;
global $REQUEST_HEADERS;
global $REQUEST_SUCCESS;
global $RESPONSE_ARRAY;
// We still need to be in this directory
assert(chdir(dirname(__FILE__)));
if($MAINTENANCE_MODE) exit; // Don't do anything on maintenance
header('Content-Type: application/json; charset=utf-8');
header(sprintf("Date: %s", gmdate('D, d M Y H:i:s T')));
$contents = ob_get_contents();
error_log($contents, 4);
if(!npps_config('DEBUG_ENVIRONMENT'))
{
$contents = "";
}
// Check if the request fails
if($REQUEST_SUCCESS != 1)
{
$output = NULL;
if($REQUEST_SUCCESS == 0)
{
header('status_code: 720');
$output = [
'response_data' => ['error_code' => 200],
'status_code' => 720
];
if(strlen($contents) > 0)
$output['message'] = $contents;
}
else
$output = [
'message' => $contents
];
if(http_response_code() == 200 && $REQUEST_SUCCESS == (-1))
// If it's not previously set, then set it
npps_http_code(error_get_last() == NULL ? 403 : 500);
ob_end_clean();
$asd = json_encode($output);
echo $asd;
exit;
}
header("status_code: {$RESPONSE_ARRAY["status_code"]}");
// If there is some leftover buffer, send it too
if(strlen($contents) > 0)
$RESPONSE_ARRAY['message'] = $contents;
// Now send the decryption keys
$RESPONSE_ARRAY['release_info'] = [];
if(npps_config('SEND_RELEASE_INFO'))
foreach(npps_decryption_key() as $id => $key)
$RESPONSE_ARRAY['release_info'][] = ['id' => $id, 'key' => $key];
ob_end_clean();
ob_start('ob_gzhandler');
$output = json_encode($RESPONSE_ARRAY);
if(strlen($output) > 2)
{
header(sprintf('X-Message-Code: %s',
hash_hmac('sha1', $output, X_MESSAGE_CODE_KEY))
);
header(sprintf('X-Message-Sign: %s',
base64_encode(str_repeat("\x00", 128)))
);
echo $output;
}
ob_end_flush();
exit;
}
/// \brief NPPS main script handler. Not merged to npps_main() to prevent
/// variable pollution
/// \param USER_ID Player user ID
/// \param TOKEN Player token
/// \param PLATFORM_ID Platform-Type value from header
/// \param module module/handler to be accessed
/// \param action action in module/handler to be accessed. `NULL` if module is
/// `api`
/// \returns 1 if request success, 0 if request failed in handler, -1 if
/// request failed in module.
function npps_main_script_handler(
int& $USER_ID,
$TOKEN,
int $PLATFORM_ID,
string $module,
$action = NULL): int
{
global $REQUEST_HEADERS;
global $RESPONSE_ARRAY;
global $UNIX_TIMESTAMP;
global $TEXT_TIMESTAMP;
global $CURRENT_MODULE;
global $CURRENT_ACTION;
$request_data = [];
// Used to isolate variables when using include
$isolator_call = function(string $___FILE, $REQUEST_DATA)
use($UNIX_TIMESTAMP, $TEXT_TIMESTAMP, &$TOKEN, &$USER_ID)
{
return (include($___FILE));
};
if(isset($_POST['request_data']))
{
if(npps_config('X_MESSAGE_CODE_CHECK'))
{
if(!isset($REQUEST_HEADERS['x-message-code']))
{
echo 'X-Message-Code header required!';
http_response_code(400);
return 0;
}
if(strcmp($REQUEST_HEADERS['x-message-code'],
hash_hmac('sha1', $_POST['request_data'], X_MESSAGE_CODE_KEY)
))
{
echo 'Invalid X-Message-Code';
http_response_code(400);
return 0;
}
}
$request_data = json_decode(
$_POST['request_data'],
true
);
if($request_data === NULL)
{
echo "Invalid JSON data: {$_POST['request_data']}";
return 0;
}
}
if(npps_config('REQUEST_LOGGING'))
{
// TODO
}
// Handler first. A handler doesn't need to have valid token
if(is_string($action))
{
$modname = "handlers/$module/$action.php";
if(is_file($modname))
{
$CURRENT_MODULE = $module;
$CURRENT_ACTION = $action;
$val = $isolator_call($modname, $request_data);
$CURRENT_MODULE = NULL;
$CURRENT_ACTION = NULL;
if($val === false)
return 0;
if(is_integer($val))
{
$RESPONSE_ARRAY['response_data'] = ['error_code' => $val];
$RESPONSE_ARRAY['status_code'] = 600;
}
else
{
$RESPONSE_ARRAY['response_data'] = $val[0];
$RESPONSE_ARRAY['status_code'] = $val[1];
}
return 1;
}
}
/* Verify credentials existence */
if($TOKEN === NULL || token_exist($TOKEN) == false || $USER_ID == 0)
{
invalid_credentials:
echo 'Invalid login, password, user_id, and/or token!';
return 0;
}
else
{
$cred = npps_query('SELECT login_key, login_pwd FROM `logged_in` WHERE token = ?', 's', $TOKEN)[0];
$connected_uid = user_id_from_credentials($cred['login_key'], $cred['login_pwd'], $TOKEN);
if($connected_uid == 0)
goto invalid_credentials;
else if($connected_uid < 0)
{
npps_http_code(423, 'Account Locked');
return 0;
}
}
// Module mode
if(strcmp($module, 'api') == 0)
{
// Multiple module/action calls
if(count($request_data) > MULTI_REQUEST_LIMIT)
{
echo '/api request limit exceeded!';
return 0;
}
define('MULTI_REQUEST', true);
$RESPONSE_ARRAY['response_data'] = [];
$RESPONSE_ARRAY['status_code'] = 200;
// Call all modules in order
foreach($request_data as $i => $rd)
{
if(isset($rd['module']) && isset($rd['action']))
{
$modname = "modules/{$rd['module']}/{$rd['action']}.php";
if(is_file($modname))
{
$REQUEST_DATA = $rd;
$CURRENT_MODULE = $module;
$CURRENT_ACTION = $action;
$val = $isolator_call($modname, $rd);
$CURRENT_MODULE = NULL;
$CURRENT_ACTION = NULL;
if($val === false)
return false;
if(is_integer($val))
$RESPONSE_ARRAY['response_data'][] = [
'result' => ['error_code' => $val],
'status' => 600,
'commandNum' => false,
'timeStamp' => $UNIX_TIMESTAMP
];
else
$RESPONSE_ARRAY['response_data'][] = [
'result' => $val[0],
'status' => $val[1],
'commandNum' => false,
'timeStamp' => $UNIX_TIMESTAMP
];
}
else
{
echo "Req idx $i: {$rd['module']}/{$rd['action']} not found";
return -1;
}
}
else
{
echo "Req idx $i: module/action key not found";
return -1;
}
}
goto request_ok;
}
else if($action !== NULL)
{
// Single module call in form /main.php/module/action
$modname = "modules/$module/$action.php";
if(is_file($modname))
{
$REQUEST_DATA = $request_data;
$CURRENT_MODULE = $module;
$CURRENT_ACTION = $action;
$val = $isolator_call($modname, $request_data);
$CURRENT_MODULE = NULL;
$CURRENT_ACTION = NULL;
if($val === false)
return -1;
if(is_integer($val))
{
$RESPONSE_ARRAY["response_data"] = ['error_code' => $val];
$RESPONSE_ARRAY["status_code"] = 600;
}
else
{
$RESPONSE_ARRAY["response_data"] = $val[0];
$RESPONSE_ARRAY["status_code"] = $val[1];
}
goto request_ok;
}
echo "Module not found! $module/$action", PHP_EOL;
return -1;
}
else
{
echo 'Invalid module/action';
return 0;
}
request_ok:
npps_user::get_instance($USER_ID)->last_active = $UNIX_TIMESTAMP;
return 1;
}
/// \brief Function to process Authorize header
/// \returns Returns string if array is supplied. Returns array if string is
/// supplied. **Returns false if the authorize parameter is invalid**
function npps_authorize_function($authorize)
{
if(is_array($authorize))
{
// Assemble authorize string
return http_build_query($authorize);
}
elseif(is_string($authorize))
{
// Disassemble authorize string
parse_str($authorize, $new_assemble);
// Check the authorize string
if(
(
isset($new_assemble["consumerKey"]) &&
strcmp($new_assemble["consumerKey"], CONSUMER_KEY) == 0
) &&
(
isset($new_assemble["version"]) &&
strcmp($new_assemble["version"], "1.1") == 0
) &&
isset($new_assemble["nonce"]) &&
isset($new_assemble["timeStamp"])
)
return $new_assemble;
return false;
}
}
// Load includes
require_once('modules/include.php');
// Initialize configuration
npps_config();
/// \brief NPPS main function. Preparation to process user request is done here
function npps_main()
{
global $MAINTENANCE_MODE;
global $REQUEST_HEADERS;
global $REQUEST_SUCCESS;
global $RESPONSE_ARRAY;
global $DATABASE;
global $MAIN_SCRIPT_HANDLER;
// Will be modified later by the server_api handler
$USER_ID = 0;
$TOKEN = NULL;
$AUTHORIZE_DATA = NULL;
$MODULE_TARGET = NULL;
$ACTION_TARGET = NULL;
// Set timezone
if(npps_config('DEFAULT_TIMEZONE'))
date_default_timezone_set(DEFAULT_TIMEZONE);
// Check if it's maintenance
if(($MAINTENANCE_MODE = (file_exists("Maintenance") ||
file_exists("Maintenance.txt") ||
file_exists("maintenance") ||
file_exists("maintenance.txt"))
))
{
header('Maintenance: 1');
exit;
}
// Check the authorize
if(isset($REQUEST_HEADERS['authorize']))
$AUTHORIZE_DATA = npps_authorize_function($REQUEST_HEADERS['authorize']);
if($AUTHORIZE_DATA === false)
{
echo 'Authorize header needed!';
exit;
}
$TOKEN = $AUTHORIZE_DATA["token"] ?? NULL;
// Check if client-version is OK
if(isset($REQUEST_HEADERS['client-version']))
{
if(npps_config('SERVER_VERSION'))
{
$ver1 = explode('.', SERVER_VERSION);
$ver2 = explode('.', $REQUEST_HEADERS['client-version']);
$trigger_version_up = NULL;
for($i = 0; $i < 3; $i++)
{
if(strcmp($ver1[$i], '*') != 0 && $ver1[$i] != $ver2[$i])
{
$trigger_version_up = str_replace('*', '0', SERVER_VERSION);
break;
}
}
$trigger_version_up = $trigger_version_up ??
$REQUEST_HEADERS['client-version'] ??
SERVER_VERSION;
header("Server-Version: $trigger_version_up");
}
else
header("Server-Version: {$REQUEST_HEADERS['client-version']}");
}
else
{
echo 'Client-Version header needed!';
exit;
}
// Check Platform-Type header
if(!isset($REQUEST_HEADERS['platform-type']))
{
echo 'Platform-Type header needed!';
exit;
}
else if(!is_numeric($REQUEST_HEADERS['platform-type']))
{
echo 'Invalid Platform-Type!';
exit;
}
// get the module and the action. Use different scope to prevent variables
// pollution
{
$x = [];
preg_match('!main.php/(\w+)/?(\w*)!', $_SERVER['REQUEST_URI'], $x);
if(isset($x[1]))
$MODULE_TARGET = $x[1];
else
{
echo 'Module needed!';
exit;
}
if(isset($x[2]) && strlen($x[2]) > 0)
$ACTION_TARGET = $x[2];
}
if(isset($REQUEST_HEADERS['user-id']) || isset($AUTHORIZE_DATA['user_id']))
{
if(isset($REQUEST_HEADERS['user-id']))
if(preg_match('/\d+/', $REQUEST_HEADERS['user-id']) == 1)
$USER_ID = intval($REQUEST_HEADERS['user-id']);
else
{
echo 'Invalid user ID';
exit;
}
}
// Load database wrapper and initialize it
$DATABASE = require('database_wrapper.php');
$DATABASE->initialize_environment();
// Call main script handler
$REQUEST_SUCCESS = npps_main_script_handler(
$USER_ID,
$TOKEN,
$REQUEST_HEADERS['platform-type'],
$MODULE_TARGET ?? 'api',
$ACTION_TARGET
);
// Check if user id changed
if($USER_ID > 0)
header("user_id: $USER_ID");
// Reassemble authorize header
{
$new_authorize = [];
foreach($AUTHORIZE_DATA as $k => $v)
$new_authorize[$k] = $v;
$new_authorize['requestTimeStamp'] = $new_authorize['timeStamp'];
$new_authorize['timeStamp'] = time();
$new_authorize['user_id'] = $USER_ID > 0 ? $USER_ID : "";
if(is_string($TOKEN))
$new_authorize['token'] = $TOKEN;
header(sprintf('authorize: %s', npps_authorize_function($new_authorize)));
}
// Exit. Let the shutdown function do the rest
exit;
}
if(!defined('WEBVIEW'))
{
register_shutdown_function('npps_shutdown');
ob_start();
npps_main();
}