forked from auxlife/TideSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidepool.inc.php
222 lines (199 loc) · 6.96 KB
/
tidepool.inc.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
<?php
include_once "db.inc.php";
if(!set_time_limit(0))
die("Could not set infinite script timeout.");
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$sessionStart = date(DATE_ATOM);
$sessionToken = FALSE;
$sessionData = array();
function CheckFile($filename) {
if(!file_exists($filename))
touch($filename);
return TRUE;
}
function Kill($string) {
echo '<h1><font color="red">Fatal Error!!</font><h1><br>'.$string;
die();
}
function ConvertToMgDl($value) {
return $value * 18.018018;
}
function ConvertToMmol($value) {
return $value / 18.018018;
}
function GetDeviceTime($value) {
$DT = strtotime($value);
return date('Y-m-d',$DT).'T'.date('H:i:s',$DT);
}
function GetUtcTime($value) {
$DT = strtotime($value);
$DT = $DT + ($GLOBALS['UTCoffset'] * -60);
return date('Y-m-d',$DT).'T'.date('H:i:s',$DT).".000Z";
}
function GetUtcOffset() {
return $GLOBALS['UTCoffset'];
}
function DownloadAllData() { // not working
$Querystring = 'METAQUERY WHERE emails CONTAINS ' . implode($GLOBALS['sessionData']['emails']) . 'QUERY TYPE IN basal, bolus, cbg, cgmSettings, deviceEvent, deviceMeta, pumpSettings, settings, smbg';
$url = $GLOBALS['baseurl'] . '/query/data';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-tidepool-session-token: ' . $GLOBALS['sessionToken'],'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $Querystring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
file_put_contents("data_download.json", $response);
return $response;
}
function UploadBG($date, $type, $value, $payload) {
if($type == "sgv") {
$JSONstring = '{"uploadId":"'.GetUploadID().'","deviceId":"'.$GLOBALS['deviceId'].'","deviceTime":"'.GetDeviceTime($date).'","time":"'.GetUtcTime($date).'","timezoneOffset":'.$GLOBALS['UTCoffset'].',"type":"cbg","units":"mmol/L","value":'.ConvertToMmol($value).','.$payload.'}';
return UploadData($JSONstring);
}
if($type == "mbg") {
$JSONstring = '{"uploadId":"'.GetUploadID().'","deviceId":"'.$GLOBALS['deviceId'].'","deviceTime":"'.GetDeviceTime($date).'","time":"'.GetUtcTime($date).'","timezoneOffset":'.$GLOBALS['UTCoffset'].',"type":"smbg","subType":"manual","units":"mmol/L","value":'.ConvertToMmol($value).','.$payload.'}';
return UploadData($JSONstring);
}
}
function UploadData($JSONstring) {
$url = $GLOBALS['uploadapi'].'/data';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-tidepool-session-token: ' . $GLOBALS['sessionToken'],'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $JSONstring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
if(GetHttpStatus($response) != 200)
{
Kill("Sorry the upload failed.<br>Request:<br>".$JSONstring."<br>Response:<br>".$response);
}
return TRUE;
}
function GetGroupIds(){ // needs repair
$url = $GLOBALS['baseurl'] . '/access/groups/' . $GLOBALS['sessionData']['userid'];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-tidepool-session-token: ' . $GLOBALS['sessionToken']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
function GetUploadID() {
return 'upid_' . substr(md5($GLOBALS['deviceId'] . '_' . $GLOBALS['sessionStart']),0, 12);
}
function GetHttpStatus($string) {
$StatusStart = 'HTTP/1.1 ';
$StatusEnd = PHP_EOL;
return SubString($string, $StatusStart, $StatusEnd);
}
function ExtractToken($string) {
$TokenStart = 'x-tidepool-session-token: ';
$TokenEnd = PHP_EOL;
return SubString($string, $TokenStart, $TokenEnd);
}
function GetJSON($string) {
$JSONStart = '{';
$JSONEnd = '}';
return '{' . SubString($string, $JSONStart, $JSONEnd) . '}';
}
function SubString($string, $start, $end) {
$string = substr($string, strpos($string, $start) + strlen($start));
return substr($string,0,strpos($string, $end));
}
function IsTokenValid() {
if($GLOBALS['sessionToken'] != FALSE)
{
$url = $GLOBALS['baseurl'] . '/auth/login';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-tidepool-session-token: ' . $GLOBALS['sessionToken']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
if(GetHttpStatus($response) == 200)
{
$GLOBALS['sessionStart'] = date(DATE_ATOM);
return TRUE;
}
$GLOBALS['sessionToken'] = FALSE;
return FALSE;
}
return FALSE;
}
function LoadToken() {
if(!file_exists("tidepool.auth"))
return FALSE;
$response = file_get_contents("tidepool.auth");
$GLOBALS['sessionData'] = json_decode(GetJSON($response), true);
$GLOBALS['sessionToken'] = ExtractToken($response);
$GLOBALS['sessionStart'] = date(DATE_ATOM);
if(!IsTokenValid())
{
unlink("tidepool.auth");
return FALSE;
}
return TRUE;
}
function Login() {
if($GLOBALS['sessionToken'] == FALSE)
{
if(LoadToken())
return TRUE;
$url = $GLOBALS['baseurl'] . '/auth/login';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . $GLOBALS['loginHash']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
if(GetHttpStatus($response) != 200)
{
Kill("Sorry the login failed.<br>Response:<br>".$response);
}
$GLOBALS['sessionData'] = json_decode(GetJSON($response), true);
$GLOBALS['sessionToken'] = ExtractToken($response);
$GLOBALS['sessionStart'] = date(DATE_ATOM);
file_put_contents("tidepool.auth", $response);
return TRUE;
}
if(IsTokenValid())
return TRUE;
else
return Login();
return FALSE;
}
function Logout() {
LoadToken();
$url = $GLOBALS['baseurl'] . '/auth/logout';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('x-tidepool-session-token: ' . $GLOBALS['sessionToken']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$response = curl_exec($ch);
curl_close($ch);
if(GetHttpStatus($response) != 200)
{
Kill("Sorry the logout failed.<br>Response:<br>".$response);
}
$GLOBALS['sessionToken'] = FALSE;
unlink("tidepool.auth");
return TRUE;
}
function GetToken() {
return $GLOBALS['sessionToken'];
}
?>