-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
179 lines (146 loc) · 5.4 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
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
<?php
require_once './vendor/autoload.php';
use Paxx\Withings\Api as WithingsApi;
use Paxx\Withings\Server\Withings as WithingsAuth;
use Carbon\Carbon;
session_start();
require_once "config.php";
//a touch of colors
$darkorange="#cc6600";
$orange= "#ff6600";
$yellow= "#fcd202";
$darkyellow="#b09303";
$blue= "#6074ea";
$darkblue= "#0f1d71";
$green= "#67d642";
$darkgreen="#235412";
$prune= "#cc00ff";
$darkprune="#660080";
$grey= "#666666";
$darkgrey= "#404040";
//sume ugly global vars
$startdate = null;
$usersize=null;
$measures=array();
$lastfetch=null;
$datarefreshed=false;
$userFirstName="";
// See if we were requested to refresh our data or obviously have to
if (array_key_exists('forcerefresh',$_GET) || !isset($_SESSION['lastfetch'])) {
$datarefreshed=true;
try {
$withingsconfig = array(
'identifier' => $config["identifier"],
'secret' => $config["secret"],
'callback_uri' => $config["callback_uri"]
);
$server = new WithingsAuth($withingsconfig);
if (isset($_SESSION['oauth_token'])) {
if (isset($_SESSION['token_credentials'])) {
$tokenCredentials = unserialize($_SESSION['token_credentials']);
} else {
// Step 2
// Retrieve the temporary credentials from step 2
$temporaryCredentials = unserialize($_SESSION['temporary_credentials']);
// Retrieve token credentials - you can save these for permanent usage
$tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_SESSION['oauth_token'], $_SESSION['oauth_verifier']);
// Store the credentials in the session.
$_SESSION['token_credentials'] = serialize($tokenCredentials);
}
// Also save the userId
$userId = $_SESSION['userid'];
} else {
// Step 1
// These identify you as a client to the server.
$temporaryCredentials = $server->getTemporaryCredentials();
// Store the credentials in the session.
$_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
// Redirect the resource owner to the login screen on Withings.
$server->authorize($temporaryCredentials);
}
$withingsconfig = $withingsconfig + array(
'access_token' => $tokenCredentials->getIdentifier(),
'token_secret' => $tokenCredentials->getSecret(),
'user_id' => $userId
);
$api = new WithingsApi($withingsconfig);
$user = $api->getUser();
//finding out the user's size :
foreach($user->getMeasures() as $measure) {
if (is_null($usersize) && !is_null($measure->getHeight())) {
$usersize=$measure->getHeight();
}
}
//if the user connected is myself, then fill in the old data from manual entry using json
if ($user->getId()==$config["userIdJson"]) {
$startdate=Carbon::createFromFormat($config["jsonLastDateFormat"], $config["jsonLastDate"]);
}
$userFirstName = $user->getFirstName();
// Building the set of measures
/**
* @var \Paxx\Withings\Entity\Measure $measure
*/
foreach($user->getMeasures() as $measure) {
if (!is_null($startdate) && $measure->getCreatedAt()->gt($startdate)) {
array_push($measures,$measure);
} elseif (is_null($startdate)){
array_push($measures,$measure);
}
}
$measures = array_reverse($measures);
// store the measures in session :
$_SESSION['measures']=serialize($measures);
$lastfetch = Carbon::now();
$_SESSION['lastfetch']=serialize($lastfetch);
$_SESSION['usersize']=serialize($usersize);
$_SESSION['userFirstName']=$userFirstName;
} catch (Exception $e) {
// something went wrong, most probably in oauth. So we clean oAuth and retry :
unset($_SESSION['oauth_token']);
unset($_SESSION['oauth_verifier']);
unset($_SESSION['userid']);
header('Location: ./forcerefresh.php');
exit;
}
} else {
//here we have not been requested to refresh our data set.
// just load it from session:
$measures = unserialize($_SESSION['measures']);
$lastfetch = unserialize($_SESSION['lastfetch']);
$usersize = unserialize($_SESSION['usersize']);
$userFirstName = $_SESSION['userFirstName'];
//check if data is not too old :
$threshold = Carbon::now()->subHours($config["refreshDelay"]);
if ($lastfetch->lt($threshold)) {
//our data is too old, force refreshing :
header('Location: ./forcerefresh.php');
exit;
}
}
include 'templates/html-part1.php';
include 'templates/html-graph-1.php';
// at this point, we only need to fill in the dataProvider before including the next html part.
//if the user connected is myself, then fill in the old data from manual entry using json
if ($_SESSION['userid']==$config["userIdJson"]) {
echo file_get_contents($config["dataJson"]);
}
foreach($measures as $measure) {
if ($measure->getWeight()) {
printf('{"TimeStamp": "%s", "Weight": "%s", "BMI": "%s",',$measure->getCreatedAt(), round($measure->getWeight(),1), round($measure->getWeight()/($usersize*$usersize),1));
if ($measure->getFatRatio()>0) {
printf(' "Fat": "%s",', round($measure->getFatRatio(),2));
}
if ($measure->getHydrationRatio()>0) {
printf(' "Water": "%s",', round($measure->getHydrationRatio(),2));
}
if ($measure->getMuscleRatio()>0) {
printf(' "Muscle": "%s",', round($measure->getMuscleRatio(),2));
}
if ($measure->getBoneRatio()>0) {
printf(' "Bone": "%s",', round($measure->getBoneRatio(),2));
}
printf(' },%s',"\n");
}
}
include 'templates/html-graph-2.php';
include 'templates/html-part2.php';