-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_base.php
700 lines (564 loc) · 31.3 KB
/
api_base.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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
<?php
/* ────────────────────────────────────────────────────────────────────────── */
/* api_base.php */
/* ────────────────────────────────────────────────────────────────────────── */
/* ──────── Made with ❤️ by darknetzz @ https://github.com/darknetzz ──────── */
/* ────────────────────────────────────────────────────────────────────────── */
/*
This file contains the API base functions - in other words essential functions
for the API to work. You can of course tweak it however you want, but most of the config
should be done in `settings` and not here, unless you know what you're doing.
*/
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function err */
/* ────────────────────────────────────────────────────────────────────────── */
function err(string $text, int $statusCode = 500, bool $fatal = true) {
log_write($text, 'verbose');
http_response_code($statusCode);
return json_encode(
[
"httpCode" => $statusCode,
"status" => "ERROR",
"data" => $text,
]
);
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function var_assert */
/* ────────────────────────────────────────────────────────────────────────── */
function var_assert(mixed &$var, mixed $assertVal = false, bool $lazy = false) : bool {
if (!isset($var) || empty($var)) {
return false;
}
if ($assertVal != false || func_num_args() > 1) {
if ($lazy != false) {
return $var == $assertVal;
}
return $var === $assertVal;
}
return true;
}
/* ────────────────────────────────────────────────────────────────────────── */
/* Get user IP */
/* ────────────────────────────────────────────────────────────────────────── */
function userIP() {
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
if (!empty($_SERVER['REMOTE_ADDR'])) {
return $_SERVER['REMOTE_ADDR'];
}
die(err("Unable to determine IP"));
}
/* ────────────────────────────────────────────────────────────────────────── */
/* Close file */
/* ────────────────────────────────────────────────────────────────────────── */
function fh_close(mixed &$fh) {
$tries = 5;
$i = 0;
while (is_resource($fh)) {
if ($i > $tries) {
break;
}
fclose($fh);
$i++;
}
return !is_resource($fh);
}
/* ────────────────────────────────────────────────────────────────────────── */
/* endpoint_open */
/* ────────────────────────────────────────────────────────────────────────── */
function endpoint_open(string $endpoint) {
if (WHITELIST_MODE == True) {
foreach (OPEN_ENDPOINTS as $openep) {
if ($endpoint == $openep || 'api_'.$endpoint == $openep) {
return True;
}
}
}
if (WHITELIST_MODE == False) {
foreach (PROTECTED_ENDPOINTS as $protep) {
if ($endpoint != $protep && 'api_'.$endpoint != $protep) {
return True;
}
}
}
return false;
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: log_write() */
/* ────────────────────────────────────────────────────────────────────────── */
function log_write($txt, $level = 'info') {
if (isset($log_enable) && $log_enable !== false) {
try {
global $apikey_logging;
if (!isset($apikey_logging) || !$apikey_logging === true) {
return;
}
$level = strtoupper($level);
$log_level = (defined('LOG_LEVEL') ? strtoupper(LOG_LEVEL) : 'INFO');
$log_enable = (defined('LOG_ENABLE') ? LOG_ENABLE : null);
$log_file = (defined('LOG_FILE') ? LOG_FILE : 'api.log');
$log_maxlines = (defined('LOG_MAXLINES') ? LOG_MAXLINES: 1000);
if (!in_array($log_level, array_keys(LOG_LEVELS))) {
die(err("You have specified a LOG_LEVEL that doesn't exist in the LOG_LEVELS array: ".$log_level." not in ".implode(', ', array_keys(LOG_LEVELS))));
}
$thisLevel = LOG_LEVELS[$level];
$myLevel = LOG_LEVELS[$log_level];
if ($myLevel < $thisLevel) {
echo "$myLevel < $thisLevel";
return;
}
if (!file_exists($log_file)) {
touch($log_file);
$testwrite = file_put_contents($log_file, 'Testing write access');
if (!$testwrite && !file_exists($log_file)) {
die(err("Function log_write was unable to write to log. Check the permissions of the log file: $log_file"));
}
unlink($log_file);
$currLog = "Logfile created at ".date('Y-m-d H:i:s')."\n";
} else {
$padding = max(array_map('strlen', LOG_LEVELS));
$plevel = str_pad("[$level] ", $padding, ' ');
$prefix = $plevel." ".userIP().": ";
$lines = file($log_file);
$remainder = $log_maxlines - count($lines);
$currLine = 0;
while ($remainder < 0) {
unset($lines[$currLine]);
$currLine++;
$remainder++;
}
// $fh = fopen($log_file, 'w+');
// $currLog = fread($fh);
// $lines = 0;
// while (!feof($fh)) {
// $line = fgets($fh);
// $lines++;
// }
// while ($log_maxlines - $lines > 0) {
// $lines--;
// }
}
$writeLog = $currLog.$prefix.$txt."\n";
$fh = fopen($log_file, 'w+');
fwrite($fh, $writeLog);
fh_close($fh);
return;
} catch(Throwable $t) {
die(err("Unable to write to log: $t"));
}
}
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function funnyResponse */
/* ────────────────────────────────────────────────────────────────────────── */
function funnyResponse(string $type, array $vars = []) : string {
/* ──────────────────────────── Mandatory checks ──────────────────────────── */
if (!isset($vars["endpoint"]) || empty($vars["endpoint"])) {
return "It's hard to generate a funny response to an endpoint that doesn't exist!";
}
if (!endpointExists($vars["endpoint"])) {
return "I'm not sure what endpoint you are trying to connect to... What is $vars[endpoint]?";
}
$func = $vars["endpoint"];
/* ────────────────────────────────── Vars ────────────────────────────────── */
$validVars = [
"endpoint",
"secondsToWait",
"requiredParams",
"requiredParamCount",
"specifiedParams",
"allParamCount",
"allParamNames",
"paramsCleanCount",
"secondsSinceLastCalled",
"noTimeOut",
];
if (!is_array($vars)) {
return "No vars passed to funnyResponse(), what am I supposed to do then?";
}
foreach ($validVars as $validVar) {
$$validVar = "";
}
foreach ($vars as $varVar => $varVal) {
if (!in_array($varVar, $validVars)) {
return "You have specified an invalid variable: $varVar";
}
$$varVar = $varVal;
}
# Not sure if this ever happens? I think $requiredParams always will be a string
if (!empty($allParamNames)) {
$csParams = "Parameters for this endpoint: ".implode(', ',$allParamNames);
} else {
$csParams = "Parameters for this endpoint: none";
}
/* ────────────────────────────────────────────────────────────────────────── */
/* ────────────────────────────────── Types ───────────────────────────────── */
$validTypes = [
"COOLDOWN" => [
"default" => "The endpoint '$func' was called a mere $secondsSinceLastCalled seconds ago! Please wait another $secondsToWait.",
"funny" => [
"Wooow, not so fast cowboy! You still need to wait $secondsToWait.",
"I understand that you're in a hurry, but could you please just wait $secondsToWait before trying again?",
"Waaaaaiiiiiittttt! Please try again in $secondsToWait!",
],
],
"WRONG_PARAM_COUNT" => [
"default" => "Wrong amount of parameters given. Endpoint '$func' has $allParamCount available parameters ($requiredParamCount required). you provided $paramsCleanCount.",
"funny" => [
"Wait a minute, you specified $paramsCleanCount, but this endpoint requires $requiredParamCount of them... $csParams",
"Alright now you are confusing me... I need $requiredParamCount parameters for this function to work, but for some reason you gave me only $paramsCleanCount. $csParams",
],
],
"ENDPOINT_FALSY" => [
"default" => "The endpoint '$func' returned an empty/false response.",
"funny" => [
"This endpoint isn't being a sport today, and returned a negative response.",
"Endpoint says no, sorry pal...",
"I think the endpoint is having a bad day (it sure looks negative to me).",
],
],
];
if (array_key_exists($type, $validTypes) === false) {
return "A funny response would be generated, if it had an existing type... But '$type' isn't what I'm looking for!";
}
if (FUNNY_RESPONSES_ENABLE !== true) {
return (!empty($validTypes[$type]["default"]) ? $validTypes[$type]["default"] : "You don't want funny responses, but you cba to set a default response? Okay then...");
}
if (!is_array($validTypes[$type]["funny"]) || count($validTypes[$type]["funny"]) < 1) {
return $validTypes[$type]["default"];
}
$count = count($validTypes[$type]["funny"]);
if ($count > 0) {
$rand = mt_rand(0, $count-1);
$response = (isset($validTypes[$type]["funny"][$rand]) ? $validTypes[$type]["funny"][$rand] : $validTypes[$type]["default"]);
} else {
$response = $validTypes[$type]["default"];
}
return $response;
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function api_response */
/* ────────────────────────────────────────────────────────────────────────── */
function api_response(string $status, mixed $data) : string {
global $_REQUEST;
$params = $_REQUEST;
log_write("api_response(): The API responded with a status of $status.");
$pretty_print = JSON_UNESCAPED_UNICODE;
if (var_assert(($params['compact']), true)) {
$pretty_print = JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT;
}
if (!array_key_exists($status, HTTP_STATUS_CODES)) {
return err("Invalid status");
}
$httpCode = HTTP_STATUS_CODES[$status];
$verboseInfo = "";
if (var_assert($data["verboseInfo"])) {
$verboseInfo = $data["verboseInfo"];
unset($data["verboseInfo"]);
}
$return = ["httpCode" => $httpCode, "status" => $status, "data" => $data];
# filter (httpCode / status / data)
if (var_assert($params['filter'])) {
$filter = $params['filter'];
if (!var_assert($return[$filter])) {
return err("The filter $filter isn't valid for this endpoint. Valid options are: ".implode(", ", VALID_FILTERS));
}
$return = [$filter => $return[$filter]];
}
# /filter
# filterdata (endpoint output filtering)
if (var_assert($params['filterdata'])) {
$filterdata = strstr($params['filterdata'], " ", "");
$filterdata = explode(",", $params['filterdata']);
$allFilters = [];
foreach ($filterdata as $thisfilter) {
if (!var_assert($return['data']['response'][$thisfilter])) {
// $validOptions = (count($verboseInfo['allParamNames']) > 0 ? implode(",", $verboseInfo['allParamNames']) : "none");
return err("The datafilter $thisfilter isn't valid for this endpoint.");
}
array_push($allFilters, $return['data']['response'][$thisfilter]);
}
$return = $allFilters;
}
# /filter
# verboseinfo
if (var_assert($params['verbose'], 'true')) {
if (!var_assert($verboseInfo)) {
$return['data']['response']['verboseInfo'] = "The verbose flag was present, but the content is empty.";
} else {
$return['data']['response']['verboseInfo'] = $verboseInfo;
}
}
# /verboseinfo
# clean
if (var_assert($params['clean'], 'true')) {
if (is_array($data['response'])) {
return err("The 'clean' option for this endpoint is disabled because it returns an array.");
}
return reset($data['response']);
}
# /clean
return json_encode($return, $pretty_print);
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function callFunction */
/* ────────────────────────────────────────────────────────────────────────── */
function callFunction(string $func, array $params = []) {
log_write("Attempting to call function $func with parameters: ".implode($params), 'verbose');
try {
/* ────────────────────────────────────────────────────────────────────────── */
/* Initial checks */
/* ────────────────────────────────────────────────────────────────────────── */
# defaults
$apikey = null;
$apikey_options = null;
$apikey_logging = null;
$valid_apikey = null;
# Check for endpoint param
if (!var_assert($params["endpoint"])) {
die(err("No endpoint was provided"));
}
$endpoint = $params["endpoint"];
# Verify existing endpoint
if (!function_exists($func)) {
die(err("Invalid endpoint '$func'"));
}
/* ────────────────────────────────────────────────────────────────────────── */
/* ────────────────────────────────────────────────────────────────────────── */
/* Not an open endpoint */
/* ────────────────────────────────────────────────────────────────────────── */
if (!endpoint_open($endpoint)) {
if (!var_assert($params['apikey'])) {
die(err("Missing required API key."));
}
# API key was provided
$apikey = $params['apikey'];
$valid_apikey = apikey_validate($apikey);
# Invalid API key
if (!$valid_apikey || empty($valid_apikey)) {
die(err("Invalid API key", 403));
}
# The API key default options are given to the API key when created: addAPIKey function
# You don't need to set defaults here, just check it directly in API_KEYS[$valid_apikey]['options']
# Get options from this API key
if (empty(API_KEYS[$valid_apikey]['options'])) {
die(err("The options for this API key cannot be found"));
}
$apikey_options = API_KEYS[$valid_apikey]['options'];
# Get logging option for this API key
if (empty($apikey_options['log_write'])) {
die(err("Option 'log_write' not specified for this API key."));
}
$apikey_logging = $apikey_options['log_write']; # this is used in log_write
# Check if this key is specifically disallowed
if (in_array($endpoint, $apikey_options["disallowedEndpoints"])) {
die(err("You are blacklisted/disallowed from using this endpoint."));
}
# Check allowed endpoints
if (!in_array("*", $apikey_options["allowedEndpoints"]) && !in_array($endpoint, $apikey_options["allowedEndpoints"])) {
die(err("You do not have access to this endpoint.", 403));
}
# Sleep
if (!empty($apikey_options["sleep"])) {
$sleep = $apikey_options["sleep"];
if ($sleep > 0) {
sleep($sleep);
}
}
}
/* ────────────────────────────────────────────────────────────────────────── */
$paramsClean = [];
foreach ($params as $paramName => $paramValue) {
if (!in_array($paramName, GLOBAL_PARAMS)) {
$paramsClean[$paramName] = $paramValue;
}
}
$functionObject = new ReflectionFunction($func);
$allParamNames = $functionObject-> getParameters();
$allParamCount = $functionObject-> getNumberOfParameters();
$requiredParamCount = $functionObject-> getNumberOfRequiredParameters();
$providedParamCount = count($params);
$paramsCleanCount = count($paramsClean);
$secondsSinceLastCalled = secondsSinceLastCalled($func, $valid_apikey);
if (!$secondsSinceLastCalled && $apikey_options['noTimeOut'] === false) {
die(err("Function secondsSinceLastCalled() failed. Please stop spamming this API.", 403));
}
$verboseInfo = [
"allParamNames" => $allParamNames,
"allParamCount" => $allParamCount,
"requiredParamCount" => $requiredParamCount,
"providedParamCount" => $providedParamCount,
"paramsCleanCount" => $paramsCleanCount,
];
# Error: Wrong amount of parameters given
if ($paramsCleanCount < $requiredParamCount) {
return err(funnyResponse(
"WRONG_PARAM_COUNT", [
"endpoint" => $func,
"allParamNames" => $allParamNames,
"allParamCount" => $allParamCount,
"paramsCleanCount" => $paramsCleanCount,
"requiredParamCount" => $requiredParamCount,
]));
}
# Error: Too quick!
if ($secondsSinceLastCalled < COOLDOWN_TIME && $apikey_options["noTimeOut"] === false) {
return err(funnyResponse(
"COOLDOWN", [
"endpoint" => $func,
"secondsSinceLastCalled" => $secondsSinceLastCalled,
"secondsToWait" => (COOLDOWN_TIME - $secondsSinceLastCalled),
"noTimeOut" => $apikey_options["noTimeOut"],
]), 403);
// return err("The endpoint '$func' was called a mere ".$secondsSinceLastCalled." seconds ago! Please wait another ".(COOLDOWN_TIME - $secondsSinceLastCalled)." seconds.");
}
$functionCall = $functionObject->invoke(...$paramsClean);
updateLastCalled($func, $valid_apikey);
if (!$functionCall) {
return err("The endpoint '$func' returned an empty/false response.");
}
if (NOTIFY_API === true) {
if (API_KEYS[$valid_apikey]["options"]["notify"] === true) {
api_sms(NOTIFY_NUMBER, "API Called by $valid_apikey: $params[endpoint]");
}
if (empty($valid_apikey)) {
api_sms(NOTIFY_NUMBER, "API Called by ".userIP().": $params[endpoint]");
}
}
return api_response(status: "OK", data: ["response" => $functionCall, "verboseInfo" => $verboseInfo]);
} catch (Throwable $e) {
return err("Exception encountered while calling endpoint $func. $e");
}
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function secondsSinceLastCalled */
/* ────────────────────────────────────────────────────────────────────────── */
function secondsSinceLastCalled($function_name, $valid_apikey = null) {
try {
if (!file_exists(LAST_CALLED_JSON)) {
touch(LAST_CALLED_JSON);
}
$json_contents = file_get_contents(LAST_CALLED_JSON);
$lf = json_decode($json_contents, true);
# This endpoint is open
if (endpoint_open($function_name) || $valid_apikey == null) {
$valid_apikey = userIP();
}
# Somehow the apikey_name is still empty
if (empty($valid_apikey)) {
die(err("updateLastCalled: This endpoint is either not open, or the api key you provided is null/invalid. IP: ".userIP()." - Name: $valid_apikey"));
}
if (!var_assert($lf[$function_name])) {
$lastcalled = (NOW - COOLDOWN_TIME);
} elseif (!var_assert($lf[$function_name][$valid_apikey])) {
$lastcalled = (NOW - COOLDOWN_TIME);
} else {
$lastcalled = $lf[$function_name][$valid_apikey];
}
return (NOW - $lastcalled);
} catch (Throwable $t) {
# This should not return false, makes it incredibly hard to troubleshoot permission error.
// return false;
die(err($t));
}
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function updateLastCalled */
/* ────────────────────────────────────────────────────────────────────────── */
function updateLastCalled($function_name, $valid_apikey = null) {
try {
if (!file_exists(LAST_CALLED_JSON)) {
touch(LAST_CALLED_JSON);
}
$json_contents = file_get_contents(LAST_CALLED_JSON);
# Create empty array if file empty
if (empty($json_contents)) {
$json_contents = "";
$lf = [];
} else {
$lf = json_decode($json_contents, true);
}
# If function array doesn't exists in JSON file, create it
if (!var_assert($lf[$function_name])) {
$lf[$function_name] = [];
}
# This endpoint is open
if (endpoint_open($function_name) || $valid_apikey == null) {
$valid_apikey = userIP();
}
# Somehow the valid_apikey is still empty
if (empty($valid_apikey)) {
die(err("updateLastCalled: This endpoint is either not open, or the api key you provided is null/invalid. IP: ".userIP()." - Name: $valid_apikey"));
}
$lf[$function_name][$valid_apikey] = NOW;
$fh = fopen(LAST_CALLED_JSON, 'w+');
fwrite($fh, json_encode($lf));
fh_close($fh);
return true;
} catch (Throwable $t) {
die(err($t));
}
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function in_md_array */
/* ────────────────────────────────────────────────────────────────────────── */
function in_md_array($name, $id, $array = API_KEYS) {
if (!is_array($array)) {
die(err("The API_KEYS constant isn't a valid array."));
}
foreach ($array as $key => $val) {
if ($val[$name] == $id) {
return $key;
}
}
return false;
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function apikey_validate */
/* ────────────────────────────────────────────────────────────────────────── */
function apikey_validate($apikey) {
$name = in_md_array("key", $apikey);
return $name;
if (!$name) {
die(err("Invalid key $apikey"));
}
return $name;
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function addAPIKey */
/* ────────────────────────────────────────────────────────────────────────── */
function addAPIKey(string $name, string $key, array $options = []) { # array $allowedEndpoints = [], bool $noTimeOut = false, $notify = true) {
global $apikeys;
if (empty($options)) {
$options = APIKEY_DEFAULT_OPTIONS;
}
foreach ($options as $optName => $optVal) {
}
foreach (APIKEY_DEFAULT_OPTIONS as $optName => $optVal) {
if (!isset($options[$optName])) {
$options[$optName] = APIKEY_DEFAULT_OPTIONS[$optName];
// echo "[DEFAULT $name: $optName = $options[$optName]]";
} else {
$options[$optName] = $options[$optName];
// echo "[CUSTOM $name: $optName = $options[$optName]]";
}
}
$apikeys[$name] =
[
"key" => $key,
"options" => $options,
];
// echo $name;
// print_r($options);
}
/* ────────────────────────────────────────────────────────────────────────── */
/* NOTE: Function endpointExists */
/* ────────────────────────────────────────────────────────────────────────── */
function endpointExists(string $endpoint) {
if (function_exists("api_".$endpoint) || function_exists($endpoint)) {
return true;
}
return false;
}