PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.8
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.8
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / vendor / wpfluent / framework / src / WPFluent / Request / Request.php

Request.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.8, at vendor/wpfluent/framework/src/WPFluent/Request/Request.php

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