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-08-30 22:09:47 -05:00
|
|
|
/**
|
2011-09-20 21:28:19 -05:00
|
|
|
* The route handling the current request.
|
2011-08-30 22:09:47 -05:00
|
|
|
*
|
2011-09-20 21:28:19 -05:00
|
|
|
* @var Routing\Route
|
2011-08-30 22:09:47 -05:00
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static $route;
|
2011-09-21 21:46:16 -05:00
|
|
|
|
2011-10-10 21:34:15 -05:00
|
|
|
/**
|
|
|
|
|
* The request URI for the current request.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
2011-10-10 22:42:43 -05:00
|
|
|
public static $uri;
|
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-09-20 23:14:09 -05:00
|
|
|
* Get the URI for the current request.
|
2011-08-26 21:42:04 -05:00
|
|
|
*
|
2011-09-20 23:14:09 -05:00
|
|
|
* Note: This method is the equivalent of calling the URI::get method.
|
2011-08-26 21:42:04 -05:00
|
|
|
*
|
2011-08-15 22:29:11 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function uri()
|
2011-08-15 22:29:11 -05:00
|
|
|
{
|
2011-10-10 21:34:15 -05:00
|
|
|
if ( ! is_null(static::$uri)) return static::$uri;
|
|
|
|
|
|
|
|
|
|
// Sniff the request URI out of the $_SERVER array. The PATH_IFNO
|
|
|
|
|
// variable contains the URI without the base URL or index page,
|
|
|
|
|
// so we will use that if possible, otherwise we will parse the
|
|
|
|
|
// URI out of the REQUEST_URI element.
|
|
|
|
|
if (isset($_SERVER['PATH_INFO']))
|
|
|
|
|
{
|
|
|
|
|
$uri = $_SERVER['PATH_INFO'];
|
|
|
|
|
}
|
2011-10-10 22:31:59 -05:00
|
|
|
elseif (isset($_SERVER['REQUEST_URI']))
|
2011-10-10 21:34:15 -05:00
|
|
|
{
|
|
|
|
|
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
|
|
|
|
|
|
|
|
|
if ($uri === false)
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("Invalid request URI. Request terminated.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new \Exception("Unable to determine the request URI.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the application URL and the application index page from
|
|
|
|
|
// the request URI. Both of these elements will interfere with the
|
|
|
|
|
// routing engine and are extraneous, so they will be removed.
|
|
|
|
|
$base = parse_url(Config::$items['application']['url'], PHP_URL_PATH);
|
|
|
|
|
|
|
|
|
|
if (strpos($uri, $base) === 0)
|
|
|
|
|
{
|
|
|
|
|
$uri = substr($uri, strlen($base));
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-11 21:41:57 -05:00
|
|
|
$index = '/'.Config::$items['application']['index'];
|
|
|
|
|
|
2011-10-11 21:44:57 -05:00
|
|
|
if (trim($index) !== '/' and strpos($uri, $index) === 0)
|
2011-10-10 21:34:15 -05:00
|
|
|
{
|
2011-10-11 21:41:57 -05:00
|
|
|
$uri = substr($uri, strlen($index));
|
2011-10-10 21:34:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Request URIs to the root of the application will be returned
|
|
|
|
|
// as a single forward slash. Otherwise, the request URI will be
|
|
|
|
|
// returned without leading or trailing slashes.
|
|
|
|
|
return static::$uri = (($uri = trim($uri, '/')) == '') ? '/' : $uri;
|
2011-09-02 19:36:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the request format.
|
|
|
|
|
*
|
|
|
|
|
* The format is determined by essentially taking the "extension" of the URI.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function format()
|
2011-09-02 19:36:19 -05:00
|
|
|
{
|
2011-10-10 21:34:15 -05:00
|
|
|
return (($extension = pathinfo(static::uri(), PATHINFO_EXTENSION)) !== '') ? $extension : 'html';
|
2011-07-06 14:57:10 -07:00
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
|
|
|
|
* Get the request method.
|
|
|
|
|
*
|
2011-08-26 21:42:04 -05:00
|
|
|
* Typically, this will be the value of the REQUEST_METHOD $_SERVER variable.
|
|
|
|
|
* However, when the request is being spoofed by a hidden form value, the request
|
|
|
|
|
* 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-05 18:32:48 -05:00
|
|
|
public static function method()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return (static::spoofed()) ? $_POST[Request::spoofer] : $_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-05 18:32:48 -05:00
|
|
|
public static function server($key = null, $default = null)
|
2011-09-20 21:28:19 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return Arr::get($_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.
|
|
|
|
|
*
|
2011-09-20 21:28:19 -05:00
|
|
|
* Hidden elements are used to spoof PUT and DELETE requests since they are not supported
|
|
|
|
|
* by HTML forms. If the request is being spoofed, Laravel considers the spoofed request
|
|
|
|
|
* method the actual request method throughout the framework.
|
2011-07-08 12:38:54 -07:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-10-05 18:32:48 -05:00
|
|
|
public static function spoofed()
|
2011-07-08 12:38:54 -07:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return is_array($_POST) and array_key_exists(Request::spoofer, $_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-05 18:32:48 -05:00
|
|
|
public static function ip($default = '0.0.0.0')
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return $_SERVER['HTTP_X_FORWARDED_FOR'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-10-05 18:32:48 -05:00
|
|
|
elseif (isset($_SERVER['HTTP_CLIENT_IP']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return $_SERVER['HTTP_CLIENT_IP'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-10-05 18:32:48 -05:00
|
|
|
elseif (isset($_SERVER['REMOTE_ADDR']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
return $_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-05 18:32:48 -05:00
|
|
|
public static function protocol()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-18 20:19:36 -05:00
|
|
|
return Arr::get($_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-05 18:32:48 -05:00
|
|
|
public static function secure()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-18 20:19:36 -05:00
|
|
|
return isset($_SERVER['HTTPS']) and strtolower($_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-05 18:32:48 -05:00
|
|
|
public static function ajax()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-10-05 18:32:48 -05:00
|
|
|
if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false;
|
2011-08-26 21:42:04 -05:00
|
|
|
|
2011-10-05 18:32:48 -05:00
|
|
|
return strtolower($_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-13 21:32:11 -05:00
|
|
|
public static function route()
|
|
|
|
|
{
|
|
|
|
|
return static::$route;
|
|
|
|
|
}
|
2011-08-23 21:04:40 -05:00
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|