|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Response class. |
|
5
|
|
|
* |
|
6
|
|
|
* handles the response text, status and headers of a HTTP response. |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* @author Omar El Gabry <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
class Response { |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
public $headers; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $content; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $version; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $statusCode; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $statusText; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $charset; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $file = null; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var array |
|
51
|
|
|
*/ |
|
52
|
|
|
private $csv = null; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Holds HTTP response statuses |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
private $statusTexts = [ |
|
60
|
|
|
200 => 'OK', |
|
61
|
|
|
302 => 'Found', |
|
62
|
|
|
400 => 'Bad Request', |
|
63
|
|
|
401 => 'Unauthorized', |
|
64
|
|
|
403 => 'Forbidden', |
|
65
|
|
|
404 => 'Not Found', |
|
66
|
|
|
500 => 'Internal Server Error' |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Holds type key to mime type mappings for known mime types. |
|
71
|
|
|
* |
|
72
|
|
|
* @var array |
|
73
|
|
|
*/ |
|
74
|
|
|
private $mimeTypes = [ |
|
75
|
|
|
'csv' => ['text/csv', 'application/vnd.ms-excel'], |
|
76
|
|
|
'doc' => 'application/msword', |
|
77
|
|
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
78
|
|
|
'pdf' => 'application/pdf', |
|
79
|
|
|
'zip' => 'application/zip', |
|
80
|
|
|
'ppt' => 'application/vnd.ms-powerpoint' |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Constructor. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $content The response content |
|
87
|
|
|
* @param int $status The response status code |
|
88
|
|
|
* @param array $headers An array of response headers |
|
89
|
|
|
* |
|
90
|
|
|
*/ |
|
91
|
|
|
public function __construct($content = '', $status = 200, $headers = array()){ |
|
92
|
|
|
|
|
93
|
|
|
$this->content = $content; |
|
94
|
|
|
$this->statusCode = $status; |
|
95
|
|
|
$this->headers = $headers; |
|
96
|
|
|
$this->statusText = $this->statusTexts[$status]; |
|
97
|
|
|
$this->version = '1.0'; |
|
98
|
|
|
$this->charset = 'UTF-8'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sends HTTP headers and content. |
|
103
|
|
|
* |
|
104
|
|
|
*/ |
|
105
|
|
|
public function send(){ |
|
106
|
|
|
|
|
107
|
|
|
$this->sendHeaders(); |
|
108
|
|
|
|
|
109
|
|
|
if ($this->file) { |
|
110
|
|
|
$this->readFile(); |
|
111
|
|
|
} else if ($this->csv) { |
|
|
|
|
|
|
112
|
|
|
$this->writeCSV(); |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->sendContent(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (function_exists('fastcgi_finish_request')) { |
|
118
|
|
|
fastcgi_finish_request(); |
|
119
|
|
|
} elseif ('cli' !== PHP_SAPI) { |
|
120
|
|
|
$this->flushBuffer(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Flushes output buffers. |
|
128
|
|
|
* |
|
129
|
|
|
*/ |
|
130
|
|
|
private function flushBuffer(){ |
|
131
|
|
|
// ob_flush(); |
|
132
|
|
|
flush(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Sends HTTP headers. |
|
137
|
|
|
* |
|
138
|
|
|
* @return Response |
|
139
|
|
|
*/ |
|
140
|
|
|
private function sendHeaders(){ |
|
141
|
|
|
|
|
142
|
|
|
// check headers have already been sent by the developer |
|
143
|
|
|
if (headers_sent()) { |
|
144
|
|
|
return $this; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// status |
|
148
|
|
|
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); |
|
149
|
|
|
|
|
150
|
|
|
// Content-Type |
|
151
|
|
|
// if Content-Type is already exists in headers, then don't send it |
|
152
|
|
|
if(!array_key_exists('Content-Type', $this->headers)){ |
|
153
|
|
|
header('Content-Type: ' . 'text/html; charset=' . $this->charset); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
// headers |
|
157
|
|
|
foreach ($this->headers as $name => $value) { |
|
158
|
|
|
header($name .': '. $value, true, $this->statusCode); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sends content for the current web response. |
|
166
|
|
|
* |
|
167
|
|
|
* @return Response |
|
168
|
|
|
*/ |
|
169
|
|
|
private function sendContent(){ |
|
170
|
|
|
echo $this->content; |
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Sets content for the current web response. |
|
176
|
|
|
* |
|
177
|
|
|
* @param string $content The response content |
|
178
|
|
|
* @return Response |
|
179
|
|
|
*/ |
|
180
|
|
|
public function setContent($content = ""){ |
|
181
|
|
|
$this->content = $content; |
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sets content for the current web response. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string|null $content The response content |
|
|
|
|
|
|
189
|
|
|
* @return Response |
|
190
|
|
|
*/ |
|
191
|
|
|
public function type($contentType = null){ |
|
192
|
|
|
|
|
193
|
|
|
if($contentType == null){ |
|
194
|
|
|
unset($this->headers['Content-Type']); |
|
195
|
|
|
}else{ |
|
196
|
|
|
$this->header['Content-Type'] = $contentType; |
|
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $this; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Stop execution of the current script. . |
|
204
|
|
|
* |
|
205
|
|
|
* @param int|string $status |
|
206
|
|
|
* @return void |
|
207
|
|
|
* @see http://php.net/exit |
|
208
|
|
|
*/ |
|
209
|
|
|
public function stop($status = 0){ |
|
210
|
|
|
exit($status); |
|
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* read file |
|
215
|
|
|
* |
|
216
|
|
|
* @return Response |
|
217
|
|
|
*/ |
|
218
|
|
|
private function readFile(){ |
|
219
|
|
|
readfile($this->file); |
|
220
|
|
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* write to CSV file |
|
225
|
|
|
* |
|
226
|
|
|
* @return Response |
|
227
|
|
|
*/ |
|
228
|
|
|
private function writeCSV(){ |
|
229
|
|
|
|
|
230
|
|
|
$cols = $this->csv["cols"]; |
|
231
|
|
|
$rows = $this->csv["rows"]; |
|
232
|
|
|
|
|
233
|
|
|
$out = fopen("php://output", 'w'); |
|
234
|
|
|
|
|
235
|
|
|
fputcsv($out, $cols, ',', '"'); |
|
236
|
|
|
foreach($rows as $row) { |
|
237
|
|
|
fputcsv($out, array_values($row), ',', '"'); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
fclose($out); |
|
241
|
|
|
return $this; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Sets the response status code & it's relevant text. |
|
246
|
|
|
* |
|
247
|
|
|
* @param int $code HTTP status code |
|
248
|
|
|
* @return Response |
|
249
|
|
|
*/ |
|
250
|
|
|
public function setStatusCode($code){ |
|
251
|
|
|
|
|
252
|
|
|
$this->statusCode = (int) $code; |
|
253
|
|
|
$this->statusText = isset($this->statusTexts[$code]) ? $this->statusTexts[$code] : ''; |
|
254
|
|
|
|
|
255
|
|
|
return $this; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Returns the mime type definition for an alias |
|
260
|
|
|
* |
|
261
|
|
|
* @param string $key |
|
262
|
|
|
* @return mixed |
|
263
|
|
|
*/ |
|
264
|
|
|
private function getMimeType($key){ |
|
265
|
|
|
|
|
266
|
|
|
if (isset($this->mimeTypes[$key])) { |
|
267
|
|
|
$mime = $this->mimeTypes[$key]; |
|
268
|
|
|
return is_array($mime) ? current($mime) : $mime; |
|
269
|
|
|
} |
|
270
|
|
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Clean (erase) the output buffer |
|
275
|
|
|
* |
|
276
|
|
|
* @return void |
|
277
|
|
|
*/ |
|
278
|
|
|
public function clearBuffer(){ |
|
279
|
|
|
|
|
280
|
|
|
// check if output_buffering is active |
|
281
|
|
|
if(ob_get_level() > 0){ |
|
282
|
|
|
return ob_clean(); |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* download a file |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $path |
|
290
|
|
|
* @param array $file |
|
291
|
|
|
* @param array $headers |
|
292
|
|
|
* @return Response |
|
293
|
|
|
*/ |
|
294
|
|
|
public function download($path, array $file, array $headers = []){ |
|
295
|
|
|
|
|
296
|
|
|
$this->file = $path; |
|
297
|
|
|
$this->setStatusCode(200); |
|
298
|
|
|
|
|
299
|
|
|
if(empty($headers)){ |
|
300
|
|
|
|
|
301
|
|
|
$mime = $this->getMimeType($file["extension"]); |
|
302
|
|
|
if(!$mime){ |
|
303
|
|
|
$mime = "application/octet-stream"; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
$headers = [ |
|
307
|
|
|
'Content-Description' => 'File Transfer', |
|
308
|
|
|
'Content-Type' => $mime, |
|
309
|
|
|
'Content-Disposition' => 'attachment; filename="'. $file["basename"].'"', |
|
310
|
|
|
'Expires' => '0', |
|
311
|
|
|
'Cache-Control' => 'must-revalidate', |
|
312
|
|
|
'Pragma' => 'public', |
|
313
|
|
|
'Content-Transfer-Encoding' => 'binary', |
|
314
|
|
|
'Content-Length' => filesize($path) |
|
315
|
|
|
]; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
$this->headers = $headers; |
|
319
|
|
|
$this->clearBuffer(); |
|
320
|
|
|
return $this; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* download CSV file |
|
325
|
|
|
* This will create csv file using $csvData |
|
326
|
|
|
* |
|
327
|
|
|
* @param array $csvData |
|
328
|
|
|
* @param array $file |
|
329
|
|
|
* @return Response |
|
330
|
|
|
* @see core/Response/writeCSV() |
|
331
|
|
|
*/ |
|
332
|
|
|
public function csv(array $csvData, array $file){ |
|
333
|
|
|
|
|
334
|
|
|
$this->csv = $csvData; |
|
335
|
|
|
$this->setStatusCode(200); |
|
336
|
|
|
|
|
337
|
|
|
$basename = $file["filename"] . ".csv"; |
|
338
|
|
|
$headers = [ |
|
339
|
|
|
'Content-Type' => 'text/csv', |
|
340
|
|
|
'Content-Disposition' => 'attachment; filename="'. $basename.'"' |
|
341
|
|
|
]; |
|
342
|
|
|
|
|
343
|
|
|
$this->headers = $headers; |
|
344
|
|
|
$this->clearBuffer(); |
|
345
|
|
|
return $this; |
|
346
|
|
|
} |
|
347
|
|
|
} |
|
348
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.