request.php 3.09 KB
Newer Older
1 2 3 4 5
<?php namespace System;

class Request {

	/**
6 7 8 9
	 * The request URI.
	 *
	 * @var string
	 */
Taylor Otwell committed
10
	public static $uri;
11 12

	/**
13 14 15 16 17 18 19
	 * The route handling the current request.
	 *
	 * @var Route
	 */
	public static $route;

	/**
20 21 22 23 24 25
	 * Get the request URI.
	 *
	 * @return string
	 */
	public static function uri()
	{
26 27 28 29 30
		if ( ! is_null(static::$uri))
		{
			return static::$uri;
		}

31 32
		if (isset($_SERVER['PATH_INFO']))
		{
33
			$uri = $_SERVER['PATH_INFO'];
34
		}
35
		elseif (isset($_SERVER['REQUEST_URI']))
36
		{
37
			$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
38
		}
39
		else
40
		{
41
			throw new \Exception('Unable to determine the request URI.');
42 43
		}

44
		if ($uri === false)
Taylor Otwell committed
45
		{
46
			throw new \Exception("Malformed request URI. Request terminated.");
Taylor Otwell committed
47 48
		}

49 50
		$uri = static::remove_from_uri($uri, parse_url(Config::get('application.url'), PHP_URL_PATH));
		$uri = static::remove_from_uri($uri, '/'.Config::get('application.index'));
51

52 53
		$uri = trim($uri, '/');

54
		return ($uri == '') ? '/' : strtolower($uri);
55 56 57
	}

	/**
58 59 60 61 62 63 64 65
	 * Remove a string from the beginning of a URI.
	 *
	 * @param  string  $uri
	 * @param  string  $value
	 * @return string
	 */
	private static function remove_from_uri($uri, $value)
	{
66
		return (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri; 
67 68 69
	}

	/**
70 71
	 * Get the request method.
	 *
72 73 74
	 * The request method may be spoofed if a hidden "REQUEST_METHOD" POST element 
	 * is present, allowing HTML forms to simulate PUT and DELETE requests.
	 *
75 76 77 78
	 * @return string
	 */
	public static function method()
	{
79
		return (array_key_exists('REQUEST_METHOD', $_POST)) ? $_POST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	}

	/**
	 * Get the requestor's IP address.
	 *
	 * @return string
	 */
	public static function ip()
	{
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			return $_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))
		{
			return $_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (isset($_SERVER['REMOTE_ADDR']))
		{
			return $_SERVER['REMOTE_ADDR'];
		}
	}

	/**
104
	 * Get the HTTP protocol for the request.
105
	 *
106
	 * @return string
107
	 */
108
	public static function protocol()
109
	{
110
		return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
111 112 113
	}

	/**
114
	 * Determine if the request is using HTTPS.
115
	 *
116
	 * @return bool
117
	 */
118
	public static function is_secure()
119
	{
120
		return (static::protocol() == 'https');
121 122 123 124 125 126 127
	}

	/**
	 * Determine if the request is an AJAX request.
	 *
	 * @return bool
	 */
128
	public static function is_ajax()
129
	{
130
		return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
131 132
	}

133
	/**
134 135 136 137 138 139 140 141 142 143 144
	 * Determine if the route handling the request is a given name.
	 *
	 * @param  string  $name
	 * @return bool
	 */
	public static function route_is($name)
	{
		return (is_array(static::$route->callback) and isset(static::$route->callback['name']) and  static::$route->callback['name'] === $name);
	}

	/**
145 146 147 148
	 * Magic Method to handle dynamic static methods.
	 */
	public static function __callStatic($method, $parameters)
	{
149
		if (strpos($method, 'route_is_') === 0)
150
		{
151
			return static::route_is(substr($method, 9));
152 153 154
		}
	}

155
}