Source for file HttpRequest.php

Documentation is available at HttpRequest.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Web
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * HttpRequest provides access scheme for request sent via HTTP.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Web
  20. 20:  *
  21. 21:  * @property   UriScript $uri 
  22. 22:  * @property-read Uri $originalUri 
  23. 23:  * @property-read array $query 
  24. 24:  * @property-read array $post 
  25. 25:  * @property-read string $postRaw 
  26. 26:  * @property-read array $files 
  27. 27:  * @property-read array $cookies 
  28. 28:  * @property-read string $method 
  29. 29:  * @property-read array $headers 
  30. 30:  * @property-read Uri $referer 
  31. 31:  * @property-read string $remoteAddress 
  32. 32:  * @property-read string $remoteHost 
  33. 33:  * @property-read bool $secured 
  34. 34:  */
  35. 35: class HttpRequest extends Object implements IHttpRequest
  36. 36: {
  37. 37:     /** @var array */
  38. 38:     protected $query;
  39. 39:  
  40. 40:     /** @var array */
  41. 41:     protected $post;
  42. 42:  
  43. 43:     /** @var array */
  44. 44:     protected $files;
  45. 45:  
  46. 46:     /** @var array */
  47. 47:     protected $cookies;
  48. 48:  
  49. 49:     /** @var UriScript {@link HttpRequest::getUri()} */
  50. 50:     protected $uri;
  51. 51:  
  52. 52:     /** @var Uri  {@link HttpRequest::getOriginalUri()} */
  53. 53:     protected $originalUri;
  54. 54:  
  55. 55:     /** @var array  {@link HttpRequest::getHeaders()} */
  56. 56:     protected $headers;
  57. 57:  
  58. 58:     /** @var array */
  59. 59:     protected $uriFilter = array(
  60. 60:         PHP_URL_PATH => array('#/{2,}#' => '/')// '%20' => ''
  61. 61:         => array()// '#[.,)]$#' => ''
  62. 62:     );
  63. 63:  
  64. 64:     /** @var string */
  65. 65:     protected $encoding;
  66. 66:  
  67. 67:  
  68. 68:  
  69. 69:     /********************* URI ****************d*g**/
  70. 70:  
  71. 71:  
  72. 72:  
  73. 73:     /**
  74. 74:      * Returns URL object.
  75. 75:      * @return UriScript 
  76. 76:      */
  77. 77:     final public function getUri()
  78. 78:     {
  79. 79:         if ($this->uri === NULL{
  80. 80:             $this->detectUri();
  81. 81:         }
  82. 82:         return $this->uri;
  83. 83:     }
  84. 84:  
  85. 85:  
  86. 86:  
  87. 87:     /**
  88. 88:      * Sets URL object.
  89. 89:      * @param  UriScript 
  90. 90:      * @return HttpRequest  provides a fluent interface
  91. 91:      */
  92. 92:     public function setUri(UriScript $uri)
  93. 93:     {
  94. 94:         $this->uri = clone $uri;
  95. 95:         $this->query = NULL;
  96. 96:         $this->uri->canonicalize();
  97. 97:         $this->uri->freeze();
  98. 98:         return $this;
  99. 99:     }
  100. 100:  
  101. 101:  
  102. 102:  
  103. 103:     /**
  104. 104:      * Returns URL object.
  105. 105:      * @return Uri 
  106. 106:      */
  107. 107:     final public function getOriginalUri()
  108. 108:     {
  109. 109:         if ($this->originalUri === NULL{
  110. 110:             $this->detectUri();
  111. 111:         }
  112. 112:         return $this->originalUri;
  113. 113:     }
  114. 114:  
  115. 115:  
  116. 116:  
  117. 117:     /**
  118. 118:      * Sets request URI filter.
  119. 119:      * @param  string  pattern to search for
  120. 120:      * @param  string  string to replace
  121. 121:      * @param  int     PHP_URL_PATH or NULL
  122. 122:      * @return void 
  123. 123:      */
  124. 124:     public function addUriFilter($pattern$replacement ''$component NULL)
  125. 125:     {
  126. 126:         $pattern '#' $pattern '#';
  127. 127:         $component $component === PHP_URL_PATH PHP_URL_PATH 0;
  128. 128:  
  129. 129:         if ($replacement === NULL{
  130. 130:             unset($this->uriFilter[$component][$pattern]);
  131. 131:         else {
  132. 132:             $this->uriFilter[$component][$pattern$replacement;
  133. 133:         }
  134. 134:         $this->uri = NULL;
  135. 135:     }
  136. 136:  
  137. 137:  
  138. 138:  
  139. 139:     /**
  140. 140:      * Returns request URI filter.
  141. 141:      * @return array 
  142. 142:      */
  143. 143:     final public function getUriFilters()
  144. 144:     {
  145. 145:         return $this->uriFilter;
  146. 146:     }
  147. 147:  
  148. 148:  
  149. 149:  
  150. 150:     /**
  151. 151:      * Detects uri, base path and script path of the request.
  152. 152:      * @return void 
  153. 153:      */
  154. 154:     protected function detectUri()
  155. 155:     {
  156. 156:         $uri $this->uri = new UriScript;
  157. 157:         $uri->scheme $this->isSecured('https' 'http';
  158. 158:         $uri->user isset($_SERVER['PHP_AUTH_USER']$_SERVER['PHP_AUTH_USER''';
  159. 159:         $uri->password isset($_SERVER['PHP_AUTH_PW']$_SERVER['PHP_AUTH_PW''';
  160. 160:  
  161. 161:         // host & port
  162. 162:         if (isset($_SERVER['HTTP_HOST'])) {
  163. 163:             $pair explode(':'$_SERVER['HTTP_HOST']);
  164. 164:  
  165. 165:         elseif (isset($_SERVER['SERVER_NAME'])) {
  166. 166:             $pair explode(':'$_SERVER['SERVER_NAME']);
  167. 167:  
  168. 168:         else {
  169. 169:             $pair array('');
  170. 170:         }
  171. 171:  
  172. 172:         $uri->host $pair[0];
  173. 173:  
  174. 174:         if (isset($pair[1])) {
  175. 175:             $uri->port = (int) $pair[1];
  176. 176:  
  177. 177:         elseif (isset($_SERVER['SERVER_PORT'])) {
  178. 178:             $uri->port = (int) $_SERVER['SERVER_PORT'];
  179. 179:         }
  180. 180:  
  181. 181:         // path & query
  182. 182:         if (isset($_SERVER['REQUEST_URI'])) // Apache, IIS 6.0
  183. 183:             $requestUri $_SERVER['REQUEST_URI'];
  184. 184:  
  185. 185:         elseif (isset($_SERVER['ORIG_PATH_INFO'])) // IIS 5.0 (PHP as CGI ?)
  186. 186:             $requestUri $_SERVER['ORIG_PATH_INFO'];
  187. 187:             if (isset($_SERVER['QUERY_STRING']&& $_SERVER['QUERY_STRING'!= ''{
  188. 188:                 $requestUri .= '?' $_SERVER['QUERY_STRING'];
  189. 189:             }
  190. 190:         else {
  191. 191:             $requestUri '';
  192. 192:         }
  193. 193:  
  194. 194:         $tmp explode('?'$requestUri2);
  195. 195:         $this->originalUri = new Uri($uri);
  196. 196:         $this->originalUri->path $tmp[0];
  197. 197:         $this->originalUri->query isset($tmp[1]$tmp[1'';
  198. 198:         $this->originalUri->freeze();
  199. 199:  
  200. 200:         $requestUri preg_replace(array_keys($this->uriFilter[0])array_values($this->uriFilter[0])$requestUri);
  201. 201:         $tmp explode('?'$requestUri2);
  202. 202:         $uri->path preg_replace(array_keys($this->uriFilter[PHP_URL_PATH])array_values($this->uriFilter[PHP_URL_PATH])$tmp[0]);
  203. 203:         $uri->path String::fixEncoding($uri->path);
  204. 204:         $uri->query isset($tmp[1]$tmp[1'';
  205. 205:  
  206. 206:         // normalized uri
  207. 207:         $uri->canonicalize();
  208. 208:  
  209. 209:         // detect base URI-path - inspired by Zend Framework (c) Zend Technologies USA Inc. (http://www.zend.com), new BSD license
  210. 210:         $filename isset($_SERVER['SCRIPT_FILENAME']basename($_SERVER['SCRIPT_FILENAME']NULL;
  211. 211:         $scriptPath '';
  212. 212:  
  213. 213:         if (isset($_SERVER['SCRIPT_NAME']&& basename($_SERVER['SCRIPT_NAME']=== $filename{
  214. 214:             $scriptPath rtrim($_SERVER['SCRIPT_NAME']'/');
  215. 215:  
  216. 216:         elseif (isset($_SERVER['PHP_SELF']&& basename($_SERVER['PHP_SELF']=== $filename{
  217. 217:             $scriptPath $_SERVER['PHP_SELF'];
  218. 218:  
  219. 219:         elseif (isset($_SERVER['ORIG_SCRIPT_NAME']&& basename($_SERVER['ORIG_SCRIPT_NAME']=== $filename{
  220. 220:             $scriptPath $_SERVER['ORIG_SCRIPT_NAME']// 1and1 shared hosting compatibility
  221. 221:  
  222. 222:         elseif (isset($_SERVER['PHP_SELF']$_SERVER['SCRIPT_FILENAME'])) {
  223. 223:             // Backtrack up the script_filename to find the portion matching php_self
  224. 224:             $path $_SERVER['PHP_SELF'];
  225. 225:             $segs explode('/'trim($_SERVER['SCRIPT_FILENAME']'/'));
  226. 226:             $segs array_reverse($segs);
  227. 227:             $index 0;
  228. 228:             $last count($segs);
  229. 229:             do {
  230. 230:                 $seg $segs[$index];
  231. 231:                 $scriptPath '/' $seg $scriptPath;
  232. 232:                 $index++;
  233. 233:             while (($last $index&& (FALSE !== ($pos strpos($path$scriptPath))) && (!= $pos));
  234. 234:         }
  235. 235:  
  236. 236:         // Does the scriptPath have anything in common with the request_uri?
  237. 237:         if (strncmp($uri->path$scriptPathstrlen($scriptPath)) === 0{
  238. 238:             // whole $scriptPath in URL
  239. 239:             $uri->scriptPath $scriptPath;
  240. 240:  
  241. 241:         elseif (strncmp($uri->path$scriptPathstrrpos($scriptPath'/'1=== 0{
  242. 242:             // directory portion of $scriptPath in URL
  243. 243:             $uri->scriptPath substr($scriptPath0strrpos($scriptPath'/'1);
  244. 244:  
  245. 245:         elseif (strpos($uri->pathbasename($scriptPath)) === FALSE{
  246. 246:             // no match whatsoever; set it blank
  247. 247:             $uri->scriptPath '/';
  248. 248:  
  249. 249:         elseif ((strlen($uri->path>= strlen($scriptPath))
  250. 250:             && ((FALSE !== ($pos strpos($uri->path$scriptPath))) && ($pos !== 0))) {
  251. 251:             // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  252. 252:             // out of scriptPath. $pos !== 0 makes sure it is not matching a value
  253. 253:             // from PATH_INFO or QUERY_STRING
  254. 254:             $uri->scriptPath substr($uri->path0$pos strlen($scriptPath));
  255. 255:  
  256. 256:         else {
  257. 257:             $uri->scriptPath $scriptPath;
  258. 258:         }
  259. 259:  
  260. 260:         $uri->freeze();
  261. 261:     }
  262. 262:  
  263. 263:  
  264. 264:  
  265. 265:     /********************* query, post, files & cookies ****************d*g**/
  266. 266:  
  267. 267:  
  268. 268:  
  269. 269:     /**
  270. 270:      * Returns variable provided to the script via URL query ($_GET).
  271. 271:      * If no key is passed, returns the entire array.
  272. 272:      * @param  string key
  273. 273:      * @param  mixed  default value
  274. 274:      * @return mixed 
  275. 275:      */
  276. 276:     final public function getQuery($key NULL$default NULL)
  277. 277:     {
  278. 278:         if ($this->query === NULL{
  279. 279:             $this->initialize();
  280. 280:         }
  281. 281:  
  282. 282:         if (func_num_args(=== 0{
  283. 283:             return $this->query;
  284. 284:  
  285. 285:         elseif (isset($this->query[$key])) {
  286. 286:             return $this->query[$key];
  287. 287:  
  288. 288:         else {
  289. 289:             return $default;
  290. 290:         }
  291. 291:     }
  292. 292:  
  293. 293:  
  294. 294:  
  295. 295:     /**
  296. 296:      * Returns variable provided to the script via POST method ($_POST).
  297. 297:      * If no key is passed, returns the entire array.
  298. 298:      * @param  string key
  299. 299:      * @param  mixed  default value
  300. 300:      * @return mixed 
  301. 301:      */
  302. 302:     final public function getPost($key NULL$default NULL)
  303. 303:     {
  304. 304:         if ($this->post === NULL{
  305. 305:             $this->initialize();
  306. 306:         }
  307. 307:  
  308. 308:         if (func_num_args(=== 0{
  309. 309:             return $this->post;
  310. 310:  
  311. 311:         elseif (isset($this->post[$key])) {
  312. 312:             return $this->post[$key];
  313. 313:  
  314. 314:         else {
  315. 315:             return $default;
  316. 316:         }
  317. 317:     }
  318. 318:  
  319. 319:  
  320. 320:  
  321. 321:     /**
  322. 322:      * Returns HTTP POST data in raw format (only for "application/x-www-form-urlencoded").
  323. 323:      * @return string 
  324. 324:      */
  325. 325:     public function getPostRaw()
  326. 326:     {
  327. 327:         return file_get_contents('php://input');
  328. 328:     }
  329. 329:  
  330. 330:  
  331. 331:  
  332. 332:     /**
  333. 333:      * Returns uploaded file.
  334. 334:      * @param  string key (or more keys)
  335. 335:      * @return HttpUploadedFile 
  336. 336:      */
  337. 337:     final public function getFile($key)
  338. 338:     {
  339. 339:         if ($this->files === NULL{
  340. 340:             $this->initialize();
  341. 341:         }
  342. 342:         $args func_get_args();
  343. 343:         return ArrayTools::get($this->files$args);
  344. 344:     }
  345. 345:  
  346. 346:  
  347. 347:  
  348. 348:     /**
  349. 349:      * Returns uploaded files.
  350. 350:      * @return array 
  351. 351:      */
  352. 352:     final public function getFiles()
  353. 353:     {
  354. 354:         if ($this->files === NULL{
  355. 355:             $this->initialize();
  356. 356:         }
  357. 357:  
  358. 358:         return $this->files;
  359. 359:     }
  360. 360:  
  361. 361:  
  362. 362:  
  363. 363:     /**
  364. 364:      * Returns variable provided to the script via HTTP cookies.
  365. 365:      * @param  string key
  366. 366:      * @param  mixed  default value
  367. 367:      * @return mixed 
  368. 368:      */
  369. 369:     final public function getCookie($key$default NULL)
  370. 370:     {
  371. 371:         if ($this->cookies === NULL{
  372. 372:             $this->initialize();
  373. 373:         }
  374. 374:  
  375. 375:         if (func_num_args(=== 0{
  376. 376:             return $this->cookies;
  377. 377:  
  378. 378:         elseif (isset($this->cookies[$key])) {
  379. 379:             return $this->cookies[$key];
  380. 380:  
  381. 381:         else {
  382. 382:             return $default;
  383. 383:         }
  384. 384:     }
  385. 385:  
  386. 386:  
  387. 387:  
  388. 388:     /**
  389. 389:      * Returns variables provided to the script via HTTP cookies.
  390. 390:      * @return array 
  391. 391:      */
  392. 392:     final public function getCookies()
  393. 393:     {
  394. 394:         if ($this->cookies === NULL{
  395. 395:             $this->initialize();
  396. 396:         }
  397. 397:  
  398. 398:         return $this->cookies;
  399. 399:     }
  400. 400:  
  401. 401:  
  402. 402:  
  403. 403:     /**
  404. 404:      * Recursively converts and checks encoding.
  405. 405:      * @param  array 
  406. 406:      * @param  string 
  407. 407:      * @return HttpRequest  provides a fluent interface
  408. 408:      */
  409. 409:     public function setEncoding($encoding)
  410. 410:     {
  411. 411:         if (strcasecmp($encoding$this->encoding)) {
  412. 412:             $this->encoding = $encoding;
  413. 413:             $this->query = $this->post = $this->cookies = $this->files = NULL// reinitialization required
  414. 414:         }
  415. 415:         return $this;
  416. 416:     }
  417. 417:  
  418. 418:  
  419. 419:  
  420. 420:     /**
  421. 421:      * Initializes $this->query, $this->files, $this->cookies and $this->files arrays
  422. 422:      * @return void 
  423. 423:      */
  424. 424:     public function initialize()
  425. 425:     {
  426. 426:         $filter (!in_array(ini_get("filter.default")array("""unsafe_raw")) || ini_get("filter.default_flags"));
  427. 427:  
  428. 428:         parse_str($this->getUri()->query$this->query);
  429. 429:         if (!$this->query{
  430. 430:             $this->query = $filter filter_input_array(INPUT_GETFILTER_UNSAFE_RAW(empty($_GETarray($_GET);
  431. 431:         }
  432. 432:         $this->post = $filter filter_input_array(INPUT_POSTFILTER_UNSAFE_RAW(empty($_POSTarray($_POST);
  433. 433:         $this->cookies = $filter filter_input_array(INPUT_COOKIEFILTER_UNSAFE_RAW(empty($_COOKIEarray($_COOKIE);
  434. 434:  
  435. 435:         $gpc = (bool) get_magic_quotes_gpc();
  436. 436:         $enc = (bool) $this->encoding;
  437. 437:         $old error_reporting(error_reporting(E_NOTICE);
  438. 438:         $nonChars '#[^\x09\x0A\x0D\x20-\x7E\xA0-\x{10FFFF}]#u';
  439. 439:  
  440. 440:  
  441. 441:         // remove fucking quotes and check (and optionally convert) encoding
  442. 442:         if ($gpc || $enc{
  443. 443:             $utf strcasecmp($this->encoding'UTF-8'=== 0;
  444. 444:             $list array($this->query$this->post$this->cookies);
  445. 445:             while (list($key$valeach($list)) {
  446. 446:                 foreach ($val as $k => $v{
  447. 447:                     unset($list[$key][$k]);
  448. 448:  
  449. 449:                     if ($gpc{
  450. 450:                         $k stripslashes($k);
  451. 451:                     }
  452. 452:  
  453. 453:                     if ($enc && is_string($k&& (preg_match($nonChars$k|| preg_last_error())) {
  454. 454:                         // invalid key -> ignore
  455. 455:  
  456. 456:                     elseif (is_array($v)) {
  457. 457:                         $list[$key][$k$v;
  458. 458:                         $list[$list[$key][$k];
  459. 459:  
  460. 460:                     else {
  461. 461:                         if ($gpc && !$filter{
  462. 462:                             $v stripSlashes($v);
  463. 463:                         }
  464. 464:                         if ($enc{
  465. 465:                             if ($utf{
  466. 466:                                 $v String::fixEncoding($v);
  467. 467:  
  468. 468:                             else {
  469. 469:                                 if (!String::checkEncoding($v)) {
  470. 470:                                     $v iconv($this->encoding'UTF-8//IGNORE'$v);
  471. 471:                                 }
  472. 472:                                 $v html_entity_decode($vENT_QUOTES'UTF-8');
  473. 473:                             }
  474. 474:                             $v preg_replace($nonChars''$v);
  475. 475:                         }
  476. 476:                         $list[$key][$k$v;
  477. 477:                     }
  478. 478:                 }
  479. 479:             }
  480. 480:             unset($list$key$val$k$v);
  481. 481:         }
  482. 482:  
  483. 483:  
  484. 484:         // structure $files and create HttpUploadedFile objects
  485. 485:         $this->files = array();
  486. 486:         $list array();
  487. 487:         if (!empty($_FILES)) {
  488. 488:             foreach ($_FILES as $k => $v{
  489. 489:                 if ($enc && is_string($k&& (preg_match($nonChars$k|| preg_last_error())) continue;
  490. 490:                 $v['@'$this->files[$k];
  491. 491:                 $list[$v;
  492. 492:             }
  493. 493:         }
  494. 494:  
  495. 495:         while (list($veach($list)) {
  496. 496:             if (!isset($v['name'])) {
  497. 497:                 continue;
  498. 498:  
  499. 499:             elseif (!is_array($v['name'])) {
  500. 500:                 if ($gpc{
  501. 501:                     $v['name'stripSlashes($v['name']);
  502. 502:                 }
  503. 503:                 if ($enc{
  504. 504:                     $v['name'preg_replace($nonChars''String::fixEncoding($v['name']));
  505. 505:                 }
  506. 506:                 $v['@'new HttpUploadedFile($v);
  507. 507:                 continue;
  508. 508:             }
  509. 509:  
  510. 510:             foreach ($v['name'as $k => $foo{
  511. 511:                 if ($enc && is_string($k&& (preg_match($nonChars$k|| preg_last_error())) continue;
  512. 512:                 $list[array(
  513. 513:                     'name' => $v['name'][$k],
  514. 514:                     'type' => $v['type'][$k],
  515. 515:                     'size' => $v['size'][$k],
  516. 516:                     'tmp_name' => $v['tmp_name'][$k],
  517. 517:                     'error' => $v['error'][$k],
  518. 518:                     '@' => $v['@'][$k],
  519. 519:                 );
  520. 520:             }
  521. 521:         }
  522. 522:  
  523. 523:         error_reporting($old);
  524. 524:     }
  525. 525:  
  526. 526:  
  527. 527:  
  528. 528:     /********************* method & headers ****************d*g**/
  529. 529:  
  530. 530:  
  531. 531:  
  532. 532:     /**
  533. 533:      * Returns HTTP request method (GET, POST, HEAD, PUT, ...). The method is case-sensitive.
  534. 534:      * @return string 
  535. 535:      */
  536. 536:     public function getMethod()
  537. 537:     {
  538. 538:         return isset($_SERVER['REQUEST_METHOD']$_SERVER['REQUEST_METHOD'NULL;
  539. 539:     }
  540. 540:  
  541. 541:  
  542. 542:  
  543. 543:     /**
  544. 544:      * Checks if the request method is the given one.
  545. 545:      * @param  string 
  546. 546:      * @return bool 
  547. 547:      */
  548. 548:     public function isMethod($method)
  549. 549:     {
  550. 550:         return isset($_SERVER['REQUEST_METHOD']strcasecmp($_SERVER['REQUEST_METHOD']$method=== FALSE;
  551. 551:     }
  552. 552:  
  553. 553:  
  554. 554:  
  555. 555:     /**
  556. 556:      * Checks if the request method is POST.
  557. 557:      * @return bool 
  558. 558:      */
  559. 559:     public function isPost()
  560. 560:     {
  561. 561:         return $this->isMethod('POST');
  562. 562:     }
  563. 563:  
  564. 564:  
  565. 565:  
  566. 566:     /**
  567. 567:      * Return the value of the HTTP header. Pass the header name as the
  568. 568:      * plain, HTTP-specified header name (e.g. 'Accept-Encoding').
  569. 569:      * @param  string 
  570. 570:      * @param  mixed 
  571. 571:      * @return mixed 
  572. 572:      */
  573. 573:     final public function getHeader($header$default NULL)
  574. 574:     {
  575. 575:         $header strtolower($header);
  576. 576:         $headers $this->getHeaders();
  577. 577:         if (isset($headers[$header])) {
  578. 578:             return $headers[$header];
  579. 579:         else {
  580. 580:             return $default;
  581. 581:         }
  582. 582:     }
  583. 583:  
  584. 584:  
  585. 585:  
  586. 586:     /**
  587. 587:      * Returns all HTTP headers.
  588. 588:      * @return array 
  589. 589:      */
  590. 590:     public function getHeaders()
  591. 591:     {
  592. 592:         if ($this->headers === NULL{
  593. 593:             // lazy initialization
  594. 594:             if (function_exists('apache_request_headers')) {
  595. 595:                 $this->headers = array_change_key_case(apache_request_headers()CASE_LOWER);
  596. 596:             else {
  597. 597:                 $this->headers = array();
  598. 598:                 foreach ($_SERVER as $k => $v{
  599. 599:                     if (strncmp($k'HTTP_'5== 0{
  600. 600:                         $this->headersstrtr(strtolower(substr($k5))'_''-'$v;
  601. 601:                     }
  602. 602:                 }
  603. 603:             }
  604. 604:         }
  605. 605:         return $this->headers;
  606. 606:     }
  607. 607:  
  608. 608:  
  609. 609:  
  610. 610:     /**
  611. 611:      * Returns referrer.
  612. 612:      * @return Uri|NULL
  613. 613:      */
  614. 614:     final public function getReferer()
  615. 615:     {
  616. 616:         $uri self::getHeader('referer');
  617. 617:         return $uri new Uri($uriNULL;
  618. 618:     }
  619. 619:  
  620. 620:  
  621. 621:  
  622. 622:     /**
  623. 623:      * Is the request is sent via secure channel (https).
  624. 624:      * @return bool 
  625. 625:      */
  626. 626:     public function isSecured()
  627. 627:     {
  628. 628:         return isset($_SERVER['HTTPS']&& strcasecmp($_SERVER['HTTPS']'off');
  629. 629:     }
  630. 630:  
  631. 631:  
  632. 632:  
  633. 633:     /**
  634. 634:      * Is AJAX request?
  635. 635:      * @return bool 
  636. 636:      */
  637. 637:     public function isAjax()
  638. 638:     {
  639. 639:         return $this->getHeader('X-Requested-With'=== 'XMLHttpRequest';
  640. 640:     }
  641. 641:  
  642. 642:  
  643. 643:  
  644. 644:     /**
  645. 645:      * Returns the IP address of the remote client.
  646. 646:      * @return string 
  647. 647:      */
  648. 648:     public function getRemoteAddress()
  649. 649:     {
  650. 650:         return isset($_SERVER['REMOTE_ADDR']$_SERVER['REMOTE_ADDR'NULL;
  651. 651:     }
  652. 652:  
  653. 653:  
  654. 654:  
  655. 655:     /**
  656. 656:      * Returns the host of the remote client.
  657. 657:      * @return string 
  658. 658:      */
  659. 659:     public function getRemoteHost()
  660. 660:     {
  661. 661:         if (!isset($_SERVER['REMOTE_HOST'])) {
  662. 662:             if (!isset($_SERVER['REMOTE_ADDR'])) {
  663. 663:                 return NULL;
  664. 664:             }
  665. 665:             $_SERVER['REMOTE_HOST'getHostByAddr($_SERVER['REMOTE_ADDR']);
  666. 666:         }
  667. 667:  
  668. 668:         return $_SERVER['REMOTE_HOST'];
  669. 669:     }
  670. 670:  
  671. 671:  
  672. 672:  
  673. 673:     /**
  674. 674:      * Parse Accept-Language header and returns prefered language.
  675. 675:      * @param  array   Supported languages
  676. 676:      * @return string 
  677. 677:      */
  678. 678:     public function detectLanguage(array $langs)
  679. 679:     {
  680. 680:         $header $this->getHeader('accept-language');
  681. 681:         if (!$headerreturn NULL;
  682. 682:  
  683. 683:         $s strtolower($header);  // case insensitive
  684. 684:         $s strtr($s'_''-');  // cs_CZ means cs-CZ
  685. 685:         rsort($langs);             // first more specific
  686. 686:         preg_match_all('#(' implode('|'$langs')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#'$s$matches);
  687. 687:  
  688. 688:         if (!$matches[0]{
  689. 689:             return NULL;
  690. 690:         }
  691. 691:  
  692. 692:         $max 0;
  693. 693:         $lang NULL;
  694. 694:         foreach ($matches[1as $key => $value{
  695. 695:             $q $matches[2][$key=== '' 1.0 : (float) $matches[2][$key];
  696. 696:             if ($q $max{
  697. 697:                 $max $q$lang $value;
  698. 698:             }
  699. 699:         }
  700. 700:  
  701. 701:         return $lang;
  702. 702:     }
  703. 703: