-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencoding_com.php
More file actions
executable file
·145 lines (119 loc) · 4.49 KB
/
encoding_com.php
File metadata and controls
executable file
·145 lines (119 loc) · 4.49 KB
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
<?php
class Encoding_com {
public function __construct($args) {
$known_options = array(
'cloud_id' => null,
'access_key' => null,
'secret_key' => null,
'api_host' => 'api.encoding.com',
'api_port' => 80,
);
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_url() {
return 'http://' . $this->api_host_and_port();
}
public function api_host_and_port() {
return $this->api_host . "/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_query($verb, $path, $data);
}
else {
$signed_query_string = $this->signed_query($verb, $path, $query);
$suffix = '?' . $signed_query_string;
}
$url = $this->api_host_and_port() . $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);
curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($curl, CURLOPT_VERBOSE, 1);
$response = curl_exec($curl);
curl_close($curl);
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) {
$auth_params = $params;
$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, array_merge($params, $auth_params));
return $auth_params;
}
public static function signature_generator($verb, $request_path, $host, $secret_key, $params = array()) {
$request_path = self::canonical_path($request_path);
$query_string = self::canonical_querystring($params);
$_verb = strtoupper($verb);
$_host = strtolower($host);
$string_to_sign =<<<END
$_verb
$_host
$request_path
$query_string
END;
$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);
}
//
// 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);
}
}
?>