-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
308 lines (257 loc) · 10.7 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', FALSE );
header( 'Pragma: no-cache' );
// Force HTTPS
if ( $_SERVER['SERVER_ADDR'] != "10.1.1.1" && $_SERVER["HTTPS"] != "on" ) {
header( "Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] );
exit();
}
if ( ! function_exists( "nxr_destroy_session" ) ) {
function nxr_destroy_session() {
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if ( ini_get( "session.use_cookies" ) ) {
$params = session_get_cookie_params();
setcookie( session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
}
}
if ( ! function_exists( "nxr" ) ) {
require_once( dirname( __FILE__ ) . "/inc/functions.php" );
}
if ( ! defined( 'DEBUG_MY_PROJECT' ) ) {
define( 'DEBUG_MY_PROJECT', FALSE );
}
// start the session
session_start();
if ( ! array_key_exists( "timeout", $_SESSION ) || ! is_numeric( $_SESSION['timeout'] ) ) {
$_SESSION['timeout'] = time() + 60 * 5;
} else if ( $_SESSION['timeout'] < time() ) {
nxr_destroy_session();
header( "Location: ./" );
}
if ( ! array_key_exists( "core_config", $_SESSION ) || ! is_array( $_SESSION['core_config'] ) || count( $_SESSION['core_config'] ) == 0 ) {
require_once( dirname( __FILE__ ) . "/config.inc.php" );
if ( isset( $config ) ) {
$_SESSION['core_config'] = $config;
}
}
// Split-up the input URL to workout whats required
if ( array_key_exists( "REDIRECT_URL", $_SERVER ) ) {
$inputURL = $_SERVER['REDIRECT_URL'];
} else {
$inputURL = "";
}
$sysPath = str_ireplace( $_SESSION['core_config']['url'], "", $_SESSION['core_config']['http/'] );
nxr( "inputURL: " . $inputURL );
nxr( "sysPath: " . $sysPath );
//nxr("sysPath: " . $_SERVER['']);
if ( $sysPath != "/" ) {
$inputURL = str_replace( $sysPath, "", $inputURL );
}
if ( substr( $inputURL, 0, 1 ) == "/" ) {
$inputURL = substr( $inputURL, 1 );
}
$inputURL = explode( "/", $inputURL );
$url_namespace = $inputURL[0];
nxr( "Namespace Called: " . $url_namespace );
if ( $url_namespace == "register" && ! array_key_exists( "_nx_fb_usr", $_COOKIE ) ) {
// Authorise a user against Fitbit's OAuth AIP
nxr( "New user registration started" );
// Setup the App
require_once( dirname( __FILE__ ) . "/inc/app.php" );
$NxFitbit = new NxFitbit();
// Sent the user off too Fitbit to authenticate
$helper = new djchen\OAuth2\Client\Provider\Fitbit( [
'clientId' => $NxFitbit->getSetting( "api_clientId", NULL, FALSE ),
'clientSecret' => $NxFitbit->getSetting( "api_clientSecret", NULL, FALSE ),
'redirectUri' => $NxFitbit->getSetting( "api_redirectUri", NULL, FALSE )
] );
// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
// (e.g. state).
$authorizationUrl = $helper->getAuthorizationUrl( array(
'scope' => array(
'activity',
'heartrate',
'location',
'nutrition',
'profile',
'settings',
'sleep',
'social',
'weight'
)
) );
// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $helper->getState();
// Redirect the user to the authorization URL.
header( 'Location: ' . $authorizationUrl );
exit;
} else if ( $url_namespace == "authorise" && array_key_exists( "_nx_fb_usr", $_COOKIE ) && $_COOKIE["_nx_fb_usr"] != "" ) {
// Authorise a user against Fitbit's OAuth AIP
if ( DEBUG_MY_PROJECT ) {
echo __FILE__ . " @" . __LINE__ . " ## authorise - " . $_COOKIE['_nx_fb_usr'] . "<br />\n";
}
// Setup the App
require_once( dirname( __FILE__ ) . "/inc/app.php" );
$NxFitbit = new NxFitbit();
// We're even talking about a valid user right?
if ( $NxFitbit->isUser( $_COOKIE['_nx_fb_usr'] ) ) {
// And lets double check we still need to register
if ( $NxFitbit->valdidateOAuth( $NxFitbit->getUserOAuthTokens( $_COOKIE['_nx_fb_usr'], FALSE ) ) ) {
if ( DEBUG_MY_PROJECT ) {
echo __FILE__ . " @" . __LINE__ . " ## " . $_COOKIE['_nx_fb_usr'] . " is already authorised with Fitbit<br />\n";
} else {
header( "Location: " . $_SESSION['core_config']['http/admin'] . "/" );
exit();
}
} else {
// Sent the user off too Fitbit to authenticate
if ( $_COOKIE['_nx_fb_usr'] == $NxFitbit->getSetting( "ownerFuid" ) ) {
$personal = "_personal";
} else {
$personal = "";
}
$helper = new djchen\OAuth2\Client\Provider\Fitbit( [
'clientId' => $NxFitbit->getSetting( "api_clientId" . $personal, NULL, FALSE ),
'clientSecret' => $NxFitbit->getSetting( "api_clientSecret" . $personal, NULL, FALSE ),
'redirectUri' => $NxFitbit->getSetting( "api_redirectUri" . $personal, NULL, FALSE )
] );
// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
// (e.g. state).
$authorizationUrl = $helper->getAuthorizationUrl( array(
'scope' => array(
'activity',
'heartrate',
'location',
'nutrition',
'profile',
'settings',
'sleep',
'social',
'weight'
)
) );
// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $helper->getState();
// Redirect the user to the authorization URL.
header( 'Location: ' . $authorizationUrl );
exit;
}
} else if ( DEBUG_MY_PROJECT ) {
echo __FILE__ . " @" . __LINE__ . " ## This is not a valid user<br />\n";
} else {
// When we don't know what to do put the user over to the user interface screens
header( "Location: " . $_SESSION['core_config']['http/admin'] . "/" );
exit();
}
} else if ( $url_namespace == "callback" || $url_namespace == "rti" ) {
// Process the return information from a Fitbit authentication flow
if ( empty( $_GET['state'] ) || ( $_GET['state'] !== $_SESSION['oauth2state'] ) ) {
unset( $_SESSION['oauth2state'] );
exit( 'Invalid state' );
} else {
try {
// Setup the App
require_once( dirname( __FILE__ ) . "/inc/app.php" );
$NxFitbit = new NxFitbit();
// Sent the user off too Fitbit to authenticate
if ( $url_namespace == "rti" ) {
$personal = "_personal";
} else {
$personal = "";
}
$helper = new djchen\OAuth2\Client\Provider\Fitbit( [
'clientId' => $NxFitbit->getSetting( "api_clientId" . $personal, NULL, FALSE ),
'clientSecret' => $NxFitbit->getSetting( "api_clientSecret" . $personal, NULL, FALSE ),
'redirectUri' => $NxFitbit->getSetting( "api_redirectUri" . $personal, NULL, FALSE )
] );
// Try to get an access token using the authorization code grant.
$accessToken = $helper->getAccessToken( 'authorization_code', [
'code' => $_GET['code']
] );
// Find out who the new OAuth keys belong too
$resourceOwner = $helper->getResourceOwner( $accessToken );
// Check again that this really is one of our users
if ( $NxFitbit->isUser( $resourceOwner->getId() ) ) {
nxr( "User OAuth credentials installed" );
// Update the users new keys
$NxFitbit->setUserOAuthTokens( $resourceOwner->getId(), $accessToken );
// Since we're done pass them back to the Admin UI
header( "Location: " . $_SESSION['core_config']['http/admin'] . "/" );
exit();
} else {
nxr( " OAuth return for new user: " . $resourceOwner->getId() );
$pre_auth = $NxFitbit->getSetting( "owners_friends" );
$pre_auth = explode( ",", $pre_auth );
array_push( $pre_auth, $NxFitbit->getSetting( "ownerFuid" ) );
if ( array_search( $resourceOwner->getId(), $pre_auth ) ) {
$newUserName = $resourceOwner->getId();
$NxFitbit->getFitbitAPI( $newUserName )->setUserAccessToken( $accessToken );
$NxFitbit->getFitbitAPI( $newUserName )->setActiveUser( $newUserName );
$newUserProfile = $NxFitbit->getFitbitAPI( $newUserName )->pullBabel( 'user/-/profile.json', TRUE );
if ( $NxFitbit->getFitbitAPI( $newUserName )->createNewUser( $newUserProfile->user ) ) {
nxr( " User sent to new password screen" );
header( "Location: " . $_SESSION['core_config']['http/admin'] . "/views/pages/register.php?usr=" . $newUserName );
}
} else {
nxr( " Non Friend registration: " . $resourceOwner->getId() );
header( "Location: " . $_SESSION['core_config']['http/admin'] . "/?err=500" );
}
// When we don't know what to do put the user over to the user interface screens
exit();
}
} catch ( \League\OAuth2\Client\Provider\Exception\IdentityProviderException $e ) {
$this->getAppClass()->getErrorRecording()->captureException( $e, array(
'extra' => array(
'php_version' => phpversion(),
'core_version' => $this->getAppClass()->getSetting( "version", "0.0.0.1", TRUE )
),
) );
exit( $e->getMessage() );
}
}
} else if ( $url_namespace == "webhook" || $url_namespace == "service" ) {
nxr( "Namespace Called: " . $url_namespace );
if ( is_array( $_GET ) && array_key_exists( "verify", $_GET ) ) {
require_once( dirname( __FILE__ ) . "/config.inc.php" );
if ( ( is_array( $config['api_subValidity'] ) and in_array( $_GET['verify'], $config['api_subValidity'] ) ) OR ( $_GET['verify'] == $config['api_subValidity'] ) ) {
header( 'Cache-Control: no-cache, must-revalidate' );
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'Content-type: text/plain' );
header( 'HTTP/1.0 204 No Content' );
nxr( "Valid subscriber request - " . $url_namespace );
} else {
header( 'Cache-Control: no-cache, must-revalidate' );
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'HTTP/1.0 404 Not Found' );
nxr( "Invalid subscriber request - '" . $_GET['verify'] . "' - " . $url_namespace );
nxr(print_r($config['api_subValidity'], true));
}
} else {
// Deal with Fitbit subscriptions
require_once( dirname( __FILE__ ) . "/service.php" );
}
} else if ( $url_namespace != "" && DEBUG_MY_PROJECT ) {
header( 'Cache-Control: no-cache, must-revalidate' );
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
header( 'HTTP/1.0 404 Not Found' );
// If we're debugging things print out the unknown namespace
nxr( "Namespace Called: " . $url_namespace );
} else {
// When we don't know what to do put the user over to the user interface screens
header( "Location: " . $_SESSION['core_config']['http/admin'] . "/" );
exit();
}