forked from jamesjonesmath/canvancement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
271 lines (252 loc) · 8.04 KB
/
api.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
<?php
class CanvasDataAPI {
private $_api_key = NULL;
private $_api_secret = NULL;
private $_api_host = 'portal.inshosteddata.com';
private $_account_id = 'self';
private $_return_json = false;
public function __construct($apikey = NULL, $apisecret = NULL, $accountid = NULL, $apihost = NULL) {
$opt_list = array (
'key' => '_api_key',
'secret' => '_api_secret',
'account' => '_account_id',
'host' => '_api_host'
);
if (empty( $apikey )) {
return;
}
if (is_array( $apikey )) {
$opts = $apikey;
$i = 0;
foreach ( $opt_list as $key => $field ) {
if (isset( $opts[$key] )) {
$this->{$field} = $opts[$key];
} elseif (isset( $opts[$i] )) {
$this->{$field} = $opts[$i];
}
$i ++;
}
} else {
$this->_api_key = $apikey;
$this->_api_secret = $apisecret;
$this->_account_id = empty( $accountid ) ? 'self' : $accountid;
$this->_api_host = empty( $apihost ) ? 'portal.inshosteddata.com' : $apihost;
}
}
protected function _getset($key = NULL, $opt = NULL) {
$value = NULL;
if (isset( $this->{$key} )) {
if (isset( $opt )) {
$this->{$key} = $opt;
} else {
$value = $this->{$key};
}
}
return $value;
}
protected function _check_init() {
$opt_list = array (
'key' => '_api_key',
'secret' => '_api_secret',
'account' => '_account_id',
'host' => '_api_host'
);
$valid = TRUE;
foreach ( $opt_list as $key => $field ) {
if (empty( $this->{$field} )) {
printf( "API not initialized. Missing %s\n", $key );
$valid = FALSE;
}
}
if (! $valid) {
throw new Exception( 'Must initialize settings before attempting to use them' );
}
return $valid;
}
protected function substitute($route = NULL, $vars = NULL) {
if (empty( $route )) {
return $route;
}
if (preg_match( '/^(GET|PUT|DELETE|POST|HEAD)\s+(.*)$/xms', $route, $route_matches )) {
$method = strtoupper( $route_matches[1] );
$path = $route_matches[2];
} else {
throw new Exception( 'Invalid route specified: ' . $route );
}
$src = explode( '/', $path );
$dst = array ();
foreach ( $src as $token ) {
if ($token == '(:accountId|self)') {
if (isset( $vars['accountId'] )) {
$token = $vars['accountId'];
} elseif (isset( $this->_account_id )) {
$token = $this->_account_id;
} else {
$token = 'self';
}
} elseif (preg_match( '/^[:](.*)$/xms', $token, $token_matches )) {
$varname = $token_matches[1];
if (isset( $vars[$varname] )) {
$token = $vars[$varname];
} else {
throw new Exception( 'Missing token: ' . $token );
}
}
$dst[] = $token;
}
$route = sprintf( '%s %s', $method, join( '/', $dst ) );
return $route;
}
protected function hmac_signature($timestamp = NULL, $url = NULL, $opts = array()) {
if (empty( $timestamp ) || empty( $url )) {
throw new Exception( 'Cannot compute signature with an empty message.' );
}
$u = parse_url( $url );
if ($u === FALSE) {
throw new Exception( 'Cannot parse URL: ' . $url );
}
$host = ! empty( $opts['host'] ) ? $opts['host'] : (! empty( $u['host'] ) ? $u['host'] : $this->_api_host);
$path = $u['path'];
$query = '';
if (! empty( $u['query'] )) {
$parms = explode( '&', $u['query'] );
ksort( $parms );
$query = join( '&', $parms );
}
$parts = array (
isset( $opts['method'] ) ? $opts['method'] : 'GET',
$host,
isset( $opts['contentType'] ) ? $opts['contentType'] : '',
isset( $opts['contentMD5'] ) ? $opts['contentMD5'] : '',
$path,
$query,
$timestamp,
$this->_api_secret
);
$message = join( "\n", $parts );
return base64_encode( hash_hmac( 'sha256', $message, $this->_api_secret, TRUE ) );
}
public function api_key($opt = NULL) {
return $this->_getset( '_api_key', $opt );
}
public function api_secret($opt = NULL) {
return $this->_getset( '_api_secret', $opt );
}
public function api_host($opt = NULL) {
return $this->_getset( '_api_host', $opt );
}
public function account($opt = NULL) {
return $this->_getset( '_account_id', $opt );
}
public function return_json($opt = NULL) {
return $this->_getset( '_return_json', $opt );
}
public function execute($config = array(), $opts = array()) {
$json = NULL;
try {
$this->_check_init();
if (! is_array( $config )) {
$config = array (
'route' => $config
);
}
$route = $this->substitute( $config['route'], $opts );
if (empty( $route )) {
throw new Exception( 'Malformed command, no route specified' );
}
$method = NULL;
$path = NULL;
if (preg_match( '/^(GET|PUT|DELETE|POST|HEAD)\s+(.*)$/xms', $route, $route_matches )) {
$method = strtoupper( $route_matches[1] );
$path = $route_matches[2];
} else {
throw new Exception( 'Invalid route' );
}
$url = 'https://' . $this->_api_host . $path;
$parms = array ();
$query = '';
if (isset( $config['allow'] )) {
foreach ( $config['allow'] as $key ) {
if (isset( $opts[$key] )) {
$parms[$key] = $opts[$key];
}
}
if (isset( $parms )) {
ksort( $parms, SORT_LOCALE_STRING );
if ($method == 'GET') {
$query = http_build_query( $parms );
if (! empty( $query )) {
$url .= '?' . $query;
}
}
}
}
$timestamp = gmdate( 'D, d M Y H:i:s T' );
$opts['method'] = $method;
$hmac = $this->hmac_signature( $timestamp, $url, $opts );
$http_headers = array (
'Authorization: HMACAuth ' . $this->_api_key . ':' . $hmac,
'Date: ' . $timestamp
);
$ch = curl_init( $url );
curl_setopt_array( $ch, array (
CURLOPT_HTTPHEADER => $http_headers,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_VERBOSE => FALSE,
CURLOPT_HEADER => TRUE
) );
$response = curl_exec( $ch );
if ($response !== FALSE) {
$header_size = curl_getinfo( $ch, CURLINFO_HEADER_SIZE );
$header = substr( $response, 0, $header_size );
$body = substr( $response, $header_size );
$json = $this->_return_json ? $body : json_decode( $body, TRUE );
}
curl_close( $ch );
} catch ( Exception $e ) {
printf( "%s\n", $e->getMessage() );
}
return $json;
}
public function get_dump($opts = array()) {
$config = array (
'route' => 'GET /api/account/(:accountId|self)/dump',
'allow' => array (
'after',
'limit'
)
);
return $this->execute( $config, $opts );
}
public function get_file_latest($opts = array()) {
return $this->execute( 'GET /api/account/(:accountId|self)/file/latest', $opts );
}
public function get_file_by_dump($opts = array()) {
return $this->execute( 'GET /api/account/(:accountId|self)/file/byDump/:dumpId', $opts );
}
public function get_file_by_table($opts = array()) {
$config = array (
'route' => 'GET /api/account/(:accountId|self)/file/byTable/:tableName',
'allow' => array (
'after',
'limit'
)
);
return $this->execute( $config, $opts );
}
public function get_file_sync($opts = array()) {
$config = array (
'route' => 'GET /api/account/(:accountId|self)/file/sync'
);
return $this->execute( $config, $opts );
}
public function get_schema($opts = array()) {
return $this->execute( 'GET /api/schema', $opts );
}
public function get_schema_latest($opts = array()) {
return $this->execute( 'GET /api/schema/latest', $opts );
}
public function get_schema_version($opts = array()) {
return $this->execute( 'GET /api/schema/:version', $opts );
}
}