2011-10-04 21:43:39 -05:00
|
|
|
<?php namespace Laravel; use Closure;
|
2011-06-08 23:45:08 -05:00
|
|
|
|
2011-08-30 22:35:32 -05:00
|
|
|
class Request {
|
2011-08-23 21:04:40 -05:00
|
|
|
|
2011-10-29 22:24:32 -05:00
|
|
|
/**
|
|
|
|
|
* The route handling the current request.
|
|
|
|
|
*
|
|
|
|
|
* @var Routing\Route
|
|
|
|
|
*/
|
|
|
|
|
public $route;
|
|
|
|
|
|
2011-08-30 22:09:47 -05:00
|
|
|
/**
|
2011-10-24 21:00:51 -05:00
|
|
|
* The request URI for the current request.
|
2011-08-30 22:09:47 -05:00
|
|
|
*
|
2011-10-24 21:00:51 -05:00
|
|
|
* @var URI
|
2011-08-30 22:09:47 -05:00
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
protected $uri;
|
2011-09-21 21:46:16 -05:00
|
|
|
|
2011-10-10 21:34:15 -05:00
|
|
|
/**
|
2011-10-29 22:24:32 -05:00
|
|
|
* The $_POST array for the request.
|
2011-10-10 21:34:15 -05:00
|
|
|
*
|
2011-10-29 22:24:32 -05:00
|
|
|
* @var array
|
2011-10-10 21:34:15 -05:00
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
protected $post;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The $_SERVER array for the request.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $server;
|
2011-10-10 21:34:15 -05:00
|
|
|
|
2011-09-21 21:46:16 -05:00
|
|
|
/**
|
|
|
|
|
* The request data key that is used to indicate a spoofed request method.
|
2011-09-02 19:36:19 -05:00
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2011-09-20 21:28:19 -05:00
|
|
|
const spoofer = '__spoofer';
|
2011-09-16 19:59:20 -05:00
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2011-10-29 22:24:32 -05:00
|
|
|
* Create a new Request instance.
|
2011-08-26 21:42:04 -05:00
|
|
|
*
|
2011-10-29 22:24:32 -05:00
|
|
|
* @param URI $uri
|
|
|
|
|
* @param array $post
|
|
|
|
|
* @param array $server
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($uri, $post, $server)
|
|
|
|
|
{
|
|
|
|
|
$this->uri = $uri;
|
|
|
|
|
$this->post = $post;
|
|
|
|
|
$this->server = $server;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current request's URI.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2011-08-15 22:29:11 -05:00
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function uri()
|
2011-08-15 22:29:11 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return $this->uri->get();
|
2011-09-02 19:36:19 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Get the request method.
|
|
|
|
|
*
|
2011-10-24 21:00:51 -05:00
|
|
|
* This will usually be the value of the REQUEST_METHOD $_SERVER variable
|
|
|
|
|
* However, when the request method is spoofed using a hidden form value,
|
|
|
|
|
* the method will be stored in the $_POST array.
|
2011-08-23 21:04:40 -05:00
|
|
|
*
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function method()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return ($this->spoofed()) ? $this->post[Request::spoofer] : $this->server['REQUEST_METHOD'];
|
2011-09-20 21:28:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an item from the $_SERVER array.
|
|
|
|
|
*
|
|
|
|
|
* Like most array retrieval methods, a default value may be specified.
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $default
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function server($key = null, $default = null)
|
2011-09-20 21:28:19 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return Arr::get($this->server, strtoupper($key), $default);
|
2011-07-08 12:38:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the request method is being spoofed by a hidden Form element.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function spoofed()
|
2011-07-08 12:38:54 -07:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return is_array($this->post) and array_key_exists(Request::spoofer, $this->post);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the requestor's IP address.
|
|
|
|
|
*
|
2011-09-20 21:28:19 -05:00
|
|
|
* @param mixed $default
|
2011-06-08 23:45:08 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function ip($default = '0.0.0.0')
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
if (isset($this->server['HTTP_X_FORWARDED_FOR']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return $this->server['HTTP_X_FORWARDED_FOR'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-10-29 22:24:32 -05:00
|
|
|
elseif (isset($this->server['HTTP_CLIENT_IP']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return $this->server['HTTP_CLIENT_IP'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-10-29 22:24:32 -05:00
|
|
|
elseif (isset($this->server['REMOTE_ADDR']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return $this->server['REMOTE_ADDR'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-09-20 21:28:19 -05:00
|
|
|
|
2011-10-04 21:43:39 -05:00
|
|
|
return ($default instanceof Closure) ? call_user_func($default) : $default;
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-08-26 21:42:04 -05:00
|
|
|
* Get the HTTP protocol for the request.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-06-25 20:20:09 -05:00
|
|
|
* @return string
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function protocol()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return Arr::get($this->server, 'SERVER_PROTOCOL', 'HTTP/1.1');
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-20 21:28:19 -05:00
|
|
|
* Determine if the current request is using HTTPS.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
2011-06-25 20:20:09 -05:00
|
|
|
* @return bool
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function secure()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return isset($this->server['HTTPS']) and strtolower($this->server['HTTPS']) !== 'off';
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-09-20 21:28:19 -05:00
|
|
|
* Determine if the current request is an AJAX request.
|
2011-06-08 23:45:08 -05:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function ajax()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
if ( ! isset($this->server['HTTP_X_REQUESTED_WITH'])) return false;
|
2011-08-26 21:42:04 -05:00
|
|
|
|
2011-10-29 22:24:32 -05:00
|
|
|
return strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-19 22:49:01 -05:00
|
|
|
/**
|
2011-09-06 22:04:52 -05:00
|
|
|
* Get the route handling the current request.
|
2011-08-24 22:51:32 -05:00
|
|
|
*
|
2011-09-06 22:04:52 -05:00
|
|
|
* @return Route
|
2011-06-18 12:00:52 -05:00
|
|
|
*/
|
2011-10-29 22:24:32 -05:00
|
|
|
public function route()
|
2011-10-13 21:32:11 -05:00
|
|
|
{
|
2011-10-29 22:24:32 -05:00
|
|
|
return $this->route;
|
2011-10-13 21:32:11 -05:00
|
|
|
}
|
2011-08-23 21:04:40 -05:00
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|