|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* soluble-flexstore library |
|
5
|
|
|
* |
|
6
|
|
|
* @author Vanvelthem Sébastien |
|
7
|
|
|
* @link https://github.com/belgattitude/soluble-flexstore |
|
8
|
|
|
* @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien |
|
9
|
|
|
* @license MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Soluble\FlexStore\Writer\Http; |
|
14
|
|
|
|
|
15
|
|
|
class SimpleHeaders |
|
16
|
|
|
{ |
|
17
|
|
|
const DIPOSITION_INLINE = 'inline'; |
|
18
|
|
|
const DIPOSITION_ATTACHEMENT = 'attachement'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Supported disposition types. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $disposition_types = ['inline', 'attachement']; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $default_params = [ |
|
31
|
|
|
'content-type' => null, |
|
32
|
|
|
'content-type-charset' => null, |
|
33
|
|
|
'content-disposition-filename' => null, |
|
34
|
|
|
'content-disposition-type' => null, |
|
35
|
|
|
'content-length' => null |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $params; |
|
42
|
|
|
|
|
43
|
8 |
|
public function __construct() |
|
44
|
|
|
{ |
|
45
|
8 |
|
$this->params = $this->default_params; |
|
46
|
8 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $content_type |
|
50
|
|
|
* @param string $charset |
|
51
|
|
|
* |
|
52
|
|
|
* @return SimpleHeaders |
|
53
|
|
|
*/ |
|
54
|
6 |
|
public function setContentType($content_type, $charset = null) |
|
55
|
|
|
{ |
|
56
|
6 |
|
$this->params['content-type'] = $content_type; |
|
57
|
6 |
|
if ($charset !== null) { |
|
58
|
6 |
|
$this->setCharset($charset); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
7 |
|
public function getContentType() |
|
65
|
|
|
{ |
|
66
|
7 |
|
return $this->params['content-type']; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the content disposition filename and type. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $filename filename when downloading |
|
73
|
|
|
* @param string $content_disposition_type by default attachment |
|
74
|
|
|
* |
|
75
|
|
|
* @return SimpleHeaders |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function setFilename($filename, $content_disposition_type = 'attachement') |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->params['content-disposition-filename'] = $filename; |
|
80
|
1 |
|
$this->setContentDispositionType($content_disposition_type); |
|
81
|
|
|
|
|
82
|
1 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return the content disposition filename. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string|null |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function getFilename() |
|
91
|
|
|
{ |
|
92
|
1 |
|
return $this->params['content-disposition-filename']; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set the content type charset. |
|
97
|
|
|
* |
|
98
|
|
|
* @throws Exception\RuntimeException |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $charset |
|
101
|
|
|
* |
|
102
|
|
|
* @return SimpleHeaders |
|
103
|
|
|
*/ |
|
104
|
7 |
|
public function setCharset($charset) |
|
105
|
|
|
{ |
|
106
|
7 |
|
if ($this->getContentType() == '') { |
|
107
|
1 |
|
throw new Exception\RuntimeException(__METHOD__ . ' Content-type must be specified prior to setting charset'); |
|
108
|
|
|
} |
|
109
|
6 |
|
$this->params['content-type-charset'] = $charset; |
|
110
|
|
|
|
|
111
|
6 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string |
|
116
|
|
|
*/ |
|
117
|
6 |
|
public function getCharset() |
|
118
|
|
|
{ |
|
119
|
6 |
|
return $this->params['content-type-charset']; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Set the preferred content disposition type 'attachement' or 'inline'. |
|
124
|
|
|
* |
|
125
|
|
|
* @throws Exception\InvalidArgumentException |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $content_disposition_type |
|
128
|
|
|
* |
|
129
|
|
|
* @return SimpleHeaders |
|
130
|
|
|
*/ |
|
131
|
3 |
|
public function setContentDispositionType($content_disposition_type) |
|
132
|
|
|
{ |
|
133
|
3 |
|
if (!in_array($content_disposition_type, $this->disposition_types, true)) { |
|
134
|
1 |
|
$supported = implode(',', $this->disposition_types); |
|
135
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . " Content disposition type '$content_disposition_type' not in supported types: $supported"); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
$this->params['content-disposition-type'] = $content_disposition_type; |
|
139
|
|
|
|
|
140
|
2 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Return the content disposition type. |
|
145
|
|
|
* |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
2 |
|
public function getContentDispositionType() |
|
149
|
|
|
{ |
|
150
|
2 |
|
return $this->params['content-disposition-type']; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param int $length |
|
155
|
|
|
* |
|
156
|
|
|
* @return SimpleHeaders |
|
157
|
|
|
*/ |
|
158
|
1 |
|
public function setContentLength($length) |
|
159
|
|
|
{ |
|
160
|
1 |
|
$this->params['content-length'] = $length; |
|
161
|
|
|
|
|
162
|
1 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return int |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function getContentLength() |
|
169
|
|
|
{ |
|
170
|
1 |
|
return $this->params['content-length']; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
public function getHeaderLines() |
|
174
|
|
|
{ |
|
175
|
1 |
|
$lines = []; |
|
176
|
1 |
|
if ($this->params['content-type'] !== null) { |
|
177
|
1 |
|
$ct = 'Content-Type: ' . $this->params['content-type']; |
|
178
|
1 |
|
if ($this->params['content-type-charset'] !== null) { |
|
179
|
1 |
|
$ct .= '; charset=' . $this->params['content-type-charset']; |
|
180
|
|
|
} |
|
181
|
1 |
|
$lines[] = $ct; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
1 |
|
if ($this->params['content-disposition-type'] !== null) { |
|
185
|
1 |
|
$cd = 'Content-Disposition: ' . $this->params['content-disposition-type']; |
|
186
|
1 |
|
if ($this->params['content-disposition-filename'] !== null) { |
|
187
|
1 |
|
$cd .= '; filename="' . $this->params['content-disposition-filename'] . '"'; |
|
188
|
|
|
} |
|
189
|
1 |
|
$lines[] = $cd; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
1 |
|
if ($this->params['content-length'] !== null) { |
|
193
|
1 |
|
$lines[] = 'Content-Length: ' . $this->params['content-length']; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
1 |
|
return $lines; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Output the headers (php). |
|
201
|
|
|
* |
|
202
|
|
|
* @param bool $replace tells to replace eventual headers |
|
203
|
|
|
*/ |
|
204
|
|
|
public function outputHeaders($replace = true) |
|
205
|
|
|
{ |
|
206
|
|
|
$lines = $this->getHeaderLines(); |
|
207
|
|
|
foreach ($lines as $line) { |
|
208
|
|
|
header($line, $replace); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|