-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathseti_api.php
More file actions
231 lines (139 loc) · 5.29 KB
/
seti_api.php
File metadata and controls
231 lines (139 loc) · 5.29 KB
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
<?php
/*
/* SETI RESTful Api
/* http://setiquest.org/
*/
include_once('config/config.php');
class SETI {
# Contructor connects user to the REST api
public function __construct($public_key, $private_key) {
$this->public_key = $public_key;
$this->private_key = $private_key;
$this->session_key = $this->get_session_key();
//Check if credentials are not valid
if(!$this->validate_credentials()) {
$this->throw_error(3, 'Your API or session key is not valid.');
return false;
} else {
return true;
}
}
# Gets a session key for the api, either from the PHP session or by requesting a new one
public function get_session_key() {
//Check if the PHP session has a key
@session_start();
if(!empty($_SESSION['seti_session_key'])) {
return $_SESSION['seti_session_key'];
} else {
//Request a new session key from the server
$_SESSION['seti_session_key'] = $this->send_request('get', 'new_session_key', 1);
return $_SESSION['seti_session_key'];
}
}
# Validates the clients credentials
public function validate_credentials($public_key = NULL, $private_key = NULL) {
//Check if alternate credentials have been given
if(isset($public_key, $private_key)) {
$this->public_key = $public_key;
$this->private_key = $private_key;
}
if(substr($this->send_request(), 0, 22) == 'HTTP/1.1 403 Forbidden') {
return false;
} else {
return true;
}
}
# Get the status of the servers
public function get_server_status() {
return json_decode($this->send_request('get', 'server_status', 1));
}
# Get images and their details
public function get_image($number, $image_id = NULL) {
$this->number = $number;
$this->image_id = $image_id;
if(isset($this->image_id)) {
$this->data = 'image/' . $this->number . '/' . $this->image_id;
} else {
$this->data = 'image/' . $this->number;
}
//Check for errors
$this->image_data = get_object_vars(json_decode($this->send_request('get', $this->data, 1)));
if(isset($this->image_data['error'])) {
$this->throw_error(1, $this->image_data['error']);
return false;
} else {
return $this->image_data;
}
}
# Send a response concerning an image (response should be set to 'true' = the image contains an anomaly or 'false' = nothing of interest
public function send_image_response($image_ids, $response) {
$image_id_array = split(":", $image_ids);
$this->response = $response;
foreach ($image_id_array as $image_id) {
$this->image_id = $image_id;
//Build post
$this->POST_array = array('using_post' => 'true', 'function' => 'send_image_response', 'image_id' => $this->image_id, 'response' => $this->response);
//Send request
if($this->send_request('post', $this->POST_array, 1) == 'true') {
//return true;
} else {
return false;
}
}
}
# Sends a request to the api. $content selects what data to return: 1 = body only, 2 = headers and body
public function send_request($verb = 'get', $data = '', $content = 2) {
global $config;
$this->verb = $verb;
$this->data = $data;
$this->content = $content;
$this->curl_handle = curl_init();
curl_setopt($this->curl_handle, CURLOPT_TIMEOUT, 10);
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, true);
//Check what data should be returned
if($this->content == 1) {
curl_setopt($this->curl_handle, CURLOPT_HEADER, false);
} elseif($this->content == 2) {
curl_setopt($this->curl_handle, CURLOPT_HEADER, true);
}
//Determine GET verb
if($this->verb == 'get') {
//Build the get var
$this->get_var = '&data=' . $this->data;
} else {
$this->get_var = '';
}
//Determine POST verb
if($this->verb == 'post') {
curl_setopt($this->curl_handle, CURLOPT_POST, true);
curl_setopt($this->curl_handle, CURLOPT_POSTFIELDS, array_merge($this->data, array('using_post' => 'true')));
}
//Determine if session_key has been set
if(!empty($this->session_key)) {
$this->session_var = '&session_key=' . $this->session_key;
} else {
$this->session_var = '';
}
curl_setopt($this->curl_handle, CURLOPT_URL, $config['server'] . '/api.php?public_key=' . $this->public_key . '&private_key=' . $this->private_key . $this->session_var . $this->get_var);
//Send request
return curl_exec($this->curl_handle);
curl_close($this->curl_handle);
}
# Displays a notice, warning or error from the SETI API
public function throw_error($status, $information) {
$this->status = $status;
$this->information = $information;
$this->output = '<strong>SETI ';
//Compile status
if($this->status == 1) {
$this->output.= 'Notice';
} elseif($this->status == 2) {
$this->output.= 'Warning';
} elseif($this->status == 3) {
$this->output.= 'Error';
}
//Compile information
echo $this->output.= '</strong>: ' . $this->information . '<br />';
}
}
?>