request.php 3.13 KB
Newer Older
1 2 3 4
<?php namespace Laravel;

use Closure;
use Laravel\Session\Payload as Session;
5

Taylor Otwell committed
6
class Request {
Taylor Otwell committed
7 8

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

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

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

	/**
37 38
	 * Get the request method.
	 *
39 40 41
	 * 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
42
	 *
43 44
	 * @return string
	 */
45
	public static function method()
46
	{
47
		return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
48 49 50 51 52 53 54 55 56 57 58
	}

	/**
	 * 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
	 */
59
	public static function server($key = null, $default = null)
60
	{
61
		return Arr::get($_SERVER, strtoupper($key), $default);
62 63 64 65 66 67 68
	}

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

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

95
		return ($default instanceof Closure) ? call_user_func($default) : $default;
96 97 98
	}

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

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

	/**
Taylor Otwell committed
119 120 121 122 123 124 125 126
	 * 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()
	{
127
		return Input::get(Session::csrf_token) !== IoC::core('session')->token();
Taylor Otwell committed
128 129 130
	}

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

139
		return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
140 141
	}

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

152
}