request.php 3.25 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 72 73 74 75
	 * Get the request method.
	 *
	 * @return string
	 */
	public static function method()
	{
76 77 78 79 80 81 82 83 84 85 86 87 88
		return (static::spoofed()) ? $_POST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD'];
	}

	/**
	 * Determine if the request method is being spoofed by a hidden Form element.
	 *
	 * Hidden form elements are used to spoof PUT and DELETE requests since
	 * they are not supported by HTML forms.
	 *
	 * @return bool
	 */
	public static function spoofed()
	{
89
		return is_array($_POST) and array_key_exists('REQUEST_METHOD', $_POST);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	}

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

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

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

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

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

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

165
}