Elgg
Version 2.3
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
engine
classes
Elgg
Filesystem
MimeTypeDetector.php
Go to the documentation of this file.
1
<?php
2
namespace
Elgg\Filesystem
;
3
7
class
MimeTypeDetector
{
8
9
const
DEFAULT_TYPE
=
'application/octet-stream'
;
10
14
public
$strategies
= [
15
[__CLASS__,
'tryFinfo'
],
16
[__CLASS__,
'tryMimeContentType'
],
17
[__CLASS__,
'tryFile'
],
18
[__CLASS__,
'tryGetimagesize'
],
19
];
20
24
public
$use_extension
=
true
;
25
29
public
$extensions
= array(
30
'txt'
=>
'text/plain'
,
31
'htm'
=>
'text/html'
,
32
'html'
=>
'text/html'
,
33
'php'
=>
'text/html'
,
34
'css'
=>
'text/css'
,
35
'js'
=>
'application/javascript'
,
36
'json'
=>
'application/json'
,
37
'xml'
=>
'application/xml'
,
38
'swf'
=>
'application/x-shockwave-flash'
,
39
'flv'
=>
'video/x-flv'
,
40
41
// images
42
'png'
=>
'image/png'
,
43
'jpe'
=>
'image/jpeg'
,
44
'jpeg'
=>
'image/jpeg'
,
45
'jpg'
=>
'image/jpeg'
,
46
'gif'
=>
'image/gif'
,
47
'bmp'
=>
'image/bmp'
,
48
'ico'
=>
'image/vnd.microsoft.icon'
,
49
'tiff'
=>
'image/tiff'
,
50
'tif'
=>
'image/tiff'
,
51
'svg'
=>
'image/svg+xml'
,
52
'svgz'
=>
'image/svg+xml'
,
53
54
// archives
55
'zip'
=>
'application/zip'
,
56
'rar'
=>
'application/x-rar-compressed'
,
57
'exe'
=>
'application/x-msdownload'
,
58
'msi'
=>
'application/x-msdownload'
,
59
'cab'
=>
'application/vnd.ms-cab-compressed'
,
60
61
// audio/video
62
'mp3'
=>
'audio/mpeg'
,
63
'qt'
=>
'video/quicktime'
,
64
'mov'
=>
'video/quicktime'
,
65
66
// adobe
67
'pdf'
=>
'application/pdf'
,
68
'psd'
=>
'image/vnd.adobe.photoshop'
,
69
'ai'
=>
'application/postscript'
,
70
'eps'
=>
'application/postscript'
,
71
'ps'
=>
'application/postscript'
,
72
73
// open office
74
'odt'
=>
'application/vnd.oasis.opendocument.text'
,
75
'ods'
=>
'application/vnd.oasis.opendocument.spreadsheet'
,
76
77
// office
78
'doc'
=>
'application/msword'
,
79
'rtf'
=>
'application/rtf'
,
80
'xls'
=>
'application/vnd.ms-excel'
,
81
'ppt'
=>
'application/vnd.ms-powerpoint'
,
82
'docx'
=>
'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
,
83
'docm'
=>
'application/vnd.ms-word.document.macroEnabled.12'
,
84
'dotx'
=>
'application/vnd.openxmlformats-officedocument.wordprocessingml.template'
,
85
'dotm'
=>
'application/vnd.ms-word.template.macroEnabled.12'
,
86
'xlsx'
=>
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
,
87
'xlsm'
=>
'application/vnd.ms-excel.sheet.macroEnabled.12'
,
88
'xltx'
=>
'application/vnd.openxmlformats-officedocument.spreadsheetml.template'
,
89
'xltm'
=>
'application/vnd.ms-excel.template.macroEnabled.12'
,
90
'xlsb'
=>
'application/vnd.ms-excel.sheet.binary.macroEnabled.12'
,
91
'xlam'
=>
'application/vnd.ms-excel.addin.macroEnabled.12'
,
92
'pptx'
=>
'application/vnd.openxmlformats-officedocument.presentationml.presentation'
,
93
'pptm'
=>
'application/vnd.ms-powerpoint.presentation.macroEnabled.12'
,
94
'ppsx'
=>
'application/vnd.openxmlformats-officedocument.presentationml.slideshow'
,
95
'ppsm'
=>
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12'
,
96
'potx'
=>
'application/vnd.openxmlformats-officedocument.presentationml.template'
,
97
'potm'
=>
'application/vnd.ms-powerpoint.template.macroEnabled.12'
,
98
'ppam'
=>
'application/vnd.ms-powerpoint.addin.macroEnabled.12'
,
99
'sldx'
=>
'application/vnd.openxmlformats-officedocument.presentationml.slide'
,
100
'sldm'
=>
'application/vnd.ms-powerpoint.slide.macroEnabled.12'
,
101
'one'
=>
'application/msonenote'
,
102
'onetoc2'
=>
'application/msonenote'
,
103
'onetmp'
=>
'application/msonenote'
,
104
'onepkg'
=>
'application/msonenote'
,
105
'thmx'
=>
'application/vnd.ms-officetheme'
,
106
);
107
115
public
function
getType
(
$file
,
$default
= self::DEFAULT_TYPE) {
116
// Check only existing files
117
if
(!is_file(
$file
) || !is_readable(
$file
)) {
118
return
$default
;
119
}
120
121
$ext = pathinfo(
$file
, PATHINFO_EXTENSION);
122
123
$type
= $this->
tryStrategies
(
$file
);
124
125
if
(
$type
) {
126
return
$this->
fixDetectionErrors
(
$type
, $ext);
127
}
128
129
if
($this->use_extension && isset($this->
extensions
[$ext])) {
130
return
$this->
extensions
[$ext];
131
}
132
133
return
$default
;
134
}
135
142
public
function
tryStrategies
(
$file
) {
143
$type
=
''
;
144
foreach
($this->strategies as $strategy) {
145
$type
= call_user_func($strategy,
$file
);
146
if
(
$type
) {
147
break
;
148
}
149
}
150
return
$type
;
151
}
152
160
public
function
fixDetectionErrors
(
$type
,
$extension
) {
161
if
(
$type
===
'application/zip'
) {
162
// hack for Microsoft zipped formats
163
switch
(
$extension
) {
164
case
'docx'
:
165
return
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
;
166
case
'xlsx'
:
167
return
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
;
168
case
'pptx'
:
169
return
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
;
170
}
171
}
172
173
// check for bad ppt detection
174
if
(
$type
===
"application/vnd.ms-office"
&&
$extension
===
"ppt"
) {
175
return
"application/vnd.ms-powerpoint"
;
176
}
177
178
// try extension detection as a fallback for octet-stream
179
if
(
$type
===
"application/octet-stream"
&& $this->use_extension && isset($this->
extensions
[
$extension
])) {
180
return
$this->
extensions
[
$extension
];
181
}
182
183
return
$type
;
184
}
185
192
public
static
function
tryFinfo
(
$file
) {
193
if
(!function_exists(
'finfo_open'
)) {
194
return
''
;
195
}
196
197
$finfo = finfo_open(FILEINFO_MIME);
198
$type
= finfo_file($finfo,
$file
);
199
finfo_close($finfo);
200
// Mimetype can come in text/plain; charset=us-ascii form
201
if
(strpos(
$type
,
';'
)) {
202
list(
$type
,) = explode(
';'
,
$type
);
203
}
204
return
$type
;
205
}
206
213
public
static
function
tryMimeContentType
(
$file
) {
214
return
function_exists(
'mime_content_type'
) ? mime_content_type(
$file
) :
''
;
215
}
216
223
public
static
function
tryFile
(
$file
) {
224
if
(DIRECTORY_SEPARATOR !==
'/'
|| !function_exists(
'exec'
)) {
225
return
''
;
226
}
227
$type
= @exec(
"file -b --mime-type "
. escapeshellarg(
$file
));
228
return
$type
?
$type
:
''
;
229
}
230
237
public
static
function
tryGetimagesize
(
$file
) {
238
$data
= @getimagesize(
$file
);
239
return
empty(
$data
[
'mime'
]) ?
''
:
$data
[
'mime'
];
240
}
241
}
Elgg\Filesystem\MimeTypeDetector\tryGetimagesize
static tryGetimagesize($file)
Detect MIME type.
Definition:
MimeTypeDetector.php:237
$file
if(!array_key_exists($filename, $text_files)) $file
Definition:
plugin_text_file.php:29
conf.extensions
list extensions
Definition:
conf.py:31
Elgg\Filesystem\MimeTypeDetector\tryStrategies
tryStrategies($file)
Detect MIME type using various strategies.
Definition:
MimeTypeDetector.php:142
Elgg\Filesystem\MimeTypeDetector\tryFinfo
static tryFinfo($file)
Detect MIME type using finfo_open.
Definition:
MimeTypeDetector.php:192
$data
$data
Definition:
opendd.php:13
$default
$default
Definition:
checkbox.php:34
Elgg\Filesystem
Elgg\Filesystem\MimeTypeDetector\$use_extension
$use_extension
Definition:
MimeTypeDetector.php:24
Elgg\Filesystem\MimeTypeDetector\getType
getType($file, $default=self::DEFAULT_TYPE)
Sniff the MIME type.
Definition:
MimeTypeDetector.php:115
Elgg\Filesystem\MimeTypeDetector\fixDetectionErrors
fixDetectionErrors($type, $extension)
Fix common type detection errors.
Definition:
MimeTypeDetector.php:160
Elgg\Filesystem\MimeTypeDetector\DEFAULT_TYPE
const DEFAULT_TYPE
Definition:
MimeTypeDetector.php:9
Elgg\Filesystem\MimeTypeDetector\tryMimeContentType
static tryMimeContentType($file)
Detect MIME type using mime_content_type.
Definition:
MimeTypeDetector.php:213
Elgg\Filesystem\MimeTypeDetector
Detect the MIME type of a file.
Definition:
MimeTypeDetector.php:7
Elgg\Filesystem\MimeTypeDetector\$strategies
$strategies
Definition:
MimeTypeDetector.php:14
Elgg\Filesystem\MimeTypeDetector\tryFile
static tryFile($file)
Detect MIME type using file(1)
Definition:
MimeTypeDetector.php:223
Elgg\Filesystem\MimeTypeDetector\$extensions
$extensions
Definition:
MimeTypeDetector.php:29
$extension
$extension
Definition:
default.php:23
$type
if(!$display_name) $type
Definition:
delete.php:27
Generated on Sun Nov 10 2024 00:01:09 for Elgg by
1.8.11