00001 <?php
00002
00006 class spunQ_CurrentHttpRequest implements spunQ_IHttpRequest {
00007
00012 private static $instance = NULL;
00013
00018 public static function getInstance() {
00019 if (self::$instance === NULL) {
00020 self::$instance = new self();
00021 }
00022 return self::$instance;
00023 }
00024
00031 private $httpHeaders = NULL;
00032
00038 private $requestDateTime = NULL;
00039
00046 private $uploadedFiles = NULL;
00047
00052 protected $clientInformation = NULL;
00053
00057 private function __construct() {
00058 return NULL;
00059 }
00060
00064 public function getHttpVersion() {
00065 if (strstr($_SERVER['SERVER_PROTOCOL'], '1.0') !== FALSE) {
00066 return self::HTTP_VERSION_1_0;
00067 } elseif (strstr($_SERVER['SERVER_PROTOCOL'], '1.1') !== FALSE) {
00068 return self::HTTP_VERSION_1_1;
00069 }
00070 return self::HTTP_VERSION_UNKNOWN;
00071 }
00072
00076 public function getRequestMethod() {
00077 switch ($_SERVER['REQUEST_METHOD']) {
00078 case 'GET':
00079 return self::REQUEST_GET;
00080 break;
00081 case 'POST':
00082 return self::REQUEST_POST;
00083 break;
00084 case 'HEAD':
00085 return self::REQUEST_HEAD;
00086 break;
00087 case 'PUT':
00088 return self::REQUEST_PUT;
00089 break;
00090 }
00091 return self::REQUEST_UNKNOWN;
00092 }
00093
00097 public function getHttpHeaders() {
00098 if ($this->httpHeaders === NULL) {
00099 $this->httpHeaders = array();
00100 foreach ($_SERVER as $key => $value) {
00101 if (substr($key, 0, 5) === 'HTTP_') {
00102 $this->httpHeaders[str_replace('_', '-', strtolower(substr($key, 5)))] = $value;
00103 }
00104 }
00105 }
00106 return $this->httpHeaders;
00107 }
00108
00112 public function getHttpHeaderValue($header) {
00113 $header = strtolower($header);
00114 $headers = $this->getHttpHeaders();
00115 if (isset($headers[$header])) {
00116 return $headers[$header];
00117 }
00118 return NULL;
00119 }
00120
00124 public function usesSsl() {
00125 if (!isset($_SERVER['HTTPS'])) {
00126 return false;
00127 }
00128 return $_SERVER['HTTPS'] != '' && $_SERVER['HTTPS'] != 'off';
00129 }
00130
00134 public function isAjax() {
00135 return strtolower($this->getHttpHeaderValue('X-Requested-With')) === 'xmlhttprequest';
00136 }
00137
00141 public function getHost() {
00142 if (isset($_SERVER['HTTP_HOST'])) {
00143 return $_SERVER['HTTP_HOST'];
00144 }
00145 return NULL;
00146 }
00147
00151 public function getUrl($includeQueryString = true) {
00152 $url = $_SERVER['REQUEST_URI'];
00153 if (!$includeQueryString) {
00154 if (($pos = strpos($url, '?')) !== false) {
00155 $url = substr($url, 0, $pos);
00156 }
00157 }
00158 $encoding = mb_detect_encoding($url, array('UTF-8', 'ISO-8859-1'), true);
00159 if ($encoding !== false && $encoding !== 'UTF-8') {
00160 $url = mb_convert_encoding($url, 'UTF-8', $encoding);
00161 }
00162 return $url;
00163 }
00164
00168 public function getClientInformation() {
00169 if ($this->clientInformation === NULL) {
00170 $ip = $_SERVER['REMOTE_ADDR'];
00171 if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
00172 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
00173 }
00174 $this->clientInformation = new spunQ_HttpClientInformation($ip, $_SERVER['REMOTE_PORT'], $this);
00175 }
00176 return $this->clientInformation;
00177 }
00178
00182 public function getRequestDateTime() {
00183 if ($this->requestDateTime === NULL) {
00184 $time = time();
00185 if (isset($_SERVER['REQUEST_TIME'])) {
00186 if (is_numeric($time)) {
00187 $time = $_SERVER['REQUEST_TIME'];
00188 } else {
00189 $parsedTime = strtotime($_SERVER['REQUEST_TIME']);
00190 if ($parsedTime) {
00191 $time = $parsedTime;
00192 }
00193 }
00194 }
00195 $this->requestDateTime = new spunQ_DateTime($time);
00196 }
00197 return $this->requestDateTime;
00198 }
00199
00203 public function getCookies() {
00204 return $_COOKIE;
00205 }
00206
00210 public function getCookieValue($cookie) {
00211 if (isset($_COOKIE[$cookie])) {
00212 return $_COOKIE[$cookie];
00213 }
00214 return NULL;
00215 }
00216
00220 public function getGetValues() {
00221 return $_GET;
00222 }
00223
00227 public function getGetValue($name) {
00228 if (isset($_GET[$name])) {
00229 return $_GET[$name];
00230 }
00231 return NULL;
00232 }
00233
00237 public function getPostValues() {
00238 return $_POST;
00239 }
00240
00244 public function getPostValue($name) {
00245 if (isset($_POST[$name])) {
00246 return $_POST[$name];
00247 }
00248 return NULL;
00249 }
00250
00254 public function getUploadedFiles() {
00255 if ($this->uploadedFiles === NULL) {
00256 $this->uploadedFiles = array();
00257 $folder = spunQ_Folder::createTemporaryFolder();
00258 foreach ($_FILES as $fieldName => $_file) {
00259 $file = $folder->getFile(basename($_file['name']));
00260 if (@move_uploaded_file($_file['tmp_name'], $file->getPath())) {
00261 $this->uploadedFiles[$fieldName] = $file;
00262 }
00263 }
00264 }
00265 return $this->uploadedFiles;
00266 }
00267
00271 public function getUploadedFile($name) {
00272 $uploadedFiles = $this->getUploadedFiles();
00273 if (isset($uploadedFiles[$name])) {
00274 return $uploadedFiles[$name];
00275 }
00276 return NULL;
00277 }
00278
00279 protected $locale = NULL;
00280
00288 public function getLocale() {
00289 if ($this->locale === NULL) {
00290 $this->locale = spunQ_UrlManager::extractLocale($this, false);
00291 if ($this->locale === NULL && $this->getHttpHeaderValue('Accept-Language') !== NULL) {
00292 $browserLocale = Locale::acceptFromHttp($this->getHttpHeaderValue('Accept-Language'));
00293 if ($browserLocale !== NULL) {
00294 $possibleLocales = spunQ_Locale::getAll();
00295 $locale = spunQ_Locale::getMostAppropriateRfcLocale($browserLocale, array_keys($possibleLocales));
00296 if (isset($possibleLocales[$locale])) {
00297 $this->locale = $possibleLocales[$locale];
00298 }
00299 }
00300 }
00301 if ($this->locale === NULL) {
00302 $this->locale = spunQ_HttpResponse::getDefaultLocale();
00303 }
00304 }
00305 return $this->locale;
00306 }
00307
00312 public function getRawPostData() {
00313 return file_get_contents('php://input');
00314 }
00315
00316 }
00317