Request.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Elgg\Http;
  3. use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
  4. use Symfony\Component\HttpFoundation\ParameterBag;
  5. use Symfony\Component\HttpFoundation\FileBag;
  6. use Symfony\Component\HttpFoundation\ServerBag;
  7. use Symfony\Component\HttpFoundation\HeaderBag;
  8. /**
  9. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  10. *
  11. * Represents an HTTP request.
  12. *
  13. * Some methods were pulled from Symfony. They are
  14. * Copyright (c) 2004-2013 Fabien Potencier
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is furnished
  21. * to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in all
  24. * copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  32. * THE SOFTWARE.
  33. *
  34. * @package Elgg.Core
  35. * @subpackage Http
  36. * @since 1.9.0
  37. * @access private
  38. */
  39. class Request extends SymfonyRequest {
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function initialize(array $query = array(), array $request = array(), array $attributes = array(),
  44. array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
  45. $this->request = new ParameterBag($this->stripSlashesIfMagicQuotes($request));
  46. $this->query = new ParameterBag($this->stripSlashesIfMagicQuotes($query));
  47. $this->attributes = new ParameterBag($attributes);
  48. $this->cookies = new ParameterBag($this->stripSlashesIfMagicQuotes($cookies));
  49. $this->files = new FileBag($files);
  50. $this->server = new ServerBag($server);
  51. $this->headers = new HeaderBag($this->server->getHeaders());
  52. $this->content = $content;
  53. $this->languages = null;
  54. $this->charsets = null;
  55. $this->encodings = null;
  56. $this->acceptableContentTypes = null;
  57. $this->pathInfo = null;
  58. $this->requestUri = null;
  59. $this->baseUrl = null;
  60. $this->basePath = null;
  61. $this->method = null;
  62. $this->format = null;
  63. }
  64. /**
  65. * Get URL segments from the path info
  66. *
  67. * @see \Elgg\Http\Request::getPathInfo()
  68. *
  69. * @return array
  70. */
  71. public function getUrlSegments() {
  72. $path = trim($this->query->get('__elgg_uri'), '/');
  73. if (!$path) {
  74. return array();
  75. }
  76. return explode('/', $path);
  77. }
  78. /**
  79. * Get first URL segment from the path info
  80. *
  81. * @see \Elgg\Http\Request::getUrlSegments()
  82. *
  83. * @return string
  84. */
  85. public function getFirstUrlSegment() {
  86. $segments = $this->getUrlSegments();
  87. if ($segments) {
  88. return array_shift($segments);
  89. } else {
  90. return '';
  91. }
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function getClientIp() {
  97. $ip = parent::getClientIp();
  98. if ($ip == $this->server->get('REMOTE_ADDR')) {
  99. // try one more
  100. $ip_addresses = $this->server->get('HTTP_X_REAL_IP');
  101. if ($ip_addresses) {
  102. return array_pop(explode(',', $ip_addresses));
  103. }
  104. }
  105. return $ip;
  106. }
  107. /**
  108. * Strip slashes if magic quotes is on
  109. *
  110. * @param mixed $data Data to strip slashes from
  111. * @return mixed
  112. */
  113. protected function stripSlashesIfMagicQuotes($data) {
  114. if (get_magic_quotes_gpc()) {
  115. return _elgg_stripslashes_deep($data);
  116. } else {
  117. return $data;
  118. }
  119. }
  120. }