-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZaapReceiver.php
142 lines (113 loc) · 2.67 KB
/
ZaapReceiver.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
<?php
/*
Zaap Receiver
v1.2
by @aaviator42
2021-09-27
*/
//Store the endpoint in $endpoint, segmented endpoint in $endpointArray
$endpoint = rtrim(substr(@$_SERVER['PATH_INFO'], 1), '/\\');
$endpointArray = explode("/", $endpoint);
$method = $_SERVER['REQUEST_METHOD'];
$params = $_GET;
if (!empty(file_get_contents('php://input'))){
$input = json_decode(file_get_contents('php://input'), true);
} else {
$input = array();
}
$output = array(
"error" => 0, //bool: 0 = all ok, 1 = error occured
"errorCode" => NULL, //if error occurs, store error code here
"errorMessage" => NULL //if error occurs, store message here
);
switch($method){
case 'PUT':
switch($endpointArray[0]){
case 'putOne':
putOne();
break;
case 'putTwo':
putTwo();
break;
default:
errorInvalidRequest();
break;
}
break;
case 'GET':
switch($endpointArray[0]){
case 'getOne':
getOne();
break;
case 'getTwo':
getTwo();
break;
default:
errorInvalidRequest();
break;
}
break;
case 'POST':
switch($endpointArray[0]){
case 'postOne':
postOne();
break;
case 'postTwo':
postTwo();
break;
default:
errorInvalidRequest();
break;
}
break;
case 'DELETE':
switch($endpointArray[0]){
case 'deleteOne':
deleteOne();
break;
case 'deleteTwo':
deleteTwo();
break;
default:
errorInvalidRequest();
break;
}
break;
default:
errorInvalidRequest();
break;
}
function errorInvalidRequest(){
global $output;
$output["error"] = 1;
$output["errorMessage"] = "API Remote: Invalid request." . PHP_EOL;
$output["errorCode"] = -1;
printOutput(400);
}
function printOutput($code = 200){
global $output;
header('Content-Type: application/json');
http_response_code($code);
echo json_encode($output);
}
//--your functions go below this line--
function getOne(){
global $endpoint, $endpointArray;
global $params, $input, $output;
//$input contains all the information received in the API request body.
//For example:
$filename = $input["filename"];
$line = $input["line"];
//We can do whatever we want with this information.
//For this example, let's just hash the filename, because why not.
$hash = md5($filename);
//We return information through $output
$output["hash"] = $hash;
$output["filename"] = $input["filename"];
//We can also send through a return code, like this:
$output["returnCode"] = 1;
//Finally, we'll print the JSON-fied output.
//The function takes an HTTP status code as the argument.
printOutput(200);
exit(0);
}