1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resta\Support; |
4
|
|
|
|
5
|
|
|
class Http |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @return void|mixed |
9
|
|
|
*/ |
10
|
|
|
public function getInputData() |
11
|
|
|
{ |
12
|
|
|
global /** @noinspection PhpUnusedLocalVariableInspection */ |
13
|
|
|
$_PUT; |
14
|
|
|
|
15
|
|
|
/* PUT data comes in on the stdin stream */ |
16
|
|
|
$putdata = fopen("php://input", "r"); |
17
|
|
|
|
18
|
|
|
$raw_data = ''; |
19
|
|
|
|
20
|
|
|
/* Read the data 1 KB at a time |
21
|
|
|
and write to the file */ |
22
|
|
|
while ($chunk = fread($putdata, 1024)) |
|
|
|
|
23
|
|
|
$raw_data .= $chunk; |
24
|
|
|
|
25
|
|
|
/* Close the streams */ |
26
|
|
|
fclose($putdata); |
|
|
|
|
27
|
|
|
|
28
|
|
|
// Fetch content and determine boundary |
29
|
|
|
$boundary = substr($raw_data, 0, strpos($raw_data, "\r\n")); |
30
|
|
|
|
31
|
|
|
if(empty($boundary)){ |
32
|
|
|
parse_str($raw_data,$data); |
33
|
|
|
$GLOBALS[ '_PUT' ] = $data; |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Fetch each part |
38
|
|
|
$parts = array_slice(explode($boundary, $raw_data), 1); |
39
|
|
|
$data = array(); |
40
|
|
|
|
41
|
|
|
foreach ($parts as $part) { |
42
|
|
|
// If this is the last part, break |
43
|
|
|
if ($part == "--\r\n") break; |
44
|
|
|
|
45
|
|
|
// Separate content from headers |
46
|
|
|
$part = ltrim($part, "\r\n"); |
47
|
|
|
list($raw_headers, $body) = explode("\r\n\r\n", $part, 2); |
48
|
|
|
|
49
|
|
|
// Parse the headers list |
50
|
|
|
$raw_headers = explode("\r\n", $raw_headers); |
51
|
|
|
$headers = array(); |
52
|
|
|
foreach ($raw_headers as $header) { |
53
|
|
|
list($name, $value) = explode(':', $header); |
54
|
|
|
$headers[strtolower($name)] = ltrim($value, ' '); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Parse the Content-Disposition to get the field name, etc. |
58
|
|
|
if (isset($headers['content-disposition'])) { |
59
|
|
|
$filename = null; |
60
|
|
|
$tmp_name = null; |
61
|
|
|
preg_match( |
62
|
|
|
'/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/', |
63
|
|
|
$headers['content-disposition'], |
64
|
|
|
$matches |
65
|
|
|
); |
66
|
|
|
/** @noinspection PhpUnusedLocalVariableInspection */ |
67
|
|
|
list(, $type, $name) = $matches; |
68
|
|
|
|
69
|
|
|
//Parse File |
70
|
|
|
if( isset($matches[4]) ) |
71
|
|
|
{ |
72
|
|
|
//if labeled the same as previous, skip |
73
|
|
|
if( isset( $_FILES[ $matches[ 2 ] ] ) ) |
74
|
|
|
{ |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
//get filename |
79
|
|
|
$filename = $matches[4]; |
80
|
|
|
|
81
|
|
|
//get tmp name |
82
|
|
|
$filename_parts = pathinfo( $filename ); |
83
|
|
|
$tmp_name = tempnam( ini_get('upload_tmp_dir'), $filename_parts['filename']); |
84
|
|
|
|
85
|
|
|
//populate $_FILES with information, size may be off in multibyte situation |
86
|
|
|
/** @noinspection PhpUndefinedVariableInspection */ |
87
|
|
|
$_FILES[ $matches[ 2 ] ] = array( |
88
|
|
|
'error'=>0, |
89
|
|
|
'name'=>$filename, |
90
|
|
|
'tmp_name'=>$tmp_name, |
91
|
|
|
'size'=>strlen( $body ), |
92
|
|
|
'type'=>$value |
|
|
|
|
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
//place in temporary directory |
96
|
|
|
file_put_contents($tmp_name, $body); |
97
|
|
|
} |
98
|
|
|
//Parse Field |
99
|
|
|
else |
100
|
|
|
{ |
101
|
|
|
$data[$name] = substr($body, 0, strlen($body) - 2); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
$GLOBALS[ '_PUT' ] = $data; |
107
|
|
|
return $data; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $method |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function httpMethodData($method='get') |
115
|
|
|
{ |
116
|
|
|
$body = []; |
117
|
|
|
if(httpMethod()==$method){ |
118
|
|
|
$rawData = json_decode(request()->getContent(),1); |
119
|
|
|
|
120
|
|
|
$get = get(); |
|
|
|
|
121
|
|
|
if(is_array($get) && count($get)){ |
|
|
|
|
122
|
|
|
$body['params'] = get(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if(is_null($rawData)){ |
126
|
|
|
$inputData = $this->getInputData(); |
127
|
|
|
|
128
|
|
|
if(!is_null($inputData)){ |
129
|
|
|
$body['body']['form-data'] = $this->getInputData(); |
130
|
|
|
} |
131
|
|
|
else{ |
132
|
|
|
|
133
|
|
|
$all = request()->request->all(); |
134
|
|
|
if(is_array($all) && count($all)){ |
135
|
|
|
$body['body']['x-www-form-urlencoded'] = $all; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
else{ |
142
|
|
|
$body['body']['raw-data'] = $rawData; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $body; |
147
|
|
|
} |
148
|
|
|
} |