/webdav/vendors/SabreDAV/lib/Sabre/HTTP/Request.php

https://github.com/joannalz/YiiBlocks · PHP · 243 lines · 87 code · 42 blank · 114 comment · 15 complexity · b493b2eb4f5885a43daa8e9dbeb65d25 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) 2007-2011 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. $name = strtoupper(str_replace(array('-'),array('_'),$name));
  63. if (isset($this->_SERVER['HTTP_' . $name])) {
  64. return $this->_SERVER['HTTP_' . $name];
  65. }
  66. // There's a few headers that seem to end up in the top-level
  67. // server array.
  68. switch($name) {
  69. case 'CONTENT_TYPE' :
  70. case 'CONTENT_LENGTH' :
  71. if (isset($this->_SERVER[$name])) {
  72. return $this->_SERVER[$name];
  73. }
  74. break;
  75. }
  76. return;
  77. }
  78. /**
  79. * Returns all (known) HTTP headers.
  80. *
  81. * All headers are converted to lower-case, and additionally all underscores
  82. * are automatically converted to dashes
  83. *
  84. * @return array
  85. */
  86. public function getHeaders() {
  87. $hdrs = array();
  88. foreach($this->_SERVER as $key=>$value) {
  89. switch($key) {
  90. case 'CONTENT_LENGTH' :
  91. case 'CONTENT_TYPE' :
  92. $hdrs[strtolower(str_replace('_','-',$key))] = $value;
  93. break;
  94. default :
  95. if (strpos($key,'HTTP_')===0) {
  96. $hdrs[substr(strtolower(str_replace('_','-',$key)),5)] = $value;
  97. }
  98. break;
  99. }
  100. }
  101. return $hdrs;
  102. }
  103. /**
  104. * Returns the HTTP request method
  105. *
  106. * This is for example POST or GET
  107. *
  108. * @return string
  109. */
  110. public function getMethod() {
  111. return $this->_SERVER['REQUEST_METHOD'];
  112. }
  113. /**
  114. * Returns the requested uri
  115. *
  116. * @return string
  117. */
  118. public function getUri() {
  119. return $this->_SERVER['REQUEST_URI'];
  120. }
  121. /**
  122. * Will return protocol + the hostname + the uri
  123. *
  124. * @return void
  125. */
  126. public function getAbsoluteUri() {
  127. // Checking if the request was made through HTTPS. The last in line is for IIS
  128. $protocol = isset($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']!='off');
  129. return ($protocol?'https':'http') . '://' . $this->getHeader('Host') . $this->getUri();
  130. }
  131. /**
  132. * Returns everything after the ? from the current url
  133. *
  134. * @return string
  135. */
  136. public function getQueryString() {
  137. return isset($this->_SERVER['QUERY_STRING'])?$this->_SERVER['QUERY_STRING']:'';
  138. }
  139. /**
  140. * Returns the HTTP request body body
  141. *
  142. * This method returns a readable stream resource.
  143. * If the asString parameter is set to true, a string is sent instead.
  144. *
  145. * @param bool asString
  146. * @return resource
  147. */
  148. public function getBody($asString = false) {
  149. if (is_null($this->body)) {
  150. if (!is_null(self::$defaultInputStream)) {
  151. $this->body = self::$defaultInputStream;
  152. } else {
  153. $this->body = fopen('php://input','r');
  154. self::$defaultInputStream = $this->body;
  155. }
  156. }
  157. if ($asString) {
  158. $body = stream_get_contents($this->body);
  159. return $body;
  160. } else {
  161. return $this->body;
  162. }
  163. }
  164. /**
  165. * Sets the contents of the HTTP request body
  166. *
  167. * This method can either accept a string, or a readable stream resource.
  168. *
  169. * If the setAsDefaultInputStream is set to true, it means for this run of the
  170. * script the supplied body will be used instead of php://input.
  171. *
  172. * @param mixed $body
  173. * @param bool $setAsDefaultInputStream
  174. * @return void
  175. */
  176. public function setBody($body,$setAsDefaultInputStream = false) {
  177. if(is_resource($body)) {
  178. $this->body = $body;
  179. } else {
  180. $stream = fopen('php://temp','r+');
  181. fputs($stream,$body);
  182. rewind($stream);
  183. // String is assumed
  184. $this->body = $stream;
  185. }
  186. if ($setAsDefaultInputStream) {
  187. self::$defaultInputStream = $this->body;
  188. }
  189. }
  190. /**
  191. * Returns a specific item from the _SERVER array.
  192. *
  193. * Do not rely on this feature, it is for internal use only.
  194. *
  195. * @param string $field
  196. * @return string
  197. */
  198. public function getRawServerValue($field) {
  199. return isset($this->_SERVER[$field])?$this->_SERVER[$field]:null;
  200. }
  201. }