1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
<?php
/**
* Router Request.
*
* @package Silla.IO
* @subpackage Core\Modules\Router
* @author Plamen Nikolov <plamen@athlonsofia.com>
* @copyright Copyright (c) 2015, Silla.io
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3.0 (GPLv3)
*/
namespace Core\Modules\Router;
use Core;
/**
* Class Request definition.
*/
final class Request
{
/**
* Request elements.
*
* @var array
* @access private
*/
private $elements = array();
/**
* Request context.
*
* @var array
* @access private
*/
private $context;
/**
* Token value.
*
* Used for validation of the request(e.g prevent CSRF).
*
* @var string
*/
private $token;
/**
* Request mode.
*
* @var array
*/
private $mode;
/**
* Init actions.
*
* Renew token on each request.
*
* @param array $mode Request Mode.
* @param array $elements Request Elements. All Routed variables.
* @param array $context Request Context Server data.
*/
public function __construct(array $mode, array $elements, array $context)
{
$this->elements = $elements;
$this->context = $context;
$this->mode = $mode;
$this->token = '';
/* Make all routed variables accessible as a GET variables */
$context = isset($this->context['_GET']) ? $this->context['_GET'] : array();
$this->context['_GET'] = array_merge($context, $elements);
}
/**
* Retrieves the controller action name.
*
* @access public
*
* @return string
*/
public function controller()
{
return $this->elements['controller'];
}
/**
* Retrieves the request action name.
*
* @access public
*
* @return string
*/
public function action()
{
return $this->elements['action'];
}
/**
* Retrives request mode or part of it.
*
* @param string $segment Mode segment name.
*
* @return array
*/
public function mode($segment = null)
{
if ($segment && isset($this->mode[$segment])) {
return $this->mode[$segment];
}
return $this->mode;
}
/**
* Retrieves the GET request params - $context[_GET] contents.
*
* @param string $key Name of the parameter(optional).
*
* @access public
*
* @return mixed
*/
public function get($key = null)
{
if ($key) {
return isset($this->context['_GET'][$key]) ? $this->context['_GET'][$key] : null;
}
return $this->context['_GET'];
}
/**
* Retrieves the POST request params - $context[_POST] contents.
*
* @param string $key Name of the parameter(optional).
*
* @access public
*
* @return mixed
*/
public function post($key = null)
{
if ($key) {
return isset($this->context['_POST'][$key]) ? $this->context['_POST'][$key] : null;
}
return $this->context['_POST'];
}
/**
* Retrieves the FILES request params - $context[_FILES] contents.
*
* @param string $key Name of the parameter(optional).
*
* @access public
*
* @return mixed
*/
public function files($key = null)
{
if ($key) {
return isset($this->context['_FILES'][$key]) ? $this->context['_FILES'][$key] : null;
}
return $this->context['_FILES'];
}
/**
* Retrieves the Request meta params - $context[_SERVER] contents.
*
* @param string $key Name of the parameter(optional).
*
* @return mixed
*/
public function meta($key = null)
{
if ($key) {
return isset($this->context['_SERVER'][$key]) ? $this->context['_SERVER'][$key] : null;
}
return $this->context['_SERVER'];
}
/**
* Retrieves all Request variables params - $context[_REQUEST] contents.
*
* @param string $key Name of the parameter(optional).
*
* @return mixed
*/
public function variables($key = null)
{
if ($key) {
return isset($this->context['_REQUEST'][$key]) ? $this->context['_REQUEST'][$key] : null;
}
return $this->context['_REQUEST'];
}
/**
* Retrieves all Request variables params - $context[_COOKIE] contents.
*
* @param string $key Name of the parameter(optional).
*
* @return mixed
*/
public function cookies($key = null)
{
if ($key) {
return isset($this->context['_COOKIE'][$key]) ? $this->context['_COOKIE'][$key] : null;
}
return $this->context['_COOKIE'];
}
/**
* Type of the request.
*
* @access public
*
* @return string
*/
public function type()
{
return isset($this->context['_SERVER']['SERVER_PROTOCOL']) ?
$this->context['_SERVER']['SERVER_PROTOCOL'] : null;
}
/**
* Method of the request.
*
* @access public
*
* @return string
*/
public function method()
{
return isset($this->context['_SERVER']['REQUEST_METHOD']) ?
strtoupper($this->context['_SERVER']['REQUEST_METHOD']) : null;
}
/**
* Get current Request token.
*
* @return string
*/
public function token()
{
return $this->token;
}
/**
* Regenerates Request token.
*
* @return void
*/
public function regenerateToken()
{
$this->token = self::generateToken();
}
/**
* Generates a unique token.
*
* @return string
*/
public static function generateToken()
{
return md5(uniqid(rand(), true));
}
/**
* Redirects the browser to a specified target.
*
* @param mixed $url Array/String representation of url.
* @param integer $status Redirect status code according to HTTP specification (301, 302, 303, 307).
*
* @access public
* @uses Core\Config()
* @uses Core\Router()
* @example
* <code>
* redirectTo(array('action' => 'show', 'id' => 5))
* redirectTo('http://www.athlonproduction.com')
* redirectTo('back') - Only current controller action name.
* </code>
*
* @return void
*/
public function redirectTo($url, $status = 302)
{
if (is_array($url)) {
$url = Core\Config()->urls('relative') . Core\Router()->toUrl($url);
} elseif ($url === 'back') {
$url = $this->context['_SERVER']['HTTP_REFERER'];
} elseif (strpos($url, '/') === false) {
$url = Core\Config()->urls('relative') .
Core\Router()->toUrl(array('controller' => $this->controller(), 'action' => $url));
}
if (headers_sent() || $this->is('xhr')) {
echo '<script type="text/javascript">' .
"setTimeout(function() { location.href = '{$url}'; }, 0);" .
'</script>';
exit;
}
switch ($status) {
case 301:
$status = '301 Moved Permanently';
break;
case 303:
$status = '303 See Other';
break;
case 307:
$status = '307 Temporary Redirect';
break;
default:
$status = '302 Found';
break;
}
header($this->type() . ' ' . $status);
header('Location: ' . str_replace('&', '&', $url));
exit;
}
/**
* Checks the type of the request.
*
* @param string $type Type of the request to check.
*
* @access public
*
* @return boolean
*/
public function is($type)
{
$type = strtoupper($type);
if ($type === 'XHR') {
return 'XMLHttpRequest' === $this->meta('HTTP_X_REQUESTED_WITH');
}
return $type === $this->method();
}
/**
* Specify request token.
*
* @param string $token Request token.
*
* @return void
*/
public function setToken($token)
{
$this->token = $token;
}
/**
* Verifies validity of the request.
*
* @return boolean
*/
public function isValid()
{
if ($this->is('post') || $this->is('put') || $this->is('delete') || $this->is('patch')) {
return $this->variables('_token') && ($this->variables('_token') === $this->token);
}
return true;
}
}