forked from rparrish/redcap-ddp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
99 lines (84 loc) · 2.49 KB
/
index.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
<?php
/*
* RESTful API which routes REDCap requests from supported projects to the
* metadata and data web services.
*
*/
// Basic initialization of web service
require ('rest/meta.php');
require ('rest/auth.php');
require ('rest/data.php');
// TODO: Define API settings, such as whether a secure connection is required
function deliver_response($api_response) {
// Define HTTP responses
$http_response_code = array (
200 => 'OK',
400 => 'Bad Request',
401 => 'Unauthorized',
403 => 'Forbidden',
404 => 'Not Found'
);
// Set HTTP Response
header ( 'HTTP/1.1 ' . $api_response ['status'] . ' ' . $http_response_code [$api_response ['status']] );
echo $api_response ['data'];
exit ();
}
// Define whether an HTTPS connection is required
$HTTPS_required = FALSE;
// Define whether user authentication is required
$authentication_required = FALSE;
// Define API response codes and their related HTTP response
$api_response_code = array (
0 => array (
'HTTP Response' => 400,
'Message' => 'Unknown Error'
),
1 => array (
'HTTP Response' => 200,
'Message' => 'Success'
),
2 => array (
'HTTP Response' => 403,
'Message' => 'HTTPS Required'
),
3 => array (
'HTTP Response' => 401,
'Message' => 'Authentication Required'
),
4 => array (
'HTTP Response' => 401,
'Message' => 'Authentication Failed'
),
5 => array (
'HTTP Response' => 404,
'Message' => 'Invalid Request'
),
6 => array (
'HTTP Response' => 400,
'Message' => 'Invalid Response Format'
)
);
// Set default HTTP response of 'ok'
$response ['code'] = 0;
$response ['status'] = 404;
$response ['data'] = NULL;
if ($authentication_required) {
$response ['data'] = auth ( $_POST ["user"], $_POST ["project_id"], $_POST ["redcap_url"] );
}
// Calls the API
$response ['code'] = 1;
$response ['status'] = $api_response_code [$response ['code']] ['HTTP Response'];
if ($_SERVER ['CONTENT_TYPE'] == 'application/x-www-form-urlencoded') {
$metaws = new Meta ();
$response ['data'] = $metaws->meta ( $_POST ["user"], $_POST ["project_id"], $_POST ["redcap_url"] );
unset($metaws);
} else if ($_SERVER ['CONTENT_TYPE'] == 'application/json') {
$json = file_get_contents ( 'php://input' );
$obj = json_decode ( $json, true );
$dataws = new Data ();
$response ['data'] = $dataws->data ( $obj ["user"], $obj ["project_id"], $obj ["redcap_url"], $obj ["id"], $obj ["fields"] );
unset($dataws);
}
// Return Response to browser
deliver_response ( $response );
?>