| 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 |
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 |
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) { |
| 145 |
$ip = $this->server('HTTP_CLIENT_IP'); |
| 146 |
} elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { |
| 147 |
$ip = $this->server('HTTP_X_FORWARDED_FOR'); |
| 148 |
} else { |
| 149 |
$ip = $this->server('REMOTE_ADDR'); |
| 150 |
} |
| 151 |
|
| 152 |
return $ip; |
| 153 |
} |
| 154 |
|
| 155 |
public function server($key = null, $default = null) |
| 156 |
{ |
| 157 |
return $key ? (isset($this->server[$key]) ? $this->server[$key] : $default) : $this->server; |
| 158 |
} |
| 159 |
|
| 160 |
public function header($key = null, $default = null) |
| 161 |
{ |
| 162 |
if (!$this->headers) { |
| 163 |
$this->headers = $this->setHeaders(); |
| 164 |
} |
| 165 |
|
| 166 |
return $key ? (isset($this->headers[$key]) ? $this->headers[$key] : $default) : $this->headers; |
| 167 |
} |
| 168 |
|
| 169 |
public function cookie($key = null, $default = null) |
| 170 |
{ |
| 171 |
return $key ? (isset($this->cookie[$key]) ? $this->cookie[$key] : $default) : $this->cookie; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Taken and modified from Symfony |
| 176 |
*/ |
| 177 |
public function setHeaders() |
| 178 |
{ |
| 179 |
$headers = array(); |
| 180 |
$parameters = $this->server; |
| 181 |
$contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true); |
| 182 |
foreach ($parameters as $key => $value) { |
| 183 |
if (0 === strpos($key, 'HTTP_')) { |
| 184 |
$headers[substr($key, 5)] = $value; |
| 185 |
} |
| 186 |
// CONTENT_* are not prefixed with HTTP_ |
| 187 |
elseif (isset($contentHeaders[$key])) { |
| 188 |
$headers[$key] = $value; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if (isset($parameters['PHP_AUTH_USER'])) { |
| 193 |
$headers['PHP_AUTH_USER'] = $parameters['PHP_AUTH_USER']; |
| 194 |
$headers['PHP_AUTH_PW'] = isset($parameters['PHP_AUTH_PW']) ? $parameters['PHP_AUTH_PW'] : ''; |
| 195 |
} else { |
| 196 |
/* |
| 197 |
* php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default |
| 198 |
* For this workaround to work, add these lines to your .htaccess file: |
| 199 |
* RewriteCond %{HTTP:Authorization} ^(.+)$ |
| 200 |
* RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
| 201 |
* |
| 202 |
* A sample .htaccess file: |
| 203 |
* RewriteEngine On |
| 204 |
* RewriteCond %{HTTP:Authorization} ^(.+)$ |
| 205 |
* RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
| 206 |
* RewriteCond %{REQUEST_FILENAME} !-f |
| 207 |
* RewriteRule ^(.*)$ app.php [QSA,L] |
| 208 |
*/ |
| 209 |
|
| 210 |
$authorizationHeader = null; |
| 211 |
if (isset($parameters['HTTP_AUTHORIZATION'])) { |
| 212 |
$authorizationHeader = $parameters['HTTP_AUTHORIZATION']; |
| 213 |
} elseif (isset($parameters['REDIRECT_HTTP_AUTHORIZATION'])) { |
| 214 |
$authorizationHeader = $parameters['REDIRECT_HTTP_AUTHORIZATION']; |
| 215 |
} |
| 216 |
|
| 217 |
if (null !== $authorizationHeader) { |
| 218 |
if (0 === stripos($authorizationHeader, 'basic ')) { |
| 219 |
// Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic |
| 220 |
$exploded = explode(':', base64_decode(substr($authorizationHeader, 6)), 2); |
| 221 |
if (count($exploded) == 2) { |
| 222 |
list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded; |
| 223 |
} |
| 224 |
} elseif (empty($parameters['PHP_AUTH_DIGEST']) && (0 === stripos($authorizationHeader, 'digest '))) { |
| 225 |
// In some circumstances PHP_AUTH_DIGEST needs to be set |
| 226 |
$headers['PHP_AUTH_DIGEST'] = $authorizationHeader; |
| 227 |
$parameters['PHP_AUTH_DIGEST'] = $authorizationHeader; |
| 228 |
} elseif (0 === stripos($authorizationHeader, 'bearer ')) { |
| 229 |
/* |
| 230 |
* XXX: Since there is no PHP_AUTH_BEARER in PHP predefined variables, |
| 231 |
* I'll just set $headers['AUTHORIZATION'] here. |
| 232 |
* http://php.net/manual/en/reserved.variables.server.php |
| 233 |
*/ |
| 234 |
$headers['AUTHORIZATION'] = $authorizationHeader; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if (isset($headers['AUTHORIZATION'])) { |
| 240 |
return $headers; |
| 241 |
} |
| 242 |
|
| 243 |
// PHP_AUTH_USER/PHP_AUTH_PW |
| 244 |
if (isset($headers['PHP_AUTH_USER'])) { |
| 245 |
$headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']); |
| 246 |
} elseif (isset($headers['PHP_AUTH_DIGEST'])) { |
| 247 |
$headers['AUTHORIZATION'] = $headers['PHP_AUTH_DIGEST']; |
| 248 |
} |
| 249 |
|
| 250 |
return $headers; |
| 251 |
} |
| 252 |
} |
| 253 |
|