|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pimf |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Pimf; |
|
9
|
|
|
|
|
10
|
|
|
use \Pimf\Util\Header, Pimf\Util\Json as UtilJson; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides a simple interface around the HTTP an HTTPCache-friendly response generating. |
|
14
|
|
|
* Use this class to build and the current HTTP response before it is returned to the client. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Pimf |
|
17
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class Response |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The request method send by the client-browser. |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $requestMethod = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* If the response attempts to send any cached headers. |
|
30
|
|
|
* |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $cached = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Type of the data will be send to the client-browser. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected static $typed = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $requestMethod |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \RuntimeException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($requestMethod) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->requestMethod = '' . strtoupper($requestMethod); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function asJSON() |
|
53
|
|
|
{ |
|
54
|
|
|
self::$typed = __FUNCTION__; |
|
55
|
|
|
Header::asJSON(); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function asHTML() |
|
61
|
|
|
{ |
|
62
|
|
|
self::$typed = __FUNCTION__; |
|
63
|
|
|
Header::asTextHTML(); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function asPDF() |
|
69
|
|
|
{ |
|
70
|
|
|
self::$typed = __FUNCTION__; |
|
71
|
|
|
Header::asPDF(); |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function asCSV() |
|
77
|
|
|
{ |
|
78
|
|
|
self::$typed = __FUNCTION__; |
|
79
|
|
|
Header::asCSV(); |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function asTEXT() |
|
85
|
|
|
{ |
|
86
|
|
|
self::$typed = __FUNCTION__; |
|
87
|
|
|
Header::asTextPlain(); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function asZIP() |
|
93
|
|
|
{ |
|
94
|
|
|
self::$typed = __FUNCTION__; |
|
95
|
|
|
Header::asZIP(); |
|
96
|
|
|
|
|
97
|
|
|
return $this; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function asXZIP() |
|
101
|
|
|
{ |
|
102
|
|
|
self::$typed = __FUNCTION__; |
|
103
|
|
|
Header::asXZIP(); |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function asMSWord() |
|
109
|
|
|
{ |
|
110
|
|
|
self::$typed = __FUNCTION__; |
|
111
|
|
|
Header::asMSWord(); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Sends a download dialog to the browser. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $stream Can be a file-path or a string. |
|
120
|
|
|
* @param string $name Name of the stream/file should be shown. |
|
121
|
|
|
* @param boolean $exit Optional for testing |
|
122
|
|
|
*/ |
|
123
|
|
|
public function sendStream($stream, $name, $exit = true) |
|
124
|
|
|
{ |
|
125
|
|
|
Header::sendDownloadDialog($stream, $name, $exit); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param mixed $data |
|
130
|
|
|
* @param bool $exit |
|
131
|
|
|
*/ |
|
132
|
|
|
public function send($data, $exit = true) |
|
133
|
|
|
{ |
|
134
|
|
|
$body = $data; |
|
135
|
|
|
|
|
136
|
|
|
if (self::$typed === 'asJSON') { |
|
137
|
|
|
$body = UtilJson::encode($data); |
|
138
|
|
|
} elseif ($data instanceof \Pimf\View) { |
|
139
|
|
|
$body = $data->render(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
echo '' . $body; |
|
143
|
|
|
if ($exit) { |
|
144
|
|
|
exit(0); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* If instead you have a page that has personalization on it |
|
150
|
|
|
* (say, for example, the splash page contains local news as well), |
|
151
|
|
|
* you can set a copy to be cached only by the browser. |
|
152
|
|
|
* |
|
153
|
|
|
* @param int $seconds Interval in seconds |
|
154
|
|
|
* |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
|
|
public function cacheBrowser($seconds) |
|
158
|
|
|
{ |
|
159
|
|
|
self::preventMultipleCaching(); |
|
160
|
|
|
self::$cached = true; |
|
161
|
|
|
Header::cacheBrowser($seconds); |
|
162
|
|
|
|
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* If you want to try as hard as possible to keep a page from being cached anywhere. |
|
168
|
|
|
* |
|
169
|
|
|
* @return $this |
|
170
|
|
|
*/ |
|
171
|
|
|
public function cacheNone() |
|
172
|
|
|
{ |
|
173
|
|
|
self::preventMultipleCaching(); |
|
174
|
|
|
self::$cached = true; |
|
175
|
|
|
Header::cacheNone(); |
|
176
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* If you want to allow a page to be cached by shared proxies for one minute. |
|
182
|
|
|
* |
|
183
|
|
|
* @param int $seconds Interval in seconds |
|
184
|
|
|
* |
|
185
|
|
|
* @return $this |
|
186
|
|
|
*/ |
|
187
|
|
|
public function cacheNoValidate($seconds = 60) |
|
188
|
|
|
{ |
|
189
|
|
|
self::preventMultipleCaching(); |
|
190
|
|
|
self::$cached = true; |
|
191
|
|
|
Header::cacheNoValidate($seconds); |
|
192
|
|
|
|
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Handles setting pages that are always to be revalidated for freshness by any cache. |
|
198
|
|
|
* |
|
199
|
|
|
* @param int $last_modified Timestamp in seconds |
|
200
|
|
|
* |
|
201
|
|
|
* @return $this |
|
202
|
|
|
*/ |
|
203
|
|
|
public function exitIfNotModifiedSince($last_modified) |
|
204
|
|
|
{ |
|
205
|
|
|
self::preventMultipleCaching(); |
|
206
|
|
|
self::$cached = true; |
|
207
|
|
|
Header::exitIfNotModifiedSince($last_modified); |
|
208
|
|
|
|
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @throws \RuntimeException |
|
214
|
|
|
*/ |
|
215
|
|
|
private function preventMultipleCaching() |
|
216
|
|
|
{ |
|
217
|
|
|
if ($this->requestMethod != 'GET') { |
|
218
|
|
|
throw new \RuntimeException('HTTP cache headers can only take effect if request was sent via GET method!'); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
if (self::$cached === true) { |
|
222
|
|
|
throw new \RuntimeException('only one HTTP cache-control can be sent!'); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|