1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base; |
3
|
|
|
|
4
|
|
|
use PSFS\base\config\Config; |
5
|
|
|
use PSFS\base\types\helpers\SecurityHelper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Service |
9
|
|
|
* @package PSFS\base |
10
|
|
|
*/ |
11
|
|
|
class Service extends Singleton |
12
|
|
|
{ |
13
|
|
|
public const CTYPE_JSON = 'application/json'; |
14
|
|
|
public const CTYPE_MULTIPART = 'multipart/form-data'; |
15
|
|
|
public const CTYPE_FORM = 'application/x-www-form-urlencoded'; |
16
|
|
|
public const CTYPE_PLAIN = 'text/plain'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var String Url de destino de la llamada |
20
|
|
|
*/ |
21
|
|
|
private $url; |
22
|
|
|
/** |
23
|
|
|
* @var array Parámetros de la llamada |
24
|
|
|
*/ |
25
|
|
|
private $params; |
26
|
|
|
/** |
27
|
|
|
* @var array Opciones llamada |
28
|
|
|
*/ |
29
|
|
|
private $options; |
30
|
|
|
/** |
31
|
|
|
* @var array Cabeceras de la llamada |
32
|
|
|
*/ |
33
|
|
|
private $headers; |
34
|
|
|
/** |
35
|
|
|
* @var string type |
36
|
|
|
*/ |
37
|
|
|
private $type; |
38
|
|
|
/** |
39
|
|
|
* @var resource $con |
40
|
|
|
*/ |
41
|
|
|
private $con; |
42
|
|
|
/** |
43
|
|
|
* @var string $result |
44
|
|
|
*/ |
45
|
|
|
private $result; |
46
|
|
|
/** |
47
|
|
|
* @var mixed |
48
|
|
|
*/ |
49
|
|
|
private $info = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @Injectable |
53
|
|
|
* @var \PSFS\base\Logger Log de las llamadas |
54
|
|
|
*/ |
55
|
|
|
protected $log; |
56
|
|
|
/** |
57
|
|
|
* @Injectable |
58
|
|
|
* @var \PSFS\base\Cache $cache |
59
|
|
|
*/ |
60
|
|
|
protected $cache; |
61
|
|
|
/** |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
protected $isJson = true; |
65
|
|
|
/** |
66
|
|
|
* @var bool |
67
|
|
|
*/ |
68
|
|
|
protected $isMultipart = false; |
69
|
|
|
|
70
|
1 |
|
private function closeConnection() { |
71
|
1 |
|
if(null !== $this->con) { |
72
|
|
|
curl_close($this->con); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
public function __destruct() |
77
|
|
|
{ |
78
|
|
|
$this->closeConnection(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return String |
83
|
|
|
*/ |
84
|
|
|
public function getUrl() |
85
|
|
|
{ |
86
|
|
|
return $this->url; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param String $url |
91
|
|
|
*/ |
92
|
|
|
public function setUrl($url) |
93
|
|
|
{ |
94
|
|
|
$this->url = $url; |
95
|
|
|
$this->initialize(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getResult() |
102
|
|
|
{ |
103
|
|
|
return $this->result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $result |
108
|
|
|
*/ |
109
|
|
|
public function setResult($result) |
110
|
|
|
{ |
111
|
|
|
$this->result = $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getParams() |
118
|
|
|
{ |
119
|
|
|
return $this->params; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Add request param |
124
|
|
|
* |
125
|
|
|
* @param $key |
126
|
|
|
* @param null $value |
127
|
|
|
* |
128
|
|
|
* @return \PSFS\base\Service |
129
|
|
|
*/ |
130
|
|
|
public function addParam($key, $value = NULL) |
131
|
|
|
{ |
132
|
|
|
$this->params[$key] = $value; |
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function getOptions() |
141
|
|
|
{ |
142
|
|
|
return $this->options; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Add request param |
147
|
|
|
* |
148
|
|
|
* @param $key |
149
|
|
|
* @param null $value |
150
|
|
|
* |
151
|
|
|
* @return \PSFS\base\Service |
152
|
|
|
*/ |
153
|
|
|
public function addOption($key, $value = NULL) |
154
|
|
|
{ |
155
|
|
|
$this->options[$key] = $value; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $params |
162
|
|
|
*/ |
163
|
|
|
public function setParams($params) |
164
|
|
|
{ |
165
|
|
|
$this->params = $params; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function getHeaders() |
172
|
|
|
{ |
173
|
|
|
return $this->headers; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param array $headers |
178
|
|
|
*/ |
179
|
|
|
public function setHeaders($headers) |
180
|
|
|
{ |
181
|
|
|
$this->headers = $headers; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param $header |
186
|
|
|
* @param null $content |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function addHeader($header, $content = NULL) |
191
|
|
|
{ |
192
|
|
|
$this->headers[$header] = $content; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function getType() |
201
|
|
|
{ |
202
|
|
|
return $this->type; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $type |
207
|
|
|
*/ |
208
|
|
|
public function setType($type) |
209
|
|
|
{ |
210
|
|
|
$this->type = $type; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @return Logger |
215
|
|
|
*/ |
216
|
|
|
public function getLog() |
217
|
|
|
{ |
218
|
|
|
return $this->log; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param Logger $log |
223
|
|
|
*/ |
224
|
1 |
|
public function setLog($log) |
225
|
|
|
{ |
226
|
1 |
|
$this->log = $log; |
227
|
1 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param bool $isJson |
231
|
|
|
*/ |
232
|
|
|
public function setIsJson($isJson = true) { |
233
|
|
|
$this->isJson = $isJson; |
234
|
|
|
if($isJson) { |
235
|
|
|
$this->setIsMultipart(false); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return bool |
241
|
|
|
*/ |
242
|
|
|
public function getIsJson() { |
243
|
|
|
return $this->isJson; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param bool $isMultipart |
248
|
|
|
*/ |
249
|
|
|
public function setIsMultipart($isMultipart = true) { |
250
|
|
|
$this->isMultipart = $isMultipart; |
251
|
|
|
if($isMultipart) { |
252
|
|
|
$this->setIsJson(false); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public function getIsMultipart() { |
260
|
|
|
return $this->isMultipart; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Método que limpia el contexto de la llamada |
265
|
|
|
*/ |
266
|
1 |
|
private function clearContext() |
267
|
|
|
{ |
268
|
1 |
|
$this->url = NULL; |
269
|
1 |
|
$this->params = array(); |
270
|
1 |
|
$this->headers = array(); |
271
|
1 |
|
Logger::log('Context service for ' . static::class . ' cleared!'); |
272
|
1 |
|
$this->closeConnection(); |
273
|
1 |
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* |
277
|
|
|
*/ |
278
|
1 |
|
public function init() |
279
|
|
|
{ |
280
|
1 |
|
parent::init(); |
281
|
1 |
|
$this->clearContext(); |
282
|
1 |
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Initialize CURL |
286
|
|
|
*/ |
287
|
|
|
private function initialize() |
288
|
|
|
{ |
289
|
|
|
$this->closeConnection(); |
290
|
|
|
$this->params = []; |
291
|
|
|
$this->con = curl_init($this->url); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Generate auth header |
296
|
|
|
* @param string $secret |
297
|
|
|
* @param string $module |
298
|
|
|
*/ |
299
|
|
|
protected function addRequestToken($secret, $module = 'PSFS') |
300
|
|
|
{ |
301
|
|
|
$this->addHeader('X-PSFS-SEC-TOKEN', SecurityHelper::generateToken($secret, $module)); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param $user |
306
|
|
|
* @param $pass |
307
|
|
|
*/ |
308
|
|
|
protected function addAuthHeader($user, $pass) { |
309
|
|
|
$this->addOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
310
|
|
|
$this->addOption(CURLOPT_USERPWD, "$user:$pass"); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
protected function applyOptions() { |
314
|
|
|
if(count($this->options)) { |
315
|
|
|
curl_setopt_array($this->con, $this->options); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
protected function applyHeaders() { |
320
|
|
|
$headers = []; |
321
|
|
|
foreach($this->headers as $key => $value) { |
322
|
|
|
$headers[] = $key . ': ' . $value; |
323
|
|
|
} |
324
|
|
|
if(count($headers)) { |
325
|
|
|
curl_setopt($this->con, CURLOPT_HTTPHEADER, $headers); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
protected function setDefaults() |
330
|
|
|
{ |
331
|
|
|
switch (strtoupper($this->type)) { |
332
|
|
|
case Request::VERB_GET: |
333
|
|
|
default: |
334
|
|
|
$this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_GET); |
335
|
|
|
if(!empty($this->params)) { |
336
|
|
|
$sep = !preg_match('/\?/', $this->getUrl()) ? '?' : ''; |
337
|
|
|
$this->url = $this->url . $sep . http_build_query($this->params); |
338
|
|
|
} |
339
|
|
|
break; |
340
|
|
|
case Request::VERB_POST: |
341
|
|
|
$this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_POST); |
342
|
|
|
if($this->getIsJson()) { |
343
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params)); |
344
|
|
|
} elseif($this->getIsMultipart()) { |
345
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, $this->params); |
346
|
|
|
} else { |
347
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params)); |
348
|
|
|
} |
349
|
|
|
break; |
350
|
|
|
case Request::VERB_DELETE: |
351
|
|
|
$this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_DELETE); |
352
|
|
|
break; |
353
|
|
|
case Request::VERB_PUT: |
354
|
|
|
$this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_PUT); |
355
|
|
|
|
356
|
|
|
if($this->getIsJson()) { |
357
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params)); |
358
|
|
|
} elseif($this->getIsMultipart()) { |
359
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, $this->params); |
360
|
|
|
} else { |
361
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params)); |
362
|
|
|
} |
363
|
|
|
break; |
364
|
|
|
case Request::VERB_PATCH: |
365
|
|
|
$this->addOption(CURLOPT_CUSTOMREQUEST, Request::VERB_PATCH); |
366
|
|
|
if($this->getIsJson()) { |
367
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, json_encode($this->params)); |
368
|
|
|
} elseif($this->getIsMultipart()) { |
369
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, $this->params); |
370
|
|
|
} else { |
371
|
|
|
$this->addOption(CURLOPT_POSTFIELDS, http_build_query($this->params)); |
372
|
|
|
} |
373
|
|
|
break; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
$this->addOption(CURLOPT_RETURNTRANSFER, true); |
377
|
|
|
$this->addOption(CURLOPT_FOLLOWLOCATION, true); |
378
|
|
|
$this->addOption(CURLOPT_SSL_VERIFYHOST, false); |
379
|
|
|
$this->addOption(CURLOPT_SSL_VERIFYPEER, false); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function callSrv() |
383
|
|
|
{ |
384
|
|
|
$this->setDefaults(); |
385
|
|
|
$this->applyOptions(); |
386
|
|
|
$this->applyHeaders(); |
387
|
|
|
if('debug' === Config::getParam('log.level')) { |
388
|
|
|
curl_setopt($this->con, CURLOPT_VERBOSE, true); |
389
|
|
|
$verbose = fopen('php://temp', 'w+'); |
390
|
|
|
curl_setopt($this->con, CURLOPT_STDERR, $verbose); |
391
|
|
|
} |
392
|
|
|
$result = curl_exec($this->con); |
393
|
|
|
$this->result = $this->isJson ? json_decode($result, true) : $result; |
394
|
|
|
if('debug' === Config::getParam('log.level')) { |
395
|
|
|
rewind($verbose); |
|
|
|
|
396
|
|
|
$verboseLog = stream_get_contents($verbose); |
397
|
|
|
Logger::log($verboseLog, LOG_DEBUG, [ |
398
|
|
|
'headers' => $this->getHeaders(), |
399
|
|
|
'options' => $this->getOptions(), |
400
|
|
|
'url' => $this->getUrl(), |
401
|
|
|
]); |
402
|
|
|
$this->info['verbose'] = $verboseLog; |
403
|
|
|
} |
404
|
|
|
Logger::log($this->url . ' response: ', LOG_DEBUG, is_array($this->result) ? $this->result : [$this->result]); |
405
|
|
|
$this->info = array_merge($this->info, curl_getinfo($this->con)); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* @return mixed |
410
|
|
|
*/ |
411
|
|
|
public function getCallInfo() { |
412
|
|
|
return $this->info; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: