diff --git a/db.inc.php b/db.inc.php
new file mode 100644
index 0000000..2823701
--- /dev/null
+++ b/db.inc.php
@@ -0,0 +1,34 @@
+
\ No newline at end of file
diff --git a/dbload.php b/dbload.php
new file mode 100644
index 0000000..9c43fb2
--- /dev/null
+++ b/dbload.php
@@ -0,0 +1,75 @@
+
\ No newline at end of file
diff --git a/dbupload.php b/dbupload.php
new file mode 100644
index 0000000..e1dd574
--- /dev/null
+++ b/dbupload.php
@@ -0,0 +1,13 @@
+
\ No newline at end of file
diff --git a/tidepool.inc.php b/tidepool.inc.php
new file mode 100644
index 0000000..dfad33d
--- /dev/null
+++ b/tidepool.inc.php
@@ -0,0 +1,226 @@
+Fatal Error!!
'.$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) {
+ 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).'}';
+ 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).'}';
+ return UploadData($JSONstring);
+ }
+
+}
+
+function UploadData($JSONstring) {
+ $url = 'https://uploads.tidepool.org/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.
Request:
".$JSONstring."
Response:
".$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.
Response:
".$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.
Response:
".$response);
+ }
+ $GLOBALS['sessionToken'] = FALSE;
+ unlink("tidepool.auth");
+ return TRUE;
+}
+
+function GetToken() {
+ return $GLOBALS['sessionToken'];
+}
+?>
diff --git a/tidepoolsync.php b/tidepoolsync.php
new file mode 100644
index 0000000..6160481
--- /dev/null
+++ b/tidepoolsync.php
@@ -0,0 +1,85 @@
+";
+usort($array, 'sortByDate');
+ConnectDB();
+foreach ($array as $entry) {
+ if($entry["type"] == "sgv")
+ echo InsertValue($entry["dateString"], $entry["type"], $entry["sgv"] , '"payload":{"internalTime":"'.GetDeviceTime($entry["dateString"]).'","delta":'.$entry["delta"].',"noiseMode":"'.$entry["noise"].'","trend":"'. $entry["direction"].'","value"'.ConvertToMmol($entry["sgv"]).'}');
+ if($entry["type"] == "mbg")
+ echo InsertValue($entry["dateString"], $entry["type"], $entry["mbg"] , '"payload":{"subType":"manual","time":"'.GetUTCTime($entry["dateString"]).'","timezoneOffset":'.GetUtcOffset().',"type":"smbg","units":"mmol/L","value":'.ConvertToMmol($entry["mbg"]).'}');
+}
+DisconnectDB();
+rename("v1/data.old", "v1/olddata/".RandomString(12).".".RandomString(4));
+echo "
Uploading to Tidepool:
";
+Login();
+ConnectDB();
+$results = RunQuery("SELECT * FROM `Data` WHERE `Uploaded` IS NULL ORDER BY `Data`.`DateString` ASC");
+while ($row = mysqli_fetch_array($results)){
+ echo UploadBG($row["DateString"],$row["Type"],$row["Value"],$row["Payload"]);
+ ConfirmUpload($row["Payload"]);
+}
+DisconnectDB();
+Logout();
+?>
\ No newline at end of file