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