-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpanda.php
179 lines (146 loc) · 5.63 KB
/
panda.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
<?php
class Panda {
public function __construct($args) {
$known_options = array(
'cloud_id' => null,
'access_key' => null,
'secret_key' => null,
'api_host' => 'api.pandastream.com', // Use api-eu.pandastream.com if your account is in the EU
'api_port' => 443,
);
foreach ($known_options as $option => $default) {
$this->$option = isset($args[$option]) ? $args[$option] : $default;
}
$this->api_version = 2;
}
//
// REST client
//
public function get($request_path, $params = array()) {
return $this->http_request('GET', $request_path, $params);
}
public function post($request_path, $params = array()) {
return $this->http_request('POST', $request_path, null, $params);
}
public function put($request_path, $params = array()) {
return $this->http_request('PUT', $request_path, null, $params);
}
public function delete($request_path, $params = array()) {
return $this->http_request('DELETE', $request_path, $params);
}
public function api_protocol() {
if ($this->api_port == 443) {
return 'https';
}else{
return 'http';
}
}
public function api_url() {
return $this->api_protocol() . '://' . $this->api_host_and_port() . $this->api_base_path();
}
public function api_host_and_port() {
$ret = $this->api_host;
if ($this->api_port != 80) {
$ret .= ":{$this->api_port}";
}
return $ret;
}
public function api_base_path() {
return "/v{$this->api_version}";
}
private function http_request($verb, $path, $query = null, $data = null) {
$verb = strtoupper($verb);
$path = self::canonical_path($path);
$suffix = '';
$signed_data = null;
if ($verb == 'POST' || $verb == 'PUT') {
$signed_data = $this->signed_params($verb, $path, $data);
if(isset($data["file"])) {
$signed_data["file"] = "@". $data["file"];
}
}
else {
$signed_query_string = $this->signed_query($verb, $path, $query);
$suffix = '?' . $signed_query_string;
}
$url = $this->api_url() . $path . $suffix;
$curl = curl_init($url);
if ($signed_data) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $signed_data);
}
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $verb);
curl_setopt($curl, CURLOPT_PORT, $this->api_port);
if (defined('CURLOPT_PROTOCOLS')) {
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS|CURLPROTO_HTTP);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLOPT_VERBOSE, 1);
$response = curl_exec($curl);
$error_code = curl_errno($curl);
$error_message = curl_error($curl);
curl_close($curl);
if ( $error_code != CURLE_OK ) {
return json_encode(array(
'error' => 'CURL_' . $error_code,
'message' => $error_message,
));
}
return $response;
}
//
// Authentication
//
public function signed_query($verb, $request_path, $params = array(), $timestamp = null) {
return self::array2query($this->signed_params($verb, $request_path, $params, $timestamp));
}
public function signed_params($verb, $request_path, $params = array(), $timestamp = null) {
date_default_timezone_set('UTC');
$auth_params = $params;
unset($auth_params["file"]);
$auth_params['cloud_id'] = $this->cloud_id;
$auth_params['access_key'] = $this->access_key;
$auth_params['timestamp'] = $timestamp ? $timestamp : date('c');
$auth_params['signature'] = $this->generate_signature($verb, $request_path, $auth_params);
return $auth_params;
}
public static function signature_generator($verb, $request_path, $host, $secret_key, $params = array()) {
$string_to_sign = self::string_to_sign($verb, $request_path, $host, $params);
$context = hash_init('sha256', HASH_HMAC, $secret_key);
hash_update($context, $string_to_sign);
return base64_encode(hash_final($context, true));
}
public function generate_signature($verb, $request_path, $params = array()) {
return self::signature_generator($verb, $request_path, $this->api_host, $this->secret_key, $params);
}
public static function string_to_sign($verb, $request_path, $host, $params = array()) {
$request_path = self::canonical_path($request_path);
$query_string = self::canonical_querystring($params);
$_verb = strtoupper($verb);
$_host = strtolower($host);
$string_to_sign = "$_verb\n$_host\n$request_path\n$query_string";
return $string_to_sign;
}
//
// Misc
//
private static function canonical_path($path) {
return '/' . trim($path, " \t\n\r\0\x0B/");
}
private static function canonical_querystring($params = array()) {
ksort($params, SORT_STRING);
return self::array2query($params);
}
private static function urlencode($str) {
$ret = urlencode($str);
$ret = str_replace("%7E", "~", $ret);
$ret = str_replace("+", "%20", $ret);
return $ret;
}
private static function array2query($array) {
$pairs = array();
foreach ($array as $key => $value) {
$pairs[] = self::urlencode($key) . '=' . self::urlencode($value);
}
return join('&', $pairs);
}
}