PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/webdav/vendor/Sabre/HTTP/Request.php

https://github.com/wrlee/gallery3-contrib
PHP | 220 lines | 68 code | 40 blank | 112 comment | 13 complexity | 805522b101163d3dca1b27af0c2d1fff MD5 | raw file
  1. <?php
  2. /**
  3. * HTTP Request information
  4. *
  5. * This object can be used to easily access information about an HTTP request.
  6. * It can additionally be used to create 'mock' requests.
  7. *
  8. * This class mostly operates indepentend, but because of the nature of a single
  9. * request per run it can operate as a singleton. For more information check out
  10. * the behaviour around 'defaultInputStream'.
  11. *
  12. * @package Sabre
  13. * @subpackage HTTP
  14. * @copyright Copyright (C) 2010 Rooftop Solutions. All rights reserved.
  15. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  16. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  17. */
  18. class Sabre_HTTP_Request {
  19. /**
  20. * PHP's $_SERVER data
  21. *
  22. * @var string
  23. */
  24. protected $_SERVER;
  25. /**
  26. * The request body, if any.
  27. *
  28. * This is stored in the form of a stream resource.
  29. *
  30. * @var resource
  31. */
  32. protected $body = null;
  33. /**
  34. * This will be set as the 'default' inputStream for a specific HTTP request
  35. * We sometimes need to retain, or rebuild this if we need multiple runs
  36. * of parsing the original HTTP request.
  37. *
  38. * @var resource
  39. */
  40. static $defaultInputStream=null;
  41. /**
  42. * Sets up the object
  43. *
  44. * The serverData array can be used to override usage of PHP's
  45. * global _SERVER variable.
  46. *
  47. * @param array $serverData
  48. */
  49. public function __construct($serverData = null) {
  50. if ($serverData) $this->_SERVER = $serverData;
  51. else $this->_SERVER =& $_SERVER;
  52. }
  53. /**
  54. * Returns the value for a specific http header.
  55. *
  56. * This method returns null if the header did not exist.
  57. *
  58. * @param string $name
  59. * @return string
  60. */
  61. public function getHeader($name) {
  62. $serverName = 'HTTP_' . strtoupper(str_replace(array('-'),array('_'),$name));
  63. return isset($this->_SERVER[$serverName])?$this->_SERVER[$serverName]:null;
  64. }
  65. /**
  66. * Returns all (known) HTTP headers.
  67. *
  68. * All headers are converted to lower-case, and additionally all underscores
  69. * are automatically converted to dashes
  70. *
  71. * @return array
  72. */
  73. public function getHeaders() {
  74. $hdrs = array();
  75. foreach($this->_SERVER as $key=>$value) {
  76. if (strpos($key,'HTTP_')===0) {
  77. $hdrs[substr(strtolower(str_replace('_','-',$key)),5)] = $value;
  78. }
  79. }
  80. return $hdrs;
  81. }
  82. /**
  83. * Returns the HTTP request method
  84. *
  85. * This is for example POST or GET
  86. *
  87. * @return string
  88. */
  89. public function getMethod() {
  90. return $this->_SERVER['REQUEST_METHOD'];
  91. }
  92. /**
  93. * Returns the requested uri
  94. *
  95. * @return string
  96. */
  97. public function getUri() {
  98. return $this->_SERVER['REQUEST_URI'];
  99. }
  100. /**
  101. * Will return protocol + the hostname + the uri
  102. *
  103. * @return void
  104. */
  105. public function getAbsoluteUri() {
  106. // Checking if the request was made through HTTPS. The last in line is for IIS
  107. $protocol = isset($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']!='off');
  108. return ($protocol?'https':'http') . '://' . $this->getHeader('Host') . $this->getUri();
  109. }
  110. /**
  111. * Returns everything after the ? from the current url
  112. *
  113. * @return string
  114. */
  115. public function getQueryString() {
  116. return isset($this->_SERVER['QUERY_STRING'])?$this->_SERVER['QUERY_STRING']:'';
  117. }
  118. /**
  119. * Returns the HTTP request body body
  120. *
  121. * This method returns a readable stream resource.
  122. * If the asString parameter is set to true, a string is sent instead.
  123. *
  124. * @param bool asString
  125. * @return resource
  126. */
  127. public function getBody($asString = false) {
  128. if (is_null($this->body)) {
  129. if (!is_null(self::$defaultInputStream)) {
  130. $this->body = self::$defaultInputStream;
  131. } else {
  132. $this->body = fopen('php://input','r');
  133. self::$defaultInputStream = $this->body;
  134. }
  135. }
  136. if ($asString) {
  137. $body = stream_get_contents($this->body);
  138. return $body;
  139. } else {
  140. return $this->body;
  141. }
  142. }
  143. /**
  144. * Sets the contents of the HTTP requet body
  145. *
  146. * This method can either accept a string, or a readable stream resource.
  147. *
  148. * If the setAsDefaultInputStream is set to true, it means for this run of the
  149. * script the supplied body will be used instead of php://input.
  150. *
  151. * @param mixed $body
  152. * @param bool $setAsDefaultInputStream
  153. * @return void
  154. */
  155. public function setBody($body,$setAsDefaultInputStream = false) {
  156. if(is_resource($body)) {
  157. $this->body = $body;
  158. } else {
  159. $stream = fopen('php://temp','r+');
  160. fputs($stream,$body);
  161. rewind($stream);
  162. // String is assumed
  163. $this->body = $stream;
  164. }
  165. if ($setAsDefaultInputStream) {
  166. self::$defaultInputStream = $this->body;
  167. }
  168. }
  169. /**
  170. * Returns a specific item from the _SERVER array.
  171. *
  172. * Do not rely on this feature, it is for internal use only.
  173. *
  174. * @param string $field
  175. * @return string
  176. */
  177. public function getRawServerValue($field) {
  178. return isset($this->_SERVER[$field])?$this->_SERVER[$field]:null;
  179. }
  180. }