-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalance.php
422 lines (377 loc) · 14 KB
/
balance.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
// for error reporting on this page
ini_set('display_errors', '1');
error_reporting(E_ALL);
// not https?
if (!isset($_SERVER['HTTPS'])) {
header("Location: https://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]");
exit;
}
// CONSTANT URLs
$bb_server = 'my.rochester.edu';
$bb_url = "https://$bb_server";
$login_path = '/webapps/login/index';
$frameset_path = '/webapps/portal/frameset.jsp';
$main_path = '/webapps/portal/execute/tabs/tabAction?tab_tab_group_id=_23_1';
$sequoia_token_path = '/webapps/bb-ecard-sso-bb_bb60/token.jsp';
$sequoia_auth_url = 'https://ecard.sequoiars.com/eCardServices/AuthenticationHandler.ashx';
$sequoia_balance_url = 'https://ecard.sequoiars.com/eCardServices/eCardServices.svc/WebHttp/GetAccountHolderInformationForCurrentUser';
$sequoia_history_url = 'https://ecard.sequoiars.com/eCardServices/eCardServices.svc/WebHttp/GetTransactionHistoryForCurrentUserSearch';
// types for curl_request() function
define('POST_APPLICATION', 0);
define('POST_MULTIPART', 1);
define('POST_JSON', 2);
function curl_request($path, $post = array(), $flags = POST_APPLICATION) {
global $bb_url;
global $cookies;
if ($path[0] == '/') {
$path = $bb_url . $path;
}
if ($flags != POST_JSON) {
$fields_string = http_build_query($post, '_', '&');
}
$cookies_string = urldecode(http_build_query($cookies, '', '; '));
$c = curl_init($path);
curl_setopt($c, CURLOPT_HEADER, true);
curl_setopt($c, CURLINFO_HEADER_OUT, true);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($c, CURLOPT_COOKIE, $cookies_string);
switch ($flags) {
case POST_APPLICATION:
case POST_MULTIPART:
curl_setopt($c, CURLOPT_POST, count($post));
if ($flags == POST_MULTIPART) {
curl_setopt($c, CURLOPT_POSTFIELDS, $post);
} else {
curl_setopt($c, CURLOPT_POSTFIELDS, $fields_string);
}
break;
case POST_JSON:
curl_setopt($c, CURLOPT_POST, 1);
curl_setopt($c, CURLOPT_POSTFIELDS, trim(json_encode($post), '"'));
curl_setopt($c, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
break;
}
$data = curl_exec($c);
//echo curl_getinfo($c, CURLINFO_HEADER_OUT);
//echo "\n\n";
//echo substr($data, 0, strpos($data, "\r\n\r\n"));
//echo "\n\n";
curl_close($c);
preg_match_all("|Set-Cookie: (.*)\r\n|U", $data, $matches);
foreach ($matches[1] as $cookie_set) {
$cookie_arr = explode('; ', $cookie_set);
foreach ($cookie_arr as $cookie) {
if (strpos($cookie, '=') > 0) {
$kv = explode('=', $cookie,2);
} else {
$kv = array(0 => $cookie, 1 => '');
}
$cookies[$kv[0]] = $kv[1];
}
}
//$data = substr($data, strpos($data, "\r\n\r\n") + 4);
return $data;
}
function bb_login($netid, $pass) {
global $login_path;
$enc_pass = base64_encode($pass);
$data = curl_request($login_path, array('user_id'=>$netid, 'encoded_pw'=>$enc_pass, 'encoded_pw_unicode' => '.'));
return strpos($data, 'Location: https://my.rochester.edu/webapps/portal/frameset.jsp') !== false;
}
function seq_token() {
global $sequoia_token_path;
$data = curl_request($sequoia_token_path);
$matches = array();
if (preg_match('/name="AUTHENTICATIONTOKEN" value="([^"]+)/', $data, $matches)) {
return $matches[1];
} else {
return false;
}
}
function seq_login($sequoia_token) {
global $sequoia_auth_url;
$post = array('AUTHENTICATIONTOKEN' => $sequoia_token);//, 'DESTINATIONURL' => 'https://ecard.sequoiars.com/rochester/eCardCardholder/StudentOverviewPage.aspx');
$data = curl_request($sequoia_auth_url, $post, POST_MULTIPART);
if (strpos(substr($data, strpos($data, "\r\n\r\n")), 'No destination url posted.')) {
return true;
} else {
return false;
}
}
function seq_get_balance() {
global $sequoia_balance_url;
$data = curl_request($sequoia_balance_url, array(), POST_JSON);
$json = substr($data, strpos($data, "\r\n\r\n"));
return $json;
}
function seq_get_hist() {
global $sequoia_history_url;
$data = curl_request($sequoia_history_url, array('startDateString'=>'','endDateString'=>'','fundCode'=>0), POST_JSON);
$json = substr($data, strpos($data, "\r\n\r\n"));
return $json;
}
function session_end() {
// 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();
}
function mon($amount, $p = '+') {
$m = $p;
if ($amount < 0) {
$amount = abs($amount);
$m = '-';
}
$cents = $amount % 100;
if ($cents < 10) $cents = '0'.$cents;
$amount = floor($amount / 100);
return $m.$amount.'.'.$cents;
}
// get username & password
$netid = isset($_POST['netid']) ? $_POST['netid'] : '';
$pass = isset($_POST['pass']) ? $_POST['pass'] : '';
// get vars
$get_hist = isset($_REQUEST['get_hist']) ? $_REQUEST['get_hist'] : 0;
$step = isset($_GET['step']) ? $_GET['step'] : 0;
function parse_result($output, $bal, $hist) {
$output['name'] = $bal['d']['FullName'];
$output['accounts'] = array();
$item_list = $bal['d']['_ItemList'];
if (count($item_list) > 0) {
foreach ($item_list as $item) {
$output['accounts'][] = array(
'name' => trim($item['Name']),
'amount' => mon($item['Balance'], ''));
}
}
if (count($hist) > 0) {
$output['history'] = array();
$item_list = $hist['d']['_ItemList'];
if (count($item_list) > 0) {
foreach ($item_list as $item) {
$output['history'][] = array(
'time' => $item['TransactionDateTimeStr'],
'location' => $item['Location'],
'account' => trim($item['FundName']),
'amount' => mon($item['Amount']),
'type' => $item['Type']
);
}
}
}
return array($output, $bal, $hist);
}
if ($step > 0) {
session_start();
if (isset($_SESSION['cookiejar'])) {
$cookies = array();
$cookie_set = explode('; ', $_SESSION['cookiejar']);
foreach ($cookie_set as $cookie) {
if (strpos($cookie, '=') > 0) {
$kv = explode('=', $cookie,2);
} else {
$kv = array(0 => $cookie, 1 => '');
}
$cookies[$kv[0]] = $kv[1];
}
} else {
// COOKIES THIS SESSION
$cookies = array('cookies_enabled' => 'yes');
}
$result = array();
$error = 'Success.';
if (!isset($_SESSION['netid'])) {
$_SESSION['netid'] = $netid;
} elseif ($_SESSION['netid'] != $netid) {
session_end();
}
switch ($step) {
case 1: // STEP 1: bb_login
if ($netid == '' || $pass == '') {
$succeed = false;
$error = 'Net ID or Password not provided.';
session_end();
} else {
$succeed = bb_login($netid, $pass);
if ($succeed) {
$text = 'Getting a Sequoia token...';
} else {
$error = 'Net ID or Password incorrect or other Blackboard error.';
session_end();
}
}
break;
case 2: // STEP 2: seq_token
$succeed = seq_token();
if ($succeed) {
$_SESSION['sequoia_token'] = $succeed;
$succeed = true;
$text = 'Authenticating with Sequoia...';
} else {
$error = 'Unable to get Sequoia token.';
session_end();
}
break;
case 3: // STEP 3: seq_login
$succeed = seq_login($_SESSION['sequoia_token']);
if ($succeed) {
$text = 'Requesting your balance...';
} else {
$error = 'Unable to log in to Sequoia.';
session_end();
}
break;
case 4: // STEP 4: seq_get_balance
$succeed = seq_get_balance();
if (strlen($succeed) > 0) {
$_SESSION['balance_json'] = $succeed;
$bal_data = $succeed;
$succeed = true;
$text = 'Requesting your transaction history...';
} else {
$succeed = false;
$error = 'Unable to get balance.';
session_end();
}
break;
case 5: // STEP 5: seq_get_histroy
$succeed = seq_get_hist();
if (strlen($succeed) > 0) {
$bal_data = $_SESSION['balance_json'];
$hist_data = $succeed;
$succeed = true;
$text = 'Generating output...';
} else {
$succeed = false;
$error = 'Unable to get transaction history.';
session_end();
}
break;
}
if ($succeed) {
$_SESSION['cookiejar'] = urldecode(http_build_query($cookies, '', '; '));
$step++;
$result['result'] = 100; // 100 Continue
$result['step'] = $step; // current progress
$result['total'] = $get_hist ? 6 : 5; // 6 part progress bar
$result['progtext'] = $text;
if ($step == 6 || ($step == 5 && $get_hist == false)) {
$result['result'] = 200; // 200 OK
$bal = json_decode($bal_data, true);
if (isset($hist_data)) {
$hist = json_decode($hist_data, true);
} else {
$hist = array();
}
list($result, $bal, $hist) = parse_result($result, $bal, $hist);
}
} else {
$result['result'] = 400; // 400 Error
$result['error'] = $error;
}
echo json_encode($result);
} elseif ($netid != '' && $pass != '') {
// SLOW METHOD... (if step= is not set)
//header('Content-Type: application/json');
$result = array();
if (bb_login($netid, $pass)) {
//echo 'Blackboard success.';echo "\n";
if (seq_login(seq_token())) {
//echo 'Got sequoia auth.';echo " token=$sequoia_token\n";
$bal_data = seq_get_balance();
if (!$bal_data) {
$result['result'] = 400;
$result['error'] = 'Sequoia balance request error.';
} else {
$result['result'] = 200;
$result['name'] = $bal_data['d']['FullName'];
$result['accounts'] = array();
$item_list = $bal_data['d']['_ItemList'];
if (count($item_list) > 0) {
foreach ($item_list as $item) {
$result['accounts'][] = array(
'name' => trim($item['Name']),
'amount' => mon($item['Balance'], ''));
}
}
if ($get_hist) {
$hist_data = seq_get_hist();
if (!$hist_data) {
$result['error'] = 'Sequoia history request error.';
} else {
$result['result'] = 200;
$result['history'] = array();
$item_list = $hist_data['d']['_ItemList'];
if (count($item_list) > 0) {
foreach ($item_list as $item) {
$result['history'][] = array(
'time' => $item['TransactionDateTimeStr'],
'location' => $item['Location'],
'account' => trim($item['FundName']),
'amount' => mon($item['Amount']),
'type' => $item['Type']
);
}
}
}
}
}
} else {
$result['result'] = 400;
$result['error'] = 'Sequoia token get failed.';
}
} else {
$result['result'] = 400;
$result['error'] = 'Blackboard says incorrect username or password.';
}
//print_r($result);
//echo print_r(json_decode(json_encode($result)));
echo json_encode($result);
flush();
ob_flush();
/*$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
// 0 => array("file", "tmp/error-output.txt", "a") // stderr is a file to write to
);
$cwd = '/var/www/html/htdocs/users/ugrads/nbook'; // working dir
$env = NULL; // env vars
$process = proc_open('./bb balance -q', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fwrite($pipes[0], "$netid\n$pass\n");
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
// echo "command returned $return_value\n";
}*/
} else {
?>
<!DOCTYPE html>
<html><head><title>Get Balance</title></head>
<body>
<form method="post" autocomplete="off" action="balance.php">
<label for="netid">Net ID:</label><input name="netid" type="text"></input>
<label for="pass">Password:</label><input name="pass" type="password"></input>
<input name="get_hist" type="checkbox" value="1">Get transaction history</input>
<input type="submit" value="Get Balance"></input>
</form>
<?php
}