Files
spa-api/laravel/request.php

217 lines
4.1 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class Request {
2011-08-23 21:04:40 -05:00
/**
* The request instance for the current request.
*
* @var Request
*/
private static $active;
/**
* The $_SERVER array for the request.
*
* @var array
*/
private $server;
/**
* The input instance for the request.
*
* @var Input
*/
public $input;
/**
* The route handling the current request.
*
2011-08-23 21:04:40 -05:00
* @var Routing\Route
*/
2011-08-23 21:04:40 -05:00
public $route;
/**
* The request URI.
*
* @var string
*/
2011-08-23 21:04:40 -05:00
private $uri;
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-23 21:04:40 -05:00
* @param array $server
* @return void
2011-06-08 23:45:08 -05:00
*/
2011-08-23 21:04:40 -05:00
public function __construct($server)
2011-06-08 23:45:08 -05:00
{
2011-08-23 21:04:40 -05:00
$this->server = $server;
2011-08-23 21:04:40 -05:00
static::$active = $this;
}
2011-08-15 22:29:11 -05:00
2011-08-23 21:04:40 -05:00
/**
* Get the request instance for the current request.
*
* @return Request
*/
public static function active()
{
return static::$active;
2011-08-15 22:29:11 -05:00
}
/**
2011-08-23 21:04:40 -05:00
* Get the raw request URI.
2011-08-15 22:29:11 -05:00
*
* @return string
*/
2011-08-23 21:04:40 -05:00
public function uri()
2011-08-15 22:29:11 -05:00
{
2011-08-23 21:04:40 -05:00
if ( ! is_null($this->uri)) return $this->uri;
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-23 21:04:40 -05:00
die('Unable to determine the request URI.');
2011-06-08 23:45:08 -05:00
}
2011-08-23 21:04:40 -05:00
if ($uri === false) die('Malformed request URI. Request terminated.');
return $this->uri = $this->remove_from_uri($uri, array(parse_url(Config::get('application.url'), PHP_URL_PATH), '/index.php'));
}
/**
* Remove an array of values from the beginning from a URI.
*
* @param string $uri
* @param array $values
* @return string
*/
private function remove_from_uri($uri, $values)
{
foreach ($values 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-23 21:04:40 -05:00
2011-08-15 22:29:11 -05:00
return $uri;
2011-07-06 14:57:10 -07:00
}
2011-06-08 23:45:08 -05:00
/**
* Get the request method.
*
2011-08-23 21:04:40 -05:00
* Note: If the request method is being spoofed, the spoofed method will be returned.
*
2011-06-08 23:45:08 -05:00
* @return string
*/
2011-08-23 21:04:40 -05:00
public function method()
2011-06-08 23:45:08 -05:00
{
2011-08-23 21:04:40 -05:00
return ($this->is_spoofed()) ? $_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-23 21:04:40 -05:00
public function is_spoofed()
2011-07-08 12:38:54 -07:00
{
return is_array($_POST) and array_key_exists('REQUEST_METHOD', $_POST);
2011-06-08 23:45:08 -05:00
}
/**
* Get the requestor's IP address.
*
* @return string
*/
2011-08-23 21:04:40 -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-23 21:04:40 -05:00
* Get the HTTP protocol for the request (http or https).
2011-06-08 23:45:08 -05:00
*
* @return string
2011-06-08 23:45:08 -05:00
*/
2011-08-23 21:04:40 -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
}
/**
* Determine if the request is using HTTPS.
2011-06-08 23:45:08 -05:00
*
* @return bool
2011-06-08 23:45:08 -05:00
*/
2011-08-23 21:04:40 -05:00
public function is_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-23 21:04:40 -05:00
public function is_ajax()
2011-06-08 23:45:08 -05:00
{
2011-08-23 21:04:40 -05:00
return (isset($this->server['HTTP_X_REQUESTED_WITH']) and strtolower($this->server['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
2011-06-08 23:45:08 -05:00
}
/**
* Determine if the route handling the request is a given name.
*
* @param string $name
* @return bool
*/
2011-08-23 21:04:40 -05:00
public function route_is($name)
{
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;
}
/**
* Magic Method to handle dynamic static methods.
*/
2011-08-23 21:04:40 -05:00
public function __call($method, $parameters)
{
if (strpos($method, 'route_is_') === 0)
{
2011-08-23 21:04:40 -05:00
return $this->route_is(substr($method, 9));
}
}
/**
* Magic Method for dynamically retrieving properties of the request instance.
*/
public function __get($key)
{
if ($key === 'input')
{
return $this->input->all();
}
}
2011-06-08 23:45:08 -05:00
}