-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathMultipartParser.php
More file actions
203 lines (177 loc) · 4.66 KB
/
MultipartParser.php
File metadata and controls
203 lines (177 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace React\Http;
/**
* Parse a multipart body
*
* Original source is from https://gist.github.com/jas-/5c3fdc26fedd11cb9fb5
*
* @author jason.gerfen@gmail.com
* @author stephane.goetz@onigoetz.ch
* @license http://www.gnu.org/licenses/gpl.html GPL License 3
*/
class MultipartParser
{
/**
* @var string
*/
protected $input;
/**
* @var string
*/
protected $boundary;
/**
* Contains the resolved posts
*
* @var array
*/
protected $post = [];
/**
* Contains the resolved files
*
* @var array
*/
protected $files = [];
/**
* @param $input
* @param $boundary
*/
public function __construct($input, $boundary)
{
$this->input = $input;
$this->boundary = $boundary;
}
/**
* @return array
*/
public function getPost()
{
return $this->post;
}
/**
* @return array
*/
public function getFiles()
{
return $this->files;
}
/**
* Do the actual parsing
*/
public function parse()
{
$blocks = $this->split($this->boundary);
foreach ($blocks as $value) {
if (empty($value)) {
continue;
}
$this->parseBlock($value);
}
}
/**
* @param $boundary string
* @returns Array
*/
protected function split($boundary)
{
$boundary = preg_quote($boundary);
$result = preg_split("/\\-+$boundary/", $this->input);
array_pop($result);
return $result;
}
/**
* Decide if we handle a file, post value or octet stream
*
* @param $string string
* @returns void
*/
protected function parseBlock($string)
{
if (strpos($string, 'filename') !== false) {
$this->file($string);
return;
}
// This may never be called, if an octet stream
// has a filename it is catched by the previous
// condition already.
if (strpos($string, 'application/octet-stream') !== false) {
$this->octetStream($string);
return;
}
$this->post($string);
}
/**
* Parse a raw octet stream
*
* @param $string
* @return array
*/
protected function octetStream($string)
{
preg_match('/name=\"([^\"]*)\".*stream[\n|\r]+([^\n\r].*)?$/s', $string, $match);
$this->addResolved('post', $match[1], $match[2]);
}
/**
* Parse a file
*
* @param $string
* @return array
*/
protected function file($string)
{
preg_match('/name=\"([^\"]*)\"; filename=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $string, $match);
preg_match('/Content-Type: (.*)?/', $match[3], $mime);
$content = preg_replace('/Content-Type: (.*)[^\n\r]/', '', $match[3]);
$content = ltrim($content, "\r\n");
// Put content in a stream
$stream = fopen('php://memory', 'r+');
if ($content !== '') {
fwrite($stream, $content);
fseek($stream, 0);
}
$data = [
'name' => $match[2],
'type' => trim($mime[1]),
'stream' => $stream, // Instead of writing to a file, we write to a stream.
'error' => UPLOAD_ERR_OK,
'size' => function_exists('mb_strlen')? mb_strlen($content, '8bit') : strlen($content),
];
//TODO :: have an option to write to files to emulate the same functionality as a real php server
//$path = tempnam(sys_get_temp_dir(), "php");
//$err = file_put_contents($path, $content);
//$data['tmp_name'] = $path;
//$data['error'] = ($err === false) ? UPLOAD_ERR_NO_FILE : UPLOAD_ERR_OK;
$this->addResolved('files', $match[1], $data);
}
/**
* Parse POST values
*
* @param $string
* @return array
*/
protected function post($string)
{
preg_match('/name=\"([^\"]*)\"[\n|\r]+([^\n\r].*)?\r$/s', $string, $match);
$this->addResolved('post', $match[1], $match[2]);
}
/**
* Put the file or post where it belongs,
* The key names can be simple, or containing []
* it can also be a named key
*
* @param $type
* @param $key
* @param $content
*/
protected function addResolved($type, $key, $content)
{
if (preg_match('/^(.*)\[(.*)\]$/i', $key, $tmp)) {
if (!empty($tmp[2])) {
$this->{$type}[$tmp[1]][$tmp[2]] = $content;
} else {
$this->{$type}[$tmp[1]][] = $content;
}
} else {
$this->{$type}[$key] = $content;
}
}
}