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