request.php 3.23 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2

Taylor Otwell committed
3
class Request {
Taylor Otwell committed
4 5

	/**
6
	 * The route handling the current request.
7
	 *
8
	 * @var Routing\Route
9
	 */
10
	public static $route;
Taylor Otwell committed
11 12

	/**
Taylor Otwell committed
13
	 * The request data key that is used to indicate a spoofed request method.
14 15 16
	 *
	 * @var string
	 */
17
	const spoofer = '__spoofer';
18 19

	/**
Taylor Otwell committed
20
	 * Get the URI for the current request.
21
	 *
22
	 * If the request is to the root of the application, a single forward slash
23 24
	 * will be returned. Otherwise, the URI will be returned with all of the
	 * leading and trailing slashes removed.
25
	 *
26
	 * @return string
Taylor Otwell committed
27
	 */
28
	public static function uri()
Taylor Otwell committed
29
	{
Phill Sparks committed
30
		return URI::current();
31 32 33
	}

	/**
34 35
	 * Get the request method.
	 *
36 37 38
	 * This will usually be the value of the REQUEST_METHOD $_SERVER variable
	 * However, when the request method is spoofed using a hidden form value,
	 * the method will be stored in the $_POST array.
Taylor Otwell committed
39
	 *
40 41
	 * @return string
	 */
42
	public static function method()
43
	{
44
		return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
45 46 47 48 49 50 51 52 53 54 55
	}

	/**
	 * Get an item from the $_SERVER array.
	 *
	 * Like most array retrieval methods, a default value may be specified.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return string
	 */
56
	public static function server($key = null, $default = null)
57
	{
58
		return Arr::get($_SERVER, strtoupper($key), $default);
59 60 61 62 63 64 65
	}

	/**
	 * Determine if the request method is being spoofed by a hidden Form element.
	 *
	 * @return bool
	 */
66
	public static function spoofed()
67
	{
68
		return is_array($_POST) and array_key_exists(Request::spoofer, $_POST);
69 70 71 72 73
	}

	/**
	 * Get the requestor's IP address.
	 *
74
	 * @param  mixed   $default
75 76
	 * @return string
	 */
77
	public static function ip($default = '0.0.0.0')
78
	{
79
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
80
		{
81
			return $_SERVER['HTTP_X_FORWARDED_FOR'];
82
		}
83
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))
84
		{
85
			return $_SERVER['HTTP_CLIENT_IP'];
86
		}
87
		elseif (isset($_SERVER['REMOTE_ADDR']))
88
		{
89
			return $_SERVER['REMOTE_ADDR'];
90
		}
91

92
		return ($default instanceof Closure) ? call_user_func($default) : $default;
93 94 95
	}

	/**
96
	 * Get the HTTP protocol for the request.
97
	 *
98
	 * @return string
99
	 */
100
	public static function protocol()
101
	{
102
		return Arr::get($_SERVER, 'SERVER_PROTOCOL', 'HTTP/1.1');
103 104 105
	}

	/**
106
	 * Determine if the current request is using HTTPS.
107
	 *
108
	 * @return bool
109
	 */
110
	public static function secure()
111
	{
112
		return isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off';
113 114 115
	}

	/**
Taylor Otwell committed
116 117 118 119 120 121 122 123
	 * Determine if the request has been forged.
	 *
	 * The session CSRF token will be compared to the CSRF token in the request input.
	 *
	 * @return bool
	 */
	public static function forged()
	{
124 125 126 127 128
		if (Config::$items['session']['driver'] == '')
		{
			throw new \LogicException("A session driver must be specified to use the CSRF filter.");
		}

129
		return Input::get('csrf_token') !== IoC::core('session')->token();
Taylor Otwell committed
130 131 132
	}

	/**
133
	 * Determine if the current request is an AJAX request.
134 135 136
	 *
	 * @return bool
	 */
137
	public static function ajax()
138
	{
139
		if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false;
140

141
		return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
142 143
	}

144
	/**
145
	 * Get the route handling the current request.
146
	 *
147
	 * @return Route
148
	 */
149
	public static function route()
150
	{
151
		return static::$route;
152
	}
Taylor Otwell committed
153

154
}