From 75160b12821f7f4299cce7f0b69c83c1502ae071 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Mon, 27 May 2024 13:08:29 +0200 Subject: 2024-02-19 upstream --- vendor/paypal/paypalhttp/lib/PayPalHttp/Curl.php | 114 ++--- .../paypal/paypalhttp/lib/PayPalHttp/Encoder.php | 229 +++++----- .../paypalhttp/lib/PayPalHttp/Environment.php | 36 +- .../paypalhttp/lib/PayPalHttp/HttpClient.php | 469 +++++++++++---------- .../paypalhttp/lib/PayPalHttp/HttpException.php | 51 ++- .../paypalhttp/lib/PayPalHttp/HttpRequest.php | 84 ++-- .../paypalhttp/lib/PayPalHttp/HttpResponse.php | 68 +-- .../paypalhttp/lib/PayPalHttp/IOException.php | 26 +- .../paypal/paypalhttp/lib/PayPalHttp/Injector.php | 38 +- .../paypalhttp/lib/PayPalHttp/Serializer.php | 58 +-- .../paypalhttp/lib/PayPalHttp/Serializer/Form.php | 92 ++-- .../lib/PayPalHttp/Serializer/FormPart.php | 50 +-- .../paypalhttp/lib/PayPalHttp/Serializer/Json.php | 76 ++-- .../lib/PayPalHttp/Serializer/Multipart.php | 268 ++++++------ .../paypalhttp/lib/PayPalHttp/Serializer/Text.php | 76 ++-- 15 files changed, 874 insertions(+), 861 deletions(-) (limited to 'vendor/paypal/paypalhttp/lib') diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Curl.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Curl.php index 08e6eaf..9d5ce8f 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/Curl.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Curl.php @@ -1,57 +1,57 @@ -curl = $curl; - } - - public function setOpt($option, $value) - { - curl_setopt($this->curl, $option, $value); - return $this; - } - - public function close() - { - curl_close($this->curl); - return $this; - } - - public function exec() - { - return curl_exec($this->curl); - } - - public function errNo() - { - return curl_errno($this->curl); - } - - public function getInfo($option) - { - return curl_getinfo($this->curl, $option); - } - - public function error() - { - return curl_error($this->curl); - } -} +curl = $curl; + } + + public function setOpt($option, $value) + { + curl_setopt($this->curl, $option, $value); + return $this; + } + + public function close() + { + curl_close($this->curl); + return $this; + } + + public function exec() + { + return curl_exec($this->curl); + } + + public function errNo() + { + return curl_errno($this->curl); + } + + public function getInfo($option) + { + return curl_getinfo($this->curl, $option); + } + + public function error() + { + return curl_error($this->curl); + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php index b7158a7..06ce88a 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Encoder.php @@ -1,114 +1,115 @@ -serializers[] = new Json(); - $this->serializers[] = new Text(); - $this->serializers[] = new Multipart(); - $this->serializers[] = new Form(); - } - - - - public function serializeRequest(HttpRequest $request) - { - if (!array_key_exists('content-type', $request->headers)) { - $message = "HttpRequest does not have Content-Type header set"; - echo $message; - throw new \Exception($message); - } - - $contentType = $request->headers['content-type']; - /** @var Serializer $serializer */ - $serializer = $this->serializer($contentType); - - if (is_null($serializer)) { - $message = sprintf("Unable to serialize request with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings())); - echo $message; - throw new \Exception($message); - } - - if (!(is_string($request->body) || is_array($request->body))) { - $message = "Body must be either string or array"; - echo $message; - throw new \Exception($message); - } - - $serialized = $serializer->encode($request); - - if (array_key_exists("content-encoding", $request->headers) && $request->headers["content-encoding"] === "gzip") { - $serialized = gzencode($serialized); - } - return $serialized; - } - - - public function deserializeResponse($responseBody, $headers) - { - - if (!array_key_exists('content-type', $headers)) { - $message = "HTTP response does not have Content-Type header set"; - echo $message; - throw new \Exception($message); - } - - $contentType = $headers['content-type']; - /** @var Serializer $serializer */ - $serializer = $this->serializer($contentType); - - if (is_null($serializer)) { - throw new \Exception(sprintf("Unable to deserialize response with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings()))); - } - - if (array_key_exists("content-encoding", $headers) && $headers["content-encoding"] === "gzip") { - $responseBody = gzdecode($responseBody); - } - - return $serializer->decode($responseBody); - } - - private function serializer($contentType) - { - /** @var Serializer $serializer */ - foreach ($this->serializers as $serializer) { - try { - if (preg_match($serializer->contentType(), $contentType) == 1) { - return $serializer; - } - } catch (\Exception $ex) { - $message = sprintf("Error while checking content type of %s: %s", get_class($serializer), $ex->getMessage()); - echo $message; - throw new \Exception($message, $ex->getCode(), $ex); - } - } - - return NULL; - } - - private function supportedEncodings() - { - $values = []; - /** @var Serializer $serializer */ - foreach ($this->serializers as $serializer) { - $values[] = $serializer->contentType(); - } - return $values; - } -} +serializers[] = new Json(); + $this->serializers[] = new Text(); + $this->serializers[] = new Multipart(); + $this->serializers[] = new Form(); + } + + + + public function serializeRequest(HttpRequest $request) + { + if (!array_key_exists('content-type', $request->headers)) { + $message = "HttpRequest does not have Content-Type header set"; + echo $message; + throw new \Exception($message); + } + + $contentType = $request->headers['content-type']; + /** @var Serializer $serializer */ + $serializer = $this->serializer($contentType); + + if (is_null($serializer)) { + $message = sprintf("Unable to serialize request with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings())); + echo $message; + throw new \Exception($message); + } + + if (!(is_string($request->body) || is_array($request->body))) { + $message = "Body must be either string or array"; + echo $message; + throw new \Exception($message); + } + + $serialized = $serializer->encode($request); + + if (array_key_exists("content-encoding", $request->headers) && $request->headers["content-encoding"] === "gzip") { + $serialized = gzencode($serialized); + } + return $serialized; + } + + + public function deserializeResponse($responseBody, $headers) + { + + if (!array_key_exists('content-type', $headers)) { + $message = "HTTP response does not have Content-Type header set"; + echo $message; + throw new \Exception($message); + } + + $contentType = $headers['content-type']; + $contentType = strtolower($contentType); + /** @var Serializer $serializer */ + $serializer = $this->serializer($contentType); + + if (is_null($serializer)) { + throw new \Exception(sprintf("Unable to deserialize response with Content-Type: %s. Supported encodings are: %s", $contentType, implode(", ", $this->supportedEncodings()))); + } + + if (array_key_exists("content-encoding", $headers) && $headers["content-encoding"] === "gzip") { + $responseBody = gzdecode($responseBody); + } + + return $serializer->decode($responseBody); + } + + private function serializer($contentType) + { + /** @var Serializer $serializer */ + foreach ($this->serializers as $serializer) { + try { + if (preg_match($serializer->contentType(), $contentType) == 1) { + return $serializer; + } + } catch (\Exception $ex) { + $message = sprintf("Error while checking content type of %s: %s", get_class($serializer), $ex->getMessage()); + echo $message; + throw new \Exception($message, $ex->getCode(), $ex); + } + } + + return NULL; + } + + private function supportedEncodings() + { + $values = []; + /** @var Serializer $serializer */ + foreach ($this->serializers as $serializer) { + $values[] = $serializer->contentType(); + } + return $values; + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Environment.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Environment.php index 4eb0ce0..0910a44 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/Environment.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Environment.php @@ -1,18 +1,18 @@ -environment = $environment; - $this->encoder = new Encoder(); - $this->curlCls = Curl::class; - } - - /** - * Injectors are blocks that can be used for executing arbitrary pre-flight logic, such as modifying a request or logging data. - * Executed in first-in first-out order. - * - * @param Injector $inj - */ - public function addInjector(Injector $inj) - { - $this->injectors[] = $inj; - } - - /** - * The method that takes an HTTP request, serializes the request, makes a call to given environment, and deserialize response - * - * @param $httpRequest HttpRequest - * @return HttpResponse - */ - public function execute(HttpRequest $httpRequest) - { - $requestCpy = clone $httpRequest; - $curl = new Curl(); - - foreach ($this->injectors as $inj) { - $inj->inject($requestCpy); - } - - $url = $this->environment->baseUrl() . $requestCpy->path; - $formattedHeaders = $this->prepareHeaders($requestCpy->headers); - if (!array_key_exists("user-agent", $formattedHeaders)) { - $requestCpy->headers["user-agent"] = $this->userAgent(); - } - - $body = ""; - if (!is_null($requestCpy->body)) { - $rawHeaders = $requestCpy->headers; - $requestCpy->headers = $formattedHeaders; - $body = $this->encoder->serializeRequest($requestCpy); - $requestCpy->headers = $this->mapHeaders($rawHeaders,$requestCpy->headers); - } - - $curl->setOpt(CURLOPT_URL, $url); - $curl->setOpt(CURLOPT_CUSTOMREQUEST, $requestCpy->verb); - $curl->setOpt(CURLOPT_HTTPHEADER, $this->serializeHeaders($requestCpy->headers)); - $curl->setOpt(CURLOPT_RETURNTRANSFER, 1); - $curl->setOpt(CURLOPT_HEADER, 0); - - if (!is_null($requestCpy->body)) { - $curl->setOpt(CURLOPT_POSTFIELDS, $body); - } - - if (strpos($this->environment->baseUrl(), "https://") === 0) { - $curl->setOpt(CURLOPT_SSL_VERIFYPEER, true); - $curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2); - } - - if ($caCertPath = $this->getCACertFilePath()) { - $curl->setOpt(CURLOPT_CAINFO, $caCertPath); - } - - $response = $this->parseResponse($curl); - $curl->close(); - - return $response; - } - - /** - * Returns an array representing headers with their keys - * to be lower case - * @param $headers - * @return array - */ - public function prepareHeaders($headers){ - return array_change_key_case($headers); - } - - /** - * Returns an array representing headers with their key in - * original cases and updated values - * @param $rawHeaders - * @param $formattedHeaders - * @return array - */ - public function mapHeaders($rawHeaders, $formattedHeaders){ - $rawHeadersKey = array_keys($rawHeaders); - foreach ($rawHeadersKey as $array_key) { - if(array_key_exists(strtolower($array_key), $formattedHeaders)){ - $rawHeaders[$array_key] = $formattedHeaders[strtolower($array_key)]; - } - } - return $rawHeaders; - } - - /** - * Returns default user-agent - * - * @return string - */ - public function userAgent() - { - return "PayPalHttp-PHP HTTP/1.1"; - } - - /** - * Return the filepath to your custom CA Cert if needed. - * @return string - */ - protected function getCACertFilePath() - { - return null; - } - - protected function setCurl(Curl $curl) - { - $this->curl = $curl; - } - - protected function setEncoder(Encoder $encoder) - { - $this->encoder = $encoder; - } - - private function serializeHeaders($headers) - { - $headerArray = []; - if ($headers) { - foreach ($headers as $key => $val) { - $headerArray[] = $key . ": " . $val; - } - } - - return $headerArray; - } - - private function parseResponse($curl) - { - $headers = []; - $curl->setOpt(CURLOPT_HEADERFUNCTION, - function($curl, $header) use (&$headers) - { - $len = strlen($header); - - $k = ""; - $v = ""; - - $this->deserializeHeader($header, $k, $v); - $headers[$k] = $v; - - return $len; - }); - - $responseData = $curl->exec(); - $statusCode = $curl->getInfo(CURLINFO_HTTP_CODE); - $errorCode = $curl->errNo(); - $error = $curl->error(); - - if ($errorCode > 0) { - throw new IOException($error, $errorCode); - } - - $body = $responseData; - - if ($statusCode >= 200 && $statusCode < 300) { - $responseBody = NULL; - - if (!empty($body)) { - $responseBody = $this->encoder->deserializeResponse($body, $this->prepareHeaders($headers)); - } - - return new HttpResponse( - $errorCode === 0 ? $statusCode : $errorCode, - $responseBody, - $headers - ); - } else { - throw new HttpException($body, $statusCode, $headers); - } - } - - private function deserializeHeader($header, &$key, &$value) - { - if (strlen($header) > 0) { - if (empty($header) || strpos($header, ':') === false) { - return NULL; - } - - list($k, $v) = explode(":", $header); - $key = trim($k); - $value = trim($v); - } - } -} +environment = $environment; + $this->encoder = new Encoder(); + $this->curlCls = Curl::class; + } + + /** + * Injectors are blocks that can be used for executing arbitrary pre-flight logic, such as modifying a request or logging data. + * Executed in first-in first-out order. + * + * @param Injector $inj + */ + public function addInjector(Injector $inj) + { + $this->injectors[] = $inj; + } + + /** + * The method that takes an HTTP request, serializes the request, makes a call to given environment, and deserialize response + * + * @param HttpRequest $httpRequest + * @return HttpResponse + * + * @throws HttpException + * @throws IOException + */ + public function execute(HttpRequest $httpRequest) + { + $requestCpy = clone $httpRequest; + $curl = new Curl(); + + foreach ($this->injectors as $inj) { + $inj->inject($requestCpy); + } + + $url = $this->environment->baseUrl() . $requestCpy->path; + $formattedHeaders = $this->prepareHeaders($requestCpy->headers); + if (!array_key_exists("user-agent", $formattedHeaders)) { + $requestCpy->headers["user-agent"] = $this->userAgent(); + } + + $body = ""; + if (!is_null($requestCpy->body)) { + $rawHeaders = $requestCpy->headers; + $requestCpy->headers = $formattedHeaders; + $body = $this->encoder->serializeRequest($requestCpy); + $requestCpy->headers = $this->mapHeaders($rawHeaders,$requestCpy->headers); + } + + $curl->setOpt(CURLOPT_URL, $url); + $curl->setOpt(CURLOPT_CUSTOMREQUEST, $requestCpy->verb); + $curl->setOpt(CURLOPT_HTTPHEADER, $this->serializeHeaders($requestCpy->headers)); + $curl->setOpt(CURLOPT_RETURNTRANSFER, 1); + $curl->setOpt(CURLOPT_HEADER, 0); + + if (!is_null($requestCpy->body)) { + $curl->setOpt(CURLOPT_POSTFIELDS, $body); + } + + if (strpos($this->environment->baseUrl(), "https://") === 0) { + $curl->setOpt(CURLOPT_SSL_VERIFYPEER, true); + $curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2); + } + + if ($caCertPath = $this->getCACertFilePath()) { + $curl->setOpt(CURLOPT_CAINFO, $caCertPath); + } + + $response = $this->parseResponse($curl); + $curl->close(); + + return $response; + } + + /** + * Returns an array representing headers with their keys + * to be lower case + * @param $headers + * @return array + */ + public function prepareHeaders($headers){ + $preparedHeaders = array_change_key_case($headers); + if (array_key_exists("content-type", $preparedHeaders)) { + $preparedHeaders["content-type"] = strtolower($preparedHeaders["content-type"]); + } + return $preparedHeaders; + } + + /** + * Returns an array representing headers with their key in + * original cases and updated values + * @param $rawHeaders + * @param $formattedHeaders + * @return array + */ + public function mapHeaders($rawHeaders, $formattedHeaders){ + $rawHeadersKey = array_keys($rawHeaders); + foreach ($rawHeadersKey as $array_key) { + if(array_key_exists(strtolower($array_key), $formattedHeaders)){ + $rawHeaders[$array_key] = $formattedHeaders[strtolower($array_key)]; + } + } + return $rawHeaders; + } + + /** + * Returns default user-agent + * + * @return string + */ + public function userAgent() + { + return "PayPalHttp-PHP HTTP/1.1"; + } + + /** + * Return the filepath to your custom CA Cert if needed. + * @return string + */ + protected function getCACertFilePath() + { + return null; + } + + protected function setCurl(Curl $curl) + { + $this->curl = $curl; + } + + protected function setEncoder(Encoder $encoder) + { + $this->encoder = $encoder; + } + + private function serializeHeaders($headers) + { + $headerArray = []; + if ($headers) { + foreach ($headers as $key => $val) { + $headerArray[] = $key . ": " . $val; + } + } + + return $headerArray; + } + + private function parseResponse($curl) + { + $headers = []; + $curl->setOpt(CURLOPT_HEADERFUNCTION, + function($curl, $header) use (&$headers) + { + $len = strlen($header); + + $k = ""; + $v = ""; + + $this->deserializeHeader($header, $k, $v); + $headers[$k] = $v; + + return $len; + }); + + $responseData = $curl->exec(); + $statusCode = $curl->getInfo(CURLINFO_HTTP_CODE); + $errorCode = $curl->errNo(); + $error = $curl->error(); + + if ($errorCode > 0) { + throw new IOException($error, $errorCode); + } + + $body = $responseData; + + if ($statusCode >= 200 && $statusCode < 300) { + $responseBody = NULL; + + if (!empty($body)) { + $responseBody = $this->encoder->deserializeResponse($body, $this->prepareHeaders($headers)); + } + + return new HttpResponse( + $errorCode === 0 ? $statusCode : $errorCode, + $responseBody, + $headers + ); + } else { + throw new HttpException($body, $statusCode, $headers); + } + } + + private function deserializeHeader($header, &$key, &$value) + { + if (strlen($header) > 0) { + if (empty($header) || strpos($header, ':') === false) { + return NULL; + } + + list($k, $v) = explode(":", $header); + $key = trim($k); + $value = trim($v); + } + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpException.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpException.php index 5f4d2db..3551786 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpException.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpException.php @@ -1,23 +1,28 @@ -statusCode = $statusCode; - $this->headers = $headers; - } -} +statusCode = $statusCode; + $this->headers = $headers; + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpRequest.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpRequest.php index 89b7480..656994a 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpRequest.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpRequest.php @@ -1,42 +1,42 @@ -path = $path; - $this->verb = $verb; - $this->body = NULL; - $this->headers = []; - } -} +path = $path; + $this->verb = $verb; + $this->body = NULL; + $this->headers = []; + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpResponse.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpResponse.php index ef8fb35..7d46dd2 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpResponse.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpResponse.php @@ -1,34 +1,34 @@ -statusCode = $statusCode; - $this->headers = $headers; - $this->result = $body; - } -} +statusCode = $statusCode; + $this->headers = $headers; + $this->result = $body; + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/IOException.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/IOException.php index 7bfc91c..401b438 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/IOException.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/IOException.php @@ -1,13 +1,13 @@ -body) || !$this->isAssociative($request->body)) - { - throw new \Exception("HttpRequest body must be an associative array when Content-Type is: " . $request->headers["Content-Type"]); - } - - return http_build_query($request->body); - } - - /** - * @param $body - * @return mixed - * @throws \Exception as multipart does not support deserialization. - */ - public function decode($body) - { - throw new \Exception("CurlSupported does not support deserialization"); - } - - private function isAssociative(array $array) - { - return array_values($array) !== $array; - } -} +body) || !$this->isAssociative($request->body)) + { + throw new \Exception("HttpRequest body must be an associative array when Content-Type is: " . $request->headers["Content-Type"]); + } + + return http_build_query($request->body); + } + + /** + * @param $body + * @return mixed + * @throws \Exception as multipart does not support deserialization. + */ + public function decode($body) + { + throw new \Exception("CurlSupported does not support deserialization"); + } + + private function isAssociative(array $array) + { + return array_values($array) !== $array; + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/FormPart.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/FormPart.php index 3779a9b..5c40026 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/FormPart.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/FormPart.php @@ -1,25 +1,25 @@ -value = $value; - $this->headers = array_merge([], $headers); - } - - public function getValue() - { - return $this->value; - } - - public function getHeaders() - { - return $this->headers; - } -} +value = $value; + $this->headers = array_merge([], $headers); + } + + public function getValue() + { + return $this->value; + } + + public function getHeaders() + { + return $this->headers; + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Json.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Json.php index 3f66314..a73897c 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Json.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Json.php @@ -1,38 +1,38 @@ -body; - if (is_string($body)) { - return $body; - } - if (is_array($body)) { - return json_encode($body); - } - throw new \Exception("Cannot serialize data. Unknown type"); - } - - public function decode($data) - { - return json_decode($data); - } -} +body; + if (is_string($body)) { + return $body; + } + if (is_array($body)) { + return json_encode($body); + } + throw new \Exception("Cannot serialize data. Unknown type"); + } + + public function decode($data) + { + return json_decode($data); + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Multipart.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Multipart.php index a420205..b9142de 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Multipart.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Multipart.php @@ -1,134 +1,134 @@ -body) || !$this->isAssociative($request->body)) - { - throw new \Exception("HttpRequest body must be an associative array when Content-Type is: " . $request->headers["content-type"]); - } - $boundary = "---------------------" . md5(mt_rand() . microtime()); - $contentTypeHeader = $request->headers["content-type"]; - $request->headers["content-type"] = "{$contentTypeHeader}; boundary={$boundary}"; - - $value_params = []; - $file_params = []; - - $disallow = ["\0", "\"", "\r", "\n"]; - - $body = []; - - foreach ($request->body as $k => $v) { - $k = str_replace($disallow, "_", $k); - if (is_resource($v)) { - $file_params[] = $this->prepareFilePart($k, $v, $boundary); - } else if ($v instanceof FormPart) { - $value_params[] = $this->prepareFormPart($k, $v, $boundary); - } else { - $value_params[] = $this->prepareFormField($k, $v, $boundary); - } - } - - $body = array_merge($value_params, $file_params); - - // add boundary for each parameters - array_walk($body, function (&$part) use ($boundary) { - $part = "--{$boundary}" . self::LINEFEED . "{$part}"; - }); - - // add final boundary - $body[] = "--{$boundary}--"; - $body[] = ""; - - return implode(self::LINEFEED, $body); - } - - public function decode($data) - { - throw new \Exception("Multipart does not support deserialization"); - } - - private function isAssociative(array $array) - { - return array_values($array) !== $array; - } - - private function prepareFormField($partName, $value, $boundary) - { - return implode(self::LINEFEED, [ - "Content-Disposition: form-data; name=\"{$partName}\"", - "", - filter_var($value), - ]); - } - - private function prepareFilePart($partName, $file, $boundary) - { - $fileInfo = new finfo(FILEINFO_MIME_TYPE); - $filePath = stream_get_meta_data($file)['uri']; - $data = file_get_contents($filePath); - $mimeType = $fileInfo->buffer($data); - - $splitFilePath = explode(DIRECTORY_SEPARATOR, $filePath); - $filePath = end($splitFilePath); - $disallow = ["\0", "\"", "\r", "\n"]; - $filePath = str_replace($disallow, "_", $filePath); - return implode(self::LINEFEED, [ - "Content-Disposition: form-data; name=\"{$partName}\"; filename=\"{$filePath}\"", - "Content-Type: {$mimeType}", - "", - $data, - ]); - } - - private function prepareFormPart($partName, $formPart, $boundary) - { - $contentDisposition = "Content-Disposition: form-data; name=\"{$partName}\""; - - $partHeaders = $formPart->getHeaders(); - $formattedheaders = array_change_key_case($partHeaders); - if (array_key_exists("content-type", $formattedheaders)) { - if ($formattedheaders["content-type"] === "application/json") { - $contentDisposition .= "; filename=\"{$partName}.json\""; - } - $tempRequest = new HttpRequest('/', 'POST'); - $tempRequest->headers = $formattedheaders; - $tempRequest->body = $formPart->getValue(); - $encoder = new Encoder(); - $partValue = $encoder->serializeRequest($tempRequest); - } else { - $partValue = $formPart->getValue(); - } - - $finalPartHeaders = []; - foreach ($partHeaders as $k => $v) { - $finalPartHeaders[] = "{$k}: {$v}"; - } - - $body = array_merge([$contentDisposition], $finalPartHeaders, [""], [$partValue]); - - return implode(self::LINEFEED, $body); - } -} +body) || !$this->isAssociative($request->body)) + { + throw new \Exception("HttpRequest body must be an associative array when Content-Type is: " . $request->headers["content-type"]); + } + $boundary = "---------------------" . md5(mt_rand() . microtime()); + $contentTypeHeader = $request->headers["content-type"]; + $request->headers["content-type"] = "{$contentTypeHeader}; boundary={$boundary}"; + + $value_params = []; + $file_params = []; + + $disallow = ["\0", "\"", "\r", "\n"]; + + $body = []; + + foreach ($request->body as $k => $v) { + $k = str_replace($disallow, "_", $k); + if (is_resource($v)) { + $file_params[] = $this->prepareFilePart($k, $v, $boundary); + } else if ($v instanceof FormPart) { + $value_params[] = $this->prepareFormPart($k, $v, $boundary); + } else { + $value_params[] = $this->prepareFormField($k, $v, $boundary); + } + } + + $body = array_merge($value_params, $file_params); + + // add boundary for each parameters + array_walk($body, function (&$part) use ($boundary) { + $part = "--{$boundary}" . self::LINEFEED . "{$part}"; + }); + + // add final boundary + $body[] = "--{$boundary}--"; + $body[] = ""; + + return implode(self::LINEFEED, $body); + } + + public function decode($data) + { + throw new \Exception("Multipart does not support deserialization"); + } + + private function isAssociative(array $array) + { + return array_values($array) !== $array; + } + + private function prepareFormField($partName, $value, $boundary) + { + return implode(self::LINEFEED, [ + "Content-Disposition: form-data; name=\"{$partName}\"", + "", + filter_var($value), + ]); + } + + private function prepareFilePart($partName, $file, $boundary) + { + $fileInfo = new finfo(FILEINFO_MIME_TYPE); + $filePath = stream_get_meta_data($file)['uri']; + $data = file_get_contents($filePath); + $mimeType = $fileInfo->buffer($data); + + $splitFilePath = explode(DIRECTORY_SEPARATOR, $filePath); + $filePath = end($splitFilePath); + $disallow = ["\0", "\"", "\r", "\n"]; + $filePath = str_replace($disallow, "_", $filePath); + return implode(self::LINEFEED, [ + "Content-Disposition: form-data; name=\"{$partName}\"; filename=\"{$filePath}\"", + "Content-Type: {$mimeType}", + "", + $data, + ]); + } + + private function prepareFormPart($partName, $formPart, $boundary) + { + $contentDisposition = "Content-Disposition: form-data; name=\"{$partName}\""; + + $partHeaders = $formPart->getHeaders(); + $formattedheaders = array_change_key_case($partHeaders); + if (array_key_exists("content-type", $formattedheaders)) { + if ($formattedheaders["content-type"] === "application/json") { + $contentDisposition .= "; filename=\"{$partName}.json\""; + } + $tempRequest = new HttpRequest('/', 'POST'); + $tempRequest->headers = $formattedheaders; + $tempRequest->body = $formPart->getValue(); + $encoder = new Encoder(); + $partValue = $encoder->serializeRequest($tempRequest); + } else { + $partValue = $formPart->getValue(); + } + + $finalPartHeaders = []; + foreach ($partHeaders as $k => $v) { + $finalPartHeaders[] = "{$k}: {$v}"; + } + + $body = array_merge([$contentDisposition], $finalPartHeaders, [""], [$partValue]); + + return implode(self::LINEFEED, $body); + } +} diff --git a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Text.php b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Text.php index e2ce0ca..d6fa3cd 100644 --- a/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Text.php +++ b/vendor/paypal/paypalhttp/lib/PayPalHttp/Serializer/Text.php @@ -1,38 +1,38 @@ -body; - if (is_string($body)) { - return $body; - } - if (is_array($body)) { - return json_encode($body); - } - return implode(" ", $body); - } - - public function decode($data) - { - return $data; - } -} +body; + if (is_string($body)) { + return $body; + } + if (is_array($body)) { + return json_encode($body); + } + return implode(" ", $body); + } + + public function decode($data) + { + return $data; + } +} -- cgit v1.2.3