-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.php
127 lines (99 loc) · 3.35 KB
/
config.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
<?php
include("globals.php");
session_start();
date_default_timezone_set("UTC");
$accessToken = '';
$refreshToken = '';
$accessTokenKey = isset($GLOBALS['ACCESS_TOKEN_KEY']) ? $GLOBALS['ACCESS_TOKEN_KEY'] : "access_token";
$refreshTokenKey = isset($GLOBALS['REFRESH_TOKEN_KEY']) ? $GLOBALS['REFRESH_TOKEN_KEY'] : "refresh_token";
$loginPath = isset($GLOBALS['LOGIN_PATH']) ? $GLOBALS['LOGIN_PATH'] : "/";
if(isset($_SESSION[$refreshTokenKey])){
$accessToken = $_SESSION[$accessTokenKey];
$refreshToken = $_SESSION[$refreshTokenKey];
} else {
if(!isset($GLOBALS['SKIP_AUTH'])){
header('Location: ' . $GLOBALS['DOMAIN'] . $loginPath);
}
}
$cronofy = new Cronofy(array(
"client_id" => $GLOBALS['CRONOFY_CLIENT_ID'],
"client_secret" => $GLOBALS['CRONOFY_CLIENT_SECRET'],
"access_token" => $accessToken,
"refresh_token" => $refreshToken
));
if (isset($GLOBALS['LOCAL_CRONOFY_URLS'])){
$cronofy->api_root_url = 'http://local.cronofy.com';
$cronofy->app_root_url = 'http://local.cronofy.com';
$cronofy->host_domain = "local.cronofy.com";
}
function CronofyRequest($call){
$result = array(
"data" => null,
"error" => null
);
try{
$result["data"] = $call();
} catch(CronofyException $ex){
if($ex->getCode() == 401){
return RefreshToken($call);
}
DebugLog("CronofyException: message=`" . $ex->getMessage() . "` error_details=`" . print_r($ex->error_details(), true) . "`");
$result["error"] = $ex;
}
return $result;
}
function RefreshToken($call){
try {
$GLOBALS['cronofy']->refresh_token();
$result = array(
"data" => null,
"error" => null
);
DebugLog("Cronofy access token has been refreshed");
$_SESSION[$GLOBALS['accessTokenKey']] = $GLOBALS['cronofy']->access_token;
$_SESSION[$GLOBALS['refreshTokenKey']] = $GLOBALS['cronofy']->refresh_token;
}
catch(CronofyException $ex){
DebugLog("Cronofy access has been revoked");
unset($_SESSION[$GLOBALS['accessTokenKey']]);
unset($_SESSION[$GLOBALS['refreshTokenKey']]);
header('Location: ' . $GLOBALS['DOMAIN'] . $GLOBALS['loginPath']);
die;
}
try{
$result["data"] = $call();
} catch(CronofyException $ex){
DebugLog("CronofyException: message=`" . $e->getMessage() . "` error_details=`" . print_r($e->error_details(), true) . "`");
$result["error"] = $ex;
}
return $result;
}
function ServerErrorBlockFromResult($result){
if(!$result || !$result["error"]){
return;
}
$errorCode = $result["error"]->getCode();
$errorStatus = $result["error"]->getMessage();
$serverError = $result["error"]->error_details();
return ServerErrorBlock($errorCode, $errorStatus, $serverError);
}
function ServerErrorBlockFromGet(){
if(!isset($_GET['errorCode'])){
return;
}
$errorCode = $_GET['errorCode'];
$errorStatus = $_GET['errorStatus'];
$serverError = $_GET['serverError'];
return ServerErrorBlock($errorCode, $errorStatus, $serverError);
}
function ServerErrorBlock($errorCode, $errorStatus, $serverError){
return "
<div id='error_explanation' class='alert alert-danger'>
<h4>$errorCode - $errorStatus</h4>
<pre>" . print_r($serverError, true) ."</pre>
</div>
";
}
function ErrorToQueryStringParams($error){
return 'errorCode=' . $error->getCode() . '&errorStatus=' . urlencode($error->getMessage()) . '&serverError=' . urlencode(print_r($error->error_details(), true));
}