-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
288 lines (251 loc) · 6.91 KB
/
lib.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
* Class for demonstrating WebMini Itemplatform.
* http://www.itemplatform.com/
* @class
*/
class Itemplatform {
const WEBMINI_API_SCHEMA = 'https://my.webmini.com/api/v1/';
const WEBMINI_WEBHOOK_HASH_ALGORITHM = 'sha1';
const API_ACCESS_METHOD__PERSONAL_TOKEN = 1;
const API_ACCESS_METHOD__OAUTH = 2;
const HTTP_METHOD__GET = 1;
const HTTP_METHOD__POST = 2;
const HTTP_METHOD__DELETE = 3;
const HTTP_METHOD__PUT = 4;
const HTTP_METHOD__PATCH = 5;
/**
* Itemplatform class contructor.
* @method
*/
function __construct(
$userAgent,
$accountId = false, // some API calls do not need an account ID
$accessMethod = self::API_ACCESS_METHOD__PERSONAL_TOKEN
) {
$this->userAgent = $userAgent;
$this->accountId = (int) $accountId;
$this->accessMethod = $accessMethod;
$this->debug = false;
}
/**
* Set access method.
* @method
*/
public function setAccessMethod(
$accessMethod = self::API_ACCESS_METHOD__PERSONAL_TOKEN
) {
$this->accessMethod = $accessMethod;
}
/**
* Turn debugging info on or off.
* If you want debugging data, set this to true and API calls will
* contain a debug value with complete HTTP call data.
* @method
*/
public function setDebug(
$debug = true
) {
$this->debug = $debug;
}
/**
* Set authentication data for Personal API Token access method.
* @method
*/
public function setPersonalApiToken(
$tokenUsername,
$tokenSecret
) {
$this->personalApiTokenUsername = $tokenUsername;
$this->personalApiTokenSecret = $tokenSecret;
}
/**
* Set authentication data for OAuth2 access method.
* @method
*/
public function setOAuth2AccessToken(
$accessToken
) {
$this->oAuth2AccessToken = $accessToken;
}
/**
* Perform HTTP call.
* @method
*/
private function httpCall(
$method,
$url,
$body = false,
$headers = array(),
$options = array()
) {
$curl = curl_init(
$url . (($method === self::HTTP_METHOD__GET && is_array($body) && count($body) > 0) ? '?' . http_build_query($body, null, '&') : '')
);
$headersFinal = array();
foreach ($headers as $headerName => $headerValue) {
$headersFinal[] = $headerName . ': ' . $headerValue;
}
$curlOptions = array();
switch ($method) {
case self::HTTP_METHOD__GET:
$curlOptions[CURLOPT_HTTPGET] = true;
break;
case self::HTTP_METHOD__POST:
$curlOptions[CURLOPT_POST] = true;
break;
case self::HTTP_METHOD__DELETE:
$curlOptions[CURLOPT_CUSTOMREQUEST] = 'DELETE';
break;
case self::HTTP_METHOD__PUT:
$curlOptions[CURLOPT_CUSTOMREQUEST] = 'PUT';
break;
case self::HTTP_METHOD__PATCH:
$curlOptions[CURLOPT_CUSTOMREQUEST] = 'PATCH';
break;
}
$curlOptions[CURLOPT_USERAGENT] = $this->userAgent;
if (
isset($options['username']) &&
isset($options['password'])
) {
$curlOptions[CURLOPT_USERPWD] = $options['username'] . ':' . $options['password'];
}
if (count($headersFinal) > 0) {
$curlOptions[CURLOPT_HTTPHEADER] = $headersFinal;
}
$curlOptions[CURLOPT_RETURNTRANSFER] = true;
if (
$body &&
$method !== self::HTTP_METHOD__GET
) {
//$curlOptions[CURLOPT_POSTFIELDS] = $body;
$curlOptions[CURLOPT_POSTFIELDS] = (is_array($body)) ? http_build_query($body, null, '&') : $body;
}
$curlOptions[CURLOPT_CONNECTTIMEOUT] = 20;
$curlOptions[CURLOPT_TIMEOUT] = 30;
$curlOptions[CURLOPT_SSL_VERIFYHOST] = 2;
$curlOptions[CURLOPT_SSL_VERIFYPEER] = true;
if ($this->debug) {
// Store debugging data
$debugFilePointer = fopen('php://temp', 'r+');
$curlOptions[CURLOPT_VERBOSE] = true;
$curlOptions[CURLOPT_STDERR] = $debugFilePointer;
}
curl_setopt_array($curl, $curlOptions);
$curlResult = curl_exec($curl);
$return = array(
'curlerror' => curl_error($curl),
'httpcode' => (int) curl_getinfo($curl, CURLINFO_HTTP_CODE),
'contenttype' => curl_getinfo($curl, CURLINFO_CONTENT_TYPE),
'result' => $curlResult
);
curl_close($curl);
if ($this->debug) {
rewind($debugFilePointer);
$return['debug'] = stream_get_contents($debugFilePointer);
fclose($debugFilePointer);
}
return $return;
}
/**
* HTML-encode string.
* @method
*/
public static function htmlEncode(
$string
) {
return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
}
/**
* Decode JSON.
* @method
*/
private static function jsonDecode(
$jsonString
) {
$jsonData = json_decode($jsonString, true);
return (json_last_error() === JSON_ERROR_NONE) ? $jsonData : null;
}
/**
* Call Itemplatform API either using a Personal API Token or OAuth2.
* @method
*/
private function apiCall($apiEndpoint, $httpMethod, $parameters = false) {
$headers = array();
$headers['Content-type'] = 'application/json';
$options = array();
switch ($this->accessMethod) {
case self::API_ACCESS_METHOD__PERSONAL_TOKEN:
$options['username'] = $this->personalApiTokenUsername;
$options['password'] = $this->personalApiTokenSecret;
break;
case self::API_ACCESS_METHOD__OAUTH:
$headers['Authorization'] = 'Bearer ' . $this->oAuth2AccessToken;
break;
}
$httpCallResult = $this->httpCall(
$httpMethod,
self::WEBMINI_API_SCHEMA . $apiEndpoint,
($parameters !== false) ? json_encode($parameters) : false,
$headers,
$options
);
return $httpCallResult;
}
/**
* Send items.
* @method
*/
public function apiSendItems($items) {
return $this->apiCall(
'itemplatform/' . $this->accountId . '/items/send',
self::HTTP_METHOD__POST,
$items
);
}
/**
* Validate WebMini webhook call.
* @method
*/
public static function webhookValidate($webhookSecret) {
// Set default values
$webhookValidationResult = array();
$webhookValidationResult['success'] = true;
$webhookValidationResult['httpcode'] = 200;
$webhookValidationResult['event'] = false;
$webhookValidationResult['payload'] = null;
// Parse request body
$webhookBody = file_get_contents('php://input');
// Check headers and validate signature
if (
!empty($_SERVER['HTTP_X_WEBMINI_EVENT']) &&
!empty($_SERVER['HTTP_X_WEBMINI_SIGNATURE']) &&
$_SERVER['HTTP_X_WEBMINI_SIGNATURE'] === self::WEBMINI_WEBHOOK_HASH_ALGORITHM . '=' . hash_hmac(
self::WEBMINI_WEBHOOK_HASH_ALGORITHM,
$webhookBody,
$webhookSecret
)
) {
$webhookValidationResult['event'] = $_SERVER['HTTP_X_WEBMINI_EVENT'];
} else {
// Webhook call has invalid headers / signature
$webhookValidationResult['success'] = false;
$webhookValidationResult['httpcode'] = 401; // Unauthorized
}
// Decode JSON
$webhookPayload = null;
if (mb_strlen($webhookBody) > 0) {
$webhookPayload = self::jsonDecode($webhookBody);
if (!is_null($webhookPayload)) {
$webhookValidationResult['payload'] = $webhookPayload;
} else {
// Payload JSON is invalid
$webhookValidationResult['success'] = false;
$webhookValidationResult['httpcode'] = 400; // Bad Request
}
}
// Return result
return $webhookValidationResult;
}
}