request.php 4.12 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
		// Use the PATH_INFO variable if it is available.
33
		// -------------------------------------------------------
34 35
		if (isset($_SERVER['PATH_INFO']))
		{
36
			$uri = $_SERVER['PATH_INFO'];
37
		}
38 39 40 41
		// -------------------------------------------------------
		// No PATH_INFO? Let's try REQUEST_URI.
		// -------------------------------------------------------
		elseif (isset($_SERVER['REQUEST_URI']))
42
		{
43 44 45 46 47 48
			$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

			if ($uri === false)
			{
				throw new \Exception("Malformed request URI. Request terminated.");
			}
49
		}
50
		else
51
		{
52
			throw new \Exception('Unable to determine the request URI.');
53 54
		}

Taylor Otwell committed
55
		// -------------------------------------------------------
56
		// Remove the application URL.
Taylor Otwell committed
57 58 59 60 61 62 63 64 65
		// -------------------------------------------------------
		$base_url = parse_url(Config::get('application.url'), PHP_URL_PATH);

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

		// -------------------------------------------------------
66
		// Remove the application index and any extra slashes.
Taylor Otwell committed
67
		// -------------------------------------------------------
68 69
		$index = Config::get('application.index');

70
		if (strpos($uri, '/'.$index) === 0)
71
		{
72
			$uri = (string) substr($uri, strlen('/'.$index));
73
		}
74

75 76
		$uri = trim($uri, '/');

77 78 79 80
		// -------------------------------------------------------
		// If the requests is to the root of the application, we
		// always return a single forward slash.
		// -------------------------------------------------------
81
		return ($uri == '') ? '/' : strtolower($uri);
82 83 84 85 86 87 88 89 90 91
	}

	/**
	 * Get the request method.
	 *
	 * @return string
	 */
	public static function method()
	{
		// --------------------------------------------------------------
92 93
		// The method can be spoofed using a POST variable, allowing HTML
		// forms to simulate PUT and DELETE requests.
94
		// --------------------------------------------------------------
95
		return Arr::get($_POST, 'REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
	}

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

	/**
120
	 * Get the HTTP protocol for the request.
121
	 *
122
	 * @return string
123
	 */
124
	public static function protocol()
125
	{
126
		return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
127 128 129
	}

	/**
130
	 * Determine if the request is using HTTPS.
131
	 *
132
	 * @return bool
133
	 */
134
	public static function is_secure()
135
	{
136
		return (static::protocol() == 'https');
137 138 139 140 141 142 143
	}

	/**
	 * Determine if the request is an AJAX request.
	 *
	 * @return bool
	 */
144
	public static function is_ajax()
145
	{
146
		return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
147 148
	}

149
	/**
150 151 152 153 154 155 156 157 158 159 160
	 * 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);
	}

	/**
161 162 163 164 165 166 167 168 169
	 * Magic Method to handle dynamic static methods.
	 */
	public static function __callStatic($method, $parameters)
	{
		// --------------------------------------------------------------
		// Dynamically call the "is" method using the given name.
		//
		// Example: Request::is_login()
		// --------------------------------------------------------------
170
		if (strpos($method, 'route_is_') === 0)
171
		{
172
			return static::route_is(substr($method, 9));
173 174 175
		}
	}

176
}