1: <?php namespace Laravel;
2:
3: class Request {
4:
5: /**
6: * All of the route instances handling the request.
7: *
8: * @var array
9: */
10: public static $route;
11:
12: /**
13: * The Symfony HttpFoundation Request instance.
14: *
15: * @var HttpFoundation\Request
16: */
17: public static $foundation;
18:
19: /**
20: * The request data key that is used to indicate a spoofed request method.
21: *
22: * @var string
23: */
24: const spoofer = '_method';
25:
26: /**
27: * Get the URI for the current request.
28: *
29: * @return string
30: */
31: public static function uri()
32: {
33: return URI::current();
34: }
35:
36: /**
37: * Get the request method.
38: *
39: * @return string
40: */
41: public static function method()
42: {
43: $method = static::foundation()->getMethod();
44:
45: return ($method == 'HEAD') ? 'GET' : $method;
46: }
47:
48: /**
49: * Get a header from the request.
50: *
51: * <code>
52: * // Get a header from the request
53: * $referer = Request::header('referer');
54: * </code>
55: *
56: * @param string $key
57: * @param mixed $default
58: * @return mixed
59: */
60: public static function header($key, $default = null)
61: {
62: return array_get(static::foundation()->headers->all(), $key, $default);
63: }
64:
65: /**
66: * Get all of the HTTP request headers.
67: *
68: * @return array
69: */
70: public static function headers()
71: {
72: return static::foundation()->headers->all();
73: }
74:
75: /**
76: * Get an item from the $_SERVER array.
77: *
78: * @param string $key
79: * @param mixed $default
80: * @return string
81: */
82: public static function server($key = null, $default = null)
83: {
84: return array_get(static::foundation()->server->all(), strtoupper($key), $default);
85: }
86:
87: /**
88: * Determine if the request method is being spoofed by a hidden Form element.
89: *
90: * @return bool
91: */
92: public static function spoofed()
93: {
94: return ! is_null(static::foundation()->get(Request::spoofer));
95: }
96:
97: /**
98: * Get the requestor's IP address.
99: *
100: * @param mixed $default
101: * @return string
102: */
103: public static function ip($default = '0.0.0.0')
104: {
105: $client_ip = static::foundation()->getClientIp();
106: return $client_ip === NULL ? $default : $client_ip;
107: }
108:
109: /**
110: * Get the list of acceptable content types for the request.
111: *
112: * @return array
113: */
114: public static function accept()
115: {
116: return static::foundation()->getAcceptableContentTypes();
117: }
118:
119: /**
120: * Determine if the request accepts a given content type.
121: *
122: * @param string $type
123: * @return bool
124: */
125: public static function accepts($type)
126: {
127: return in_array($type, static::accept());
128: }
129:
130: /**
131: * Get the languages accepted by the client's browser.
132: *
133: * @return array
134: */
135: public static function languages()
136: {
137: return static::foundation()->getLanguages();
138: }
139:
140: /**
141: * Determine if the current request is using HTTPS.
142: *
143: * @return bool
144: */
145: public static function secure()
146: {
147: return static::foundation()->isSecure() and Config::get('application.ssl');
148: }
149:
150: /**
151: * Determine if the request has been forged.
152: *
153: * The session CSRF token will be compared to the CSRF token in the request input.
154: *
155: * @return bool
156: */
157: public static function forged()
158: {
159: return Input::get(Session::csrf_token) !== Session::token();
160: }
161:
162: /**
163: * Determine if the current request is an AJAX request.
164: *
165: * @return bool
166: */
167: public static function ajax()
168: {
169: return static::foundation()->isXmlHttpRequest();
170: }
171:
172: /**
173: * Get the HTTP referrer for the request.
174: *
175: * @return string
176: */
177: public static function referrer()
178: {
179: return static::foundation()->headers->get('referer');
180: }
181:
182: /**
183: * Get the timestamp of the time when the request was started.
184: *
185: * @return int
186: */
187: public static function time()
188: {
189: return (int) LARAVEL_START;
190: }
191:
192: /**
193: * Determine if the current request is via the command line.
194: *
195: * @return bool
196: */
197: public static function cli()
198: {
199: return defined('STDIN') || (PHP_SAPI != "cgi-fcgi" && substr(PHP_SAPI, 0, 3) == 'cgi' && getenv('TERM'));
200: }
201:
202: /**
203: * Get the Laravel environment for the current request.
204: *
205: * @return string|null
206: */
207: public static function env()
208: {
209: return static::foundation()->server->get('LARAVEL_ENV');
210: }
211:
212: /**
213: * Set the Laravel environment for the current request.
214: *
215: * @param string $env
216: * @return void
217: */
218: public static function set_env($env)
219: {
220: static::foundation()->server->set('LARAVEL_ENV', $env);
221: }
222:
223: /**
224: * Determine the current request environment.
225: *
226: * @param string $env
227: * @return bool
228: */
229: public static function is_env($env)
230: {
231: return static::env() === $env;
232: }
233:
234: /**
235: * Detect the current environment from an environment configuration.
236: *
237: * @param array $environments
238: * @param string $uri
239: * @return string|null
240: */
241: public static function detect_env(array $environments, $uri)
242: {
243: foreach ($environments as $environment => $patterns)
244: {
245: // Essentially we just want to loop through each environment pattern
246: // and determine if the current URI matches the pattern and if so
247: // we will simply return the environment for that URI pattern.
248: foreach ($patterns as $pattern)
249: {
250: if (Str::is($pattern, $uri) or $pattern == gethostname())
251: {
252: return $environment;
253: }
254: }
255: }
256: }
257:
258: /**
259: * Get the main route handling the request.
260: *
261: * @return Route
262: */
263: public static function route()
264: {
265: return static::$route;
266: }
267:
268: /**
269: * Get the Symfony HttpFoundation Request instance.
270: *
271: * @return HttpFoundation\Request
272: */
273: public static function foundation()
274: {
275: return static::$foundation;
276: }
277:
278: /**
279: * Pass any other methods to the Symfony request.
280: *
281: * @param string $method
282: * @param array $parameters
283: * @return mixed
284: */
285: public static function __callStatic($method, $parameters)
286: {
287: return call_user_func_array(array(static::foundation(), $method), $parameters);
288: }
289:
290: }
291: