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