1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
<?php
namespace FluentMail\Includes\Request;
use FluentMail\Includes\Support\Arr;
class Request { use FileHandler, Cleaner;
protected $app = null; protected $headers = array(); protected $server = array(); protected $cookie = array(); protected $json = array(); protected $get = array(); protected $post = array(); protected $files = array(); protected $request = array();
public function __construct($app, $get, $post, $files) { $this->app = $app; $this->server = $_SERVER; $this->cookie = $_COOKIE; $this->files = $this->prepareFiles($files); $this->request = array_merge( $this->get = $this->clean($get), $this->post = $this->clean($post) ); }
/** * Variable exists * @param string $key * @return bool */ public function exists($key) { return Arr::has($this->request, $key); }
/** * Variable exists and has truthy value * @param string $key * @return bool */ public function has($key) { return $this->exists($key) && !empty(Arr::get($this->request, $key)); }
public function set($key, $value) { Arr::set($this->request, $key, $value);
return $this; }
public function all() { return $this->get(); }
public function get($key = null, $default = null) { return Arr::get($this->request, $key, $default); }
/** * Get the files from the request. * * @return array */ public function files() { return $this->files; }
public function query($key = null, $default = null) { return $key ? Arr::get($this->get, $key, $default) : $this->get; }
public function post($key = null, $default = null) { return $key ? Arr::get($this->post, $key, $default) : $this->post; }
public function only($keys) { return Arr::only($this->request, $keys); }
public function except($args) { return Arr::except($this->request, $args); }
public function merge(array $data = []) { $this->request = array_replace($this->request, $data);
return $this; }
/** * Get user ip address * @return string */ public function getIp() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $this->server('HTTP_CLIENT_IP'); } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $this->server('HTTP_X_FORWARDED_FOR'); } else { $ip = $this->server('REMOTE_ADDR'); }
return $ip; }
public function server($key = null, $default = null) { return $key ? Arr::get($this->server, $key, $default) : $this->server; }
public function header($key = null, $default = null) { if (!$this->headers) { $this->headers = $this->setHeaders(); }
return $key ? Arr::get($this->headers, $key, $default) : $this->headers; }
public function method() { return $this->server('REQUEST_METHOD'); }
public function cookie($key = null, $default = null) { return $key ? Arr::get($this->cookie, $key, $default) : $this->cookie; }
/** * Taken and modified from Symfony */ public function setHeaders() { $headers = array(); $parameters = $this->server; $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true); foreach ($parameters as $key => $value) { if (0 === strpos($key, 'HTTP_')) { $headers[substr($key, 5)] = $value; } // CONTENT_* are not prefixed with HTTP_ elseif (isset($contentHeaders[$key])) { $headers[$key] = $value; } }
if (isset($parameters['PHP_AUTH_USER'])) { $headers['PHP_AUTH_USER'] = $parameters['PHP_AUTH_USER']; $headers['PHP_AUTH_PW'] = isset($parameters['PHP_AUTH_PW']) ? $parameters['PHP_AUTH_PW'] : ''; } else { /* * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default * For this workaround to work, add these lines to your .htaccess file: * RewriteCond %{HTTP:Authorization} ^(.+)$ * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] * * A sample .htaccess file: * RewriteEngine On * RewriteCond %{HTTP:Authorization} ^(.+)$ * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] * RewriteCond %{REQUEST_FILENAME} !-f * RewriteRule ^(.*)$ app.php [QSA,L] */
$authorizationHeader = null; if (isset($parameters['HTTP_AUTHORIZATION'])) { $authorizationHeader = $parameters['HTTP_AUTHORIZATION']; } elseif (isset($parameters['REDIRECT_HTTP_AUTHORIZATION'])) { $authorizationHeader = $parameters['REDIRECT_HTTP_AUTHORIZATION']; }
if (null !== $authorizationHeader) { if (0 === stripos($authorizationHeader, 'basic ')) { // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2); if (count($exploded) == 2) { list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded; } } elseif (empty($parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader, 'digest '))) { // In some circumstances PHP_AUTH_DIGEST needs to be set $headers['PHP_AUTH_DIGEST'] = $authorizationHeader; $parameters['PHP_AUTH_DIGEST'] = $authorizationHeader; } elseif (0 === stripos($authorizationHeader, 'bearer ')) { /* * XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables, * I'll just set $headers['AUTHORIZATION'] here. * http://php.net/manual/en/reserved.variables.server.php */ $headers['AUTHORIZATION'] = $authorizationHeader; } } }
if (isset($headers['AUTHORIZATION'])) { return $headers; }
// PHP_AUTH_USER/PHP_AUTH_PW if (isset($headers['PHP_AUTH_USER'])) { $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']); } elseif (isset($headers['PHP_AUTH_DIGEST'])) { $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST']; }
return $headers; }
/** * Get an input element from the request. * * @param string $key * @return mixed */ public function __get($key) { return $this->get($key); } }
|