1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JiraRestApi; |
4
|
|
|
|
5
|
|
|
use JiraRestApi\Configuration\ConfigurationInterface; |
6
|
|
|
use JiraRestApi\Configuration\DotEnvConfiguration; |
7
|
|
|
use Monolog\Handler\StreamHandler; |
8
|
|
|
use Monolog\Logger as Logger; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Interact jira server with REST API. |
13
|
|
|
*/ |
14
|
|
|
class JiraClient |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Json Mapper. |
18
|
|
|
* |
19
|
|
|
* @var \JsonMapper |
20
|
|
|
*/ |
21
|
|
|
protected $json_mapper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* HTTP response code. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $http_response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* JIRA REST API URI. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $api_uri = '/rest/api/2'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* CURL instance. |
39
|
|
|
* |
40
|
|
|
* @var resource |
41
|
|
|
*/ |
42
|
|
|
protected $curl; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Monolog instance. |
46
|
|
|
* |
47
|
|
|
* @var \Monolog\Logger |
48
|
|
|
*/ |
49
|
|
|
protected $log; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Jira Rest API Configuration. |
53
|
|
|
* |
54
|
|
|
* @var ConfigurationInterface |
55
|
|
|
*/ |
56
|
|
|
protected $configuration; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Constructor. |
60
|
|
|
* |
61
|
|
|
* @param ConfigurationInterface $configuration |
62
|
|
|
* @param Logger $logger |
63
|
|
|
* @param string $path |
64
|
|
|
* |
65
|
|
|
* @throws JiraException |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
|
|
public function __construct(ConfigurationInterface $configuration = null, LoggerInterface $logger = null, $path = './') |
69
|
|
|
{ |
70
|
|
|
if ($configuration === null) { |
71
|
|
|
if (!file_exists($path.'.env')) { |
72
|
|
|
// If calling the getcwd() on laravel it will returning the 'public' directory. |
73
|
|
|
$path = '../'; |
74
|
|
|
} |
75
|
|
|
$this->configuration = new DotEnvConfiguration($path); |
76
|
|
|
} else { |
77
|
|
|
$this->configuration = $configuration; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->json_mapper = new \JsonMapper(); |
81
|
|
|
|
82
|
|
|
// Fix "\JiraRestApi\JsonMapperHelper::class" syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' |
83
|
|
|
$this->json_mapper->undefinedPropertyHandler = [new \JiraRestApi\JsonMapperHelper(), 'setUndefinedProperty']; |
84
|
|
|
|
85
|
|
|
// Properties that are annotated with `@var \DateTimeInterface` should result in \DateTime objects being created. |
86
|
|
|
$this->json_mapper->classMap['\\'.\DateTimeInterface::class] = \DateTime::class; |
87
|
|
|
|
88
|
|
|
// create logger |
89
|
|
|
if ($logger) { |
90
|
|
|
$this->log = $logger; |
|
|
|
|
91
|
|
|
} else { |
92
|
|
|
$this->log = new Logger('JiraClient'); |
93
|
|
|
$this->log->pushHandler(new StreamHandler( |
94
|
|
|
$this->configuration->getJiraLogFile(), |
95
|
|
|
$this->convertLogLevel($this->configuration->getJiraLogLevel()) |
96
|
|
|
)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->http_response = 200; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Convert log level. |
104
|
|
|
* |
105
|
|
|
* @param $log_level |
106
|
|
|
* |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
|
|
private function convertLogLevel($log_level) |
110
|
|
|
{ |
111
|
|
|
$log_level = strtoupper($log_level); |
112
|
|
|
|
113
|
|
|
switch ($log_level) { |
114
|
|
|
case 'EMERGENCY': |
115
|
|
|
return Logger::EMERGENCY; |
116
|
|
|
case 'ALERT': |
117
|
|
|
return Logger::ALERT; |
118
|
|
|
case 'CRITICAL': |
119
|
|
|
return Logger::CRITICAL; |
120
|
|
|
case 'ERROR': |
121
|
|
|
return Logger::ERROR; |
122
|
|
|
case 'WARNING': |
123
|
|
|
return Logger::WARNING; |
124
|
|
|
case 'NOTICE': |
125
|
|
|
return Logger::NOTICE; |
126
|
|
|
case 'DEBUG': |
127
|
|
|
return Logger::DEBUG; |
128
|
|
|
case 'INFO': |
129
|
|
|
return Logger::INFO; |
130
|
|
|
default: |
131
|
|
|
return Logger::WARNING; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Serilize only not null field. |
137
|
|
|
* |
138
|
|
|
* @param array $haystack |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function filterNullVariable($haystack) |
143
|
|
|
{ |
144
|
|
|
foreach ($haystack as $key => $value) { |
145
|
|
|
if (is_array($value)) { |
146
|
|
|
$haystack[$key] = $this->filterNullVariable($haystack[$key]); |
147
|
|
|
} elseif (is_object($value)) { |
148
|
|
|
$haystack[$key] = $this->filterNullVariable(get_class_vars(get_class($value))); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (is_null($haystack[$key]) || empty($haystack[$key])) { |
152
|
|
|
unset($haystack[$key]); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $haystack; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Execute REST request. |
161
|
|
|
* |
162
|
|
|
* @param string $context Rest API context (ex.:issue, search, etc..) |
163
|
|
|
* @param string $post_data |
164
|
|
|
* @param string $custom_request [PUT|DELETE] |
165
|
|
|
* @param string $cookieFile cookie file |
166
|
|
|
* |
167
|
|
|
* @throws JiraException |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function exec($context, $post_data = null, $custom_request = null, $cookieFile = null) |
172
|
|
|
{ |
173
|
|
|
$url = $this->createUrlByContext($context); |
174
|
|
|
|
175
|
|
|
if (is_string($post_data)) { |
176
|
|
|
$this->log->info("Curl $custom_request: $url JsonData=".$post_data); |
177
|
|
|
} elseif (is_array($post_data)) { |
|
|
|
|
178
|
|
|
$this->log->info("Curl $custom_request: $url JsonData=".json_encode($post_data, JSON_UNESCAPED_UNICODE)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$ch = curl_init(); |
182
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
183
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
184
|
|
|
|
185
|
|
|
// post_data |
186
|
|
|
if (!is_null($post_data)) { |
187
|
|
|
// PUT REQUEST |
188
|
|
|
if (!is_null($custom_request) && $custom_request == 'PUT') { |
189
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); |
190
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
191
|
|
|
} |
192
|
|
|
if (!is_null($custom_request) && $custom_request == 'DELETE') { |
193
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
194
|
|
|
} else { |
195
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
196
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
197
|
|
|
} |
198
|
|
|
} else { |
199
|
|
|
if (!is_null($custom_request) && $custom_request == 'DELETE') { |
200
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$this->authorization($ch, $cookieFile); |
205
|
|
|
|
206
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost()); |
207
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer()); |
208
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, $this->getConfiguration()->getCurlOptUserAgent()); |
209
|
|
|
|
210
|
|
|
// curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set |
211
|
|
|
if (!function_exists('ini_get') || !ini_get('open_basedir')) { |
212
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
curl_setopt($ch, CURLOPT_ENCODING, ''); |
216
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, |
217
|
|
|
['Accept: */*', 'Content-Type: application/json', 'X-Atlassian-Token: no-check']); |
218
|
|
|
|
219
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose()); |
220
|
|
|
|
221
|
|
|
// Add proxy settings to the curl. |
222
|
|
|
$this->proxyConfigCurlHandle($ch); |
223
|
|
|
|
224
|
|
|
$this->log->debug('Curl exec='.$url); |
225
|
|
|
$response = curl_exec($ch); |
226
|
|
|
|
227
|
|
|
// if request failed or have no result. |
228
|
|
|
if (!$response) { |
229
|
|
|
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
230
|
|
|
$body = curl_error($ch); |
231
|
|
|
curl_close($ch); |
232
|
|
|
|
233
|
|
|
/* |
234
|
|
|
* 201: The request has been fulfilled, resulting in the creation of a new resource. |
235
|
|
|
* 204: The server successfully processed the request, but is not returning any content. |
236
|
|
|
*/ |
237
|
|
|
if ($this->http_response === 204 || $this->http_response === 201 || $this->http_response === 200) { |
238
|
|
|
return true; |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// HostNotFound, No route to Host, etc Network error |
242
|
|
|
$msg = sprintf('CURL Error: http response=%d, %s', $this->http_response, $body); |
243
|
|
|
|
244
|
|
|
$this->log->error($msg); |
245
|
|
|
|
246
|
|
|
throw new JiraException($msg); |
247
|
|
|
} else { |
248
|
|
|
// if request was ok, parsing http response code. |
249
|
|
|
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
250
|
|
|
|
251
|
|
|
curl_close($ch); |
252
|
|
|
|
253
|
|
|
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION |
254
|
|
|
if ($this->http_response != 200 && $this->http_response != 201) { |
255
|
|
|
throw new JiraException('CURL HTTP Request Failed: Status Code : ' |
256
|
|
|
.$this->http_response.', URL:'.$url |
257
|
|
|
."\nError Message : ".$response, $this->http_response); |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return $response; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Create upload handle. |
266
|
|
|
* |
267
|
|
|
* @param string $url Request URL |
268
|
|
|
* @param string $upload_file Filename |
269
|
|
|
* |
270
|
|
|
* @return resource |
271
|
|
|
*/ |
272
|
|
|
private function createUploadHandle($url, $upload_file) |
273
|
|
|
{ |
274
|
|
|
$ch = curl_init(); |
275
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
276
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
277
|
|
|
|
278
|
|
|
// send file |
279
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
280
|
|
|
|
281
|
|
|
if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 5) { |
282
|
|
|
$attachments = realpath($upload_file); |
283
|
|
|
$filename = basename($upload_file); |
284
|
|
|
|
285
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, |
286
|
|
|
['file' => '@'.$attachments.';filename='.$filename]); |
287
|
|
|
|
288
|
|
|
$this->log->debug('using legacy file upload'); |
289
|
|
|
} else { |
290
|
|
|
// CURLFile require PHP > 5.5 |
291
|
|
|
$attachments = new \CURLFile(realpath($upload_file)); |
292
|
|
|
$attachments->setPostFilename(basename($upload_file)); |
293
|
|
|
|
294
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, |
295
|
|
|
['file' => $attachments]); |
296
|
|
|
|
297
|
|
|
$this->log->debug('using CURLFile='.var_export($attachments, true)); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
$this->authorization($ch); |
301
|
|
|
|
302
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost()); |
303
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer()); |
304
|
|
|
|
305
|
|
|
$this->proxyConfigCurlHandle($ch); |
306
|
|
|
|
307
|
|
|
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); cannot be activated when an open_basedir is set |
308
|
|
|
if (!function_exists('ini_get') || !ini_get('open_basedir')) { |
309
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
310
|
|
|
} |
311
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
312
|
|
|
'Accept: */*', |
313
|
|
|
'Content-Type: multipart/form-data', |
314
|
|
|
'X-Atlassian-Token: nocheck', |
315
|
|
|
]); |
316
|
|
|
|
317
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose()); |
318
|
|
|
|
319
|
|
|
$this->log->debug('Curl exec='.$url); |
320
|
|
|
|
321
|
|
|
return $ch; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* File upload. |
326
|
|
|
* |
327
|
|
|
* @param string $context url context |
328
|
|
|
* @param array $filePathArray upload file path. |
329
|
|
|
* |
330
|
|
|
* @throws JiraException |
331
|
|
|
* |
332
|
|
|
* @return array |
333
|
|
|
*/ |
334
|
|
|
public function upload($context, $filePathArray) |
335
|
|
|
{ |
336
|
|
|
$url = $this->createUrlByContext($context); |
337
|
|
|
|
338
|
|
|
// return value |
339
|
|
|
$result_code = 200; |
340
|
|
|
|
341
|
|
|
$chArr = []; |
342
|
|
|
$results = []; |
343
|
|
|
$mh = curl_multi_init(); |
344
|
|
|
|
345
|
|
|
for ($idx = 0; $idx < count($filePathArray); $idx++) { |
|
|
|
|
346
|
|
|
$file = $filePathArray[$idx]; |
347
|
|
|
if (file_exists($file) == false) { |
|
|
|
|
348
|
|
|
$body = "File $file not found"; |
349
|
|
|
$result_code = -1; |
350
|
|
|
$this->closeCURLHandle($chArr, $mh, $body, $result_code); |
351
|
|
|
|
352
|
|
|
return $results; |
353
|
|
|
} |
354
|
|
|
$chArr[$idx] = $this->createUploadHandle($url, $filePathArray[$idx]); |
355
|
|
|
|
356
|
|
|
curl_multi_add_handle($mh, $chArr[$idx]); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
$running = null; |
360
|
|
|
do { |
361
|
|
|
curl_multi_exec($mh, $running); |
362
|
|
|
} while ($running > 0); |
363
|
|
|
|
364
|
|
|
// Get content and remove handles. |
365
|
|
|
$body = ''; |
366
|
|
|
for ($idx = 0; $idx < count($chArr); $idx++) { |
|
|
|
|
367
|
|
|
$ch = $chArr[$idx]; |
368
|
|
|
|
369
|
|
|
$results[$idx] = curl_multi_getcontent($ch); |
370
|
|
|
|
371
|
|
|
// if request failed. |
372
|
|
|
if (!$results[$idx]) { |
373
|
|
|
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
374
|
|
|
$body = curl_error($ch); |
375
|
|
|
|
376
|
|
|
//The server successfully processed the request, but is not returning any content. |
377
|
|
|
if ($this->http_response == 204) { |
378
|
|
|
continue; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
// HostNotFound, No route to Host, etc Network error |
382
|
|
|
$result_code = -1; |
383
|
|
|
$body = 'CURL Error: = '.$body; |
384
|
|
|
$this->log->error($body); |
385
|
|
|
} else { |
386
|
|
|
// if request was ok, parsing http response code. |
387
|
|
|
$result_code = $this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
388
|
|
|
|
389
|
|
|
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION |
390
|
|
|
if ($this->http_response != 200 && $this->http_response != 201) { |
391
|
|
|
$body = 'CURL HTTP Request Failed: Status Code : ' |
392
|
|
|
.$this->http_response.', URL:'.$url; |
393
|
|
|
|
394
|
|
|
$this->log->error($body); |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
$this->closeCURLHandle($chArr, $mh, $body, $result_code); |
400
|
|
|
|
401
|
|
|
return $results; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @param array $chArr |
406
|
|
|
* @param $mh |
407
|
|
|
* @param $body |
408
|
|
|
* @param $result_code |
409
|
|
|
* |
410
|
|
|
* @throws \JiraRestApi\JiraException |
411
|
|
|
*/ |
412
|
|
|
protected function closeCURLHandle(array $chArr, $mh, $body, $result_code) |
413
|
|
|
{ |
414
|
|
|
foreach ($chArr as $ch) { |
415
|
|
|
$this->log->debug('CURL Close handle..'); |
416
|
|
|
curl_multi_remove_handle($mh, $ch); |
417
|
|
|
curl_close($ch); |
418
|
|
|
} |
419
|
|
|
$this->log->debug('CURL Multi Close handle..'); |
420
|
|
|
curl_multi_close($mh); |
421
|
|
|
if ($result_code != 200) { |
422
|
|
|
// @TODO $body might have not been defined |
423
|
|
|
throw new JiraException('CURL Error: = '.$body, $result_code); |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Get URL by context. |
429
|
|
|
* |
430
|
|
|
* @param string $context |
431
|
|
|
* |
432
|
|
|
* @return string |
433
|
|
|
*/ |
434
|
|
|
protected function createUrlByContext($context) |
435
|
|
|
{ |
436
|
|
|
$host = $this->getConfiguration()->getJiraHost(); |
437
|
|
|
|
438
|
|
|
return $host.$this->api_uri.'/'.preg_replace('/\//', '', $context, 1); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Add authorize to curl request. |
443
|
|
|
* |
444
|
|
|
* @param resource $ch |
445
|
|
|
*/ |
446
|
|
|
protected function authorization($ch, $cookieFile = null) |
447
|
|
|
{ |
448
|
|
|
// use cookie |
449
|
|
|
if ($this->getConfiguration()->isCookieAuthorizationEnabled()) { |
450
|
|
|
if ($cookieFile === null) { |
451
|
|
|
$cookieFile = $this->getConfiguration()->getCookieFile(); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); |
455
|
|
|
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); |
456
|
|
|
|
457
|
|
|
$this->log->debug('Using cookie..'); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
// if cookie file not exist, using id/pwd login |
461
|
|
|
if (!file_exists($cookieFile)) { |
462
|
|
|
$username = $this->getConfiguration()->getJiraUser(); |
463
|
|
|
$password = $this->getConfiguration()->getJiraPassword(); |
464
|
|
|
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); |
465
|
|
|
} |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* Jira Rest API Configuration. |
470
|
|
|
* |
471
|
|
|
* @return ConfigurationInterface |
472
|
|
|
*/ |
473
|
|
|
public function getConfiguration() |
474
|
|
|
{ |
475
|
|
|
return $this->configuration; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* Set a custom Jira API URI for the request. |
480
|
|
|
* |
481
|
|
|
* @param string $api_uri |
482
|
|
|
*/ |
483
|
|
|
public function setAPIUri($api_uri) |
484
|
|
|
{ |
485
|
|
|
$this->api_uri = $api_uri; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* convert to query array to http query parameter. |
490
|
|
|
* |
491
|
|
|
* @param $paramArray |
492
|
|
|
* |
493
|
|
|
* @return string |
494
|
|
|
*/ |
495
|
|
|
public function toHttpQueryParameter($paramArray) |
496
|
|
|
{ |
497
|
|
|
$queryParam = '?'; |
498
|
|
|
|
499
|
|
|
foreach ($paramArray as $key => $value) { |
500
|
|
|
$v = null; |
501
|
|
|
|
502
|
|
|
// some param field(Ex: expand) type is array. |
503
|
|
|
if (is_array($value)) { |
504
|
|
|
$v = implode(',', $value); |
505
|
|
|
} else { |
506
|
|
|
$v = $value; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
$queryParam .= $key.'='.$v.'&'; |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
return $queryParam; |
513
|
|
|
} |
514
|
|
|
|
515
|
|
|
/** |
516
|
|
|
* download and save into outDir. |
517
|
|
|
* |
518
|
|
|
* @param $url full url |
|
|
|
|
519
|
|
|
* @param $outDir save dir |
520
|
|
|
* @param $file save filename |
|
|
|
|
521
|
|
|
* @param $cookieFile cookie filename |
|
|
|
|
522
|
|
|
* |
523
|
|
|
* @throws JiraException |
524
|
|
|
* |
525
|
|
|
* @return bool|mixed |
526
|
|
|
*/ |
527
|
|
|
public function download($url, $outDir, $file, $cookieFile = null) |
528
|
|
|
{ |
529
|
|
|
$file = fopen($outDir.DIRECTORY_SEPARATOR.$file, 'w'); |
530
|
|
|
|
531
|
|
|
$ch = curl_init(); |
532
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
533
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
534
|
|
|
|
535
|
|
|
// output to file handle |
536
|
|
|
curl_setopt($ch, CURLOPT_FILE, $file); |
537
|
|
|
|
538
|
|
|
$this->authorization($ch, $cookieFile); |
539
|
|
|
|
540
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost()); |
541
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer()); |
542
|
|
|
$this->proxyConfigCurlHandle($ch); |
543
|
|
|
|
544
|
|
|
// curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set |
545
|
|
|
if (!function_exists('ini_get') || !ini_get('open_basedir')) { |
546
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, |
550
|
|
|
['Accept: */*', 'Content-Type: application/json', 'X-Atlassian-Token: no-check']); |
551
|
|
|
|
552
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose()); |
553
|
|
|
|
554
|
|
|
$this->log->debug('Curl exec='.$url); |
555
|
|
|
$response = curl_exec($ch); |
556
|
|
|
|
557
|
|
|
// if request failed. |
558
|
|
|
if (!$response) { |
559
|
|
|
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
560
|
|
|
$body = curl_error($ch); |
561
|
|
|
curl_close($ch); |
562
|
|
|
fclose($file); |
|
|
|
|
563
|
|
|
|
564
|
|
|
/* |
565
|
|
|
* 201: The request has been fulfilled, resulting in the creation of a new resource. |
566
|
|
|
* 204: The server successfully processed the request, but is not returning any content. |
567
|
|
|
*/ |
568
|
|
|
if ($this->http_response === 204 || $this->http_response === 201) { |
569
|
|
|
return true; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
// HostNotFound, No route to Host, etc Network error |
573
|
|
|
$msg = sprintf('CURL Error: http response=%d, %s', $this->http_response, $body); |
574
|
|
|
|
575
|
|
|
$this->log->error($msg); |
576
|
|
|
|
577
|
|
|
throw new JiraException($msg); |
578
|
|
|
} else { |
579
|
|
|
// if request was ok, parsing http response code. |
580
|
|
|
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
581
|
|
|
|
582
|
|
|
curl_close($ch); |
583
|
|
|
fclose($file); |
584
|
|
|
|
585
|
|
|
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION |
586
|
|
|
if ($this->http_response != 200 && $this->http_response != 201) { |
587
|
|
|
throw new JiraException('CURL HTTP Request Failed: Status Code : ' |
588
|
|
|
.$this->http_response.', URL:'.$url |
589
|
|
|
."\nError Message : ".$response, $this->http_response); |
590
|
|
|
} |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
return $response; |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
/** |
597
|
|
|
* setting cookie file path. |
598
|
|
|
* |
599
|
|
|
* @param $cookieFile |
600
|
|
|
* |
601
|
|
|
* @return $this |
602
|
|
|
*/ |
603
|
|
|
public function setCookieFile($cookieFile) |
604
|
|
|
{ |
605
|
|
|
$this->cookieFile = $cookieFile; |
|
|
|
|
606
|
|
|
|
607
|
|
|
return $this; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Config a curl handle with proxy configuration (if set) from ConfigurationInterface. |
612
|
|
|
* |
613
|
|
|
* @param $ch |
614
|
|
|
*/ |
615
|
|
|
private function proxyConfigCurlHandle($ch) |
616
|
|
|
{ |
617
|
|
|
// Add proxy settings to the curl. |
618
|
|
|
if ($this->getConfiguration()->getProxyServer()) { |
619
|
|
|
curl_setopt($ch, CURLOPT_PROXY, $this->getConfiguration()->getProxyServer()); |
620
|
|
|
curl_setopt($ch, CURLOPT_PROXYPORT, $this->getConfiguration()->getProxyPort()); |
621
|
|
|
|
622
|
|
|
$username = $this->getConfiguration()->getProxyUser(); |
623
|
|
|
$password = $this->getConfiguration()->getProxyPassword(); |
624
|
|
|
curl_setopt($ch, CURLOPT_PROXYUSERPWD, "$username:$password"); |
625
|
|
|
} |
626
|
|
|
} |
627
|
|
|
} |
628
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.