|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2015 zepi |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
15
|
|
|
* all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23
|
|
|
* THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The Response holds all information which are outputtet at the end |
|
29
|
|
|
* of the request. |
|
30
|
|
|
* |
|
31
|
|
|
* @package Zepi\Turbo\Response |
|
32
|
|
|
* @author Matthias Zobrist <[email protected]> |
|
33
|
|
|
* @copyright Copyright (c) 2015 zepi |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
namespace Zepi\Turbo\Response; |
|
37
|
|
|
|
|
38
|
|
|
use \Zepi\Turbo\Request\RequestAbstract; |
|
39
|
|
|
use \Zepi\Turbo\Request\WebRequest; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The Response holds all information which are outputtet at the end |
|
43
|
|
|
* of the request. |
|
44
|
|
|
* |
|
45
|
|
|
* @author Matthias Zobrist <[email protected]> |
|
46
|
|
|
* @copyright Copyright (c) 2015 zepi |
|
47
|
|
|
*/ |
|
48
|
|
|
class Response |
|
49
|
|
|
{ |
|
50
|
|
|
/** |
|
51
|
|
|
* @access protected |
|
52
|
|
|
* @var RequestAbstract |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $request; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @access protected |
|
58
|
|
|
* @var array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $data = array(); |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @access protected |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $outputParts = array(); |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @access protected |
|
70
|
|
|
* @var string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $output; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var boolean |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $outputLocked = false; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Constructs the object |
|
81
|
|
|
* |
|
82
|
|
|
* @access public |
|
83
|
|
|
* @param \Zepi\Turbo\Request\RequestAbstract $request |
|
84
|
|
|
*/ |
|
85
|
25 |
|
public function __construct(RequestAbstract $request) |
|
86
|
|
|
{ |
|
87
|
25 |
|
$this->request = $request; |
|
88
|
25 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return the data for the given key. If the key does |
|
92
|
|
|
* not exists the function will return false. |
|
93
|
|
|
* |
|
94
|
|
|
* @access public |
|
95
|
|
|
* @param string $key |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
4 |
|
public function getData($key) |
|
99
|
|
|
{ |
|
100
|
4 |
|
if (!$this->hasData($key)) { |
|
101
|
1 |
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
return $this->data[$key]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns true if the given key is set. |
|
109
|
|
|
* |
|
110
|
|
|
* @access public |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @return boolean |
|
113
|
|
|
*/ |
|
114
|
4 |
|
public function hasData($key) |
|
115
|
|
|
{ |
|
116
|
4 |
|
return (isset($this->data[$key])); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Saves the value for the given key in the response object. |
|
121
|
|
|
* |
|
122
|
|
|
* @access public |
|
123
|
|
|
* @param string $key |
|
124
|
|
|
* @param mixed $value |
|
125
|
|
|
*/ |
|
126
|
3 |
|
public function setData($key, $value) |
|
127
|
|
|
{ |
|
128
|
3 |
|
$this->data[$key] = $value; |
|
129
|
3 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the output for the given key. If the key does |
|
133
|
|
|
* not exists the function will return false. |
|
134
|
|
|
* |
|
135
|
|
|
* @access public |
|
136
|
|
|
* @param string $key |
|
137
|
|
|
* @return false|string |
|
138
|
|
|
*/ |
|
139
|
2 |
|
public function getOutputPart($key) |
|
140
|
|
|
{ |
|
141
|
2 |
|
if (!$this->hasOutputPart($key)) { |
|
142
|
1 |
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
return $this->outputParts[$key]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns true if the given key exists as output key. |
|
150
|
|
|
* |
|
151
|
|
|
* @access public |
|
152
|
|
|
* @param string $key |
|
153
|
|
|
* @return boolean |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function hasOutputPart($key) |
|
156
|
|
|
{ |
|
157
|
2 |
|
return (isset($this->outputParts[$key])); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Saves the output for the given key in the Response object. |
|
162
|
|
|
* |
|
163
|
|
|
* @access public |
|
164
|
|
|
* @param string $key |
|
165
|
|
|
* @param string $output |
|
166
|
|
|
*/ |
|
167
|
2 |
|
public function setOutputPart($key, $output) |
|
168
|
|
|
{ |
|
169
|
2 |
|
$this->outputParts[$key] = $output; |
|
170
|
2 |
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns all output parts of the Response object. |
|
174
|
|
|
* |
|
175
|
|
|
* @access public |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function getOutputParts() |
|
179
|
|
|
{ |
|
180
|
1 |
|
return $this->outputParts; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns the output of the response. |
|
185
|
|
|
* |
|
186
|
|
|
* @access public |
|
187
|
|
|
* @return string |
|
188
|
|
|
*/ |
|
189
|
3 |
|
public function getOutput() |
|
190
|
|
|
{ |
|
191
|
3 |
|
return $this->output; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Returns true if the response has an output. |
|
196
|
|
|
* |
|
197
|
|
|
* @access public |
|
198
|
|
|
* @return boolean |
|
199
|
|
|
*/ |
|
200
|
1 |
|
public function hasOutput() |
|
201
|
|
|
{ |
|
202
|
1 |
|
return ($this->output != ''); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Sets the output of the response. |
|
207
|
|
|
* If $lock is true the output will be locked and the method |
|
208
|
|
|
* will not accept any other output. |
|
209
|
|
|
* |
|
210
|
|
|
* @access public |
|
211
|
|
|
* @param string $output |
|
212
|
|
|
* @param boolean $lock |
|
213
|
|
|
* @return boolean |
|
214
|
|
|
*/ |
|
215
|
1 |
|
public function setOutput($output, $lock = false) |
|
216
|
|
|
{ |
|
217
|
1 |
|
if ($this->outputLocked) { |
|
218
|
|
|
return false; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
1 |
|
if ($lock) { |
|
222
|
|
|
$this->outputLocked = true; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
1 |
|
$this->output = $output; |
|
226
|
1 |
|
return true; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Returns true if the output in the response is locked. |
|
231
|
|
|
* |
|
232
|
|
|
* @return boolean |
|
233
|
|
|
*/ |
|
234
|
|
|
public function isOutputLocked() |
|
235
|
|
|
{ |
|
236
|
|
|
return ($this->outputLocked); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Set the Location header to redirect a request |
|
241
|
|
|
* |
|
242
|
|
|
* @access public |
|
243
|
|
|
* @param string $target |
|
244
|
|
|
* @param integer $headerCode |
|
245
|
|
|
* @param boolean $withOrigin |
|
246
|
|
|
*/ |
|
247
|
2 |
|
public function redirectTo($target, $headerCode = 301, $withOrigin = false) |
|
248
|
|
|
{ |
|
249
|
2 |
|
if (!($this->request instanceof WebRequest)) { |
|
250
|
|
|
return; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
2 |
|
if (!preg_match('/http(s?)\:\/\//', $target)) { |
|
254
|
1 |
|
$target = $this->request->getFullRoute($target); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
2 |
|
if ($withOrigin) { |
|
258
|
|
|
$target = $this->addOriginToTargetUrl($target); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
2 |
|
header("Location: " . $target, true, $headerCode); |
|
262
|
2 |
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Sends a header |
|
266
|
|
|
* |
|
267
|
|
|
* @access public |
|
268
|
|
|
* @param string $message |
|
269
|
|
|
* @param integer $code |
|
270
|
|
|
*/ |
|
271
|
|
|
public function sendHeader($message, $code = null) |
|
272
|
|
|
{ |
|
273
|
|
|
if (!($this->request instanceof WebRequest)) { |
|
274
|
|
|
return; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
if ($code !== null) { |
|
278
|
|
|
header($message, true, $code); |
|
279
|
|
|
} else { |
|
280
|
|
|
header($message); |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Sends the status code for the give code |
|
286
|
|
|
* |
|
287
|
|
|
* @access public |
|
288
|
|
|
* @param integer $code |
|
289
|
|
|
* @param boolean $resetOutput |
|
290
|
|
|
*/ |
|
291
|
|
|
public function sendHttpStatus($code, $resetOutput = false) |
|
292
|
|
|
{ |
|
293
|
|
|
if (!($this->request instanceof WebRequest)) { |
|
294
|
|
|
return; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
$codes = array( |
|
298
|
|
|
100 => 'Continue', |
|
299
|
|
|
101 => 'Switching Protocols', |
|
300
|
|
|
102 => 'Processing', |
|
301
|
|
|
200 => 'OK', |
|
302
|
|
|
201 => 'Created', |
|
303
|
|
|
202 => 'Accepted', |
|
304
|
|
|
203 => 'Non-Authoritative Information', |
|
305
|
|
|
204 => 'No Content', |
|
306
|
|
|
205 => 'Reset Content', |
|
307
|
|
|
206 => 'Partial Content', |
|
308
|
|
|
207 => 'Multi-Status', |
|
309
|
|
|
300 => 'Multiple Choices', |
|
310
|
|
|
301 => 'Moved Permanently', |
|
311
|
|
|
302 => 'Found', |
|
312
|
|
|
303 => 'See Other', |
|
313
|
|
|
304 => 'Not Modified', |
|
314
|
|
|
305 => 'Use Proxy', |
|
315
|
|
|
306 => 'Switch Proxy', |
|
316
|
|
|
307 => 'Temporary Redirect', |
|
317
|
|
|
400 => 'Bad Request', |
|
318
|
|
|
401 => 'Unauthorized', |
|
319
|
|
|
402 => 'Payment Required', |
|
320
|
|
|
403 => 'Forbidden', |
|
321
|
|
|
404 => 'Not Found', |
|
322
|
|
|
405 => 'Method Not Allowed', |
|
323
|
|
|
406 => 'Not Acceptable', |
|
324
|
|
|
407 => 'Proxy Authentication Required', |
|
325
|
|
|
408 => 'Request Timeout', |
|
326
|
|
|
409 => 'Conflict', |
|
327
|
|
|
410 => 'Gone', |
|
328
|
|
|
411 => 'Length Required', |
|
329
|
|
|
412 => 'Precondition Failed', |
|
330
|
|
|
413 => 'Request Entity Too Large', |
|
331
|
|
|
414 => 'Request-URI Too Long', |
|
332
|
|
|
415 => 'Unsupported Media Type', |
|
333
|
|
|
416 => 'Requested Range Not Satisfiable', |
|
334
|
|
|
417 => 'Expectation Failed', |
|
335
|
|
|
418 => 'I\'m a teapot', |
|
336
|
|
|
422 => 'Unprocessable Entity', |
|
337
|
|
|
423 => 'Locked', |
|
338
|
|
|
424 => 'Failed Dependency', |
|
339
|
|
|
425 => 'Unordered Collection', |
|
340
|
|
|
426 => 'Upgrade Required', |
|
341
|
|
|
449 => 'Retry With', |
|
342
|
|
|
450 => 'Blocked by Windows Parental Controls', |
|
343
|
|
|
500 => 'Internal Server Error', |
|
344
|
|
|
501 => 'Not Implemented', |
|
345
|
|
|
502 => 'Bad Gateway', |
|
346
|
|
|
503 => 'Service Unavailable', |
|
347
|
|
|
504 => 'Gateway Timeout', |
|
348
|
|
|
505 => 'HTTP Version Not Supported', |
|
349
|
|
|
506 => 'Variant Also Negotiates', |
|
350
|
|
|
507 => 'Insufficient Storage', |
|
351
|
|
|
509 => 'Bandwidth Limit Exceeded', |
|
352
|
|
|
510 => 'Not Extended' |
|
353
|
|
|
); |
|
354
|
|
|
|
|
355
|
|
|
if (!isset($codes[$code])) { |
|
356
|
|
|
return false; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
$message = $this->request->getProtocol() . ' ' . $code . ' ' . $codes[$code]; |
|
360
|
|
|
header($message, true, $code); |
|
361
|
|
|
|
|
362
|
|
|
if ($resetOutput) { |
|
363
|
|
|
$this->setOutput($message); |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
|
|
367
|
|
|
/** |
|
368
|
|
|
* Returns a full url for the given url parts array |
|
369
|
|
|
* from the function `parse_url()`. |
|
370
|
|
|
* |
|
371
|
|
|
* @access public |
|
372
|
|
|
* @param array $urlParts |
|
373
|
|
|
* @return string |
|
374
|
|
|
*/ |
|
375
|
|
|
public function buildUrl($urlParts) |
|
376
|
|
|
{ |
|
377
|
|
|
return http_build_url($urlParts); |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Adds the origin and returns the given target url |
|
382
|
|
|
* with the origin query parameter. |
|
383
|
|
|
* |
|
384
|
|
|
* @param string $target |
|
385
|
|
|
* @return void|string |
|
386
|
|
|
*/ |
|
387
|
|
|
protected function addOriginToTargetUrl($target) |
|
388
|
|
|
{ |
|
389
|
|
|
$origin = $this->request->getFullRoute(); |
|
390
|
|
|
$additionalQuery = '_origin=' . base64_encode($origin); |
|
391
|
|
|
|
|
392
|
|
|
$parts = parse_url($target); |
|
393
|
|
|
|
|
394
|
|
|
if ($parts === false) { |
|
395
|
|
|
return $target; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
if (!isset($parts['query'])) { |
|
399
|
|
|
$parts['query'] = ''; |
|
400
|
|
|
} else if ($parts['query'] !== '') { |
|
401
|
|
|
$parts['query'] .= '&'; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
$parts['query'] .= $additionalQuery; |
|
405
|
|
|
return $this->buildUrl($parts); |
|
406
|
|
|
} |
|
407
|
|
|
} |
|
408
|
|
|
|