1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Marius Orcsik <[email protected]> |
4
|
|
|
* @created 2015-07-28 |
5
|
|
|
*/ |
6
|
|
|
namespace vsc\presentation\responses; |
7
|
|
|
|
8
|
|
|
use vsc\infrastructure\vsc; |
9
|
|
|
|
10
|
|
|
trait HttpHeadersTrait { |
11
|
|
|
/** |
12
|
|
|
* @return string |
13
|
|
|
* @see HttpResponseA::getServerProtocol |
14
|
|
|
*/ |
15
|
|
|
abstract public function getServerProtocol(); |
16
|
|
|
/** |
17
|
|
|
* @return int |
18
|
|
|
* @see HttpResponseA::getStatus |
19
|
|
|
*/ |
20
|
|
|
abstract public function getStatus(); |
21
|
|
|
/** |
22
|
|
|
* @return string |
23
|
|
|
* @see HttpResponseA::getLocation |
24
|
|
|
*/ |
25
|
|
|
abstract public function getLocation(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
* @see HttpResponseA::getContentType |
30
|
|
|
*/ |
31
|
|
|
abstract public function getContentType(); |
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
* @see HttpResponseA::getContentDisposition |
35
|
|
|
*/ |
36
|
|
|
abstract public function getContentDisposition(); |
37
|
|
|
/** |
38
|
|
|
* @return string |
39
|
|
|
* @see HttpResponseA::getContentEncoding |
40
|
|
|
*/ |
41
|
|
|
abstract public function getContentEncoding(); |
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
* @see HttpResponseA::getContentLanguage |
45
|
|
|
*/ |
46
|
|
|
abstract public function getContentLanguage(); |
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
* @see HttpResponseA::getContentLength |
50
|
|
|
*/ |
51
|
|
|
abstract public function getContentLength(); |
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
* @see HttpResponseA::getContentLocation |
55
|
|
|
*/ |
56
|
|
|
abstract public function getContentLocation(); |
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
* @see HttpResponseA::getContentMd5 |
60
|
|
|
*/ |
61
|
|
|
abstract public function getContentMd5(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
* @see HttpResponseA::getCacheControl |
66
|
|
|
*/ |
67
|
|
|
abstract public function getCacheControl(); |
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
* @see HttpResponseA::getDate |
71
|
|
|
*/ |
72
|
|
|
abstract public function getDate(); |
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
* @see HttpResponseA::getETag |
76
|
|
|
*/ |
77
|
|
|
abstract public function getETag(); |
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
* @see HttpResponseA::getExpires |
81
|
|
|
*/ |
82
|
|
|
abstract public function getExpires(); |
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
* @see HttpResponseA::getLastModified |
86
|
|
|
*/ |
87
|
|
|
abstract public function getLastModified(); |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
* @see HttpResponseA::getCustomHeaders |
92
|
|
|
*/ |
93
|
|
|
abstract protected function getCustomHeaders(); |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
1 |
|
public function outputStatusHeader () { |
100
|
1 |
|
if (vsc::isCli()) { return false; } |
101
|
|
|
if ($this->getStatus()) { |
102
|
|
|
header(HttpResponseA::getHttpStatusString($this->getServerProtocol(), $this->getStatus())); |
103
|
|
|
} |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
1 |
|
protected function outputLocationHeaders() { |
111
|
1 |
|
if (vsc::isCli()) { return false; } |
112
|
|
|
$sLocation = $this->getLocation(); |
113
|
|
|
if ($sLocation) { |
114
|
|
|
header('Location:' . $sLocation); |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
1 |
|
public function outputContentHeaders() { |
124
|
1 |
|
if (vsc::isCli()) { return false; } |
125
|
|
|
$sContentType = $this->getContentType(); |
126
|
|
|
if ($sContentType) { |
127
|
|
|
header('Content-Type: ' . $sContentType); |
128
|
|
|
} |
129
|
|
|
$sContentDisposition = $this->getContentDisposition(); |
130
|
|
|
if ($sContentDisposition) { |
131
|
|
|
header('Content-Disposition: ' . $sContentDisposition); |
132
|
|
|
} |
133
|
|
|
$sContentEncoding = $this->getContentEncoding(); |
134
|
|
|
if ($sContentEncoding) { |
135
|
|
|
header('Content-Encoding: ' . $sContentEncoding); |
136
|
|
|
} |
137
|
|
|
$sContentLanguage = $this->getContentLanguage(); |
138
|
|
|
if ($sContentLanguage) { |
139
|
|
|
header('Content-Language: ' . $sContentLanguage); |
140
|
|
|
} |
141
|
|
|
$iContentLength = $this->getContentLength(); |
142
|
|
|
if ($iContentLength !== null) { |
143
|
|
|
header('Content-Length: ' . $iContentLength); |
144
|
|
|
} |
145
|
|
|
$sContentLocation = $this->getContentLocation(); |
146
|
|
|
if ($sContentLocation) { |
147
|
|
|
header('Content-Location: ' . $sContentLocation); |
148
|
|
|
} |
149
|
|
|
$sMd5 = $this->getContentMd5(); |
150
|
|
|
if ($sMd5) { |
151
|
|
|
header('Content-MD5: ' . $sMd5); |
152
|
|
|
} |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
1 |
|
protected function outputCachingHeaders() { |
160
|
1 |
|
if (vsc::isCli()) { return false; } |
161
|
|
|
$sCacheControl = $this->getCacheControl(); |
162
|
|
|
if ($sCacheControl) { |
163
|
|
|
header('Cache-Control: ' . $sCacheControl); |
164
|
|
|
} |
165
|
|
|
$sDate = $this->getDate(); |
166
|
|
|
if ($sDate) { |
167
|
|
|
header('Date: ' . $sDate); |
168
|
|
|
} |
169
|
|
|
$sETag = $this->getETag(); |
170
|
|
|
if ($sETag) { |
171
|
|
|
header('ETag: "' . $sETag . '"'); // the ETag is enclosed in quotes (i imagine it's because it might contain EOL's ?) |
172
|
|
|
} |
173
|
|
|
$sExpires = $this->getExpires(); |
174
|
|
|
if ($sExpires) { |
175
|
|
|
header('Expires: ' . $sExpires); |
176
|
|
|
} |
177
|
|
|
$sLastModified = $this->getLastModified(); |
178
|
|
|
if ($sLastModified) { |
179
|
|
|
header('Last-Modified: ' . $sLastModified); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
1 |
|
protected function outputCustomHeaders () { |
189
|
1 |
|
if (vsc::isCli()) { return false; } |
190
|
|
|
$aHeaders = $this->getCustomHeaders(); |
191
|
|
|
if (is_array($aHeaders)) { |
192
|
|
|
foreach ($aHeaders as $sHeaderName => $sHeaderValue) { |
193
|
|
|
if (is_null($sHeaderValue)) { |
194
|
|
|
header_remove($sHeaderName); |
195
|
|
|
} else { |
196
|
|
|
header($sHeaderName . ': ' . $sHeaderValue); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
return true; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
1 |
|
public function outputHeaders() { |
207
|
1 |
|
if (vsc::isCli()) { return false; } |
208
|
|
|
if (headers_sent()) { |
209
|
|
|
header_remove(); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$this->outputStatusHeader(); |
213
|
|
|
if ($this->outputLocationHeaders()) { |
214
|
|
|
return true; |
215
|
|
|
} |
216
|
|
|
$this->outputContentHeaders(); |
217
|
|
|
$this->outputCachingHeaders(); |
218
|
|
|
$this->outputCustomHeaders(); |
219
|
|
|
return true; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|