request.php 3.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php namespace System;

class Request {

	/**
	 * Get the request URI.
	 *
	 * @return string
	 */
	public static function uri()
	{
12 13 14
		// -------------------------------------------------------
		// If the PATH_INFO is available, use it.
		// -------------------------------------------------------
15 16
		if (isset($_SERVER['PATH_INFO']))
		{
17
			$uri = $_SERVER['PATH_INFO'];
18
		}
19 20 21 22
		// -------------------------------------------------------
		// No PATH_INFO? Let's try REQUEST_URI.
		// -------------------------------------------------------
		elseif (isset($_SERVER['REQUEST_URI']))
23
		{
24 25 26 27 28 29
			$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

			if ($uri === false)
			{
				throw new \Exception("Malformed request URI. Request terminated.");
			}
30
		}
31 32 33 34
		// -------------------------------------------------------
		// Neither PATH_INFO or REQUEST_URI are available.
		// -------------------------------------------------------
		else
35
		{
36
			throw new \Exception('Unable to determine the request URI.');
37 38
		}

Taylor Otwell committed
39 40 41 42 43 44 45 46 47 48 49
		// -------------------------------------------------------
		// Remove the application URL.
		// -------------------------------------------------------
		$base_url = parse_url(Config::get('application.url'), PHP_URL_PATH);

		if (strpos($uri, $base_url) === 0)
		{
			$uri = (string) substr($uri, strlen($base_url));
		}

		// -------------------------------------------------------
50
		// Remove the application index and any extra slashes.
Taylor Otwell committed
51
		// -------------------------------------------------------
52
		$uri = trim(str_replace('/index.php', '', $uri), '/');
53

54 55 56 57
		// -------------------------------------------------------
		// If the requests is to the root of the application, we
		// always return a single forward slash.
		// -------------------------------------------------------
58
		return ($uri == '') ? '/' : Str::lower($uri);
59 60 61 62 63 64 65 66 67 68
	}

	/**
	 * Get the request method.
	 *
	 * @return string
	 */
	public static function method()
	{
		// --------------------------------------------------------------
69 70
		// The method can be spoofed using a POST variable, allowing HTML
		// forms to simulate PUT and DELETE requests.
71
		// --------------------------------------------------------------
72
		return Arr::get($_POST, 'REQUEST_METHOD', $_SERVER['REQUEST_METHOD']);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	}

	/**
	 * Get the requestor's IP address.
	 *
	 * @return string
	 */
	public static function ip()
	{
		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			return $_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))
		{
			return $_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (isset($_SERVER['REMOTE_ADDR']))
		{
			return $_SERVER['REMOTE_ADDR'];
		}
	}

	/**
	 * Determine if the request is using HTTPS.
	 *
	 * @return bool
	 */
	public static function is_secure()
	{
		return (static::protocol() == 'https');
	}

	/**
	 * Get the HTTP protocol for the request.
	 *
	 * @return string
	 */
	public static function protocol()
	{
		return (isset($_SERVER['HTTPS']) and $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
	}

	/**
	 * Determine if the request is an AJAX request.
	 *
	 * @return bool
	 */
	public static function is_ajax()
	{
		return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and Str::lower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest');
	}

}