PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.7.2
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.7.2
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 1.7.2, at framework/Request/Request.php

250 lines 7.0 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 = 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
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 return array_key_exists($key, $this->request);
55 }
56
57 /**
58 * Variable exists and has truthy value
59 * @param string $key
60 * @return bool
61 */
62 public function has($key)
63 {
64 return $this->exists($key) && !empty($this->request[$key]);
65 }
66
67 public function set($key, $value)
68 {
69 $this->request[$key] = $value;
70 return $this;
71 }
72
73 public function all()
74 {
75 return $this->get();
76 }
77
78 public function get($key = null, $default = null)
79 {
80 if (!$key) {
81 return $this->request;
82 } else {
83 return $this->exists($key) ? $this->request[$key] : $default;
84 }
85 }
86
87 /**
88 * Get the files from the request.
89 *
90 * @return array
91 */
92 public function files()
93 {
94 return $this->files;
95 }
96
97 public function query($key = null, $default = null)
98 {
99 return $key ? (isset($this->get[$key]) ? $this->get[$key] : $default) : $this->get;
100 }
101
102 public function post($key = null, $default = null)
103 {
104 return $key ? (isset($this->post[$key]) ? $this->post[$key] : $default) : $this->post;
105 }
106
107 public function only($args)
108 {
109 $values = [];
110 $keys = is_array($args) ? $args : func_get_args();
111 foreach ($keys as $key) {
112 $values[$key] = $this->get($key);
113 }
114 return $values;
115 }
116
117 public function except($args)
118 {
119 $values = [];
120 $keys = is_array($args) ? $args : func_get_args();
121 foreach ($this->request as $key => $value) {
122 if (!in_array($key, $keys)) {
123 $values[$key] = $this->get($key);
124 }
125 }
126 return $values;
127 }
128
129 public function merge(array $data = [])
130 {
131 $this->request = array_merge($this->request, $data);
132 return $this;
133 }
134
135 /**
136 * Get user ip address
137 * @return string
138 */
139 public function getIp()
140 {
141 if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
142 $ip = $this->server('HTTP_CLIENT_IP');
143 } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
144 $ip = $this->server('HTTP_X_FORWARDED_FOR');
145 } else {
146 $ip = $this->server('REMOTE_ADDR');
147 }
148
149 return $ip;
150 }
151
152 public function server($key = null, $default = null)
153 {
154 return $key ? (isset($this->server[$key]) ? $this->server[$key] : $default) : $this->server;
155 }
156
157 public function header($key = null, $default = null)
158 {
159 if (!$this->headers) {
160 $this->headers = $this->setHeaders();
161 }
162
163 return $key ? (isset($this->headers[$key]) ? $this->headers[$key] : $default) : $this->headers;
164 }
165
166 public function cookie($key = null, $default = null)
167 {
168 return $key ? (isset($this->cookie[$key]) ? $this->cookie[$key] : $default) : $this->cookie;
169 }
170
171 /**
172 * Taken and modified from Symfony
173 */
174 public function setHeaders()
175 {
176 $headers = array();
177 $parameters = $this->server;
178 $contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true);
179 foreach ($parameters as $key => $value) {
180 if (0 === strpos($key, 'HTTP_')) {
181 $headers[substr($key, 5)] = $value;
182 }
183 // CONTENT_* are not prefixed with HTTP_
184 elseif (isset($contentHeaders[$key])) {
185 $headers[$key] = $value;
186 }
187 }
188
189 if (isset($parameters['PHP_AUTH_USER'])) {
190 $headers['PHP_AUTH_USER'] = $parameters['PHP_AUTH_USER'];
191 $headers['PHP_AUTH_PW'] = isset($parameters['PHP_AUTH_PW']) ? $parameters['PHP_AUTH_PW'] : '';
192 } else {
193 /*
194 * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
195 * For this workaround to work, add these lines to your .htaccess file:
196 * RewriteCond %{HTTP:Authorization} ^(.+)$
197 * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
198 *
199 * A sample .htaccess file:
200 * RewriteEngine On
201 * RewriteCond %{HTTP:Authorization} ^(.+)$
202 * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
203 * RewriteCond %{REQUEST_FILENAME} !-f
204 * RewriteRule ^(.*)$ app.php [QSA,L]
205 */
206
207 $authorizationHeader = null;
208 if (isset($parameters['HTTP_AUTHORIZATION'])) {
209 $authorizationHeader = $parameters['HTTP_AUTHORIZATION'];
210 } elseif (isset($parameters['REDIRECT_HTTP_AUTHORIZATION'])) {
211 $authorizationHeader = $parameters['REDIRECT_HTTP_AUTHORIZATION'];
212 }
213
214 if (null !== $authorizationHeader) {
215 if (0 === stripos($authorizationHeader, 'basic ')) {
216 // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
217 $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2);
218 if (count($exploded) == 2) {
219 list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
220 }
221 } elseif (empty($parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader, 'digest '))) {
222 // In some circumstances PHP_AUTH_DIGEST needs to be set
223 $headers['PHP_AUTH_DIGEST'] = $authorizationHeader;
224 $parameters['PHP_AUTH_DIGEST'] = $authorizationHeader;
225 } elseif (0 === stripos($authorizationHeader, 'bearer ')) {
226 /*
227 * XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables,
228 * I'll just set $headers['AUTHORIZATION'] here.
229 * http://php.net/manual/en/reserved.variables.server.php
230 */
231 $headers['AUTHORIZATION'] = $authorizationHeader;
232 }
233 }
234 }
235
236 if (isset($headers['AUTHORIZATION'])) {
237 return $headers;
238 }
239
240 // PHP_AUTH_USER/PHP_AUTH_PW
241 if (isset($headers['PHP_AUTH_USER'])) {
242 $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
243 } elseif (isset($headers['PHP_AUTH_DIGEST'])) {
244 $headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST'];
245 }
246
247 return $headers;
248 }
249 }
250