Dragonqos /
php-jira-rest-client
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace JiraRestApi; |
||
| 4 | |||
| 5 | use GuzzleHttp\ClientInterface; |
||
| 6 | use GuzzleHttp\Exception\ConnectException; |
||
| 7 | use GuzzleHttp\Exception\RequestException; |
||
| 8 | use GuzzleHttp\RequestOptions; |
||
| 9 | use JiraRestApi\Interfaces\ConfigurationInterface; |
||
| 10 | use Psr\Http\Message\ResponseInterface; |
||
| 11 | use Psr\Log\LoggerInterface; |
||
| 12 | use Symfony\Component\HttpFoundation\Request; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Interact jira server with REST API. |
||
| 16 | */ |
||
| 17 | class JiraClient |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Json Mapper. |
||
| 21 | * |
||
| 22 | * @var \JsonMapper |
||
| 23 | */ |
||
| 24 | protected $json_mapper; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * JIRA REST API URI. |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $api_uri = '/rest/api/2'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Logger instance. |
||
| 35 | * |
||
| 36 | * @var \Psr\Log\LoggerInterface |
||
| 37 | */ |
||
| 38 | protected $log; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ClientInterface string |
||
| 42 | */ |
||
| 43 | protected $transport; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Jira Rest API Configuration. |
||
| 47 | * |
||
| 48 | * @var ConfigurationInterface |
||
| 49 | */ |
||
| 50 | protected $configuration; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * JiraClient constructor. |
||
| 54 | * |
||
| 55 | * @param ConfigurationInterface|null $configuration |
||
| 56 | * @param ClientInterface $transport |
||
| 57 | * @param LoggerInterface $log |
||
| 58 | */ |
||
| 59 | public function __construct(ConfigurationInterface $configuration = null, ClientInterface $transport, LoggerInterface $log) |
||
| 60 | { |
||
| 61 | $this->configuration = $configuration; |
||
| 62 | |||
| 63 | $this->json_mapper = new \JsonMapper(); |
||
| 64 | $this->json_mapper->bEnforceMapType = false; |
||
| 65 | $this->json_mapper->setLogger($log); |
||
| 66 | $this->json_mapper->undefinedPropertyHandler = function ($obj, $val) { |
||
| 67 | $this->log->debug('Handle undefined property', [$val, $obj]); |
||
| 68 | }; |
||
| 69 | |||
| 70 | $this->log = $log; |
||
| 71 | $this->transport = $transport; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Execute REST request. |
||
| 76 | * |
||
| 77 | * @param string $context RestAPI context (ex.:issue, search, etc..) |
||
| 78 | * @param null $post_data |
||
| 79 | * @param string $httpMethod |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | * |
||
| 83 | * @throws JiraException |
||
| 84 | */ |
||
| 85 | public function exec($context, $post_data = null, $httpMethod = Request::METHOD_GET) |
||
| 86 | { |
||
| 87 | $url = $this->createUrlByContext($context); |
||
| 88 | |||
| 89 | $options = [ |
||
| 90 | RequestOptions::HEADERS => [ |
||
| 91 | 'Accept' => '*/*', |
||
| 92 | 'Content-Type' => 'application/json', |
||
| 93 | 'charset' => 'UTF-8' |
||
| 94 | ] |
||
| 95 | ]; |
||
| 96 | |||
| 97 | if ($httpMethod == Request::METHOD_GET) { |
||
| 98 | $options[RequestOptions::QUERY] = $post_data; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (in_array($httpMethod, [Request::METHOD_POST, Request::METHOD_PUT, Request::METHOD_DELETE])) { |
||
| 102 | $options[RequestOptions::JSON] = $post_data; |
||
| 103 | } |
||
| 104 | |||
| 105 | try { |
||
| 106 | $this->log->info('JiraRestApi request: ', [$httpMethod, $url, $options]); |
||
| 107 | $response = $this->transport->request($httpMethod, $url, $options); |
||
| 108 | $this->log->info('JiraRestApi response: ', [$response->getHeaders(), (string)$response->getBody()]); |
||
| 109 | } catch (ConnectException $e) { |
||
| 110 | $this->log->critical('JiraRestApi connection exception: ', [$e->getMessage()]); |
||
| 111 | } catch (RequestException $e) { |
||
| 112 | $this->log->error('JiraRestApi response fail with code : ' . $e->getCode(), [ |
||
| 113 | $httpMethod, $url, $options, |
||
| 114 | (string)$e->getRequest()->getBody(), |
||
| 115 | $e->getRequest()->getHeaders(), |
||
| 116 | (string)$e->getResponse()->getBody() |
||
| 117 | ]);g |
||
| 118 | $response = $e->getResponse(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 119 | } |
||
| 120 | |||
| 121 | return isset($response) && $response instanceof ResponseInterface |
||
| 122 | ? $this->parseResponse($response) |
||
| 123 | : false; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * File upload. |
||
| 128 | * |
||
| 129 | * @param string $context url context |
||
| 130 | * @param array $filePathArray upload file path. |
||
| 131 | * |
||
| 132 | * @return array |
||
| 133 | * |
||
| 134 | * @throws JiraException |
||
| 135 | */ |
||
| 136 | public function upload($context, array $filePathArray) |
||
| 137 | { |
||
| 138 | $url = $this->createUrlByContext($context); |
||
| 139 | |||
| 140 | $options = [ |
||
| 141 | RequestOptions::HEADERS => [ |
||
| 142 | 'X-Atlassian-Token' => 'no-check' |
||
| 143 | ] |
||
| 144 | ]; |
||
| 145 | |||
| 146 | $promises = []; |
||
| 147 | |||
| 148 | if(!empty($filePathArray)) { |
||
| 149 | |||
| 150 | foreach ($filePathArray as $filename => $filePath) { |
||
| 151 | // load each files separately |
||
| 152 | if (file_exists($filePath) == false) { |
||
| 153 | // Ignore if file not found |
||
| 154 | $this->log->error('JiraRestApi: Unable to upload file "' . $filePath . '". File not Found'); |
||
| 155 | continue; |
||
| 156 | } |
||
| 157 | |||
| 158 | $ex = explode("/", $filePath); |
||
| 159 | $options[RequestOptions::MULTIPART] = [ |
||
| 160 | [ |
||
| 161 | 'name' => 'file', |
||
| 162 | 'contents' => fopen($filePath, 'r'), |
||
| 163 | 'filename' => is_numeric($filename) ? end($ex) : $filename |
||
| 164 | ] |
||
| 165 | ]; |
||
| 166 | |||
| 167 | $this->log->info('JiraRestApi requestAsync: ', [Request::METHOD_POST, $url, $options]); |
||
| 168 | $promises[] = $this->transport |
||
| 169 | ->requestAsync(Request::METHOD_POST, $url, $options) |
||
| 170 | ->then(function (ResponseInterface $response) { |
||
| 171 | $this->log->info('JiraRestApi responseAsync: ', [$response->getHeaders(), (string) $response->getBody()]); |
||
| 172 | return $response; |
||
| 173 | }, function (RequestException $e) { |
||
| 174 | if($e instanceof ConnectException) { |
||
| 175 | $this->log->critical('JiraRestApi connection exception: ', [$e->getMessage()]); |
||
| 176 | return false; |
||
| 177 | } else { |
||
| 178 | $this->log->error('JiraRestApi responseAsync fail with code : ' . $e->getCode(), [(string) $e->getRequest()->getBody(), $e->getRequest()->getHeaders(), (string) $e->getResponse()->getBody()]); |
||
| 179 | return $e->getResponse(); |
||
| 180 | } |
||
| 181 | }); |
||
| 182 | } |
||
| 183 | |||
| 184 | $responses = \GuzzleHttp\Promise\settle($promises)->wait(); |
||
| 185 | |||
| 186 | $result = []; |
||
| 187 | foreach ($responses as $response) { |
||
| 188 | if (isset($response['value']) && $response['value'] instanceof ResponseInterface) { |
||
| 189 | $result[] = $this->parseResponse($response['value']); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | return $result; |
||
| 194 | } |
||
| 195 | |||
| 196 | return false; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Access to JiraResources using JiraCredentials |
||
| 201 | * @param $fromUrl |
||
| 202 | * @param $toResource |
||
| 203 | * |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | public function download($fromUrl, $toResource = null) |
||
| 207 | { |
||
| 208 | $options = is_null($toResource) |
||
| 209 | ? [RequestOptions::STREAM => true] |
||
| 210 | : [RequestOptions::SINK => $toResource]; |
||
| 211 | |||
| 212 | try { |
||
| 213 | $this->log->info('JiraRestApi request: ', ['GET', $fromUrl, $options]); |
||
| 214 | $response = $this->transport->get($fromUrl, $options); |
||
| 215 | $this->log->info('JiraRestApi response: ', [$response->getHeaders()]); |
||
| 216 | } catch (ConnectException $e) { |
||
| 217 | $this->log->critical('JiraRestApi connection exception: ', [$e->getMessage()]); |
||
| 218 | } catch (RequestException $e) { |
||
| 219 | $this->log->error('JiraRestApi response fail with code : ' . $e->getCode(), [(string) $e->getRequest()->getBody(), $e->getRequest()->getHeaders()]); |
||
| 220 | $response = $e->getResponse(); |
||
| 221 | } |
||
| 222 | |||
| 223 | return isset($response) && $response instanceof ResponseInterface |
||
| 224 | ? $response |
||
| 225 | : false; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param $array |
||
| 230 | * @param callable|null $callback |
||
| 231 | * |
||
| 232 | * @return mixed |
||
| 233 | */ |
||
| 234 | protected function filterNullVariable($array, callable $callback = null) |
||
| 235 | { |
||
| 236 | $array = json_decode(json_encode($array), true); // toArray |
||
| 237 | |||
| 238 | $array = is_callable($callback) ? array_filter($array, $callback) : array_filter((array)$array); |
||
| 239 | foreach ($array as &$value) { |
||
| 240 | if (is_array($value)) { |
||
| 241 | $value = call_user_func([$this, 'filterNullVariable'], $value, $callback); |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | return $array; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param $rawResponse |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | */ |
||
| 253 | public function parseResponse(ResponseInterface $rawResponse) |
||
| 254 | { |
||
| 255 | return (new JiraClientResponse($rawResponse, $this->log))->parse(); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $result |
||
| 260 | * @param array $responseCodes |
||
| 261 | * @param \Closure $callback |
||
| 262 | * |
||
| 263 | * @return mixed |
||
| 264 | */ |
||
| 265 | protected function extractErrors($result, array $responseCodes = [200], \Closure $callback) |
||
| 266 | { |
||
| 267 | if ($result instanceof JiraClientResponse && |
||
| 268 | !$result->hasErrors() && |
||
| 269 | in_array($result->getCode(), $responseCodes) |
||
| 270 | ) { |
||
| 271 | return $callback(); |
||
| 272 | } |
||
| 273 | |||
| 274 | if ($result && !in_array($result->getCode(), $responseCodes)) { |
||
| 275 | $result->setError('Unexpected response code, expected "' . implode(', ', $responseCodes) . '", ' . $result->getCode() . ' given'); |
||
| 276 | } |
||
| 277 | |||
| 278 | return $result; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get URL by context. |
||
| 283 | * |
||
| 284 | * @param string $context |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | protected function createUrlByContext($context) |
||
| 289 | { |
||
| 290 | return $this->api_uri . '/' . preg_replace('/\//', '', $context, 1); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Jira Rest API Configuration. |
||
| 295 | * |
||
| 296 | * @return ConfigurationInterface |
||
| 297 | */ |
||
| 298 | public function getConfiguration() |
||
| 299 | { |
||
| 300 | return $this->configuration; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return \JsonMapper |
||
| 305 | */ |
||
| 306 | public function getJsonMapper() |
||
| 307 | { |
||
| 308 | return $this->json_mapper; |
||
| 309 | } |
||
| 310 | } |
||
| 311 |