-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.php
390 lines (303 loc) · 10.9 KB
/
api.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
<?php
/**
*
* sSSs .S S. .S_sSSs .S sSSs
* d%%SP .SS SS. .SS~YS%%b .SS d%%SP
* d%S' S%S S%S S%S `S%b S%S d%S'
* S%S S%S S%S S%S S%S S%S S%|
* S&S S%S SSSS%S S%S d* S S&S S&S
* S&S S&S SSS&S S&S .S* S S&S Y&Ss
* S&S S&S S&S S&S_sdSSS S&S `S&&S
* S&S S&S S&S S&S~YSY%b S&S `S*S
* S*b S*S S*S S*S `S%b S*S l*S
* S*S. S*S S*S S*S S%S S*S .S*P
* SSSbs S*S S*S S*S S&S S*S sSS*S
* YSSP SSS S*S S*S SSS S*S YSS'
* SP SP SP
* Y Y Y
*
* R E L O A D E D
*
* (c) 2012 Fetal-Neonatal Neuroimaging & Developmental Science Center
* Boston Children's Hospital
*
* http://childrenshospital.org/FNNDSC/
* dev@babyMRI.org
*
*/
// we define a valid entry point
define('__CHRIS_ENTRY_POINT__', 666);
//define('CHRIS_CONFIG_DEBUG', true);
// include the configuration
require_once ('config.inc.php');
// include the template class
require_once (joinPaths(CHRIS_CONTROLLER_FOLDER, 'security.controller.php'));
require_once (joinPaths(CHRIS_CONTROLLER_FOLDER, 'data.controller.php'));
require_once (joinPaths(CHRIS_CONTROLLER_FOLDER, 'feed.controller.php'));
require_once (joinPaths(CHRIS_CONTROLLER_FOLDER, 'file.controller.php'));
require_once (joinPaths(CHRIS_CONTROLLER_FOLDER, 'user.controller.php'));
require_once (joinPaths(CHRIS_CONTROLLER_FOLDER, 'tag.controller.php'));
require_once (joinPaths(CHRIS_CONTROLLER_FOLDER, 'token.controller.php'));
// ssh - to be removed when user::controller works
require_once ('Net/SSH2.php');
// enable cross origin requests
header("Access-Control-Allow-Origin: *");
// return values
$start_time = new DateTime();
$result = array(
'status' => 'not-processed',
'username' => 'unknown',
'userid' => -1,
'timestamp' => '',
'execution_time' => 0,
'action' => null,
'what' => null,
'id' => null,
'parameters' => null,
'result' => null);
//
// validate the credentials
//
// check if a token was passed
$loggedIn = false;
if (isset($_GET['token'])){
$token = $_GET['token'];
} else if (isset($_POST['token'])) {
$token = $_POST['token'];
}
if (isset($token)) {
// token provided
$loggedIn = TokenC::validate($token);
} else {
// no token provided
// if we don't have a token, we need to login
$loggedIn = SecurityC::login();
}
if (!$loggedIn) {
// invalid credentials
// destroy the session
session_destroy();
$result['status'] = 'access denied';
} else {
// propagate user attributes
if (isset($_SESSION['username'])) {
$result['username'] = $_SESSION['username'];
}
if (isset($_SESSION['userid'])) {
$result['userid'] = $_SESSION['userid'];
}
//
// API FUNCTIONS
//
if (isset($_GET['action'])){
$action = $_GET['action'];
} else if (isset($_POST['action'])) {
$action = $_POST['action'];
} else {
$action = 'ping';
}
if (isset($_GET['what'])){
$what = $_GET['what'];
} else if (isset($_POST['what'])) {
$what = $_POST['what'];
} else {
$what = '*';
}
if (isset($_GET['id'])){
$id = $_GET['id'];
} else if (isset($_POST['id'])) {
$id = $_POST['id'];
} else {
$id = '*';
}
if (isset($_GET['parameters'])){
$parameters = $_GET['parameters'];
} else if (isset($_POST['parameters'])) {
$parameters = $_POST['parameters'];
} else {
$parameters = Array();
}
// validate inputs, we need at least action + what
if ($action != 'ping' && $action != 'help' && $what == 'dont_know') {
// this is an error
$result['status'] = 'error';
$result['result'] = 'parameter "what" required';
} else {
// check if maintenance is active
if (CHRIS_MAINTENANCE) {
$action = "maintenance";
}
// valid minimal parameters
// check actions
switch($action) {
case "add":
if ($what == 'file') {
$result['result'] = FileC::add($_POST['targetFeed'], $_FILES);
} else if($what == 'tag'){
// user_id, tagname, tagcolor
$result['result'] = TagC::add($_SESSION['userid'], $_POST['tagname'], $_POST['tagcolor']);
}
break;
case "remove":
if($what == 'tag'){
// user_id, tag_id
$result['result'] = TagC::remove($_SESSION['userid'], $_POST['tagid']);
}
break;
case "count":
if ($what == 'feed') {
$result['result'] = FeedC::getCount($_SESSION['userid']);
} else if ($what == 'data') {
$result['result'] = DataC::getCount($_SESSION['userid']);
} else if ($what == 'running') {
$result['result'] = FeedC::getRunningCount($_SESSION['userid']);
} else if ($what == 'datafeedrunning') {
$result['result'] = Array();
$result['result'][] = DataC::getCount($_SESSION['userid']);
$result['result'][] = FeedC::getCount($_SESSION['userid']);
$result['result'][] = FeedC::getRunningCount($_SESSION['userid']);
}
break;
case "set":
if($what == 'feed_status'){
$result['result'] = FeedC::status($_POST['feedid'], $_POST['status'], $_POST['op']);
}
if($what == 'tag'){
$result['result'] = FeedC::tag($_POST['feedid'], $_POST['tagid'], $_POST['remove']);
}
else if ($what == 'feed_favorite') {
$result['result'] = FeedC::favorite($id);
}
else if($what == 'feed_share'){
$result['result'] = FeedC::share($id, $result['userid'], $result['username'], $parameters);
}
else if($what == 'feed_archive'){
$result['result'] = FeedC::archive($id);
}
else if($what == 'feed_cancel'){
$result['result'] = FeedC::cancel($id);
}
else if($what == 'file') {
// here we store content to a file
$name = joinPaths(CHRIS_USERS, $parameters[0]);
// make sure to escape line breaks etc.
$content = escapeshellarg($parameters[1]);
// replace file contents
shell_exec('sudo su '.$_SESSION['username'].' -c "echo '.$content.' > '.$name.'"');
$result['result'] = $name.' written.';
} else if($what == 'feed_merge') {
// merge on server
// grab the master id
$master_feed_id = $id;
// .. and the slave id
$slave_feed_id = $parameters;
// merge the feeds
$merged = FeedC::mergeFeeds($master_feed_id, $slave_feed_id);
if ($merged) {
// and archive the slave
FeedC::archive($slave_feed_id);
$result['result'] = 'done';
} else {
// feeds not merged since there was a collision
$result['result'] = 'error';
}
} else if($what == 'feed_name') {
// rename on server
$result['result'] = FeedC::updateName($id, $parameters);
}
break;
case "get":
if ($what == 'feed_updates') {
$result['result'] = FeedC::updateClient($_SESSION['userid'], $parameters[0]);
} else if($what == 'feed_previous'){
$result['result'] = FeedC::scrollClient($_SESSION['userid'], $parameters[0], $parameters[1]);
} else if($what == 'feed_search'){
// if 'TAGPLUGIN flan, we do a quick search'
if(isset ($parameters[1]) && $parameters[1] == 'TAGPLUGIN' ){
$result['result'] = FeedC::searchTagPlugin($_SESSION['userid'], $parameters[0]);
}
else{
$result['result'] = FeedC::searchClient($_SESSION['userid'], $parameters[0]);
}
} else if($what == 'file') {
$name = joinPaths(CHRIS_USERS, $parameters);
// enable cross origin requests
header("Access-Control-Allow-Origin: *");
// if the file does not exist, just die
if (!is_file($name)) {
die();
}
$fp = fopen($name, 'rb');
fpassthru($fp);
die();
} else if($what == 'users') {
$result['result'] = UserC::get();
} else if($what == 'tag'){
$result['result'] = TagC::get($_SESSION['userid']);
} else if($what == 'directory_content'){
$output = array();
exec('/usr/bin/php5 '.CHRIS_CONTROLLER_FOLDER.'/feed.browser.connector.php -d '.$_POST['dir'], $output);
$result['result'] = implode($output);
} else if($what == 'token') {
$result['result'] = TokenC::create();
} else if($what == 'dicomscene') {
$name = joinPaths(CHRIS_USERS, $parameters);
// this only works with directories
if (!is_dir($name)) {
die();
}
$dicom_files = glob($name."/{*.dcm,*.dicom}",GLOB_BRACE);
$output = array("volume"=>array("file"=>array()));
foreach ($dicom_files as &$df) {
// 1. create a token
$token = TokenC::create();
// 2. generate url (including the token)
$url = CHRIS_TRANSFER_PROTOCOL."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?token=".$token."&action=download&what=file¶meters=".joinPaths($parameters,basename($df));
// 3. attach to output
$output['volume']['file'][] = $url;
}
// return JSON encoded output
die(json_encode($output));
}
break;
case "download":
if($what == 'file') {
// here we don't create JSON but just pass thru the file content
$name = joinPaths(CHRIS_USERS, $parameters);
// if the file does not exist, just die
if (!is_file($name)) {
die();
}
$fp = fopen($name, 'rb');
header("Content-Length: " . filesize($name));
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".basename($name)."\"");
fpassthru($fp);
die();
}
case "help":
$result['result'] = 'Perform actions on ChRIS.. Examples: COUNT: ?action=count&what=feed --- GET: ?action=get&what=feed&id=3 --- All parameters can be GET or POST.';
break;
case "maintenance":
// this is maintenance mode
$result['result'] = 'maintenance';
break;
case "ping":
default:
// this is a ping
$result['result'] = 'Up and running.';
}
}
$result['action'] = $action;
$result['what'] = $what;
$result['id'] = $id;
$result['parameters'] = $parameters;
$result['status'] = 'done';
}
// return the results
$end_time = new DateTime();
$execution_time = $end_time->diff($start_time);
$result['timestamp'] = $end_time->format('Y-m-d H:i:s');
$result['execution_time'] = $execution_time->format('%s seconds');
echo json_encode($result);
?>