]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/HTTP/Request.php
Initial Release of the calendar plugin
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / HTTP / Request.php
1 <?php
2
3 /**
4  * HTTP Request information
5  *
6  * This object can be used to easily access information about an HTTP request.
7  * It can additionally be used to create 'mock' requests.
8  *
9  * This class mostly operates independent, but because of the nature of a single
10  * request per run it can operate as a singleton. For more information check out
11  * the behaviour around 'defaultInputStream'.
12  *
13  * @package Sabre
14  * @subpackage HTTP
15  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
16  * @author Evert Pot (http://www.rooftopsolutions.nl/)
17  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
18  */
19 class Sabre_HTTP_Request {
20
21     /**
22      * PHP's $_SERVER data
23      *
24      * @var array
25      */
26     protected $_SERVER;
27
28     /**
29      * PHP's $_POST data
30      *
31      * @var array
32      */
33     protected $_POST;
34
35     /**
36      * The request body, if any.
37      *
38      * This is stored in the form of a stream resource.
39      *
40      * @var resource
41      */
42     protected $body = null;
43
44     /**
45      * This will be set as the 'default' inputStream for a specific HTTP request
46      * We sometimes need to retain, or rebuild this if we need multiple runs
47      * of parsing the original HTTP request.
48      *
49      * @var resource
50      */
51     static $defaultInputStream=null;
52
53     /**
54      * Sets up the object
55      *
56      * The serverData and postData array can be used to override usage of PHP's
57      * global _SERVER and _POST variable respectively.
58      *
59      * @param array $serverData
60      * @param array $postData
61      */
62     public function __construct(array $serverData = null, array $postData = null) {
63
64        if ($serverData) $this->_SERVER = $serverData;
65        else $this->_SERVER =& $_SERVER;
66
67        if ($postData) $this->_POST = $postData;
68        else $this->_POST =& $_POST;
69
70     }
71
72     /**
73      * Returns the value for a specific http header.
74      *
75      * This method returns null if the header did not exist.
76      *
77      * @param string $name
78      * @return string
79      */
80     public function getHeader($name) {
81
82         $name = strtoupper(str_replace(array('-'),array('_'),$name));
83         if (isset($this->_SERVER['HTTP_' . $name])) {
84             return $this->_SERVER['HTTP_' . $name];
85         }
86
87         // There's a few headers that seem to end up in the top-level
88         // server array.
89         switch($name) {
90             case 'CONTENT_TYPE' :
91             case 'CONTENT_LENGTH' :
92                 if (isset($this->_SERVER[$name])) {
93                     return $this->_SERVER[$name];
94                 }
95                 break;
96
97         }
98         return;
99
100     }
101
102     /**
103      * Returns all (known) HTTP headers.
104      *
105      * All headers are converted to lower-case, and additionally all underscores
106      * are automatically converted to dashes
107      *
108      * @return array
109      */
110     public function getHeaders() {
111
112         $hdrs = array();
113         foreach($this->_SERVER as $key=>$value) {
114
115             switch($key) {
116                 case 'CONTENT_LENGTH' :
117                 case 'CONTENT_TYPE' :
118                     $hdrs[strtolower(str_replace('_','-',$key))] = $value;
119                     break;
120                 default :
121                     if (strpos($key,'HTTP_')===0) {
122                         $hdrs[substr(strtolower(str_replace('_','-',$key)),5)] = $value;
123                     }
124                     break;
125             }
126
127         }
128
129         return $hdrs;
130
131     }
132
133     /**
134      * Returns the HTTP request method
135      *
136      * This is for example POST or GET
137      *
138      * @return string
139      */
140     public function getMethod() {
141
142         return $this->_SERVER['REQUEST_METHOD'];
143
144     }
145
146     /**
147      * Returns the requested uri
148      *
149      * @return string
150      */
151     public function getUri() {
152
153         return $this->_SERVER['REQUEST_URI'];
154
155     }
156
157     /**
158      * Will return protocol + the hostname + the uri
159      *
160      * @return string
161      */
162     public function getAbsoluteUri() {
163
164         // Checking if the request was made through HTTPS. The last in line is for IIS
165         $protocol = isset($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']!='off');
166         return ($protocol?'https':'http') . '://'  . $this->getHeader('Host') . $this->getUri();
167
168     }
169
170     /**
171      * Returns everything after the ? from the current url
172      *
173      * @return string
174      */
175     public function getQueryString() {
176
177         return isset($this->_SERVER['QUERY_STRING'])?$this->_SERVER['QUERY_STRING']:'';
178
179     }
180
181     /**
182      * Returns the HTTP request body body
183      *
184      * This method returns a readable stream resource.
185      * If the asString parameter is set to true, a string is sent instead.
186      *
187      * @param bool $asString
188      * @return resource
189      */
190     public function getBody($asString = false) {
191
192         if (is_null($this->body)) {
193             if (!is_null(self::$defaultInputStream)) {
194                 $this->body = self::$defaultInputStream;
195             } else {
196                 $this->body = fopen('php://input','r');
197                 self::$defaultInputStream = $this->body;
198             }
199         }
200         if ($asString) {
201             $body = stream_get_contents($this->body);
202             return $body;
203         } else {
204             return $this->body;
205         }
206
207     }
208
209     /**
210      * Sets the contents of the HTTP request body
211      *
212      * This method can either accept a string, or a readable stream resource.
213      *
214      * If the setAsDefaultInputStream is set to true, it means for this run of the
215      * script the supplied body will be used instead of php://input.
216      *
217      * @param mixed $body
218      * @param bool $setAsDefaultInputStream
219      * @return void
220      */
221     public function setBody($body,$setAsDefaultInputStream = false) {
222
223         if(is_resource($body)) {
224             $this->body = $body;
225         } else {
226
227             $stream = fopen('php://temp','r+');
228             fputs($stream,$body);
229             rewind($stream);
230             // String is assumed
231             $this->body = $stream;
232         }
233         if ($setAsDefaultInputStream) {
234             self::$defaultInputStream = $this->body;
235         }
236
237     }
238
239     /**
240      * Returns PHP's _POST variable.
241      *
242      * The reason this is in a method is so it can be subclassed and
243      * overridden.
244      *
245      * @return array
246      */
247     public function getPostVars() {
248
249         return $this->_POST;
250
251     }
252
253     /**
254      * Returns a specific item from the _SERVER array.
255      *
256      * Do not rely on this feature, it is for internal use only.
257      *
258      * @param string $field
259      * @return string
260      */
261     public function getRawServerValue($field) {
262
263         return isset($this->_SERVER[$field])?$this->_SERVER[$field]:null;
264
265     }
266
267 }
268