PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/HttpFoundation/Request.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 962 lines | 548 code | 128 blank | 286 comment | 103 complexity | 7062e39ab4dae9c40d644b9168e58e0e MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
  12. use Symfony\Component\HttpFoundation\File\UploadedFile;
  13. /**
  14. * Request represents an HTTP request.
  15. *
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. */
  18. class Request
  19. {
  20. /**
  21. * @var \Symfony\Component\HttpFoundation\ParameterBag
  22. */
  23. public $attributes;
  24. /**
  25. * @var \Symfony\Component\HttpFoundation\ParameterBag
  26. */
  27. public $request;
  28. /**
  29. * @var \Symfony\Component\HttpFoundation\ParameterBag
  30. */
  31. public $query;
  32. /**
  33. * @var \Symfony\Component\HttpFoundation\ParameterBag
  34. */
  35. public $server;
  36. /**
  37. * @var \Symfony\Component\HttpFoundation\ParameterBag
  38. */
  39. public $files;
  40. /**
  41. * @var \Symfony\Component\HttpFoundation\ParameterBag
  42. */
  43. public $cookies;
  44. /**
  45. * @var \Symfony\Component\HttpFoundation\HeaderBag
  46. */
  47. public $headers;
  48. protected $content;
  49. protected $languages;
  50. protected $charsets;
  51. protected $acceptableContentTypes;
  52. protected $pathInfo;
  53. protected $requestUri;
  54. protected $baseUrl;
  55. protected $basePath;
  56. protected $method;
  57. protected $format;
  58. protected $session;
  59. static protected $formats;
  60. /**
  61. * Constructor.
  62. *
  63. * @param array $query The GET parameters
  64. * @param array $request The POST parameters
  65. * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  66. * @param array $cookies The COOKIE parameters
  67. * @param array $files The FILES parameters
  68. * @param array $server The SERVER parameters
  69. * @param string $content The raw body data
  70. */
  71. public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  72. {
  73. $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content);
  74. }
  75. /**
  76. * Sets the parameters for this request.
  77. *
  78. * This method also re-initializes all properties.
  79. *
  80. * @param array $query The GET parameters
  81. * @param array $request The POST parameters
  82. * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  83. * @param array $cookies The COOKIE parameters
  84. * @param array $files The FILES parameters
  85. * @param array $server The SERVER parameters
  86. * @param string $content The raw body data
  87. */
  88. public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null)
  89. {
  90. $this->request = new ParameterBag($request);
  91. $this->query = new ParameterBag($query);
  92. $this->attributes = new ParameterBag($attributes);
  93. $this->cookies = new ParameterBag($cookies);
  94. $this->files = new FileBag($files);
  95. $this->server = new ServerBag($server);
  96. $this->headers = new HeaderBag($this->server->getHeaders());
  97. $this->content = $content;
  98. $this->languages = null;
  99. $this->charsets = null;
  100. $this->acceptableContentTypes = null;
  101. $this->pathInfo = null;
  102. $this->requestUri = null;
  103. $this->baseUrl = null;
  104. $this->basePath = null;
  105. $this->method = null;
  106. $this->format = null;
  107. }
  108. /**
  109. * Creates a new request with values from PHP's super globals.
  110. *
  111. * @return Request A new request
  112. */
  113. static public function createfromGlobals()
  114. {
  115. return new static($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
  116. }
  117. /**
  118. * Creates a Request based on a given URI and configuration.
  119. *
  120. * @param string $uri The URI
  121. * @param string $method The HTTP method
  122. * @param array $parameters The request (GET) or query (POST) parameters
  123. * @param array $cookies The request cookies ($_COOKIE)
  124. * @param array $files The request files ($_FILES)
  125. * @param array $server The server parameters ($_SERVER)
  126. * @param string $content The raw body data
  127. *
  128. * @return Request A Request instance
  129. */
  130. static public function create($uri, $method = 'GET', $parameters = array(), $cookies = array(), $files = array(), $server = array(), $content = null)
  131. {
  132. $defaults = array(
  133. 'SERVER_NAME' => 'localhost',
  134. 'SERVER_PORT' => 80,
  135. 'HTTP_HOST' => 'localhost',
  136. 'HTTP_USER_AGENT' => 'Symfony/2.X',
  137. 'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  138. 'HTTP_ACCEPT_LANGUAGE' => 'en-us,en;q=0.5',
  139. 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  140. 'REMOTE_ADDR' => '127.0.0.1',
  141. 'SCRIPT_NAME' => '',
  142. 'SCRIPT_FILENAME' => '',
  143. );
  144. $components = parse_url($uri);
  145. if (isset($components['host'])) {
  146. $defaults['SERVER_NAME'] = $components['host'];
  147. $defaults['HTTP_HOST'] = $components['host'];
  148. }
  149. if (isset($components['scheme'])) {
  150. if ('https' === $components['scheme']) {
  151. $defaults['HTTPS'] = 'on';
  152. $defaults['SERVER_PORT'] = 443;
  153. }
  154. }
  155. if (isset($components['port'])) {
  156. $defaults['SERVER_PORT'] = $components['port'];
  157. $defaults['HTTP_HOST'] = $defaults['HTTP_HOST'].':'.$components['port'];
  158. }
  159. if (in_array(strtoupper($method), array('POST', 'PUT', 'DELETE'))) {
  160. $request = $parameters;
  161. $query = array();
  162. $defaults['CONTENT_TYPE'] = 'application/x-www-form-urlencoded';
  163. } else {
  164. $request = array();
  165. $query = $parameters;
  166. if (false !== $pos = strpos($uri, '?')) {
  167. $qs = substr($uri, $pos + 1);
  168. parse_str($qs, $params);
  169. $query = array_merge($params, $query);
  170. }
  171. }
  172. $queryString = isset($components['query']) ? html_entity_decode($components['query']) : '';
  173. parse_str($queryString, $qs);
  174. if (is_array($qs)) {
  175. $query = array_replace($qs, $query);
  176. }
  177. $uri = $components['path'] . ($queryString ? '?'.$queryString : '');
  178. $server = array_replace($defaults, $server, array(
  179. 'REQUEST_METHOD' => strtoupper($method),
  180. 'PATH_INFO' => '',
  181. 'REQUEST_URI' => $uri,
  182. 'QUERY_STRING' => $queryString,
  183. ));
  184. return new static($query, $request, array(), $cookies, $files, $server, $content);
  185. }
  186. /**
  187. * Clones a request and overrides some of its parameters.
  188. *
  189. * @param array $query The GET parameters
  190. * @param array $request The POST parameters
  191. * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  192. * @param array $cookies The COOKIE parameters
  193. * @param array $files The FILES parameters
  194. * @param array $server The SERVER parameters
  195. */
  196. public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
  197. {
  198. $dup = clone $this;
  199. $dup->initialize(
  200. null !== $query ? $query : $this->query->all(),
  201. null !== $request ? $request : $this->request->all(),
  202. null !== $attributes ? $attributes : $this->attributes->all(),
  203. null !== $cookies ? $cookies : $this->cookies->all(),
  204. null !== $files ? $files : $this->files->all(),
  205. null !== $server ? $server : $this->server->all()
  206. );
  207. return $dup;
  208. }
  209. /**
  210. * Clones the current request.
  211. *
  212. * Note that the session is not cloned as duplicated requests
  213. * are most of the time sub-requests of the main one.
  214. */
  215. public function __clone()
  216. {
  217. $this->query = clone $this->query;
  218. $this->request = clone $this->request;
  219. $this->attributes = clone $this->attributes;
  220. $this->cookies = clone $this->cookies;
  221. $this->files = clone $this->files;
  222. $this->server = clone $this->server;
  223. $this->headers = clone $this->headers;
  224. }
  225. /**
  226. * Overrides the PHP global variables according to this request instance.
  227. *
  228. * It overrides $_GET, $_POST, $_REQUEST, $_SERVER, $_COOKIE, and $_FILES.
  229. */
  230. public function overrideGlobals()
  231. {
  232. $_GET = $this->query->all();
  233. $_POST = $this->request->all();
  234. $_SERVER = $this->server->all();
  235. $_COOKIE = $this->cookies->all();
  236. // FIXME: populate $_FILES
  237. foreach ($this->headers->all() as $key => $value) {
  238. $_SERVER['HTTP_'.strtoupper(str_replace('-', '_', $key))] = implode(', ', $value);
  239. }
  240. // FIXME: should read variables_order and request_order
  241. // to know which globals to merge and in which order
  242. $_REQUEST = array_merge($_GET, $_POST);
  243. }
  244. // Order of precedence: GET, PATH, POST, COOKIE
  245. // Avoid using this method in controllers:
  246. // * slow
  247. // * prefer to get from a "named" source
  248. // This method is mainly useful for libraries that want to provide some flexibility
  249. public function get($key, $default = null)
  250. {
  251. return $this->query->get($key, $this->attributes->get($key, $this->request->get($key, $default)));
  252. }
  253. public function getSession()
  254. {
  255. return $this->session;
  256. }
  257. public function hasSession()
  258. {
  259. return $this->cookies->has(session_name());
  260. }
  261. public function setSession(Session $session)
  262. {
  263. $this->session = $session;
  264. }
  265. /**
  266. * Returns the client IP address.
  267. *
  268. * @param Boolean $proxy Whether the current request has been made behind a proxy or not
  269. *
  270. * @return string The client IP address
  271. */
  272. public function getClientIp($proxy = false)
  273. {
  274. if ($proxy) {
  275. if ($this->server->has('HTTP_CLIENT_IP')) {
  276. return $this->server->get('HTTP_CLIENT_IP');
  277. } elseif ($this->server->has('HTTP_X_FORWARDED_FOR')) {
  278. return $this->server->get('HTTP_X_FORWARDED_FOR');
  279. }
  280. }
  281. return $this->server->get('REMOTE_ADDR');
  282. }
  283. /**
  284. * Returns current script name.
  285. *
  286. * @return string
  287. */
  288. public function getScriptName()
  289. {
  290. return $this->server->get('SCRIPT_NAME', $this->server->get('ORIG_SCRIPT_NAME', ''));
  291. }
  292. /**
  293. * Returns the path being requested relative to the executed script.
  294. *
  295. * Suppose this request is instantiated from /mysite on localhost:
  296. *
  297. * * http://localhost/mysite returns an empty string
  298. * * http://localhost/mysite/about returns '/about'
  299. * * http://localhost/mysite/about?var=1 returns '/about'
  300. *
  301. * @return string
  302. */
  303. public function getPathInfo()
  304. {
  305. if (null === $this->pathInfo) {
  306. $this->pathInfo = $this->preparePathInfo();
  307. }
  308. return $this->pathInfo;
  309. }
  310. /**
  311. * Returns the root path from which this request is executed.
  312. *
  313. * Suppose that an index.php file instantiates this request object:
  314. *
  315. * * http://localhost/index.php returns an empty string
  316. * * http://localhost/index.php/page returns an empty string
  317. * * http://localhost/web/index.php return '/web'
  318. *
  319. * @return string
  320. */
  321. public function getBasePath()
  322. {
  323. if (null === $this->basePath) {
  324. $this->basePath = $this->prepareBasePath();
  325. }
  326. return $this->basePath;
  327. }
  328. /**
  329. * Returns the root url from which this request is executed.
  330. *
  331. * This is similar to getBasePath(), except that it also includes the
  332. * script filename (e.g. index.php) if one exists.
  333. *
  334. * @return string
  335. */
  336. public function getBaseUrl()
  337. {
  338. if (null === $this->baseUrl) {
  339. $this->baseUrl = $this->prepareBaseUrl();
  340. }
  341. return $this->baseUrl;
  342. }
  343. public function getScheme()
  344. {
  345. return ($this->server->get('HTTPS') == 'on') ? 'https' : 'http';
  346. }
  347. public function getPort()
  348. {
  349. return $this->server->get('SERVER_PORT');
  350. }
  351. /**
  352. * Returns the HTTP host being requested.
  353. *
  354. * The port name will be appended to the host if it's non-standard.
  355. *
  356. * @return string
  357. */
  358. public function getHttpHost()
  359. {
  360. $host = $this->headers->get('HOST');
  361. if (!empty($host)) {
  362. return $host;
  363. }
  364. $scheme = $this->getScheme();
  365. $name = $this->server->get('SERVER_NAME');
  366. $port = $this->getPort();
  367. if (('http' == $scheme && $port == 80) || ('https' == $scheme && $port == 443)) {
  368. return $name;
  369. } else {
  370. return $name.':'.$port;
  371. }
  372. }
  373. public function getRequestUri()
  374. {
  375. if (null === $this->requestUri) {
  376. $this->requestUri = $this->prepareRequestUri();
  377. }
  378. return $this->requestUri;
  379. }
  380. /**
  381. * Generates a normalized URI for the Request.
  382. *
  383. * @return string A normalized URI for the Request
  384. *
  385. * @see getQueryString()
  386. */
  387. public function getUri()
  388. {
  389. $qs = $this->getQueryString();
  390. if (null !== $qs) {
  391. $qs = '?'.$qs;
  392. }
  393. return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$this->getPathInfo().$qs;
  394. }
  395. /**
  396. * Generates a normalized URI for the given path.
  397. *
  398. * @param string $path A path to use instead of the current one
  399. *
  400. * @return string The normalized URI for the path
  401. */
  402. public function getUriForPath($path)
  403. {
  404. return $this->getScheme().'://'.$this->getHttpHost().$this->getBaseUrl().$path;
  405. }
  406. /**
  407. * Generates the normalized query string for the Request.
  408. *
  409. * It builds a normalized query string, where keys/value pairs are alphabetized
  410. * and have consistent escaping.
  411. *
  412. * @return string A normalized query string for the Request
  413. */
  414. public function getQueryString()
  415. {
  416. if (!$qs = $this->server->get('QUERY_STRING')) {
  417. return null;
  418. }
  419. $parts = array();
  420. $order = array();
  421. foreach (explode('&', $qs) as $segment) {
  422. if (false === strpos($segment, '=')) {
  423. $parts[] = $segment;
  424. $order[] = $segment;
  425. } else {
  426. $tmp = explode('=', urldecode($segment), 2);
  427. $parts[] = urlencode($tmp[0]).'='.urlencode($tmp[1]);
  428. $order[] = $tmp[0];
  429. }
  430. }
  431. array_multisort($order, SORT_ASC, $parts);
  432. return implode('&', $parts);
  433. }
  434. public function isSecure()
  435. {
  436. return (
  437. (strtolower($this->server->get('HTTPS')) == 'on' || $this->server->get('HTTPS') == 1)
  438. ||
  439. (strtolower($this->headers->get('SSL_HTTPS')) == 'on' || $this->headers->get('SSL_HTTPS') == 1)
  440. ||
  441. (strtolower($this->headers->get('X_FORWARDED_PROTO')) == 'https')
  442. );
  443. }
  444. /**
  445. * Returns the host name.
  446. *
  447. * @return string
  448. */
  449. public function getHost()
  450. {
  451. if ($host = $this->headers->get('X_FORWARDED_HOST')) {
  452. $elements = explode(',', $host);
  453. $host = trim($elements[count($elements) - 1]);
  454. } else {
  455. if (!$host = $this->headers->get('HOST')) {
  456. if (!$host = $this->server->get('SERVER_NAME')) {
  457. $host = $this->server->get('SERVER_ADDR', '');
  458. }
  459. }
  460. }
  461. // Remove port number from host
  462. $elements = explode(':', $host);
  463. return trim($elements[0]);
  464. }
  465. public function setMethod($method)
  466. {
  467. $this->method = null;
  468. $this->server->set('REQUEST_METHOD', $method);
  469. }
  470. /**
  471. * Gets the request method.
  472. *
  473. * @return string The request method
  474. */
  475. public function getMethod()
  476. {
  477. if (null === $this->method) {
  478. $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
  479. if ('POST' === $this->method) {
  480. $this->method = strtoupper($this->request->get('_method', 'POST'));
  481. }
  482. }
  483. return $this->method;
  484. }
  485. /**
  486. * Gets the mime type associated with the format.
  487. *
  488. * @param string $format The format
  489. *
  490. * @return string The associated mime type (null if not found)
  491. */
  492. public function getMimeType($format)
  493. {
  494. if (null === static::$formats) {
  495. static::initializeFormats();
  496. }
  497. return isset(static::$formats[$format]) ? static::$formats[$format][0] : null;
  498. }
  499. /**
  500. * Gets the format associated with the mime type.
  501. *
  502. * @param string $mimeType The associated mime type
  503. *
  504. * @return string The format (null if not found)
  505. */
  506. public function getFormat($mimeType)
  507. {
  508. if (null === static::$formats) {
  509. static::initializeFormats();
  510. }
  511. foreach (static::$formats as $format => $mimeTypes) {
  512. if (in_array($mimeType, (array) $mimeTypes)) {
  513. return $format;
  514. }
  515. }
  516. return null;
  517. }
  518. /**
  519. * Associates a format with mime types.
  520. *
  521. * @param string $format The format
  522. * @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
  523. */
  524. public function setFormat($format, $mimeTypes)
  525. {
  526. if (null === static::$formats) {
  527. static::initializeFormats();
  528. }
  529. static::$formats[$format] = is_array($mimeTypes) ? $mimeTypes : array($mimeTypes);
  530. }
  531. /**
  532. * Gets the request format.
  533. *
  534. * Here is the process to determine the format:
  535. *
  536. * * format defined by the user (with setRequestFormat())
  537. * * _format request parameter
  538. * * null
  539. *
  540. * @return string The request format
  541. */
  542. public function getRequestFormat()
  543. {
  544. if (null === $this->format) {
  545. $this->format = $this->get('_format', 'html');
  546. }
  547. return $this->format;
  548. }
  549. public function setRequestFormat($format)
  550. {
  551. $this->format = $format;
  552. }
  553. public function isMethodSafe()
  554. {
  555. return in_array($this->getMethod(), array('GET', 'HEAD'));
  556. }
  557. /**
  558. * Returns the request body content.
  559. *
  560. * @param Boolean $asResource If true, a resource will be returned
  561. *
  562. * @return string|resource The request body content or a resource to read the body stream.
  563. */
  564. public function getContent($asResource = false)
  565. {
  566. if (false === $this->content || (true === $asResource && null !== $this->content)) {
  567. throw new \LogicException('getContent() can only be called once when using the resource return type.');
  568. }
  569. if (true === $asResource) {
  570. $this->content = false;
  571. return fopen('php://input', 'rb');
  572. }
  573. if (null === $this->content) {
  574. $this->content = file_get_contents('php://input');
  575. }
  576. return $this->content;
  577. }
  578. public function getETags()
  579. {
  580. return preg_split('/\s*,\s*/', $this->headers->get('if_none_match'), null, PREG_SPLIT_NO_EMPTY);
  581. }
  582. public function isNoCache()
  583. {
  584. return $this->headers->hasCacheControlDirective('no-cache') || 'no-cache' == $this->headers->get('Pragma');
  585. }
  586. /**
  587. * Returns the preferred language.
  588. *
  589. * @param array $locales An array of ordered available locales
  590. *
  591. * @return string The preferred locale
  592. */
  593. public function getPreferredLanguage(array $locales = null)
  594. {
  595. $preferredLanguages = $this->getLanguages();
  596. if (null === $locales) {
  597. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : null;
  598. }
  599. if (!$preferredLanguages) {
  600. return $locales[0];
  601. }
  602. $preferredLanguages = array_values(array_intersect($preferredLanguages, $locales));
  603. return isset($preferredLanguages[0]) ? $preferredLanguages[0] : $locales[0];
  604. }
  605. /**
  606. * Gets a list of languages acceptable by the client browser.
  607. *
  608. * @return array Languages ordered in the user browser preferences
  609. */
  610. public function getLanguages()
  611. {
  612. if (null !== $this->languages) {
  613. return $this->languages;
  614. }
  615. $languages = $this->splitHttpAcceptHeader($this->headers->get('Accept-Language'));
  616. foreach ($languages as $lang) {
  617. if (strstr($lang, '-')) {
  618. $codes = explode('-', $lang);
  619. if ($codes[0] == 'i') {
  620. // Language not listed in ISO 639 that are not variants
  621. // of any listed language, which can be registered with the
  622. // i-prefix, such as i-cherokee
  623. if (count($codes) > 1) {
  624. $lang = $codes[1];
  625. }
  626. } else {
  627. for ($i = 0, $max = count($codes); $i < $max; $i++) {
  628. if ($i == 0) {
  629. $lang = strtolower($codes[0]);
  630. } else {
  631. $lang .= '_'.strtoupper($codes[$i]);
  632. }
  633. }
  634. }
  635. }
  636. $this->languages[] = $lang;
  637. }
  638. return $this->languages;
  639. }
  640. /**
  641. * Gets a list of charsets acceptable by the client browser.
  642. *
  643. * @return array List of charsets in preferable order
  644. */
  645. public function getCharsets()
  646. {
  647. if (null !== $this->charsets) {
  648. return $this->charsets;
  649. }
  650. return $this->charsets = $this->splitHttpAcceptHeader($this->headers->get('Accept-Charset'));
  651. }
  652. /**
  653. * Gets a list of content types acceptable by the client browser
  654. *
  655. * @return array Languages ordered in the user browser preferences
  656. */
  657. public function getAcceptableContentTypes()
  658. {
  659. if (null !== $this->acceptableContentTypes) {
  660. return $this->acceptableContentTypes;
  661. }
  662. return $this->acceptableContentTypes = $this->splitHttpAcceptHeader($this->headers->get('Accept'));
  663. }
  664. /**
  665. * Returns true if the request is a XMLHttpRequest.
  666. *
  667. * It works if your JavaScript library set an X-Requested-With HTTP header.
  668. * It is known to work with Prototype, Mootools, jQuery.
  669. *
  670. * @return Boolean true if the request is an XMLHttpRequest, false otherwise
  671. */
  672. public function isXmlHttpRequest()
  673. {
  674. return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
  675. }
  676. /**
  677. * Splits an Accept-* HTTP header.
  678. *
  679. * @param string $header Header to split
  680. */
  681. public function splitHttpAcceptHeader($header)
  682. {
  683. if (!$header) {
  684. return array();
  685. }
  686. $values = array();
  687. foreach (array_filter(explode(',', $header)) as $value) {
  688. // Cut off any q-value that might come after a semi-colon
  689. if ($pos = strpos($value, ';')) {
  690. $q = (float) trim(substr($value, strpos($value, '=') + 1));
  691. $value = trim(substr($value, 0, $pos));
  692. } else {
  693. $q = 1;
  694. }
  695. if (0 < $q) {
  696. $values[trim($value)] = $q;
  697. }
  698. }
  699. arsort($values);
  700. return array_keys($values);
  701. }
  702. /*
  703. * The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
  704. *
  705. * Code subject to the new BSD license (http://framework.zend.com/license/new-bsd).
  706. *
  707. * Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  708. */
  709. protected function prepareRequestUri()
  710. {
  711. $requestUri = '';
  712. if ($this->headers->has('X_REWRITE_URL')) {
  713. // check this first so IIS will catch
  714. $requestUri = $this->headers->get('X_REWRITE_URL');
  715. } elseif ($this->server->get('IIS_WasUrlRewritten') == '1' && $this->server->get('UNENCODED_URL') != '') {
  716. // IIS7 with URL Rewrite: make sure we get the unencoded url (double slash problem)
  717. $requestUri = $this->server->get('UNENCODED_URL');
  718. } elseif ($this->server->has('REQUEST_URI')) {
  719. $requestUri = $this->server->get('REQUEST_URI');
  720. // HTTP proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path
  721. $schemeAndHttpHost = $this->getScheme().'://'.$this->getHttpHost();
  722. if (strpos($requestUri, $schemeAndHttpHost) === 0) {
  723. $requestUri = substr($requestUri, strlen($schemeAndHttpHost));
  724. }
  725. } elseif ($this->server->has('ORIG_PATH_INFO')) {
  726. // IIS 5.0, PHP as CGI
  727. $requestUri = $this->server->get('ORIG_PATH_INFO');
  728. if ($this->server->get('QUERY_STRING')) {
  729. $requestUri .= '?'.$this->server->get('QUERY_STRING');
  730. }
  731. }
  732. return $requestUri;
  733. }
  734. protected function prepareBaseUrl()
  735. {
  736. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  737. if (basename($this->server->get('SCRIPT_NAME')) === $filename) {
  738. $baseUrl = $this->server->get('SCRIPT_NAME');
  739. } elseif (basename($this->server->get('PHP_SELF')) === $filename) {
  740. $baseUrl = $this->server->get('PHP_SELF');
  741. } elseif (basename($this->server->get('ORIG_SCRIPT_NAME')) === $filename) {
  742. $baseUrl = $this->server->get('ORIG_SCRIPT_NAME'); // 1and1 shared hosting compatibility
  743. } else {
  744. // Backtrack up the script_filename to find the portion matching
  745. // php_self
  746. $path = $this->server->get('PHP_SELF', '');
  747. $file = $this->server->get('SCRIPT_FILENAME', '');
  748. $segs = explode('/', trim($file, '/'));
  749. $segs = array_reverse($segs);
  750. $index = 0;
  751. $last = count($segs);
  752. $baseUrl = '';
  753. do {
  754. $seg = $segs[$index];
  755. $baseUrl = '/'.$seg.$baseUrl;
  756. ++$index;
  757. } while (($last > $index) && (false !== ($pos = strpos($path, $baseUrl))) && (0 != $pos));
  758. }
  759. // Does the baseUrl have anything in common with the request_uri?
  760. $requestUri = $this->getRequestUri();
  761. if ($baseUrl && 0 === strpos($requestUri, $baseUrl)) {
  762. // full $baseUrl matches
  763. return $baseUrl;
  764. }
  765. if ($baseUrl && 0 === strpos($requestUri, dirname($baseUrl))) {
  766. // directory portion of $baseUrl matches
  767. return rtrim(dirname($baseUrl), '/');
  768. }
  769. $truncatedRequestUri = $requestUri;
  770. if (($pos = strpos($requestUri, '?')) !== false) {
  771. $truncatedRequestUri = substr($requestUri, 0, $pos);
  772. }
  773. $basename = basename($baseUrl);
  774. if (empty($basename) || !strpos($truncatedRequestUri, $basename)) {
  775. // no match whatsoever; set it blank
  776. return '';
  777. }
  778. // If using mod_rewrite or ISAPI_Rewrite strip the script filename
  779. // out of baseUrl. $pos !== 0 makes sure it is not matching a value
  780. // from PATH_INFO or QUERY_STRING
  781. if ((strlen($requestUri) >= strlen($baseUrl)) && ((false !== ($pos = strpos($requestUri, $baseUrl))) && ($pos !== 0))) {
  782. $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl));
  783. }
  784. return rtrim($baseUrl, '/');
  785. }
  786. protected function prepareBasePath()
  787. {
  788. $filename = basename($this->server->get('SCRIPT_FILENAME'));
  789. $baseUrl = $this->getBaseUrl();
  790. if (empty($baseUrl)) {
  791. return '';
  792. }
  793. if (basename($baseUrl) === $filename) {
  794. $basePath = dirname($baseUrl);
  795. } else {
  796. $basePath = $baseUrl;
  797. }
  798. if ('\\' === DIRECTORY_SEPARATOR) {
  799. $basePath = str_replace('\\', '/', $basePath);
  800. }
  801. return rtrim($basePath, '/');
  802. }
  803. protected function preparePathInfo()
  804. {
  805. $baseUrl = $this->getBaseUrl();
  806. if (null === ($requestUri = $this->getRequestUri())) {
  807. return '';
  808. }
  809. $pathInfo = '';
  810. // Remove the query string from REQUEST_URI
  811. if ($pos = strpos($requestUri, '?')) {
  812. $requestUri = substr($requestUri, 0, $pos);
  813. }
  814. if ((null !== $baseUrl) && (false === ($pathInfo = substr($requestUri, strlen($baseUrl))))) {
  815. // If substr() returns false then PATH_INFO is set to an empty string
  816. return '';
  817. } elseif (null === $baseUrl) {
  818. return $requestUri;
  819. }
  820. return (string) $pathInfo;
  821. }
  822. static protected function initializeFormats()
  823. {
  824. static::$formats = array(
  825. 'txt' => array('text/plain'),
  826. 'js' => array('application/javascript', 'application/x-javascript', 'text/javascript'),
  827. 'css' => array('text/css'),
  828. 'json' => array('application/json', 'application/x-json'),
  829. 'xml' => array('text/xml', 'application/xml', 'application/x-xml'),
  830. 'rdf' => array('application/rdf+xml'),
  831. 'atom' => array('application/atom+xml'),
  832. );
  833. }
  834. }