. */ /** * This file contains the Request class. * * @author Dinu Florin * @package Core */ /** * Request class. * * @author Dinu Florin * @package Core */ class Request extends Pt { private static $headers = null; //Holds the request headers. private static $method = null; //Holds the request method. private static $protocol = null; //The protocol version. /** * Get the base URL of the website, eg: http://example.com or https://example.com:8080. * * @note Only works with HTTP and HTTPS. * @return string */ public static function getBaseURL() { static $base = null; if(is_null($base)) { //Start building the URL //First check the protocol if(!empty($_SERVER['HTTPS'])) { $$base = 'https://'; } else { $base = 'http://'; } //The server name, eg: example.com $base .= $_SERVER['SERVER_NAME']; //The port if(!empty($_SERVER['HTTP_PORT']) && $_SERVER['HTTP_PORT'] != '80') { $base .= ":{$_SERVER['HTTP_PORT']}"; } } return $base; } /** * Get the client's IP address. * * @return string The IP address (xxx.yyy.zzz.qqq) */ public static function getClientIP() { if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif(isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } else { $ip = $_SERVER['REMOTE_ADDR']; } $ip = str_replace(',', '.', $ip); $txt = explode('.', $ip, 15); $ip = "{$txt[0]}.{$txt[1]}.{$txt[2]}.{$txt[3]}"; return $ip; } /** * Get the HTTP protocol version. * * @return string */ public static function getProtocol() { if(is_null(self::$protocol)) { switch($_SERVER['SERVER_PROTOCOL']) { case 'HTTP/1.0': self::$protocol = HTTP::HTTP1_0; break; case 'HTTP/1.1': self::$protocol = HTTP::HTTP1_1; break; default: Response::getInstance()->setStatus(HTTP::HTTP_VERSION_NOT_SUPPORTED); throw new GenericException('Unsuported protocol'); } } } /** * Get the request headers as an array. * * @return array. */ public static function getHeaders() { if(is_null(self::$headers)) { self::$headers = array(); foreach($_SERVER as $name=>$val) { if(strpos($name, 'HTTP_') !== false) { $name = substr($name, strlen('HTTP_')); $name = str_replace('_', '-', $name); $name = HTTP::makeStdHeaderName($name); self::$headers[$name] = $val; } } } return self::$headers; } /** * Get a single header. * * @param string $name The header name. * @return string The header value. * @see Request::getHeaders() */ public static function getHeader($name) { $name = HTTP::makeStdHeaderName($name); if(is_null(self::$headers)) { self::getHeaders(); } if(isset(self::$headers[$name])) { return self::$headers[$name]; } else { return null; } } /** * Return the method used to request the page. * * @return mixed. */ public static function getMethod() { if(is_null(self::$method)) { switch($_SERVER['REQUEST_METHOD']) { case 'GET': self::$method = HTTP::GET; break; case 'POST': self::$method = HTTP::POST; break; case 'PUT': self::$method = HTTP::PUT; break; case 'HEAD': self::$method = HTTP::HEAD; break; case 'DELETE': self::$method = HTTP::DELETE; break; case 'OPTIONS': self::$method = HTTP::OPTIONS; break; case 'TRACE': self::$method = HTTP::TRACE; break; case 'CONNECT': self::$method = HTTP::CONNECT; default: Response::setStatus(Response::NOT_IMPLEMENTED); throw new GenericException('Unknown request type'); } } return self::$method; } /** * Constructor. Don't call this, it will throw an exception, this class is a Singleton. */ public function __construct() { parent::__construct(); throw new GenericException('You can\'t instantiate the Request class'); } } ?>