forked from jpatokal/openflights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripit_common.php
76 lines (66 loc) · 2.03 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
75
76
<?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) {
$sth = null;
try {
$sql = "select auth_token, auth_token_secret from tripit_tokens where uid = ? and active = 'Y'";
$sth = $dbh->prepare($sql);
$sth->execute(array($uid));
} catch (PDOException $e) {
die("Internal error.");
}
if ($sth->rowCount()) {
// User has a token.
$row = $sth->fetch();
return array("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(strstr($response, "<detailed_error_code>106.1</detailed_error_code>")) {
# 106.1 - Token invalid. Ask user to rendezvous again.
# This shouldn't happen, but let's be paranoid.
$uid = $_SESSION["uid"];
if (!$uid or 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(array($uid));
header("Location: /php/tripit_rendezvous.php");
exit();
}
}
?>