forked from sublyro/optimizely-canvas-php-template
/
canvas.php
186 lines (132 loc) · 3.73 KB
/
canvas.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
<?php include 'optimizely.php';?>
<?php include 'canvas-log.php';?>
<?php
Class Canvas {
private $config_file = "canvas.config";
protected $app_id;
protected $app_version;
protected $app_name;
protected $private_key;
public $debug;
protected $optimizely;
protected $canvas_log;
private $token;
private $token_type;
private $project_id;
public function __construct() {
$this->readConfig();
$params = $_GET;
if (!array_key_exists('signed_request', $params)) {
$this->error("Error: Canvas signed_request parameter is missing");
exit();
}
$signed_request = $params['signed_request'];
if ($signed_request == null) {
$this->error("Error: Canvas signed_request parameter is missing");
exit();
}
$signed_request = explode(".", $signed_request);
if (count($signed_request) != 2) {
$this->error("Error: Invalid Canvas signed_request parameter");
exit();
}
// authorise the canvas app
if (!$this->authorize($signed_request)) {
$this->error("Error: The Canvas authentication failed. Please contact the App developer");
exit();
}
// initialise Optimizely
$this->optimizely = new Optimizely($this->token);
// make sure we set the token type to bearer
$this->optimizely->set_token_type($this->token_type);
}
private function authorize($signed_request) {
$hash = sha256Encode($signed_request[1], $this->private_key);
$hash = base64Encode($hash);
if ($hash != $signed_request[0]) {
return false;
}
$context = json_decode(base64Decode($signed_request[1]));
$context = $context->context;
$this->token = $context->client->access_token;
$this->token_type = $context->client->token_type;
$this->project_id = $context->environment->current_project;
return true;
}
private function readConfig() {
$CONFIG = parse_ini_file($this->config_file);
if (!array_key_exists('SHOW_ERROR', $CONFIG)) {
$this->debug = false;
$this->canvas_log = new Canvas_log(false);
} else {
$this->debug = $CONFIG['SHOW_ERROR'];
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$this->canvas_log = new Canvas_log(true);
}
if (!array_key_exists('APP_ID', $CONFIG)) {
$this->error("Error: Invalid Canvas config. APP_ID is missing");
exit();
}
$this->app_id = $CONFIG['APP_ID'];
if (!array_key_exists('APP_VERSION', $CONFIG)) {
$this->error("Error: Invalid Canvas config. APP_VERSION is missing");
exit();
}
$this->app_version = $CONFIG['APP_VERSION'];
if (!array_key_exists('APP_NAME', $CONFIG)) {
$this->error("Error: Invalid Canvas config. APP_NAME is missing");
exit();
}
$this->app_name = $CONFIG['APP_NAME'];
if (!array_key_exists('PRIVATE_KEY', $CONFIG)) {
$this->error("Error: Invalid Canvas config. PRIVATE_KEY is missing");
exit();
}
$this->private_key = $CONFIG['PRIVATE_KEY'];
}
public function error($msg) {
$this->canvas_log->error($msg);
}
public function debug($msg) {
$this->canvas_log->debug($msg);
}
public function info($msg) {
$this->canvas_log->info($msg);
}
public function get_project_id() {
return $this->project_id;
}
public function get_app_id() {
return $this->app_id;
}
public function get_app_name() {
return $this->app_name;
}
public function get_app_version() {
return $this->app_version;
}
public function get_private_key() {
return $this->private_key;
}
public function get_optimizely() {
return $this->optimizely;
}
}
$canvas = new Canvas();
//-----------------------------------
function sha256Encode($str, $key) {
return hash_hmac('sha256', $str, $key);
}
function base64Encode($str) {
return base64_encode($str);
}
function base64Decode($str) {
return base64_decode($str);
}
function debug($msg) {
echo "<pre>";
print_r($msg);
echo "</pre>";
}
?>