1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Goetas\MultipartUploadBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Riverline\MultiPartParser\Converters\HttpFoundation; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
10
|
|
|
|
11
|
|
|
class MultipartRequestListener |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private $injectFirstPart; |
17
|
|
|
|
18
|
13 |
|
public function __construct(bool $injectFirstPart = true) |
19
|
|
|
{ |
20
|
13 |
|
$this->injectFirstPart = $injectFirstPart; |
21
|
13 |
|
} |
22
|
|
|
|
23
|
13 |
|
public function onKernelRequest(GetResponseEvent $event) |
24
|
|
|
{ |
25
|
|
|
try { |
26
|
13 |
|
$this->processRequest($event->getRequest()); |
27
|
4 |
|
} catch (\LogicException $e) { |
28
|
4 |
|
$message = 'Bad Request'; |
29
|
|
|
|
30
|
4 |
|
if ($e->getMessage()) { |
31
|
4 |
|
$message .= ': ' . $e->getMessage(); |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
$response = new Response($message, 400); |
35
|
|
|
|
36
|
4 |
|
$event->setResponse($response); |
37
|
|
|
} |
38
|
13 |
|
} |
39
|
|
|
|
40
|
13 |
|
private function processRequest(Request $request) |
41
|
|
|
{ |
42
|
13 |
|
$contentType = $request->headers->get('Content-Type'); |
43
|
13 |
|
if (0 === strpos($contentType, 'multipart/')) { |
44
|
|
|
|
45
|
13 |
|
$streamedPart = HttpFoundation::convert($request); |
46
|
|
|
|
47
|
9 |
|
if ($this->injectFirstPart === true) { |
48
|
8 |
|
$request->headers->remove('Content-Type'); |
49
|
8 |
|
$request->headers->remove('Content-Length'); |
50
|
|
|
} |
51
|
9 |
|
$attachments = []; |
52
|
9 |
|
$relatedParts = $streamedPart->getParts(); |
53
|
|
|
|
54
|
9 |
|
foreach ($relatedParts as $k => $part) { |
55
|
|
|
|
56
|
9 |
|
if ($this->injectFirstPart === true && 0 === $k) { |
57
|
8 |
|
$request->headers->add($part->getHeaders()); |
58
|
8 |
|
if ('application/x-www-form-urlencoded' === $part->getHeader('Content-Type')) { |
59
|
|
|
$output = []; |
60
|
|
|
parse_str($part->getBody(), $output); |
61
|
|
|
$request->request->add($output); |
|
|
|
|
62
|
|
|
} else { |
63
|
8 |
|
$this->setRequestContent($request, $part->getBody()); |
64
|
|
|
} |
65
|
8 |
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
$contentDisposition = $part->getHeader('Content-Disposition'); |
69
|
|
|
|
70
|
5 |
|
if ($contentDisposition === null) { |
71
|
2 |
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
$fileName = $part->getFileName(); |
75
|
|
|
|
76
|
3 |
|
if ($fileName!== null) { |
77
|
2 |
|
$fp = tmpfile(); |
78
|
2 |
|
fwrite($fp, $part->getBody()); |
79
|
2 |
|
rewind($fp); |
80
|
|
|
|
81
|
2 |
|
$tmpPath = stream_get_meta_data($fp)['uri']; |
82
|
|
|
|
83
|
2 |
|
$ref = new \ReflectionClass('Symfony\Component\HttpFoundation\File\UploadedFile'); |
84
|
2 |
|
$params = $ref->getConstructor()->getParameters(); |
85
|
2 |
|
if ('error' === $params[3]->getName()) { // symfony 4 |
86
|
2 |
|
$file = new UploadedFile($tmpPath, urldecode($fileName), $part->getMimeType(), null, true); |
87
|
|
|
} else { // symfony < 4 |
88
|
|
|
$file = new UploadedFile($tmpPath, urldecode($fileName), $part->getMimeType(), filesize($tmpPath), null, false); |
89
|
|
|
} |
90
|
2 |
|
@$file->ref = $fp; |
|
|
|
|
91
|
2 |
|
$attachments[] = $file; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
if (($formName = $this->isDispositionFormData($contentDisposition)) !== null) { |
95
|
2 |
|
$formPath = $this->parseKey($formName); |
96
|
|
|
|
97
|
2 |
|
if ($fileName !== null) { |
98
|
1 |
|
$files = $request->files->all(); |
99
|
1 |
|
$files = $this->mergeFormArray($files, $formPath, $file); |
|
|
|
|
100
|
1 |
|
$request->files->replace($files); |
101
|
|
|
} else { |
102
|
1 |
|
$data = $request->request->all(); |
103
|
1 |
|
$data = $this->mergeFormArray($data, $formPath, $part->getBody()); |
104
|
3 |
|
$request->request->replace($data); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
9 |
|
$request->attributes->set('attachments', $attachments); |
110
|
9 |
|
$request->attributes->set('related-parts', $relatedParts); |
111
|
|
|
} |
112
|
9 |
|
} |
113
|
|
|
|
114
|
2 |
|
protected function mergeFormArray($array, $path, $data) |
115
|
|
|
{ |
116
|
2 |
|
if (count($path) > 0) { |
117
|
2 |
|
$key = array_shift($path); |
118
|
|
|
|
119
|
2 |
|
if (!is_array($array)) { |
120
|
|
|
$array = []; |
121
|
|
|
} |
122
|
|
|
|
123
|
2 |
|
if (!empty($key)) { |
124
|
2 |
|
$array[$key] = $this->mergeFormArray($array[$key] ?? [], $path, $data); |
125
|
|
|
} else { |
126
|
2 |
|
$array[] = $data; |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $array; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $data; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
private function parseKey($key) |
136
|
|
|
{ |
137
|
2 |
|
return array_map( |
138
|
|
|
function ($v) { |
139
|
2 |
|
return trim($v, ']'); |
140
|
2 |
|
}, |
141
|
2 |
|
explode('[', $key) |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
private function isDispositionFormData($value) |
146
|
|
|
{ |
147
|
3 |
|
if (preg_match('/(?:^|form-data;\s*)name="?([^";]+)("|;|$)/', $value, $matches)) { |
148
|
2 |
|
return trim($matches[1]); |
149
|
|
|
} |
150
|
1 |
|
return null; |
151
|
|
|
} |
152
|
|
|
|
153
|
8 |
|
private function setRequestContent(Request $request, $content) |
154
|
|
|
{ |
155
|
8 |
|
$p = new \ReflectionProperty(Request::class, 'content'); |
156
|
8 |
|
$p->setAccessible(true); |
157
|
8 |
|
$p->setValue($request, $content); |
158
|
8 |
|
} |
159
|
|
|
} |
160
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.