PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v1.4.5
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / Request / Request.php

Request.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.4.5, at vendor/wpfluent/framework/src/WPFluent/Request/Request.php

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