. */ class Staple_Request { protected static $inst; protected $uri; protected $sport; protected $rport; protected $path; protected $method; protected static $secure; protected $referrer; protected $userAgent; protected $IP; protected $auth; public function __construct() { $this->uri = $_SERVER['REQUEST_URI']; $this->sport = $_SERVER['SERVER_PORT']; $this->rport = $_SERVER['REMOTE_PORT']; $this->method = $_SERVER['REQUEST_METHOD']; if(array_key_exists('PATH_INFO', $_SERVER)) { $this->path = trim($_SERVER['PATH_INFO']); } $this->IP = $_SERVER['REMOTE_ADDR']; $this->userAgent = $_SERVER['HTTP_USER_AGENT']; if(array_key_exists('HTTP_REFERER', $_SERVER)) { $this->referrer = $_SERVER['HTTP_REFERER']; } $this->isSecure(); $this->auth = array(); if(array_key_exists('PHP_AUTH_USER', $_SERVER)) $this->auth['basicuser'] = $_SERVER['PHP_AUTH_USER']; if(array_key_exists('PHP_AUTH_PW', $_SERVER)) $this->auth['basicpw'] = $_SERVER['PHP_AUTH_PW']; if(array_key_exists('PHP_AUTH_DIGEST', $_SERVER)) $this->auth['digest'] = $_SERVER['PHP_AUTH_DIGEST']; } public function __get($name) { $method = 'get' . $name; if (!method_exists($this, $method)) { throw new Exception('Request Object Not Found'); } return $this->$method(); } public function Get() { if (!isset(self::$inst)) { $c = __CLASS__; self::$inst = new $c(); } return self::$inst; } /** * Checks the PHP $_SERVER var for the presence of HTTPS. Returns a boolean. * @return boolean */ public static function isSecure() { if(!isset(self::$secure)) { if(array_key_exists('HTTPS', $_SERVER)) { if($_SERVER['HTTPS'] == 'on') { self::$secure = true; } else { self::$secure = false; } } elseif(array_key_exists('SERVER_PORT', $_SERVER)) { if($_SERVER['SERVER_PORT'] == '443') { self::$secure = true; } else { self::$secure = false; } } else { self::$secure = false; } } return (bool)self::$secure; } public function Path() { return $this->path; } public function URI() { return $this->URI(); } public function IP() { return $this->IP; } public function Agent() { return $this->userAgent; } public static function getGETString(array $exclude = array()) { $getstring = ''; foreach($_GET as $key=>$value) { if(!in_array($key, $exclude)) { if($getstring == '') { $getstring = urlencode($key).'='.urlencode($value); } else { $getstring .= '&'.urlencode($key).'='.urlencode($value); } } } } public static function Redirect(Staple_Route $route, array $get = array()) { $to = (string)$route; if($get != array()) { $to .= '?'.Staple_Link::getArraytoString($get); } header('Location: '.$to); exit(0); } }