aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/lib/SabreDAV/lib/OldSabre/HTTP/Request.php
blob: d4cad65b294e5d383b163bf77f65150e03a738d5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php

namespace OldSabre\HTTP;

/**
 * HTTP Request information
 *
 * This object can be used to easily access information about an HTTP request.
 * It can additionally be used to create 'mock' requests.
 *
 * This class mostly operates independent, but because of the nature of a single
 * request per run it can operate as a singleton. For more information check out
 * the behaviour around 'defaultInputStream'.
 *
 * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
 * @author Evert Pot (http://evertpot.com/)
 * @license http://sabre.io/license/ Modified BSD License
 */
class Request {

    /**
     * PHP's $_SERVER data
     *
     * @var array
     */
    protected $_SERVER;

    /**
     * PHP's $_POST data
     *
     * @var array
     */
    protected $_POST;

    /**
     * The request body, if any.
     *
     * This is stored in the form of a stream resource.
     *
     * @var resource
     */
    protected $body = null;

    /**
     * This will be set as the 'default' inputStream for a specific HTTP request
     * We sometimes need to retain, or rebuild this if we need multiple runs
     * of parsing the original HTTP request.
     *
     * @var resource
     */
    static $defaultInputStream=null;

    /**
     * Sets up the object
     *
     * The serverData and postData array can be used to override usage of PHP's
     * global _SERVER and _POST variable respectively.
     *
     * @param array $serverData
     * @param array $postData
     */
    public function __construct(array $serverData = null, array $postData = null) {

       if ($serverData) $this->_SERVER = $serverData;
       else $this->_SERVER =& $_SERVER;

       if ($postData) $this->_POST = $postData;
       else $this->_POST =& $_POST;

    }

    /**
     * Returns the value for a specific http header.
     *
     * This method returns null if the header did not exist.
     *
     * @param string $name
     * @return string
     */
    public function getHeader($name) {

        $name = strtoupper(str_replace(array('-'),array('_'),$name));
        if (isset($this->_SERVER['HTTP_' . $name])) {
            return $this->_SERVER['HTTP_' . $name];
        }

        // There's a few headers that seem to end up in the top-level
        // server array.
        switch($name) {
            case 'CONTENT_TYPE' :
            case 'CONTENT_LENGTH' :
                if (isset($this->_SERVER[$name])) {
                    return $this->_SERVER[$name];
                }
                break;

        }
        return;

    }

    /**
     * Returns all (known) HTTP headers.
     *
     * All headers are converted to lower-case, and additionally all underscores
     * are automatically converted to dashes
     *
     * @return array
     */
    public function getHeaders() {

        $hdrs = array();
        foreach($this->_SERVER as $key=>$value) {

            switch($key) {
                case 'CONTENT_LENGTH' :
                case 'CONTENT_TYPE' :
                    $hdrs[strtolower(str_replace('_','-',$key))] = $value;
                    break;
                default :
                    if (strpos($key,'HTTP_')===0) {
                        $hdrs[substr(strtolower(str_replace('_','-',$key)),5)] = $value;
                    }
                    break;
            }

        }

        return $hdrs;

    }

    /**
     * Returns the HTTP request method
     *
     * This is for example POST or GET
     *
     * @return string
     */
    public function getMethod() {

        return $this->_SERVER['REQUEST_METHOD'];

    }

    /**
     * Returns the requested uri
     *
     * @return string
     */
    public function getUri() {

        return $this->_SERVER['REQUEST_URI'];

    }

    /**
     * Will return protocol + the hostname + the uri
     *
     * @return string
     */
    public function getAbsoluteUri() {

        // Checking if the request was made through HTTPS. The last in line is for IIS
        $protocol = isset($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']) && ($this->_SERVER['HTTPS']!='off');
        return ($protocol?'https':'http') . '://'  . $this->getHeader('Host') . $this->getUri();

    }

    /**
     * Returns everything after the ? from the current url
     *
     * @return string
     */
    public function getQueryString() {

        return isset($this->_SERVER['QUERY_STRING'])?$this->_SERVER['QUERY_STRING']:'';

    }

    /**
     * Returns the HTTP request body body
     *
     * This method returns a readable stream resource.
     * If the asString parameter is set to true, a string is sent instead.
     *
     * @param bool $asString
     * @return resource
     */
    public function getBody($asString = false) {

        if (is_null($this->body)) {
            if (!is_null(self::$defaultInputStream)) {
                $this->body = self::$defaultInputStream;
            } else {
                $this->body = fopen('php://input','r');
                self::$defaultInputStream = $this->body;
            }
        }
        if ($asString) {
            $body = stream_get_contents($this->body);
            return $body;
        } else {
            return $this->body;
        }

    }

    /**
     * Sets the contents of the HTTP request body
     *
     * This method can either accept a string, or a readable stream resource.
     *
     * If the setAsDefaultInputStream is set to true, it means for this run of the
     * script the supplied body will be used instead of php://input.
     *
     * @param mixed $body
     * @param bool $setAsDefaultInputStream
     * @return void
     */
    public function setBody($body,$setAsDefaultInputStream = false) {

        if(is_resource($body)) {
            $this->body = $body;
        } else {

            $stream = fopen('php://temp','r+');
            fputs($stream,$body);
            rewind($stream);
            // String is assumed
            $this->body = $stream;
        }
        if ($setAsDefaultInputStream) {
            self::$defaultInputStream = $this->body;
        }

    }

    /**
     * Returns PHP's _POST variable.
     *
     * The reason this is in a method is so it can be subclassed and
     * overridden.
     *
     * @return array
     */
    public function getPostVars() {

        return $this->_POST;

    }

    /**
     * Returns a specific item from the _SERVER array.
     *
     * Do not rely on this feature, it is for internal use only.
     *
     * @param string $field
     * @return string
     */
    public function getRawServerValue($field) {

        return isset($this->_SERVER[$field])?$this->_SERVER[$field]:null;

    }

    /**
     * Returns the HTTP version specified within the request.
     *
     * @return string
     */
    public function getHTTPVersion() {

        $protocol = $this->getRawServerValue('SERVER_PROTOCOL');
        if ($protocol==='HTTP/1.0') {
            return '1.0';
        } else {
            return '1.1';
        }

    }

}

© 2014-2024 Faster IT GmbH | imprint | privacy policy