1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Djunehor\PutHelper; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Http\UploadedFile; |
8
|
|
|
|
9
|
|
|
class PutRequestMiddleware |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Handle an incoming request. |
13
|
|
|
* |
14
|
|
|
* @param \Illuminate\Http\Request $request |
15
|
|
|
* @param \Closure $next |
16
|
|
|
* @return mixed |
17
|
|
|
*/ |
18
|
|
|
|
19
|
5 |
|
public function handle(Request $request, Closure $next) |
20
|
|
|
{ |
21
|
|
|
// to indicate put middleware was called |
22
|
5 |
|
session()->put(self::class, true); |
23
|
5 |
|
if ($request->isMethod('PUT')) { |
24
|
|
|
// to indicate that action is performed for PUT request |
25
|
4 |
|
session()->put(self::class, session()->token()); |
26
|
4 |
|
$string = file_get_contents('php://input'); |
27
|
4 |
|
$array = explode("Content-Disposition: form-data;", $string); |
28
|
4 |
|
$payload = []; |
29
|
|
|
|
30
|
|
|
// reformat payload |
31
|
4 |
|
foreach ($array as $item) { |
32
|
4 |
|
if (stripos($item, 'name') !== false) { |
33
|
|
|
$key = $this->getStringBetween($item, 'name="', '"'); |
34
|
|
|
|
35
|
|
|
// if it's a file |
36
|
|
|
if (stripos($item, 'filename') != false) { |
|
|
|
|
37
|
|
|
$filename = $this->getStringBetween($item, 'filename="', '"'); |
38
|
|
|
if (!$filename) continue; |
39
|
|
|
|
40
|
|
|
$contentType = $this->getStringBetween($item, 'Content-Type: ', "\r"); |
41
|
|
|
$newArray = explode('Content-Type: ' . $contentType, $string); |
42
|
|
|
$filePath = sys_get_temp_dir() . $filename; |
43
|
|
|
file_put_contents($filePath, trim($newArray[1])); |
44
|
|
|
$value = new UploadedFile($filePath, $filename, null, null, false, true); |
45
|
|
|
|
46
|
|
|
$payload[$key] = $value; |
47
|
|
|
$_FILES[$key] = [ |
48
|
|
|
'name' => $filename, |
49
|
|
|
'type' => $contentType, |
50
|
|
|
'tmp_name' => $filePath, |
51
|
|
|
"error" => 0, |
52
|
|
|
"size" => $value->getSize() |
53
|
|
|
]; |
54
|
|
|
} else { |
55
|
|
|
$value = $this->getStringBetween($item, 'name="' . $key . '"', '----------------------------'); |
56
|
|
|
$payload[$key] = $value; |
57
|
4 |
|
$_POST[$key] = $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
$request->merge($payload); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
$response = $next($request); |
67
|
|
|
|
68
|
|
|
// let's delete tmp uploaded file, if any, to save space |
69
|
5 |
|
if (isset($filePath)) { |
70
|
|
|
@unlink($filePath); |
|
|
|
|
71
|
|
|
} |
72
|
5 |
|
return $response; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
private function getStringBetween($string, $start, $end) |
76
|
|
|
{ |
77
|
|
|
$string = ' ' . $string; |
78
|
|
|
$ini = strpos($string, $start); |
79
|
|
|
if ($ini == 0) return ''; |
80
|
|
|
$ini += strlen($start); |
81
|
|
|
$len = strpos($string, $end, $ini) - $ini; |
82
|
|
|
$newString = substr($string, $ini, $len); |
83
|
|
|
return trim($newString); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|