forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtripit_common.php
74 lines (64 loc) · 2.05 KB
/
tripit_common.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
<?php
require_once "tripit_api.php";
require_once "db_pdo.php";
require_once "secrets.php";
$tripit_api_url = "https://api.tripit.com";
/**
* Validates that a user has a valid linked TripIt account. If not, redirect the user to
* rendezvous start.
* @param $dbh PDO Database handle
* @param $uid int User ID
* @return array|null Tokens if linked, redirection if not.
*/
function require_tripit_link($dbh, $uid) {
$tripit_tokens = get_request_tokens($dbh, $uid);
if ($tripit_tokens == null) {
header("Location: /php/tripit_rendezvous.php");
exit();
} else {
return $tripit_tokens;
}
}
/**
* @param $dbh PDO Database handle
* @param $uid int User ID
* @return array|null Tokens if linked, null if not.
*/
function get_request_tokens($dbh, $uid) {
try {
$sql = "SELECT auth_token, auth_token_secret FROM tripit_tokens WHERE uid = ? AND active = 'Y'";
$sth = $dbh->prepare($sql);
$sth->execute([$uid]);
} catch (PDOException $e) {
die("Internal error.");
}
if ($sth->rowCount()) {
// User has a token.
$row = $sth->fetch();
return ["token" => $row["auth_token"], "secret" => $row["auth_token_secret"]];
} else {
return null;
}
}
/**
* Handle TripIt error responses in potentially a user-friendly way. Does nothing if it can't handle it.
*
* @param $response string Error response body from TripIt
*/
function handle_tripit_response($response) {
global $dbh;
if (strpos($response, "<detailed_error_code>106.1</detailed_error_code>") !== false) {
# 106.1 - Token invalid. Ask user to rendezvous again.
# This shouldn't happen, but let's be paranoid.
$uid = $_SESSION["uid"];
if (!$uid || empty($uid)) {
print _("Not logged in, aborting");
exit();
}
# Disable the old tokens.
$sth = $dbh->prepare("UPDATE tripit_tokens SET active='N' WHERE uid = ?");
$sth->execute([$uid]);
header("Location: /php/tripit_rendezvous.php");
exit();
}
}