forked from CrushedPixel/Mineskull
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_utils.php
77 lines (68 loc) · 1.81 KB
/
api_utils.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
<?php
function api_done($response) {
header('Content-Type: application/json');
echo json_encode($response);
exit;
}
function api_error($err) {
//Return other response code than 200
http_response_code(400);
api_done($err);
}
function argreq($name) {
if (isset($_GET[$name]))
return $_GET[$name];
if (isset($_POST[$name]))
return $_POST[$name];
api_error(param_missing($name));
}
function argopt($name, $default) {
if (isset($_GET[$name]))
return $_GET[$name];
if (isset($_POST[$name]))
return $_POST[$name];
return $default;
}
function generateAuthkey($length = 32) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function check_category($category) {
global $INVALID_CATEGORY;
if(is_numeric($category) === False or (int)$category < 0 or (int)$category > 2) {
api_error($INVALID_CATEGORY);
}
}
function check_array_length($array, $max, $param) {
if(count($array) > $max) {
api_error(too_many_values($param));
}
}
function array_get($array, $key) {
global $INVALID_FILE_UPLOAD;
if(array_key_exists($key, $array)) {
return $array[$key];
} else {
api_error($INVALID_FILE_UPLOAD);
}
}
function readfile_chunked($filename) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
print $buffer;
ob_flush();
flush();
}
return fclose($handle);
}
?>