1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PlugHttp\Body; |
4
|
|
|
|
5
|
|
|
use PlugHttp\Globals\GlobalServer; |
6
|
|
|
use PlugHttp\Utils\ContentHelper; |
7
|
|
|
|
8
|
|
|
class FormData implements Handler, Advancer |
9
|
|
|
{ |
10
|
|
|
private $handler; |
11
|
|
|
|
12
|
|
|
public function getBody($content) |
13
|
|
|
{ |
14
|
|
|
$values = []; |
15
|
|
|
|
16
|
|
|
if (!empty($content)) { |
17
|
|
|
preg_match_all('/"(.+)"+\s+(.+(?:-{5,})?)/', $content, $matches); |
18
|
|
|
|
19
|
|
|
foreach ($matches[1] as $key => $match) { |
20
|
|
|
$matchKey = $this->removeCaractersOfString($match, ["'", '"']); |
21
|
|
|
|
22
|
|
|
$values[$matchKey] = $this->getValueFormData($matches[2][$key]); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $values; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getValueFormData($value) |
30
|
|
|
{ |
31
|
|
|
$valueCleared = $this->removeCaractersOfString($value, ["'", '"']); |
32
|
|
|
$onlyHasTraces = preg_split("/-{20,}/", $valueCleared, PREG_SPLIT_OFFSET_CAPTURE); |
33
|
|
|
return $this->checkIfValueIsEmpty($onlyHasTraces) > 1 ? '' : $valueCleared; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function checkIfValueIsEmpty($onlyHasTraces) |
37
|
|
|
{ |
38
|
|
|
$response = true; |
39
|
|
|
|
40
|
|
|
if (is_array($onlyHasTraces)) { |
41
|
|
|
$response = count($onlyHasTraces) > 1; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $response; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function removeCaractersOfString($str, array $caracters) |
48
|
|
|
{ |
49
|
|
|
return str_replace($caracters, '', $str); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function next(Handler $handler) |
53
|
|
|
{ |
54
|
|
|
$this->handler = $handler; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function checkIsFormData(GlobalServer $server) |
58
|
|
|
{ |
59
|
|
|
return ContentHelper::contentIs($server, 'form-data') && $server->method() !== 'POST'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function handle($server) |
63
|
|
|
{ |
64
|
|
|
if ($this->checkIsFormData($server)) { |
65
|
|
|
return $this->getBody($server->getContent()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->handler->handle($server); |
69
|
|
|
} |
70
|
|
|
} |