Mimetype   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 160
c 0
b 0
f 0
dl 0
loc 195
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getType() 0 13 1
B privBuildMimeArray() 0 156 1
A privFindType() 0 7 1
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\News;
4
5
/**
6
 * Copyright (C) 2002 Jason Sheets <[email protected]>.
7
 * All rights reserved.
8
 *
9
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
10
 * Redistribution and use in source and binary forms, with or without
11
 * modification, are permitted provided that the following conditions
12
 * are met:
13
 *
14
 * 1. Redistributions of source code must retain the above copyright
15
 * notice, this list of conditions and the following disclaimer.
16
 *
17
 * 2. Redistributions in binary form must reproduce the above copyright
18
 * notice, this list of conditions and the following disclaimer in the
19
 * documentation and/or other materials provided with the distribution.
20
 *
21
 * 3. Neither the name of the project nor the names of its contributors
22
 * may be used to endorse or promote products derived from this software
23
 * without specific prior written permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
26
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
29
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35
 * SUCH DAMAGE.
36
 **/
37
38
/**
39
 * Name: PHP MimeType Class
40
 *
41
 * Version: 1.0
42
 * Date Released: 10/20/02
43
 *
44
 * Description: This class allows a PHP script to determine the mime type
45
 * a file based on the file extension.  This class is handy for PHP versions
46
 * without the benefit of other tools to determine the mime type.  The class
47
 * defaults to octet-stream so if the file type is not recognized the user
48
 * is presented with a file download prompt.
49
 *
50
 * NOTES: The mime types for this version are based on Apache 1.3.27
51
 * mime types may change or need to be added in the future, the mime types
52
 * are stored in an array so that an awk or other script can automatically
53
 * generate the code to make the array.
54
 *
55
 * Usage:
56
 *
57
 * First an instance of the mimetype class must be created, then the
58
 * getType method should be called with the filename.  It will return
59
 * the mime type, an example follows.
60
 *
61
 * Example:
62
 *
63
 * $mimetype = new mimetype();
64
 * print $mimetype->getType('acrobat.pdf');
65
 *
66
 * Author: Jason Sheets <[email protected]>
67
 *
68
 * License: This script is distributed under the BSD License, you are free
69
 * to use, or modify it however you like.  If you find this script useful please
70
 * e-mail me.
71
 **/
72
73
/**
74
 * Class Mimetype
75
 */
76
class Mimetype
77
{
78
    /**
79
     * @param $filename
80
     *
81
     * @return string
82
     */
83
    public function getType($filename)
84
    {
85
        // get base name of the filename provided by user
86
        $filename = \basename($filename);
87
88
        // break file into parts seperated by .
89
        $filename = \explode('.', $filename);
90
91
        // take the last part of the file to get the file extension
92
        $filename = $filename[\count($filename) - 1];
93
94
        // find mime type
95
        return $this->privFindType($filename);
96
    }
97
98
    /**
99
     * @param $ext
100
     *
101
     * @return string
102
     */
103
    public function privFindType($ext)
104
    {
105
        // create mimetypes array
106
        $mimetypes = $this->privBuildMimeArray();
107
108
        // return mime type for extension
109
        return $mimetypes[$ext] ?? 'unknown';
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function privBuildMimeArray()
116
    {
117
        return [
118
            'ez'      => 'application/andrew-inset',
119
            'hqx'     => 'application/mac-binhex40',
120
            'cpt'     => 'application/mac-compactpro',
121
            'doc'     => 'application/msword',
122
            'DOC'     => 'application/msword',
123
            'Doc'     => 'application/msword',
124
            'xls'     => 'application/vnd.ms-excel',
125
            'Xls'     => 'application/vnd.ms-excel',
126
            'XLS'     => 'application/vnd.ms-excel',
127
            'bin'     => 'application/octet-stream',
128
            'dms'     => 'application/octet-stream',
129
            'lha'     => 'application/octet-stream',
130
            'lzh'     => 'application/octet-stream',
131
            'exe'     => 'application/octet-stream',
132
            'class'   => 'application/octet-stream',
133
            'so'      => 'application/octet-stream',
134
            'dll'     => 'application/octet-stream',
135
            'oda'     => 'application/oda',
136
            'pdf'     => 'application/pdf',
137
            'PDF'     => 'application/pdf',
138
            'Pdf'     => 'application/pdf',
139
            'ai'      => 'application/postscript',
140
            'eps'     => 'application/postscript',
141
            'ps'      => 'application/postscript',
142
            'smi'     => 'application/smil',
143
            'smil'    => 'application/smil',
144
            'wbxml'   => 'application/vnd.wap.wbxml',
145
            'wmlc'    => 'application/vnd.wap.wmlc',
146
            'wmlsc'   => 'application/vnd.wap.wmlscriptc',
147
            'bcpio'   => 'application/x-bcpio',
148
            'vcd'     => 'application/x-cdlink',
149
            'pgn'     => 'application/x-chess-pgn',
150
            'cpio'    => 'application/x-cpio',
151
            'csh'     => 'application/x-csh',
152
            'dcr'     => 'application/x-director',
153
            'dir'     => 'application/x-director',
154
            'dxr'     => 'application/x-director',
155
            'dvi'     => 'application/x-dvi',
156
            'spl'     => 'application/x-futuresplash',
157
            'gtar'    => 'application/x-gtar',
158
            'hdf'     => 'application/x-hdf',
159
            'js'      => 'application/x-javascript',
160
            'skp'     => 'application/x-koan',
161
            'skd'     => 'application/x-koan',
162
            'skt'     => 'application/x-koan',
163
            'skm'     => 'application/x-koan',
164
            'latex'   => 'application/x-latex',
165
            'nc'      => 'application/x-netcdf',
166
            'cdf'     => 'application/x-netcdf',
167
            'sh'      => 'application/x-sh',
168
            'shar'    => 'application/x-shar',
169
            'swf'     => 'application/x-shockwave-flash',
170
            'sit'     => 'application/x-stuffit',
171
            'sv4cpio' => 'application/x-sv4cpio',
172
            'sv4crc'  => 'application/x-sv4crc',
173
            'tar'     => 'application/x-tar',
174
            'tcl'     => 'application/x-tcl',
175
            'tex'     => 'application/x-tex',
176
            'texinfo' => 'application/x-texinfo',
177
            'texi'    => 'application/x-texinfo',
178
            't'       => 'application/x-troff',
179
            'tr'      => 'application/x-troff',
180
            'roff'    => 'application/x-troff',
181
            'man'     => 'application/x-troff-man',
182
            'me'      => 'application/x-troff-me',
183
            'ms'      => 'application/x-troff-ms',
184
            'ustar'   => 'application/x-ustar',
185
            'src'     => 'application/x-wais-source',
186
            'xhtml'   => 'application/xhtml+xml',
187
            'xht'     => 'application/xhtml+xml',
188
            'zip'     => 'application/x-zip-compressed',
189
            'Zip'     => 'application/x-zip-compressed',
190
            'ZIP'     => 'application/x-zip-compressed',
191
            'au'      => 'audio/basic',
192
            'XM'      => 'audio/fasttracker',
193
            'snd'     => 'audio/basic',
194
            'mid'     => 'audio/midi',
195
            'midi'    => 'audio/midi',
196
            'kar'     => 'audio/midi',
197
            'mpga'    => 'audio/mpeg',
198
            'mp2'     => 'audio/mpeg',
199
            'mp3'     => 'audio/mpeg',
200
            'aif'     => 'audio/x-aiff',
201
            'aiff'    => 'audio/x-aiff',
202
            'aifc'    => 'audio/x-aiff',
203
            'm3u'     => 'audio/x-mpegurl',
204
            'ram'     => 'audio/x-pn-realaudio',
205
            'rm'      => 'audio/x-pn-realaudio',
206
            'rpm'     => 'audio/x-pn-realaudio-plugin',
207
            'ra'      => 'audio/x-realaudio',
208
            'wav'     => 'audio/x-wav',
209
            'wax'     => 'audio/x-windows-media',
210
            'pdb'     => 'chemical/x-pdb',
211
            'xyz'     => 'chemical/x-xyz',
212
            'bmp'     => 'image/bmp',
213
            'gif'     => 'image/gif',
214
            'ief'     => 'image/ief',
215
            //            'jpeg'    => 'image/pjpeg',
216
            'jpeg'    => 'image/jpeg',
217
            'jpg'     => 'image/jpeg',
218
            'jpe'     => 'image/jpeg',
219
            'png'     => 'image/x-png',
220
            'tiff'    => 'image/tiff',
221
            'tif'     => 'image/tif',
222
            'ico'     => 'image/icon',
223
            'djvu'    => 'image/vnd.djvu',
224
            'djv'     => 'image/vnd.djvu',
225
            'wbmp'    => 'image/vnd.wap.wbmp',
226
            'ras'     => 'image/x-cmu-raster',
227
            'pnm'     => 'image/x-portable-anymap',
228
            'pbm'     => 'image/x-portable-bitmap',
229
            'pgm'     => 'image/x-portable-graymap',
230
            'ppm'     => 'image/x-portable-pixmap',
231
            'rgb'     => 'image/x-rgb',
232
            'xbm'     => 'image/x-xbitmap',
233
            'xpm'     => 'image/x-xpixmap',
234
            'xwd'     => 'image/x-windowdump',
235
            'igs'     => 'model/iges',
236
            'iges'    => 'model/iges',
237
            'msh'     => 'model/mesh',
238
            'mesh'    => 'model/mesh',
239
            'silo'    => 'model/mesh',
240
            'wrl'     => 'model/vrml',
241
            'vrml'    => 'model/vrml',
242
            'css'     => 'text/css',
243
            'html'    => 'text/html',
244
            'htm'     => 'text/html',
245
            'asc'     => 'text/plain',
246
            'txt'     => 'text/plain',
247
            'Log'     => 'text/plain',
248
            'log'     => 'text/plain',
249
            'rtx'     => 'text/richtext',
250
            'rtf'     => 'text/rtf',
251
            'sgml'    => 'text/sgml',
252
            'sgm'     => 'text/sgml',
253
            'tsv'     => 'text/tab-seperated-values',
254
            'wml'     => 'text/vnd.wap.wml',
255
            'wmls'    => 'text/vnd.wap.wmlscript',
256
            'etx'     => 'text/x-setext',
257
            'xml'     => 'text/xml',
258
            'xsl'     => 'text/xml',
259
            'mpeg'    => 'video/mpeg',
260
            'mpg'     => 'video/mpeg',
261
            'mpe'     => 'video/mpeg',
262
            'qt'      => 'video/quicktime',
263
            'mov'     => 'video/quicktime',
264
            'mxu'     => 'video/vnd.mpegurl',
265
            'avi'     => 'video/x-msvideo',
266
            'movie'   => 'video/x-sgi-movie',
267
            'php'     => 'text/php',
268
            'php3'    => 'text/php3',
269
            'ice'     => 'x-conference-xcooltalk',
270
            'unknown' => 'application/octet-stream',
271
        ];
272
    }
273
}
274