Form.php
5 years ago
FormPart.php
5 years ago
Json.php
5 years ago
Multipart.php
5 years ago
Text.php
5 years ago
Multipart.php
135 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PayPalHttp\Serializer; |
| 4 | |
| 5 | use finfo; |
| 6 | use PayPalHttp\HttpRequest; |
| 7 | use PayPalHttp\Serializer; |
| 8 | use PayPalHttp\Encoder; |
| 9 | use PayPalHttp\Serializer\FormPart; |
| 10 | |
| 11 | /** |
| 12 | * Class Multipart |
| 13 | * @package PayPalHttp\Serializer |
| 14 | * |
| 15 | * Serializer for multipart. |
| 16 | */ |
| 17 | class Multipart implements Serializer |
| 18 | { |
| 19 | const LINEFEED = "\r\n"; |
| 20 | |
| 21 | public function contentType() |
| 22 | { |
| 23 | return "/^multipart\/.*$/"; |
| 24 | } |
| 25 | |
| 26 | public function encode(HttpRequest $request) |
| 27 | { |
| 28 | if (!is_array($request->body) || !$this->isAssociative($request->body)) |
| 29 | { |
| 30 | throw new \Exception("HttpRequest body must be an associative array when Content-Type is: " . $request->headers["content-type"]); |
| 31 | } |
| 32 | $boundary = "---------------------" . md5(mt_rand() . microtime()); |
| 33 | $contentTypeHeader = $request->headers["content-type"]; |
| 34 | $request->headers["content-type"] = "{$contentTypeHeader}; boundary={$boundary}"; |
| 35 | |
| 36 | $value_params = []; |
| 37 | $file_params = []; |
| 38 | |
| 39 | $disallow = ["\0", "\"", "\r", "\n"]; |
| 40 | |
| 41 | $body = []; |
| 42 | |
| 43 | foreach ($request->body as $k => $v) { |
| 44 | $k = str_replace($disallow, "_", $k); |
| 45 | if (is_resource($v)) { |
| 46 | $file_params[] = $this->prepareFilePart($k, $v, $boundary); |
| 47 | } else if ($v instanceof FormPart) { |
| 48 | $value_params[] = $this->prepareFormPart($k, $v, $boundary); |
| 49 | } else { |
| 50 | $value_params[] = $this->prepareFormField($k, $v, $boundary); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | $body = array_merge($value_params, $file_params); |
| 55 | |
| 56 | // add boundary for each parameters |
| 57 | array_walk($body, function (&$part) use ($boundary) { |
| 58 | $part = "--{$boundary}" . self::LINEFEED . "{$part}"; |
| 59 | }); |
| 60 | |
| 61 | // add final boundary |
| 62 | $body[] = "--{$boundary}--"; |
| 63 | $body[] = ""; |
| 64 | |
| 65 | return implode(self::LINEFEED, $body); |
| 66 | } |
| 67 | |
| 68 | public function decode($data) |
| 69 | { |
| 70 | throw new \Exception("Multipart does not support deserialization"); |
| 71 | } |
| 72 | |
| 73 | private function isAssociative(array $array) |
| 74 | { |
| 75 | return array_values($array) !== $array; |
| 76 | } |
| 77 | |
| 78 | private function prepareFormField($partName, $value, $boundary) |
| 79 | { |
| 80 | return implode(self::LINEFEED, [ |
| 81 | "Content-Disposition: form-data; name=\"{$partName}\"", |
| 82 | "", |
| 83 | filter_var($value), |
| 84 | ]); |
| 85 | } |
| 86 | |
| 87 | private function prepareFilePart($partName, $file, $boundary) |
| 88 | { |
| 89 | $fileInfo = new finfo(FILEINFO_MIME_TYPE); |
| 90 | $filePath = stream_get_meta_data($file)['uri']; |
| 91 | $data = file_get_contents($filePath); |
| 92 | $mimeType = $fileInfo->buffer($data); |
| 93 | |
| 94 | $splitFilePath = explode(DIRECTORY_SEPARATOR, $filePath); |
| 95 | $filePath = end($splitFilePath); |
| 96 | $disallow = ["\0", "\"", "\r", "\n"]; |
| 97 | $filePath = str_replace($disallow, "_", $filePath); |
| 98 | return implode(self::LINEFEED, [ |
| 99 | "Content-Disposition: form-data; name=\"{$partName}\"; filename=\"{$filePath}\"", |
| 100 | "Content-Type: {$mimeType}", |
| 101 | "", |
| 102 | $data, |
| 103 | ]); |
| 104 | } |
| 105 | |
| 106 | private function prepareFormPart($partName, $formPart, $boundary) |
| 107 | { |
| 108 | $contentDisposition = "Content-Disposition: form-data; name=\"{$partName}\""; |
| 109 | |
| 110 | $partHeaders = $formPart->getHeaders(); |
| 111 | $formattedheaders = array_change_key_case($partHeaders); |
| 112 | if (array_key_exists("content-type", $formattedheaders)) { |
| 113 | if ($formattedheaders["content-type"] === "application/json") { |
| 114 | $contentDisposition .= "; filename=\"{$partName}.json\""; |
| 115 | } |
| 116 | $tempRequest = new HttpRequest('/', 'POST'); |
| 117 | $tempRequest->headers = $formattedheaders; |
| 118 | $tempRequest->body = $formPart->getValue(); |
| 119 | $encoder = new Encoder(); |
| 120 | $partValue = $encoder->serializeRequest($tempRequest); |
| 121 | } else { |
| 122 | $partValue = $formPart->getValue(); |
| 123 | } |
| 124 | |
| 125 | $finalPartHeaders = []; |
| 126 | foreach ($partHeaders as $k => $v) { |
| 127 | $finalPartHeaders[] = "{$k}: {$v}"; |
| 128 | } |
| 129 | |
| 130 | $body = array_merge([$contentDisposition], $finalPartHeaders, [""], [$partValue]); |
| 131 | |
| 132 | return implode(self::LINEFEED, $body); |
| 133 | } |
| 134 | } |
| 135 |