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