-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtility.php
More file actions
executable file
·557 lines (516 loc) · 19.5 KB
/
Utility.php
File metadata and controls
executable file
·557 lines (516 loc) · 19.5 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
<?php
/**
* Utility/Helper where all the helper and utility functions will be available.
*
* PHP version 5
*
* @category PHP
* @package Contentstack
* @author Uttam K Ukkoji <uttamukkoji@gmail.com>
* @author Rohit Mishra <rhtmishra4545@gmail.com>
* @copyright 2012-2021 Contentstack. All Rights Reserved
* @license https://github.com/contentstack/contentstack-php/blob/master/LICENSE.txt MIT Licence
* @link https://pear.php.net/package/contentstack
* */
namespace Contentstack\Support;
use Contentstack\Error\CSException;
use Contentstack\Stack\Result;
/**
* Utility/Helper where all the helper and utility functions will be available.
*
* @category PHP
* @package Contentstack
* @author Uttam K Ukkoji <uttamukkoji@gmail.com>
* @author Rohit Mishra <rhtmishra4545@gmail.com>
* @copyright 2012-2021 Contentstack. All Rights Reserved
* @license https://github.com/contentstack/contentstack-php/blob/master/LICENSE.txt MIT Licence
* @link https://pear.php.net/package/contentstack
* */
#[\AllowDynamicProperties]
class Utility
{
/**
* Validation for all the parameters required for the SDK
*
* @param string $type - type of the value to be validated
* @param object $input - value of the respective type
*
* @return array
* */
public static function validateInput($type = '', $input = array())
{
$msg = '';
try {
switch ($type) {
case 'stack' :
if ($input['region']) {
if (!(Utility::isKeySet($input, 'api_key')
&& Utility::isKeySet($input, 'access_token')
&& Utility::isKeySet($input, 'environment')
&& Utility::isKeySet($input, 'region'))
) {
$msg = 'Please provide valid api_key,
access_token,
environment and region';
}
break;
} else {
if (!(Utility::isKeySet($input, 'api_key')
&& Utility::isKeySet($input, 'access_token')
&& Utility::isKeySet($input, 'environment'))
) {
$msg = 'Please provide valid api_key,
access_token and environment';
}
break;
}
case 'port' :
if (Utility::isEmpty($input) || !is_numeric($input)) {
$msg = 'Please provide valid string for '.$type;
}
break;
case 'protocol' :
if (Utility::isEmpty($input)
|| !is_string($input)
|| !array_search($input, array('http', 'https'))
) {
$msg = 'Please provide valid string for '
.$type.' And it should be either http or https';
}
break;
case 'host' :
case 'access_token' :
case 'environment' :
case 'api_key' :
if (Utility::isEmpty($input) || !is_string($input)) {
$msg = 'Please provide valid string for '.$type;
}
break;
}
if (!Utility::isEmpty($msg)) {
throw new \Exception($msg);
}
return $input;
} catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
public static function isLivePreview($query) {
if ($query && isset($query->contentType)) {
return ($query->contentType->stack->live_preview['enable'] == true && array_key_exists('content_type_uid', $query->contentType->stack->live_preview) && strcmp($query->contentType->uid, $query->contentType->stack->live_preview['content_type_uid']) == 0);
}
return false;
}
/**
* Get the domain from the current object
*
* @param Stack $query - Instance of S.tack
*
* @return String
* */
public static function getDomain($query)
{
$stack = $query;
if ($query && isset($query->contentType)) {
$stack = $query->contentType->stack;
}
if ($query && isset($query->stack)) {
$stack = $query->stack;
}
if ($query && isset($query->assets)) {
$stack = $query->assets->stack;
}
$host = $stack->getHost();
if (Utility::isLivePreview($query)) {
$host = $stack->live_preview['host'];
}
return $stack->getProtocol()
.'://'.$host
.':'
.$stack->getPort().VERSION;
}
/**
* Contentstack URL method to create the url based on the request
*
* @param object $queryObject - Query Object
* @param string $type - type for url
*
* @return string
* */
public static function contentstackUrl($queryObject = '', $type = '')
{
$URL = '';
switch ($type) {
case 'set_environment':
if (!Utility::isLivePreview($queryObject)) {
$URL = Utility::getDomain($queryObject).ENVIRONMENTS.''
.$queryObject->contentType->stack->getEnvironment();
}
break;
case 'get_last_activites':
$URL = Utility::getDomain($queryObject).CONTENT_TYPES;
break;
case 'getcontentTypes':
$URL = Utility::getDomain($queryObject).CONTENT_TYPES;
break;
case 'asset':
$URL = Utility::getDomain($queryObject).ASSETS.$queryObject->assetUid;
break;
case 'assets':
$URL = Utility::getDomain($queryObject).ASSETS;
break;
case 'sync':
$URL = Utility::getDomain($queryObject).SYNC;
break;
default:
$URL = Utility::getDomain($queryObject).CONTENT_TYPES
.$queryObject->contentType->uid.ENTRIES;
if (isset($queryObject->entryUid)) {
$URL.=$queryObject->entryUid;
}
}
$queryParams = Utility::generateQueryParams($queryObject);
return $URL.'?'.$queryParams;
}
/**
* Header transformation as it required format
*
* @param array $query - input headers in key value pair
*
* @return array
* */
public static function headers($query = '')
{
$headers = array();
if ($query) {
if (isset($query->header)) {
$headers = $query->header;
} elseif ($query && isset($query->stack)) {
$headers = $query->stack->header;
} elseif ($query && isset($query->contentType)) {
$headers = $query->contentType->stack->header;
} else {
$headers = $query->assets->stack->header;
}
}
return $headers;
}
/**
* POST formatted query for the API server
*
* @param array $query - Query array
*
* @return json
* */
public static function generateQuery($query = array())
{
$result = array();
if (isset($query->contentType)) {
$query->_query['environment'] = $query->contentType
->stack->getEnvironment();
$subQuery = array();
if (count($query->subQuery) > 0) {
$subQuery['query'] = json_encode($query->subQuery);
}
$include_schema = array_search(
'include_schema',
array_keys($query->_query)
);
$include_content_type = array_search(
'include_content_type',
array_keys($query->_query)
);
if ($include_schema < $include_content_type) {
foreach ($query->_query as $key => $value) {
if ($key == 'include_schema') {
unset($query->_query['include_schema']);
$query->_query["include_schema"] = "true";
}
}
}
$result = array_merge($query->_query, $subQuery);
} elseif (isset($query->stack)) {
$query->_query['environment'] = $query->stack->getEnvironment();
$result = array_merge($result, $query->_query);
} elseif (isset($query->assets)) {
$query->_query['environment'] = $query->assets->stack->getEnvironment();
$subQuery = array();
if (count($query->subQuery) > 0) {
$subQuery['query'] = json_encode($query->subQuery);
}
$result = array_merge($query->_query, $subQuery);
} else {
$query->_query['environment'] = $query->getEnvironment();
$result = array_merge($result, $query->_query);
}
return $result;
}
/**
* Sending the GET requests with all the parameters in POST as well as GET
*
* @param array $query - Query array
*
* @return QueryParameters
* */
public static function generateQueryParams($query = array())
{
$result = Utility::generateQuery($query);
return http_build_query($result);
}
/**
* Wrap Result
*
* @param object $result - Response content
* @param object $queryObject - Query object
*
* @return ResultWrapped Object
* */
public static function wrapResult($result = '', $queryObject = '')
{
$result = $wrapper = json_decode($result, true);
if ($result && $queryObject && isset($queryObject->operation)) {
$flag = (isset($queryObject->json_translate)
&& $queryObject->json_translate);
switch ($queryObject->operation) {
case 'findOne':
if (Utility::isKeySet($result, 'entries')
&& count($result['entries']) > 0
) {
$wrapper = ($flag) ? $result['entries'][0] :
new Result(
$result['entries'][0]
);
} else {
$wrapper = json_encode(
array(
"error_code" => 119,
"error_message" => "The requested entry doesn't exists"
)
);
}
break;
case 'fetch':
if (Utility::isKeySet($result, 'entry')) {
$wrapper = (!$flag) ?
new Result(
$result['entry']
)
: $result['entry'];
} elseif (Utility::isKeySet($result, 'asset')) {
$wrapper = (!$flag) ?
new Result(
$result['asset']
)
: $result['asset'];
} elseif (Utility::isKeySet($result, 'schema')) {
array_push($wrapper, $result['schema']);
} elseif (Utility::isKeySet($result, 'content_type')) {
array_push($wrapper, $result['content_type']);
}
break;
case 'find':
$wrapper = array();
if (Utility::isKeySet($result, 'entries')) {
if (!is_numeric($result['entries'])) {
for ($i = 0,
$_i = count($result['entries']);
$i < $_i && !$flag; $i++) {
$result['entries'][$i] = new Result(
$result['entries'][$i]
);
}
}
array_push($wrapper, $result['entries']);
}
if (Utility::isKeySet($result, 'assets')) {
if (!is_numeric($result['assets'])) {
for ($i = 0,
$_i = count($result['assets']);
$i < $_i && !$flag; $i++
) {
$result['assets'][$i] = new Result(
$result['assets'][$i]
);
}
}
array_push($wrapper, $result['assets']);
}
if (Utility::isKeySet($result, 'schema')) {
array_push($wrapper, $result['schema']);
}
if (Utility::isKeySet($result, 'content_type')) {
array_push($wrapper, $result['content_type']);
}
if (Utility::isKeySet($result, 'count')) {
array_push($wrapper, $result['count']);
}
break;
}
}
return $wrapper;
}
/**
* Contentstack request to the API server based on the data
*
* @param object $queryObject - Query Object
* @param object $type - type of request
*
* @return Result
* */
public static function contentstackRequest($stack, $queryObject = '', $type = '', $count = 0)
{
$server_output = '';
STATIC $live_response_decode = '';
STATIC $entry_uid = '';
STATIC $content_type_uid = '';
$retryDelay = $stack->retryDelay;
$retryLimit = $stack->retryLimit;
$errorRetry = $stack->errorRetry;
if ($queryObject) {
if (Utility::isLivePreview($queryObject)) {
$queryObject->_query['live_preview'] = ($queryObject->contentType->stack->live_preview['live_preview'] ?? 'init');
}
$http = curl_init(Utility::contentstackUrl($queryObject, $type));
// setting the HTTP Headers
$Headers = Utility::headers($queryObject);
$request_headers = array();
$request_headers[] = 'x-user-agent: contentstack-php/2.3.1';
$request_headers[] = 'api_key: '.$Headers["api_key"];
if (Utility::isLivePreview($queryObject)) {
$request_headers[] = 'authorization: '.$queryObject->contentType->stack->live_preview['management_token'] ;
}else {
$request_headers[] = 'access_token: '.$Headers["access_token"];
}
if ($Headers["branch"] !== '' && $Headers["branch"] !== "undefined") {
$request_headers[] = 'branch: '.$Headers["branch"];
}
$proxy_details = $stack->proxy;
$timeout = $stack->timeout;
curl_setopt($http, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($http, CURLOPT_HEADER, false);
// setting the GET request
curl_setopt($http, CURLOPT_CUSTOMREQUEST, "GET");
// receive server response ...
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
// set the cURL time out
curl_setopt($http, CURLOPT_TIMEOUT_MS, $timeout);
if(array_key_exists("url",$proxy_details) && array_key_exists("port",$proxy_details)){
if($proxy_details['url'] != '' && $proxy_details['port'] != '') {
// Set the proxy IP
curl_setopt($http, CURLOPT_PROXY, $proxy_details['url']);
// Set the port
curl_setopt($http, CURLOPT_PROXYPORT, $proxy_details['port']);
if(array_key_exists("username",$proxy_details) && array_key_exists("password",$proxy_details)){
if($proxy_details['username'] != '' && $proxy_details['password'] != '') {
$proxyauth = $proxy_details['username'].":".$proxy_details['password'];
// Set the username and password
curl_setopt($http, CURLOPT_PROXYUSERPWD, $proxyauth);
}
}
}
}
$response = curl_exec($http);
// status code extraction
$httpcode = curl_getinfo($http, CURLINFO_HTTP_CODE);
// close the curl
curl_close($http);
if(in_array($httpcode,$errorRetry)){
if($count < $retryLimit){
$retryDelay = round($retryDelay/1000); //converting retry_delay from milliseconds into seconds
sleep($retryDelay); //sleep method requires time in seconds
$count += 1;
return Utility::contentstackRequest($stack, $queryObject, $type, $count);
}
} else {
if ($httpcode > 199 && $httpcode < 300) {
if (!Utility::isLivePreview($queryObject)) {
$result = json_decode($response, true);
Utility::to_render_content($result, $entry_uid, $live_response_decode);
$response = json_encode($result, true);
}
else
{
$entry_uid = $queryObject->entryUid;
$content_type_uid = $queryObject->contentType->uid;
$response_decode = json_decode($response, true);
if (Utility::isKeySet($response_decode, 'entry')) {
$live_response_decode = $response_decode['entry'];
}
else{
$live_response_decode = $response_decode;
}
}
// wrapper the server result
$response = Utility::wrapResult($response, $queryObject);
}
else{
throw new CSException($response, $httpcode);
}
}
}
return $response;
}
public static function to_render_content(&$resp, $entry_uid, $live_response_decode ){
if (is_array($resp)) {
if(array_key_exists('uid', $resp) && $resp['uid'] == $entry_uid){
$resp = $live_response_decode;
}else
{
foreach ($resp as $key => $value) {
Utility::to_render_content($resp[$key], $entry_uid, $live_response_decode);
}
}
}
}
/**
* Validate the key is set or not
*
* @param array $input - input
* @param string $key - key to check
*
* @return boolean
* */
public static function isKeySet($input = array(), $key = '')
{
return ($key && isset($input[$key])) ? true : false;
}
/**
* Validate the String
*
* @param object $input - object to check for empty
*
* @return boolean
* */
public static function isEmpty($input)
{
return (empty($input));
}
/**
* Get Last activities
*
* @param object $queryObject - query object
*
* @return object
* */
public static function getLastActivites($queryObject)
{
return request($queryObject, 'get_last_activites');
}
/**
* DEBUGGING MESSAGE
*
* @param object $input - object to debug
* @param boolean $exit - to exit on debug
*
* @return object
* */
public static function debug($input, $exit = false)
{
echo "<pre>";
print_r($input);
echo "</pre>";
if ($exit) {
exit();
}
}
}