1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\RestServer; |
4
|
|
|
|
5
|
|
|
use ByJG\RestServer\Exception\OperationIdInvalidException; |
6
|
|
|
use ByJG\Util\Psr7\Request; |
7
|
|
|
|
8
|
|
|
class MockHttpRequest extends HttpRequest |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Request |
12
|
|
|
*/ |
13
|
|
|
protected $psrRequest ; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* |
17
|
|
|
* @param Request $psrRequest |
18
|
|
|
*/ |
19
|
3 |
|
public function __construct(Request $psrRequest) |
20
|
|
|
{ |
21
|
3 |
|
$this->psrRequest = $psrRequest; |
22
|
|
|
|
23
|
3 |
|
$this->initializePhpVariables(); |
24
|
|
|
|
25
|
3 |
|
parent::__construct($this->get, $this->post, $this->server, $this->session, $this->cookie); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private $payload; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get the payload passed during the request(the same as php://input). If not found return empty. |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function payload() |
36
|
|
|
{ |
37
|
|
|
if (is_null($this->payload)) { |
38
|
|
|
$this->payload = $this->psrRequest->getBody()->getContents(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->payload; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Initilize PHP variables based on the request |
47
|
|
|
*/ |
48
|
3 |
|
protected function initializePhpVariables() |
49
|
|
|
{ |
50
|
3 |
|
$this->session = []; |
51
|
|
|
|
52
|
3 |
|
$this->server = []; |
53
|
3 |
|
$this->server["REMOTE_ADDR"] = "127.0.0.1"; |
54
|
3 |
|
$this->server["REMOTE_PORT"] = rand(1000, 60000); |
55
|
3 |
|
$this->server["SERVER_SOFTWARE"] = "Mock"; |
56
|
3 |
|
$this->server["SERVER_PROTOCOL"] = "HTTP/" . $this->psrRequest->getProtocolVersion(); |
57
|
3 |
|
$this->server["SERVER_NAME"] = $this->psrRequest->getUri()->getHost(); |
58
|
3 |
|
$this->server["SERVER_PORT"] = $this->psrRequest->getUri()->getPort(); |
59
|
3 |
|
$this->server["REQUEST_URI"] = $this->psrRequest->getRequestTarget(); |
60
|
3 |
|
$this->server["REQUEST_METHOD"] = $this->psrRequest->getMethod(); |
61
|
3 |
|
$this->server["SCRIPT_NAME"] = $this->psrRequest->getUri()->getPath(); |
62
|
3 |
|
$this->server["SCRIPT_FILENAME"] = __FILE__; |
63
|
3 |
|
$this->server["PHP_SELF"] = $this->psrRequest->getUri()->getPath(); |
64
|
3 |
|
$this->server["QUERY_STRING"] = $this->psrRequest->getUri()->getQuery(); |
65
|
3 |
|
$this->server["HTTP_HOST"] = $this->psrRequest->getHeaderLine("Host"); |
66
|
3 |
|
$this->server["HTTP_USER_AGENT"] = $this->psrRequest->getHeaderLine("User-Agent"); |
67
|
3 |
|
$this->server["REQUEST_TIME"] = time(); |
68
|
3 |
|
$this->server["REQUEST_TIME_FLOAT"] = microtime(true); |
69
|
|
|
|
70
|
|
|
// Headers and Cookies |
71
|
3 |
|
$this->cookie = []; |
72
|
3 |
|
foreach ($this->psrRequest->getHeaders() as $key => $value) { |
73
|
3 |
|
$this->server["HTTP_" . strtoupper($key)] = $this->psrRequest->getHeaderLine($key); |
74
|
|
|
|
75
|
3 |
|
if ($key == "Cookie") { |
76
|
1 |
|
parse_str(preg_replace("/;\s*/", "&", $this->psrRequest->getHeaderLine($key)), $this->cookie); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
$this->phpRequest = []; |
81
|
3 |
|
$this->get = []; |
82
|
3 |
|
$this->post = []; |
83
|
|
|
|
84
|
3 |
|
if (!empty($this->server["QUERY_STRING"])) { |
85
|
3 |
|
parse_str($this->server["QUERY_STRING"], $this->phpRequest); |
86
|
3 |
|
parse_str($this->server["QUERY_STRING"], $this->get); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
if ($this->psrRequest->getHeaderLine("content-type") == "application/x-www-form-urlencoded") { |
90
|
2 |
|
parse_str($this->psrRequest->getBody()->getContents(), $this->post); |
91
|
2 |
|
$this->phpRequest = array_merge($this->phpRequest, $this->post); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Overriding PHP Values |
95
|
3 |
|
$_SERVER = $this->server; |
96
|
3 |
|
$_GET = $this->get; |
97
|
3 |
|
$_POST = $this->post; |
98
|
3 |
|
$_REQUEST = $this->phpRequest; |
99
|
3 |
|
$_COOKIE = $this->cookie; |
100
|
|
|
|
101
|
3 |
|
$this->initializePhpFileVar(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Inicialize the PHP variable $_FILE |
106
|
|
|
*/ |
107
|
3 |
|
protected function initializePhpFileVar() |
108
|
|
|
{ |
109
|
3 |
|
$_FILES = []; |
110
|
|
|
|
111
|
3 |
|
$contentType = $this->psrRequest->getHeaderLine("Content-Type"); |
112
|
3 |
|
if (empty($contentType) || strpos($contentType, "multipart/") === false) { |
113
|
2 |
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
$body = $this->psrRequest->getBody()->getContents(); |
117
|
1 |
|
$matches = []; |
118
|
|
|
|
119
|
1 |
|
preg_match('/boundary=(.*)$/', $contentType, $matches); |
120
|
1 |
|
$boundary = $matches[1]; |
121
|
|
|
|
122
|
|
|
// split content by boundary and get rid of last -- element |
123
|
1 |
|
$blocks = preg_split("/-+$boundary/", $body); |
124
|
1 |
|
array_pop($blocks); |
125
|
|
|
|
126
|
|
|
// loop data blocks |
127
|
1 |
|
foreach ($blocks as $id => $block) { |
128
|
1 |
|
if (empty($block)) |
129
|
1 |
|
continue; |
130
|
|
|
|
131
|
1 |
|
$name = []; |
132
|
1 |
|
preg_match('/\bname=\"([^\"]*)\"\s*;/s', $block, $name); |
133
|
|
|
|
134
|
1 |
|
$filename = []; |
135
|
1 |
|
preg_match('/\bfilename=\"([^\"]*)\"\s*;/s', $block, $filename); |
136
|
|
|
|
137
|
1 |
|
$contentType = []; |
138
|
1 |
|
preg_match('/Content-Type\s*:([^\r\n]*)/s', $block, $contentType); |
139
|
|
|
|
140
|
1 |
|
$content = []; |
141
|
1 |
|
preg_match('/\r?\n\r?\n(.*)$/s', $block, $content); |
142
|
|
|
|
143
|
1 |
|
if (empty($name)) { |
144
|
|
|
throw new OperationIdInvalidException("The multipart should provide a name"); |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
$content = (empty($content) ? "" : $content[1]); |
148
|
1 |
|
if (empty($filename)) { // Is post... |
149
|
1 |
|
$this->post[$name[1]] = $content; |
150
|
1 |
|
$this->phpRequest[$name[1]] = $content; |
151
|
1 |
|
$_POST[$name[1]] = $this->post[$name[1]]; |
152
|
1 |
|
$_REQUEST[$name[1]] = $this->post[$name[1]]; |
153
|
1 |
|
continue; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
$strut = [ |
157
|
1 |
|
'name' => $filename[1], |
158
|
1 |
|
'type' => (empty($contentType) ? "" : trim($contentType[1])), |
159
|
1 |
|
'size' => strlen($content), |
160
|
1 |
|
'tmp_name' => sys_get_temp_dir() . "/" . $filename[1], |
161
|
1 |
|
'error' => null |
162
|
1 |
|
]; |
163
|
1 |
|
file_put_contents($strut["tmp_name"], $content); |
164
|
|
|
|
165
|
1 |
|
$_FILES[$name[1]] = $strut; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|