|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WPEmerge\Requests; |
|
4
|
|
|
|
|
5
|
|
|
use WPEmerge\Helpers\Url; |
|
6
|
|
|
use WPEmerge\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A server request representation |
|
10
|
|
|
*/ |
|
11
|
|
|
class Request { |
|
12
|
|
|
/** |
|
13
|
|
|
* GET parameters |
|
14
|
|
|
* |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $get = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* POST parameters |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $post = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* COOKIE parameters |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $cookie = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* FILES parameters |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $files = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* SERVER parameters |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $server = []; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Headers |
|
49
|
|
|
* |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $headers = []; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create a new instance from php superglobals |
|
56
|
|
|
* |
|
57
|
|
|
* @return Request |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public static function fromGlobals() { |
|
60
|
1 |
|
return new static( stripslashes_deep( $_GET ), stripslashes_deep( $_POST ), $_COOKIE, $_FILES, $_SERVER, getallheaders() ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Constructor |
|
65
|
|
|
* |
|
66
|
|
|
* @param array $get |
|
67
|
|
|
* @param array $post |
|
68
|
|
|
* @param array $cookie |
|
69
|
|
|
* @param array $files |
|
70
|
|
|
* @param array $server |
|
71
|
|
|
* @param array $headers |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function __construct( $get, $post, $cookie, $files, $server, $headers ) { |
|
74
|
1 |
|
$this->get = $get; |
|
75
|
1 |
|
$this->post = $post; |
|
76
|
1 |
|
$this->cookie = $cookie; |
|
77
|
1 |
|
$this->files = $files; |
|
78
|
1 |
|
$this->server = $server; |
|
79
|
1 |
|
$this->headers = $headers; |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the request method |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getMethod() { |
|
88
|
1 |
|
$method = (string) $this->server( 'REQUEST_METHOD', 'GET' ); |
|
89
|
|
|
|
|
90
|
1 |
|
$override = (string) $this->headers( 'X-HTTP-METHOD-OVERRIDE' ); |
|
91
|
1 |
|
if ( $method === 'POST' && $override ) { |
|
92
|
1 |
|
$method = $override; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
return strtoupper( $method ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the request url |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
3 |
|
public function getUrl() { |
|
104
|
3 |
|
$https = $this->server( 'HTTPS' ); |
|
105
|
|
|
|
|
106
|
3 |
|
$protocol = $https ? 'https' : 'http'; |
|
107
|
3 |
|
$host = (string) $this->server( 'HTTP_HOST', '' ); |
|
108
|
3 |
|
$uri = (string) $this->server( 'REQUEST_URI', '' ); |
|
109
|
3 |
|
$uri = Url::addLeadingSlash( $uri ); |
|
110
|
|
|
|
|
111
|
3 |
|
return $protocol . '://' . $host . $uri; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get a value from any of the request parameters |
|
116
|
|
|
* |
|
117
|
|
|
* @see \WPEmerge\Support\Arr |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
4 |
|
protected function input() { |
|
121
|
4 |
|
$args = func_get_args(); |
|
122
|
4 |
|
$source = $this->{$args[0]}; |
|
123
|
|
|
|
|
124
|
4 |
|
if ( count( $args ) === 1 ) { |
|
125
|
1 |
|
return $source; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
3 |
|
$args[0] = $source; |
|
129
|
3 |
|
return call_user_func_array( [Arr::class, 'get'], $args ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get a value from the GET parameters |
|
134
|
|
|
* |
|
135
|
|
|
* @see \WPEmerge\Support\Arr |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
4 |
|
public function get() { |
|
139
|
4 |
|
return call_user_func_array( [$this, 'input'], array_merge( ['get'], func_get_args() ) ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get a value from the POST parameters |
|
144
|
|
|
* |
|
145
|
|
|
* @see \WPEmerge\Support\Arr |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
*/ |
|
148
|
4 |
|
public function post() { |
|
149
|
4 |
|
return call_user_func_array( [$this, 'input'], array_merge( ['post'], func_get_args() ) ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get a value from the COOKIE parameters |
|
154
|
|
|
* |
|
155
|
|
|
* @see \WPEmerge\Support\Arr |
|
156
|
|
|
* @return mixed |
|
157
|
|
|
*/ |
|
158
|
4 |
|
public function cookie() { |
|
159
|
4 |
|
return call_user_func_array( [$this, 'input'], array_merge( ['cookie'], func_get_args() ) ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get a value from the FILES parameters |
|
164
|
|
|
* |
|
165
|
|
|
* @see \WPEmerge\Support\Arr |
|
166
|
|
|
* @return mixed |
|
167
|
|
|
*/ |
|
168
|
4 |
|
public function files() { |
|
169
|
4 |
|
return call_user_func_array( [$this, 'input'], array_merge( ['files'], func_get_args() ) ); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get a value from the SERVER parameters |
|
174
|
|
|
* |
|
175
|
|
|
* @see \WPEmerge\Support\Arr |
|
176
|
|
|
* @return mixed |
|
177
|
|
|
*/ |
|
178
|
4 |
|
public function server() { |
|
179
|
4 |
|
return call_user_func_array( [$this, 'input'], array_merge( ['server'], func_get_args() ) ); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get a value from the headers |
|
184
|
|
|
* |
|
185
|
|
|
* @see \WPEmerge\Support\Arr |
|
186
|
|
|
* @return mixed |
|
187
|
|
|
*/ |
|
188
|
4 |
|
public function headers() { |
|
189
|
4 |
|
return call_user_func_array( [$this, 'input'], array_merge( ['headers'], func_get_args() ) ); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|