Request.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace OAuth2;
  3. /**
  4. * OAuth2\Request
  5. * This class is taken from the Symfony2 Framework and is part of the Symfony package.
  6. * See Symfony\Component\HttpFoundation\Request (https://github.com/symfony/symfony)
  7. */
  8. class Request implements RequestInterface
  9. {
  10. public $attributes;
  11. public $request;
  12. public $query;
  13. public $server;
  14. public $files;
  15. public $cookies;
  16. public $headers;
  17. public $content;
  18. /**
  19. * Constructor.
  20. *
  21. * @param array $query The GET parameters
  22. * @param array $request The POST parameters
  23. * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  24. * @param array $cookies The COOKIE parameters
  25. * @param array $files The FILES parameters
  26. * @param array $server The SERVER parameters
  27. * @param string $content The raw body data
  28. *
  29. * @api
  30. */
  31. public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null, array $headers = null)
  32. {
  33. $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content, $headers);
  34. }
  35. /**
  36. * Sets the parameters for this request.
  37. *
  38. * This method also re-initializes all properties.
  39. *
  40. * @param array $query The GET parameters
  41. * @param array $request The POST parameters
  42. * @param array $attributes The request attributes (parameters parsed from the PATH_INFO, ...)
  43. * @param array $cookies The COOKIE parameters
  44. * @param array $files The FILES parameters
  45. * @param array $server The SERVER parameters
  46. * @param string $content The raw body data
  47. *
  48. * @api
  49. */
  50. public function initialize(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null, array $headers = null)
  51. {
  52. $this->request = $request;
  53. $this->query = $query;
  54. $this->attributes = $attributes;
  55. $this->cookies = $cookies;
  56. $this->files = $files;
  57. $this->server = $server;
  58. $this->content = $content;
  59. $this->headers = is_null($headers) ? $this->getHeadersFromServer($this->server) : $headers;
  60. }
  61. public function query($name, $default = null)
  62. {
  63. return isset($this->query[$name]) ? $this->query[$name] : $default;
  64. }
  65. public function request($name, $default = null)
  66. {
  67. return isset($this->request[$name]) ? $this->request[$name] : $default;
  68. }
  69. public function server($name, $default = null)
  70. {
  71. return isset($this->server[$name]) ? $this->server[$name] : $default;
  72. }
  73. public function headers($name, $default = null)
  74. {
  75. $headers = array_change_key_case($this->headers);
  76. $name = strtolower($name);
  77. return isset($headers[$name]) ? $headers[$name] : $default;
  78. }
  79. public function getAllQueryParameters()
  80. {
  81. return $this->query;
  82. }
  83. /**
  84. * Returns the request body content.
  85. *
  86. * @param Boolean $asResource If true, a resource will be returned
  87. *
  88. * @return string|resource The request body content or a resource to read the body stream.
  89. */
  90. public function getContent($asResource = false)
  91. {
  92. if (false === $this->content || (true === $asResource && null !== $this->content)) {
  93. throw new \LogicException('getContent() can only be called once when using the resource return type.');
  94. }
  95. if (true === $asResource) {
  96. $this->content = false;
  97. return fopen('php://input', 'rb');
  98. }
  99. if (null === $this->content) {
  100. $this->content = file_get_contents('php://input');
  101. }
  102. return $this->content;
  103. }
  104. private function getHeadersFromServer($server)
  105. {
  106. $headers = array();
  107. foreach ($server as $key => $value) {
  108. if (0 === strpos($key, 'HTTP_')) {
  109. $headers[substr($key, 5)] = $value;
  110. }
  111. // CONTENT_* are not prefixed with HTTP_
  112. elseif (in_array($key, array('CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'))) {
  113. $headers[$key] = $value;
  114. }
  115. }
  116. if (isset($server['PHP_AUTH_USER'])) {
  117. $headers['PHP_AUTH_USER'] = $server['PHP_AUTH_USER'];
  118. $headers['PHP_AUTH_PW'] = isset($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW'] : '';
  119. } else {
  120. /*
  121. * php-cgi under Apache does not pass HTTP Basic user/pass to PHP by default
  122. * For this workaround to work, add this line to your .htaccess file:
  123. * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  124. *
  125. * A sample .htaccess file:
  126. * RewriteEngine On
  127. * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
  128. * RewriteCond %{REQUEST_FILENAME} !-f
  129. * RewriteRule ^(.*)$ app.php [QSA,L]
  130. */
  131. $authorizationHeader = null;
  132. if (isset($server['HTTP_AUTHORIZATION'])) {
  133. $authorizationHeader = $server['HTTP_AUTHORIZATION'];
  134. } elseif (isset($server['REDIRECT_HTTP_AUTHORIZATION'])) {
  135. $authorizationHeader = $server['REDIRECT_HTTP_AUTHORIZATION'];
  136. } elseif (function_exists('apache_request_headers')) {
  137. $requestHeaders = (array) apache_request_headers();
  138. // Server-side fix for bug in old Android versions (a nice side-effect of this fix means we don't care about capitalization for Authorization)
  139. $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders));
  140. if (isset($requestHeaders['Authorization'])) {
  141. $authorizationHeader = trim($requestHeaders['Authorization']);
  142. }
  143. }
  144. if (null !== $authorizationHeader) {
  145. $headers['AUTHORIZATION'] = $authorizationHeader;
  146. // Decode AUTHORIZATION header into PHP_AUTH_USER and PHP_AUTH_PW when authorization header is basic
  147. if (0 === stripos($authorizationHeader, 'basic')) {
  148. $exploded = explode(':', base64_decode(substr($authorizationHeader, 6)));
  149. if (count($exploded) == 2) {
  150. list($headers['PHP_AUTH_USER'], $headers['PHP_AUTH_PW']) = $exploded;
  151. }
  152. }
  153. }
  154. }
  155. // PHP_AUTH_USER/PHP_AUTH_PW
  156. if (isset($headers['PHP_AUTH_USER'])) {
  157. $headers['AUTHORIZATION'] = 'Basic '.base64_encode($headers['PHP_AUTH_USER'].':'.$headers['PHP_AUTH_PW']);
  158. }
  159. return $headers;
  160. }
  161. /**
  162. * Creates a new request with values from PHP's super globals.
  163. *
  164. * @return Request A new request
  165. *
  166. * @api
  167. */
  168. public static function createFromGlobals()
  169. {
  170. $class = get_called_class();
  171. $request = new $class($_GET, $_POST, array(), $_COOKIE, $_FILES, $_SERVER);
  172. $contentType = $request->server('CONTENT_TYPE', '');
  173. $requestMethod = $request->server('REQUEST_METHOD', 'GET');
  174. if (0 === strpos($contentType, 'application/x-www-form-urlencoded')
  175. && in_array(strtoupper($requestMethod), array('PUT', 'DELETE'))
  176. ) {
  177. parse_str($request->getContent(), $data);
  178. $request->request = $data;
  179. } elseif (0 === strpos($contentType, 'application/json')
  180. && in_array(strtoupper($requestMethod), array('POST', 'PUT', 'DELETE'))
  181. ) {
  182. $data = json_decode($request->getContent(), true);
  183. $request->request = $data;
  184. }
  185. return $request;
  186. }
  187. }