-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEAPConnector.php
96 lines (73 loc) · 2.61 KB
/
LEAPConnector.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
<?php
namespace LEAP\LEAPConnector;
use ExternalModules\AbstractExternalModule;
use ExternalModules\ExternalModules;
use REDCap;
class LEAPConnector extends AbstractExternalModule {
// Generates an auth token and saves it in the configuration
// TODO: this does not work well - the configuration needs to reload for the key to show up. Also, the field should not be editable
function generateAuthToken() {
// Generate a key
$key = bin2hex(random_bytes(16));
// Share key in config options
$this->setSystemSetting('leap_auth', $key);
// Refresh the page
window.location.reload(true);
}
// Check auth
function checkAuthToken($token) {
if (strval($token) == $this->getSystemSetting('leap_auth')) {
return true;
}
return false;
}
// Escape strings for SQL
function escapeString($str) {
$value = db_escape($str);
if (!is_numeric($value)) {
$value = db_real_escape_string($value);
}
return $value;
}
// Gets data from SQL based on fields and filters
function getSqlResult($query) {
try {
// Query SQL using the external modules query() function
$result = $this->query($query);
// Read data
$data = array();
if (db_num_rows($result)) {
while ($row = db_fetch_assoc($result)) {
$data[] = $row;
}
}
// Return query results.
echo json_encode(array('success' => true, 'data' => $data));
exit;
} catch (Exception $e) {
// Return SQL error
// TODO: ensure that sensitive information is not returned in the error
// $this->getSystemSetting('leap_auth')
$this->returnErrorResponse($e->getMessage());
}
}
function getData($filters, $fields, $pid) {
$params = [];
$params['project_id'] = $pid;
$params['return_format'] = 'json';
if ($filters != "") {
$params['filterLogic'] = $filters;
}
if ($fields != "") {
$params['fields'] = explode(', ', $fields);
}
$data = REDCap::getData($params);
echo json_encode(array('success' => true, 'data' => json_decode($data)));
//$all_export_field_names = REDCap::getExportFieldNames();
//echo json_encode($all_export_field_names);
}
function returnErrorResponse($msg) {
echo json_encode(array('success' => false, 'error' => $msg));
exit;
}
}