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

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

	/**
6
	 * All of the route instances handling the request.
7
	 *
8
	 * @var array
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
	 * @return string
Taylor Otwell committed
23
	 */
24
	public static function uri()
Taylor Otwell committed
25
	{
Phill Sparks committed
26
		return URI::current();
27 28 29
	}

	/**
30 31 32 33
	 * Get the request method.
	 *
	 * @return string
	 */
34
	public static function method()
35
	{
36 37 38 39 40
		if ($_SERVER['REQUEST_METHOD'] == 'HEAD')
		{
			return 'GET';
		}

41
		return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
42 43 44 45 46 47 48 49 50
	}

	/**
	 * Get an item from the $_SERVER array.
	 *
	 * @param  string  $key
	 * @param  mixed   $default
	 * @return string
	 */
51
	public static function server($key = null, $default = null)
52
	{
53
		return array_get($_SERVER, strtoupper($key), $default);
54 55 56 57 58 59 60
	}

	/**
	 * Determine if the request method is being spoofed by a hidden Form element.
	 *
	 * @return bool
	 */
61
	public static function spoofed()
62
	{
63
		return is_array($_POST) and array_key_exists(Request::spoofer, $_POST);
64 65 66 67 68
	}

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

87
		return value($default);
88 89 90
	}

	/**
91
	 * Get the HTTP protocol for the request.
92
	 *
93
	 * @return string
94
	 */
95
	public static function protocol()
96
	{
97
		return array_get($_SERVER, 'SERVER_PROTOCOL', 'HTTP/1.1');
98 99 100
	}

	/**
101
	 * Determine if the current request is using HTTPS.
102
	 *
103
	 * @return bool
104
	 */
105
	public static function secure()
106
	{
107
		return isset($_SERVER['HTTPS']) and strtolower($_SERVER['HTTPS']) !== 'off';
108 109 110
	}

	/**
Taylor Otwell committed
111 112 113 114 115 116 117 118
	 * 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()
	{
119
		return Input::get(Session::csrf_token) !== Session::token();
Taylor Otwell committed
120 121 122
	}

	/**
123
	 * Determine if the current request is an AJAX request.
124 125 126
	 *
	 * @return bool
	 */
127
	public static function ajax()
128
	{
129
		if ( ! isset($_SERVER['HTTP_X_REQUESTED_WITH'])) return false;
130

131
		return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
132 133
	}

134
	/**
135 136 137 138 139 140 141 142 143 144
	 * Get the HTTP referrer for the request.
	 *
	 * @return string
	 */
	public static function referrer()
	{
		return array_get($_SERVER, 'HTTP_REFERER');
	}

	/**
145 146 147 148 149 150 151 152 153 154
	 * Determine if the current request is via the command line.
	 *
	 * @return bool
	 */
	public static function cli()
	{
		return defined('STDIN');
	}

	/**
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	 * Get the Laravel environment for the current request.
	 *
	 * @return string|null
	 */
	public static function env()
	{
		if (isset($_SERVER['LARAVEL_ENV'])) return $_SERVER['LARAVEL_ENV'];
	}

	/**
	 * Determine the current request environment.
	 *
	 * @param  string  $env
	 * @return bool
	 */
	public static function is_env($env)
	{
		return static::env() === $env;
	}

	/**
176
	 * Get the main route handling the request.
177
	 *
178
	 * @return Route
179
	 */
180
	public static function route()
181
	{
182
		return static::$route;
183
	}
Taylor Otwell committed
184

185
}