Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Mimes
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 guessTypeFromExtension
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 guessExtensionFromType
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace Config;
4
5/**
6 * This file contains an array of mime types.  It is used by the
7 * Upload class to help identify allowed file types.
8 *
9 * When more than one variation for an extension exist (like jpg, jpeg, etc)
10 * the most common one should be first in the array to aid the guess*
11 * methods. The same applies when more than one mime-type exists for a
12 * single extension.
13 *
14 * When working with mime types, please make sure you have the ´fileinfo´
15 * extension enabled to reliably detect the media types.
16 */
17class Mimes
18{
19    /**
20     * Map of extensions to mime types.
21     *
22     * @var array<string, list<string>|string>
23     */
24    public static array $mimes = [
25        'hqx' => [
26            'application/mac-binhex40',
27            'application/mac-binhex',
28            'application/x-binhex40',
29            'application/x-mac-binhex40',
30        ],
31        'cpt' => 'application/mac-compactpro',
32        'csv' => [
33            'text/csv',
34            'text/x-comma-separated-values',
35            'text/comma-separated-values',
36            'application/vnd.ms-excel',
37            'application/x-csv',
38            'text/x-csv',
39            'application/csv',
40            'application/excel',
41            'application/vnd.msexcel',
42            'text/plain',
43        ],
44        'bin' => [
45            'application/macbinary',
46            'application/mac-binary',
47            'application/octet-stream',
48            'application/x-binary',
49            'application/x-macbinary',
50        ],
51        'dms' => 'application/octet-stream',
52        'lha' => 'application/octet-stream',
53        'lzh' => 'application/octet-stream',
54        'exe' => [
55            'application/octet-stream',
56            'application/vnd.microsoft.portable-executable',
57            'application/x-dosexec',
58            'application/x-msdownload',
59        ],
60        'class' => 'application/octet-stream',
61        'psd'   => [
62            'application/x-photoshop',
63            'image/vnd.adobe.photoshop',
64        ],
65        'so'  => 'application/octet-stream',
66        'sea' => 'application/octet-stream',
67        'dll' => 'application/octet-stream',
68        'oda' => 'application/oda',
69        'pdf' => [
70            'application/pdf',
71            'application/force-download',
72            'application/x-download',
73        ],
74        'ai' => [
75            'application/pdf',
76            'application/postscript',
77        ],
78        'eps'  => 'application/postscript',
79        'ps'   => 'application/postscript',
80        'smi'  => 'application/smil',
81        'smil' => 'application/smil',
82        'mif'  => 'application/vnd.mif',
83        'xls'  => [
84            'application/vnd.ms-excel',
85            'application/msexcel',
86            'application/x-msexcel',
87            'application/x-ms-excel',
88            'application/x-excel',
89            'application/x-dos_ms_excel',
90            'application/xls',
91            'application/x-xls',
92            'application/excel',
93            'application/download',
94            'application/vnd.ms-office',
95            'application/msword',
96        ],
97        'ppt' => [
98            'application/vnd.ms-powerpoint',
99            'application/powerpoint',
100            'application/vnd.ms-office',
101            'application/msword',
102        ],
103        'pptx' => [
104            'application/vnd.openxmlformats-officedocument.presentationml.presentation',
105        ],
106        'wbxml' => 'application/wbxml',
107        'wmlc'  => 'application/wmlc',
108        'dcr'   => 'application/x-director',
109        'dir'   => 'application/x-director',
110        'dxr'   => 'application/x-director',
111        'dvi'   => 'application/x-dvi',
112        'gtar'  => 'application/x-gtar',
113        'gz'    => 'application/x-gzip',
114        'gzip'  => 'application/x-gzip',
115        'php'   => [
116            'application/x-php',
117            'application/x-httpd-php',
118            'application/php',
119            'text/php',
120            'text/x-php',
121            'application/x-httpd-php-source',
122        ],
123        'php4'  => 'application/x-httpd-php',
124        'php3'  => 'application/x-httpd-php',
125        'phtml' => 'application/x-httpd-php',
126        'phps'  => 'application/x-httpd-php-source',
127        'js'    => [
128            'application/x-javascript',
129            'text/plain',
130        ],
131        'swf' => 'application/x-shockwave-flash',
132        'sit' => 'application/x-stuffit',
133        'tar' => 'application/x-tar',
134        'tgz' => [
135            'application/x-tar',
136            'application/x-gzip-compressed',
137        ],
138        'z'     => 'application/x-compress',
139        'xhtml' => 'application/xhtml+xml',
140        'xht'   => 'application/xhtml+xml',
141        'zip'   => [
142            'application/x-zip',
143            'application/zip',
144            'application/x-zip-compressed',
145            'application/s-compressed',
146            'multipart/x-zip',
147        ],
148        'rar' => [
149            'application/vnd.rar',
150            'application/x-rar',
151            'application/rar',
152            'application/x-rar-compressed',
153        ],
154        'mid'  => 'audio/midi',
155        'midi' => 'audio/midi',
156        'mpga' => 'audio/mpeg',
157        'mp2'  => 'audio/mpeg',
158        'mp3'  => [
159            'audio/mpeg',
160            'audio/mpg',
161            'audio/mpeg3',
162            'audio/mp3',
163        ],
164        'aif' => [
165            'audio/x-aiff',
166            'audio/aiff',
167        ],
168        'aiff' => [
169            'audio/x-aiff',
170            'audio/aiff',
171        ],
172        'aifc' => 'audio/x-aiff',
173        'ram'  => 'audio/x-pn-realaudio',
174        'rm'   => 'audio/x-pn-realaudio',
175        'rpm'  => 'audio/x-pn-realaudio-plugin',
176        'ra'   => 'audio/x-realaudio',
177        'rv'   => 'video/vnd.rn-realvideo',
178        'wav'  => [
179            'audio/x-wav',
180            'audio/wave',
181            'audio/wav',
182        ],
183        'bmp' => [
184            'image/bmp',
185            'image/x-bmp',
186            'image/x-bitmap',
187            'image/x-xbitmap',
188            'image/x-win-bitmap',
189            'image/x-windows-bmp',
190            'image/ms-bmp',
191            'image/x-ms-bmp',
192            'application/bmp',
193            'application/x-bmp',
194            'application/x-win-bitmap',
195        ],
196        'gif' => 'image/gif',
197        'jpg' => [
198            'image/jpeg',
199            'image/pjpeg',
200        ],
201        'jpeg' => [
202            'image/jpeg',
203            'image/pjpeg',
204        ],
205        'jpe' => [
206            'image/jpeg',
207            'image/pjpeg',
208        ],
209        'jp2' => [
210            'image/jp2',
211            'video/mj2',
212            'image/jpx',
213            'image/jpm',
214        ],
215        'j2k' => [
216            'image/jp2',
217            'video/mj2',
218            'image/jpx',
219            'image/jpm',
220        ],
221        'jpf' => [
222            'image/jp2',
223            'video/mj2',
224            'image/jpx',
225            'image/jpm',
226        ],
227        'jpg2' => [
228            'image/jp2',
229            'video/mj2',
230            'image/jpx',
231            'image/jpm',
232        ],
233        'jpx' => [
234            'image/jp2',
235            'video/mj2',
236            'image/jpx',
237            'image/jpm',
238        ],
239        'jpm' => [
240            'image/jp2',
241            'video/mj2',
242            'image/jpx',
243            'image/jpm',
244        ],
245        'mj2' => [
246            'image/jp2',
247            'video/mj2',
248            'image/jpx',
249            'image/jpm',
250        ],
251        'mjp2' => [
252            'image/jp2',
253            'video/mj2',
254            'image/jpx',
255            'image/jpm',
256        ],
257        'png' => [
258            'image/png',
259            'image/x-png',
260        ],
261        'webp' => 'image/webp',
262        'tif'  => 'image/tiff',
263        'tiff' => 'image/tiff',
264        'css'  => [
265            'text/css',
266            'text/plain',
267        ],
268        'html' => [
269            'text/html',
270            'text/plain',
271        ],
272        'htm' => [
273            'text/html',
274            'text/plain',
275        ],
276        'shtml' => [
277            'text/html',
278            'text/plain',
279        ],
280        'txt'  => 'text/plain',
281        'text' => 'text/plain',
282        'log'  => [
283            'text/plain',
284            'text/x-log',
285        ],
286        'rtx' => 'text/richtext',
287        'rtf' => 'text/rtf',
288        'xml' => [
289            'application/xml',
290            'text/xml',
291            'text/plain',
292        ],
293        'xsl' => [
294            'application/xml',
295            'text/xsl',
296            'text/xml',
297        ],
298        'mpeg' => 'video/mpeg',
299        'mpg'  => 'video/mpeg',
300        'mpe'  => 'video/mpeg',
301        'qt'   => 'video/quicktime',
302        'mov'  => 'video/quicktime',
303        'avi'  => [
304            'video/x-msvideo',
305            'video/msvideo',
306            'video/avi',
307            'application/x-troff-msvideo',
308        ],
309        'movie' => 'video/x-sgi-movie',
310        'doc'   => [
311            'application/msword',
312            'application/vnd.ms-office',
313        ],
314        'docx' => [
315            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
316            'application/zip',
317            'application/msword',
318            'application/x-zip',
319        ],
320        'dot' => [
321            'application/msword',
322            'application/vnd.ms-office',
323        ],
324        'dotx' => [
325            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
326            'application/zip',
327            'application/msword',
328        ],
329        'xlsx' => [
330            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
331            'application/zip',
332            'application/vnd.ms-excel',
333            'application/msword',
334            'application/x-zip',
335        ],
336        'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
337        'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
338        'word' => [
339            'application/msword',
340            'application/octet-stream',
341        ],
342        'xl'   => 'application/excel',
343        'eml'  => 'message/rfc822',
344        'json' => [
345            'application/json',
346            'text/json',
347        ],
348        'pem' => [
349            'application/x-x509-user-cert',
350            'application/x-pem-file',
351            'application/octet-stream',
352        ],
353        'p10' => [
354            'application/x-pkcs10',
355            'application/pkcs10',
356        ],
357        'p12' => 'application/x-pkcs12',
358        'p7a' => 'application/x-pkcs7-signature',
359        'p7c' => [
360            'application/pkcs7-mime',
361            'application/x-pkcs7-mime',
362        ],
363        'p7m' => [
364            'application/pkcs7-mime',
365            'application/x-pkcs7-mime',
366        ],
367        'p7r' => 'application/x-pkcs7-certreqresp',
368        'p7s' => 'application/pkcs7-signature',
369        'crt' => [
370            'application/x-x509-ca-cert',
371            'application/x-x509-user-cert',
372            'application/pkix-cert',
373        ],
374        'crl' => [
375            'application/pkix-crl',
376            'application/pkcs-crl',
377        ],
378        'der' => 'application/x-x509-ca-cert',
379        'kdb' => 'application/octet-stream',
380        'pgp' => 'application/pgp',
381        'gpg' => 'application/gpg-keys',
382        'sst' => 'application/octet-stream',
383        'csr' => 'application/octet-stream',
384        'rsa' => 'application/x-pkcs7',
385        'cer' => [
386            'application/pkix-cert',
387            'application/x-x509-ca-cert',
388        ],
389        '3g2' => 'video/3gpp2',
390        '3gp' => [
391            'video/3gp',
392            'video/3gpp',
393        ],
394        'mp4' => 'video/mp4',
395        'm4a' => 'audio/x-m4a',
396        'f4v' => [
397            'video/mp4',
398            'video/x-f4v',
399        ],
400        'flv'  => 'video/x-flv',
401        'webm' => 'video/webm',
402        'aac'  => 'audio/x-acc',
403        'm4u'  => 'application/vnd.mpegurl',
404        'm3u'  => 'text/plain',
405        'xspf' => 'application/xspf+xml',
406        'vlc'  => 'application/videolan',
407        'wmv'  => [
408            'video/x-ms-wmv',
409            'video/x-ms-asf',
410        ],
411        'au'   => 'audio/x-au',
412        'ac3'  => 'audio/ac3',
413        'flac' => 'audio/x-flac',
414        'ogg'  => [
415            'audio/ogg',
416            'video/ogg',
417            'application/ogg',
418        ],
419        'kmz' => [
420            'application/vnd.google-earth.kmz',
421            'application/zip',
422            'application/x-zip',
423        ],
424        'kml' => [
425            'application/vnd.google-earth.kml+xml',
426            'application/xml',
427            'text/xml',
428        ],
429        'ics'  => 'text/calendar',
430        'ical' => 'text/calendar',
431        'zsh'  => 'text/x-scriptzsh',
432        '7zip' => [
433            'application/x-compressed',
434            'application/x-zip-compressed',
435            'application/zip',
436            'multipart/x-zip',
437        ],
438        'cdr' => [
439            'application/cdr',
440            'application/coreldraw',
441            'application/x-cdr',
442            'application/x-coreldraw',
443            'image/cdr',
444            'image/x-cdr',
445            'zz-application/zz-winassoc-cdr',
446        ],
447        'wma' => [
448            'audio/x-ms-wma',
449            'video/x-ms-asf',
450        ],
451        'jar' => [
452            'application/java-archive',
453            'application/x-java-application',
454            'application/x-jar',
455            'application/x-compressed',
456        ],
457        'svg' => [
458            'image/svg+xml',
459            'image/svg',
460            'application/xml',
461            'text/xml',
462        ],
463        'vcf' => 'text/x-vcard',
464        'srt' => [
465            'text/srt',
466            'text/plain',
467        ],
468        'vtt' => [
469            'text/vtt',
470            'text/plain',
471        ],
472        'ico' => [
473            'image/x-icon',
474            'image/x-ico',
475            'image/vnd.microsoft.icon',
476        ],
477        'stl' => [
478            'application/sla',
479            'application/vnd.ms-pki.stl',
480            'application/x-navistyle',
481            'model/stl',
482            'application/octet-stream',
483        ],
484    ];
485
486    /**
487     * Attempts to determine the best mime type for the given file extension.
488     *
489     * @return string|null The mime type found, or none if unable to determine.
490     */
491    public static function guessTypeFromExtension(string $extension)
492    {
493        $extension = trim(strtolower($extension), '. ');
494
495        if (! array_key_exists($extension, static::$mimes)) {
496            return null;
497        }
498
499        return is_array(static::$mimes[$extension]) ? static::$mimes[$extension][0] : static::$mimes[$extension];
500    }
501
502    /**
503     * Attempts to determine the best file extension for a given mime type.
504     *
505     * @param string|null $proposedExtension - default extension (in case there is more than one with the same mime type)
506     *
507     * @return string|null The extension determined, or null if unable to match.
508     */
509    public static function guessExtensionFromType(string $type, ?string $proposedExtension = null)
510    {
511        $type = trim(strtolower($type), '. ');
512
513        $proposedExtension = trim(strtolower($proposedExtension ?? ''));
514
515        if (
516            $proposedExtension !== ''
517            && array_key_exists($proposedExtension, static::$mimes)
518            && in_array($type, (array) static::$mimes[$proposedExtension], true)
519        ) {
520            // The detected mime type matches with the proposed extension.
521            return $proposedExtension;
522        }
523
524        // Reverse check the mime type list if no extension was proposed.
525        // This search is order sensitive!
526        foreach (static::$mimes as $ext => $types) {
527            if (in_array($type, (array) $types, true)) {
528                return $ext;
529            }
530        }
531
532        return null;
533    }
534}