request.php 3.51 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 38 39 40 41 42
			$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

			if ($uri === false)
			{
				throw new \Exception("Malformed request URI. Request terminated.");
			}
43
		}
44
		else
45
		{
46
			throw new \Exception('Unable to determine the request URI.');
47 48
		}

Taylor Otwell committed
49
		// -------------------------------------------------------
50
		// Remove the application URL.
Taylor Otwell committed
51 52 53 54 55 56 57 58 59
		// -------------------------------------------------------
		$base_url = parse_url(Config::get('application.url'), PHP_URL_PATH);

		if (strpos($uri, $base_url) === 0)
		{
			$uri = (string) substr($uri, strlen($base_url));
		}

		// -------------------------------------------------------
60
		// Remove the application index and any extra slashes.
Taylor Otwell committed
61
		// -------------------------------------------------------
62 63
		$index = Config::get('application.index');

64
		if (strpos($uri, '/'.$index) === 0)
65
		{
66
			$uri = (string) substr($uri, strlen('/'.$index));
67
		}
68

69 70
		$uri = trim($uri, '/');

71 72 73 74
		// -------------------------------------------------------
		// If the requests is to the root of the application, we
		// always return a single forward slash.
		// -------------------------------------------------------
75
		return ($uri == '') ? '/' : strtolower($uri);
76 77 78 79 80
	}

	/**
	 * Get the request method.
	 *
81 82 83
	 * The request method may be spoofed if a hidden "REQUEST_METHOD" POST element 
	 * is present, allowing HTML forms to simulate PUT and DELETE requests.
	 *
84 85 86 87
	 * @return string
	 */
	public static function method()
	{
88
		return (array_key_exists('REQUEST_METHOD', $_POST)) ? $_POST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	}

	/**
	 * 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'];
		}
	}

	/**
113
	 * Get the HTTP protocol for the request.
114
	 *
115
	 * @return string
116
	 */
117
	public static function protocol()
118
	{
119
		return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
120 121 122
	}

	/**
123
	 * Determine if the request is using HTTPS.
124
	 *
125
	 * @return bool
126
	 */
127
	public static function is_secure()
128
	{
129
		return (static::protocol() == 'https');
130 131 132 133 134 135 136
	}

	/**
	 * Determine if the request is an AJAX request.
	 *
	 * @return bool
	 */
137
	public static function is_ajax()
138
	{
139
		return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
140 141
	}

142
	/**
143 144 145 146 147 148 149 150 151 152 153
	 * 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);
	}

	/**
154 155 156 157
	 * Magic Method to handle dynamic static methods.
	 */
	public static function __callStatic($method, $parameters)
	{
158
		if (strpos($method, 'route_is_') === 0)
159
		{
160
			return static::route_is(substr($method, 9));
161 162 163
		}
	}

164
}