|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FileEye\MimeMap; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class for working with MIME types |
|
7
|
|
|
*/ |
|
8
|
|
|
class Type |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Short format [e.g. image/jpeg] for strings. |
|
12
|
|
|
*/ |
|
13
|
|
|
const SHORT_TEXT = 0; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Full format [e.g. image/jpeg; p="1"] for strings. |
|
17
|
|
|
*/ |
|
18
|
|
|
const FULL_TEXT = 1; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Full format with comments [e.g. image/jpeg; p="1" (comment)] for strings. |
|
22
|
|
|
*/ |
|
23
|
|
|
const FULL_TEXT_WITH_COMMENTS = 2; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The MIME media type. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $media; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The MIME media type comment. |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $mediaComment; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The MIME media sub-type. |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $subType; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The MIME media sub-type comment. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $subTypeComment; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Optional MIME parameters |
|
55
|
|
|
* |
|
56
|
|
|
* @var TypeParameter[] |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $parameters = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Constructor. |
|
62
|
|
|
* |
|
63
|
|
|
* The type string will be parsed and the appropriate class vars set. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $type MIME type |
|
66
|
|
|
*/ |
|
67
|
50 |
|
public function __construct($type) |
|
68
|
|
|
{ |
|
69
|
50 |
|
$this->parse($type); |
|
70
|
43 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Parse a mime-type and set the class variables. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $type MIME type to parse |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
50 |
|
protected function parse($type) |
|
80
|
|
|
{ |
|
81
|
|
|
// Media and SubType are separated by a slash '/'. |
|
82
|
50 |
|
$media = Parser::parseStringPart($type, 0, '/'); |
|
83
|
|
|
|
|
84
|
49 |
|
if (!$media['string']) { |
|
85
|
3 |
|
throw new MalformedTypeException('Media type not found'); |
|
86
|
|
|
} |
|
87
|
46 |
|
if (!$media['delimiter_matched']) { |
|
88
|
1 |
|
throw new MalformedTypeException('Slash \'/\' to separate media type and subtype not found'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
45 |
|
$this->media = strtolower($media['string']); |
|
92
|
45 |
|
$this->mediaComment = $media['comment']; |
|
93
|
|
|
|
|
94
|
|
|
// SubType and Parameters are separated by semicolons ';'. |
|
95
|
45 |
|
$sub = Parser::parseStringPart($type, $media['end_offset'] + 1, ';'); |
|
96
|
|
|
|
|
97
|
44 |
|
if (!$sub['string']) { |
|
98
|
1 |
|
throw new MalformedTypeException('Media subtype not found'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
43 |
|
$this->subType = strtolower($sub['string']); |
|
102
|
43 |
|
$this->subTypeComment = $sub['comment']; |
|
103
|
|
|
|
|
104
|
|
|
// Loops through the parameter. |
|
105
|
43 |
|
while ($sub['delimiter_matched']) { |
|
106
|
26 |
|
$sub = Parser::parseStringPart($type, $sub['end_offset'] + 1, ';'); |
|
107
|
26 |
|
$tmp = explode('=', $sub['string'], 2); |
|
108
|
26 |
|
$p_name = trim($tmp[0]); |
|
109
|
26 |
|
$p_val = trim($tmp[1]); |
|
110
|
26 |
|
if ($p_val[0] == '"' && $p_val[strlen($p_val) - 1] == '"') { |
|
111
|
|
|
$p_val = substr($p_val, 1, -1); |
|
112
|
|
|
} |
|
113
|
26 |
|
$p_val = str_replace('\\"', '"', $p_val); |
|
114
|
26 |
|
$this->addParameter($p_name, $p_val, $sub['comment']); |
|
115
|
|
|
} |
|
116
|
43 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Does this type have any parameters? |
|
120
|
|
|
* |
|
121
|
|
|
* @return boolean true if type has parameters, false otherwise |
|
122
|
|
|
*/ |
|
123
|
28 |
|
public function hasParameters() |
|
124
|
|
|
{ |
|
125
|
28 |
|
return (bool) $this->parameters; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get a MIME type's parameters |
|
130
|
|
|
* |
|
131
|
|
|
* @return TypeParameter[] Type's parameters |
|
132
|
|
|
*/ |
|
133
|
29 |
|
public function getParameters() |
|
134
|
|
|
{ |
|
135
|
29 |
|
return $this->parameters; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get a MIME type's parameter |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name Parameter name |
|
142
|
|
|
* |
|
143
|
|
|
* @return TypeParameter|null |
|
144
|
|
|
*/ |
|
145
|
23 |
|
public function getParameter($name) |
|
146
|
|
|
{ |
|
147
|
23 |
|
return isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get a MIME type's media. |
|
152
|
|
|
* |
|
153
|
|
|
* Note: 'media' refers to the portion before the first slash. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string Type's media. |
|
156
|
|
|
*/ |
|
157
|
32 |
|
public function getMedia() |
|
158
|
|
|
{ |
|
159
|
32 |
|
return $this->media; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get a MIME type's media comment. |
|
164
|
|
|
* |
|
165
|
|
|
* @return string Type's media comment. |
|
166
|
|
|
*/ |
|
167
|
28 |
|
public function getMediaComment() |
|
168
|
|
|
{ |
|
169
|
28 |
|
return $this->mediaComment; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get a MIME type's subtype. |
|
174
|
|
|
* |
|
175
|
|
|
* @return string Type's subtype, null if invalid mime type. |
|
176
|
|
|
*/ |
|
177
|
33 |
|
public function getSubType() |
|
178
|
|
|
{ |
|
179
|
33 |
|
return $this->subType; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get a MIME type's subtype comment. |
|
184
|
|
|
* |
|
185
|
|
|
* @return string Type's subtype comment, null if invalid mime type. |
|
186
|
|
|
*/ |
|
187
|
28 |
|
public function getSubTypeComment() |
|
188
|
|
|
{ |
|
189
|
28 |
|
return $this->subTypeComment; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Create a textual MIME type from object values |
|
194
|
|
|
* |
|
195
|
|
|
* This function performs the opposite function of parse(). |
|
196
|
|
|
* |
|
197
|
|
|
* @param int $format The format of the output string. |
|
198
|
|
|
* |
|
199
|
|
|
* @return string MIME type string |
|
200
|
|
|
*/ |
|
201
|
35 |
|
public function toString($format = Type::FULL_TEXT) |
|
202
|
|
|
{ |
|
203
|
35 |
|
$type = strtolower($this->media); |
|
204
|
35 |
|
if ($format > Type::FULL_TEXT && isset($this->mediaComment)) { |
|
205
|
2 |
|
$type .= ' (' . $this->mediaComment . ')'; |
|
206
|
|
|
} |
|
207
|
35 |
|
$type .= '/' . strtolower($this->subType); |
|
208
|
35 |
|
if ($format > Type::FULL_TEXT && isset($this->subTypeComment)) { |
|
209
|
5 |
|
$type .= ' (' . $this->subTypeComment . ')'; |
|
210
|
|
|
} |
|
211
|
35 |
|
if ($format > Type::SHORT_TEXT && count($this->parameters)) { |
|
212
|
23 |
|
foreach ($this->parameters as $parameter) { |
|
213
|
23 |
|
$type .= '; ' . $parameter->toString($format); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
35 |
|
return $type; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Is this type experimental? |
|
221
|
|
|
* |
|
222
|
|
|
* Note: Experimental types are denoted by a leading 'x-' in the media or |
|
223
|
|
|
* subtype, e.g. text/x-vcard or x-world/x-vrml. |
|
224
|
|
|
* |
|
225
|
|
|
* @return boolean true if type is experimental, false otherwise |
|
226
|
|
|
*/ |
|
227
|
1 |
|
public function isExperimental() |
|
228
|
|
|
{ |
|
229
|
1 |
|
if (substr($this->getMedia(), 0, 2) == 'x-' || substr($this->getSubType(), 0, 2) == 'x-') { |
|
230
|
1 |
|
return true; |
|
231
|
|
|
} |
|
232
|
1 |
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Is this a vendor MIME type? |
|
237
|
|
|
* |
|
238
|
|
|
* Note: Vendor types are denoted with a leading 'vnd. in the subtype. |
|
239
|
|
|
* |
|
240
|
|
|
* @return boolean true if type is a vendor type, false otherwise |
|
241
|
|
|
*/ |
|
242
|
1 |
|
public function isVendor() |
|
243
|
|
|
{ |
|
244
|
1 |
|
if (substr($this->getSubType(), 0, 4) == 'vnd.') { |
|
245
|
1 |
|
return true; |
|
246
|
|
|
} |
|
247
|
1 |
|
return false; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Is this a wildcard type? |
|
252
|
|
|
* |
|
253
|
|
|
* @return boolean true if type is a wildcard, false otherwise |
|
254
|
|
|
*/ |
|
255
|
3 |
|
public function isWildcard() |
|
256
|
|
|
{ |
|
257
|
|
|
// xxx also if a subtype can be submatched i.e. vnd.ms-excel.* |
|
258
|
3 |
|
if (($this->getMedia() === '*' && $this->getSubtype() === '*') || $this->getSubtype() === '*') { |
|
259
|
2 |
|
return true; |
|
260
|
|
|
} |
|
261
|
2 |
|
return false; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Perform a wildcard match on a MIME type |
|
266
|
|
|
* |
|
267
|
|
|
* Example: |
|
268
|
|
|
* $type = new Type('image/png'); |
|
269
|
|
|
* $type->wildcardMatch('image/*'); |
|
270
|
|
|
* |
|
271
|
|
|
* @param string $card Wildcard to check against |
|
272
|
|
|
* |
|
273
|
|
|
* @return boolean true if there was a match, false otherwise |
|
274
|
|
|
*/ |
|
275
|
2 |
|
public function wildcardMatch($card) |
|
276
|
|
|
{ |
|
277
|
2 |
|
$match_type = new static($card); |
|
278
|
|
|
|
|
279
|
2 |
|
if (!$match_type->isWildcard()) { |
|
280
|
1 |
|
return false; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
1 |
|
if ($match_type->getMedia() === '*' && $match_type->getSubType() === '*') { |
|
284
|
1 |
|
return true; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
1 |
|
if ($match_type->getMedia() === $this->getMedia()) { |
|
288
|
1 |
|
return true; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
1 |
|
return false; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
/** |
|
295
|
|
|
* Add a parameter to this type |
|
296
|
|
|
* |
|
297
|
|
|
* @param string $name Parameter name |
|
298
|
|
|
* @param string $value Parameter value |
|
299
|
|
|
* @param string $comment Comment for this parameter |
|
300
|
|
|
* |
|
301
|
|
|
* @return void |
|
302
|
|
|
*/ |
|
303
|
26 |
|
public function addParameter($name, $value, $comment = null) |
|
304
|
|
|
{ |
|
305
|
26 |
|
$this->parameters[$name] = new TypeParameter($name, $value, $comment); |
|
306
|
26 |
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Remove a parameter from this type |
|
310
|
|
|
* |
|
311
|
|
|
* @param string $name Parameter name |
|
312
|
|
|
* |
|
313
|
|
|
* @return void |
|
314
|
|
|
*/ |
|
315
|
1 |
|
public function removeParameter($name) |
|
316
|
|
|
{ |
|
317
|
1 |
|
unset($this->parameters[$name]); |
|
318
|
1 |
|
} |
|
319
|
|
|
|
|
320
|
4 |
|
public function getDefaultExtension($strict = true) |
|
321
|
|
|
{ |
|
322
|
4 |
|
$extensions = $this->getExtensions($strict); |
|
323
|
3 |
|
return isset($extensions[0]) ? $extensions[0] : null; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
5 |
|
public function getExtensions($strict = true) |
|
327
|
|
|
{ |
|
328
|
5 |
|
$type = $this->toString(static::SHORT_TEXT); |
|
329
|
|
|
|
|
330
|
5 |
|
$map = new MapHandler(); |
|
331
|
5 |
|
if (!isset($map->get()['types'][$type])) { |
|
332
|
2 |
|
if ($strict) { |
|
333
|
1 |
|
throw new MappingException('MIME type ' . $type . ' not found in map'); |
|
334
|
|
|
} else { |
|
335
|
1 |
|
return []; |
|
336
|
|
|
} |
|
337
|
|
|
} |
|
338
|
4 |
|
return $map->get()['types'][$type]; |
|
339
|
|
|
} |
|
340
|
|
|
} |
|
341
|
|
|
|