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

use Closure;
4

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

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

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

	/**
Taylor Otwell committed
22
	 * Get the URI for the current request.
23
	 *
24
	 * @return string
Taylor Otwell committed
25
	 */
26
	public static function uri()
Taylor Otwell committed
27
	{
Phill Sparks committed
28
		return URI::current();
29 30 31
	}

	/**
32 33 34 35
	 * Get the request method.
	 *
	 * @return string
	 */
36
	public static function method()
37
	{
38
		return (static::spoofed()) ? $_POST[Request::spoofer] : $_SERVER['REQUEST_METHOD'];
39 40 41 42 43 44 45 46 47
	}

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

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

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

84
		return value($default);
85 86 87
	}

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

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

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

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

128
		return strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
129 130
	}

131
	/**
132 133 134 135 136 137 138 139 140 141
	 * Determine if the current request is via the command line.
	 *
	 * @return bool
	 */
	public static function cli()
	{
		return defined('STDIN');
	}

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

151
}