2011-08-18 19:56:29 -05:00
|
|
|
<?php namespace Laravel;
|
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-06-18 12:00:52 -05:00
|
|
|
/**
|
2011-08-26 21:42:04 -05:00
|
|
|
* The $_SERVER array for the request.
|
2011-06-18 12:00:52 -05:00
|
|
|
*
|
2011-08-26 21:42:04 -05:00
|
|
|
* @var array
|
2011-06-18 12:00:52 -05:00
|
|
|
*/
|
2011-08-26 21:42:04 -05:00
|
|
|
public $server;
|
2011-06-18 12:00:52 -05:00
|
|
|
|
2011-08-31 00:07:45 -05:00
|
|
|
/**
|
2011-08-31 21:57:37 -05:00
|
|
|
* The route handling the current request.
|
2011-08-31 00:07:45 -05:00
|
|
|
*
|
2011-08-31 21:57:37 -05:00
|
|
|
* @var Routing\Route
|
2011-08-31 00:07:45 -05:00
|
|
|
*/
|
2011-08-31 21:57:37 -05:00
|
|
|
public $route;
|
2011-08-31 00:07:45 -05:00
|
|
|
|
2011-08-04 10:47:47 -05:00
|
|
|
/**
|
2011-08-31 21:57:37 -05:00
|
|
|
* The $_POST array for the request.
|
2011-08-04 10:47:47 -05:00
|
|
|
*
|
2011-08-31 21:57:37 -05:00
|
|
|
* @var array
|
2011-08-04 10:47:47 -05:00
|
|
|
*/
|
2011-08-31 21:57:37 -05:00
|
|
|
private $post;
|
2011-08-04 10:47:47 -05:00
|
|
|
|
2011-08-30 22:09:47 -05:00
|
|
|
/**
|
|
|
|
|
* The base URL of the application.
|
|
|
|
|
*
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $url;
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
/**
|
2011-08-23 21:04:40 -05:00
|
|
|
* Create a new request instance.
|
2011-07-09 22:31:06 -05:00
|
|
|
*
|
2011-08-26 21:42:04 -05:00
|
|
|
* @param array $server
|
2011-08-31 00:07:45 -05:00
|
|
|
* @param array $post
|
2011-08-26 21:42:04 -05:00
|
|
|
* @param string $url
|
2011-08-23 21:04:40 -05:00
|
|
|
* @return void
|
2011-06-08 23:45:08 -05:00
|
|
|
*/
|
2011-08-31 00:07:45 -05:00
|
|
|
public function __construct($server, $post, $url)
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-30 22:09:47 -05:00
|
|
|
$this->url = $url;
|
2011-08-31 00:07:45 -05:00
|
|
|
$this->post = $post;
|
2011-08-23 21:04:40 -05:00
|
|
|
$this->server = $server;
|
2011-08-15 22:29:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-08-26 21:42:04 -05:00
|
|
|
* Determine the request URI.
|
2011-08-15 22:29:11 -05:00
|
|
|
*
|
2011-08-26 21:42:04 -05:00
|
|
|
* The request URI will be trimmed to remove to the application URL and application index file.
|
|
|
|
|
* If the request is to the root of the application, the URI will be set to a forward slash.
|
|
|
|
|
*
|
|
|
|
|
* If the $_SERVER "PATH_INFO" variable is available, it will be used; otherwise, we will try
|
|
|
|
|
* to determine the URI using the REQUEST_URI variable. If neither are available, an exception
|
|
|
|
|
* will be thrown by the method.
|
|
|
|
|
*
|
2011-08-15 22:29:11 -05:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-08-30 22:09:47 -05:00
|
|
|
public function uri()
|
2011-08-15 22:29:11 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
if (isset($this->server['PATH_INFO']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
$uri = $this->server['PATH_INFO'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-08-23 21:04:40 -05:00
|
|
|
elseif (isset($this->server['REQUEST_URI']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
$uri = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-06-14 17:27:11 -05:00
|
|
|
else
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-24 22:51:32 -05:00
|
|
|
throw new \Exception('Unable to determine the request URI.');
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
2011-08-24 22:51:32 -05:00
|
|
|
if ($uri === false) throw new \Exception('Malformed request URI. Request terminated.');
|
2011-08-23 21:04:40 -05:00
|
|
|
|
2011-08-30 22:09:47 -05:00
|
|
|
foreach (array(parse_url($this->url, PHP_URL_PATH), '/index.php') as $value)
|
2011-06-16 07:22:49 -07:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
$uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
|
2011-06-16 07:22:49 -07:00
|
|
|
}
|
2011-08-26 21:42:04 -05:00
|
|
|
|
|
|
|
|
return (($uri = trim($uri, '/')) == '') ? '/' : $uri;
|
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-08-30 22:09:47 -05:00
|
|
|
public function method()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-31 00:07:45 -05:00
|
|
|
return ($this->spoofed()) ? $this->post['REQUEST_METHOD'] : $this->server['REQUEST_METHOD'];
|
2011-07-08 12:38:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the request method is being spoofed by a hidden Form element.
|
|
|
|
|
*
|
2011-08-23 21:04:40 -05:00
|
|
|
* Hidden elements are used to spoof PUT and DELETE requests since they are not supported by HTML forms.
|
2011-07-08 12:38:54 -07:00
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-08-30 22:09:47 -05:00
|
|
|
public function spoofed()
|
2011-07-08 12:38:54 -07:00
|
|
|
{
|
2011-08-31 00:07:45 -05:00
|
|
|
return is_array($this->post) and array_key_exists('REQUEST_METHOD', $this->post);
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the requestor's IP address.
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-08-30 22:09:47 -05:00
|
|
|
public function ip()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
if (isset($this->server['HTTP_X_FORWARDED_FOR']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
return $this->server['HTTP_X_FORWARDED_FOR'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-08-23 21:04:40 -05:00
|
|
|
elseif (isset($this->server['HTTP_CLIENT_IP']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
return $this->server['HTTP_CLIENT_IP'];
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
2011-08-23 21:04:40 -05:00
|
|
|
elseif (isset($this->server['REMOTE_ADDR']))
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
return $this->server['REMOTE_ADDR'];
|
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-08-30 22:09:47 -05:00
|
|
|
public function protocol()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
return (isset($this->server['HTTPS']) and $this->server['HTTPS'] !== 'off') ? 'https' : 'http';
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-25 20:20:09 -05:00
|
|
|
* Determine if the 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-08-30 22:09:47 -05:00
|
|
|
public function secure()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
return ($this->protocol() == 'https');
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine if the request is an AJAX request.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-08-30 22:09:47 -05:00
|
|
|
public function ajax()
|
2011-06-08 23:45:08 -05:00
|
|
|
{
|
2011-08-26 21:42:04 -05:00
|
|
|
if ( ! isset($this->server['HTTP_X_REQUESTED_WITH'])) return false;
|
|
|
|
|
|
|
|
|
|
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-08-24 22:51:32 -05:00
|
|
|
* Determine if the route handling the request has a given name.
|
|
|
|
|
*
|
|
|
|
|
* <code>
|
|
|
|
|
* // Determine if the route handling the request is named "profile"
|
|
|
|
|
* $profile = Request::active()->route_is('profile');
|
|
|
|
|
* </code>
|
2011-06-19 22:49:01 -05:00
|
|
|
*
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2011-08-23 21:04:40 -05:00
|
|
|
public function route_is($name)
|
2011-06-19 22:49:01 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
if (is_null($this->route) or ! is_array($this->route->callback) or ! isset($this->route->callback['name'])) return false;
|
|
|
|
|
|
|
|
|
|
return $this->route->callback['name'] === $name;
|
2011-06-19 22:49:01 -05:00
|
|
|
}
|
|
|
|
|
|
2011-06-18 12:00:52 -05:00
|
|
|
/**
|
2011-08-24 22:51:32 -05:00
|
|
|
* Magic Method to handle dynamic method calls to determine the route handling the request.
|
|
|
|
|
*
|
|
|
|
|
* <code>
|
|
|
|
|
* // Determine if the route handling the request is named "profile"
|
|
|
|
|
* $profile = Request::active()->route_is_profile();
|
|
|
|
|
* </code>
|
2011-06-18 12:00:52 -05:00
|
|
|
*/
|
2011-08-23 21:04:40 -05:00
|
|
|
public function __call($method, $parameters)
|
2011-06-18 12:00:52 -05:00
|
|
|
{
|
2011-06-19 22:49:01 -05:00
|
|
|
if (strpos($method, 'route_is_') === 0)
|
2011-06-18 12:00:52 -05:00
|
|
|
{
|
2011-08-23 21:04:40 -05:00
|
|
|
return $this->route_is(substr($method, 9));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-08 23:45:08 -05:00
|
|
|
}
|