PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.17
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.17
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 3.6.66 All 195 releases
fluentform / framework / Request / Request.php

Request.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 4.3.17, at framework/Request/Request.php

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