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