Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mimetype.php
1<?php
2namespace Bitrix\Main\Web;
3
5
6final class MimeType
7{
8 protected static $mimes = [
9 'hqx' => 'application/mac-binhex40',
10 'cpt' => 'application/mac-compactpro',
11 'csv' => 'text/x-comma-separated-values',
12 'bin' => 'application/octet-stream',
13 'dms' => 'application/octet-stream',
14 'lha' => 'application/octet-stream',
15 'lzh' => 'application/octet-stream',
16 'exe' => 'application/octet-stream',
17 'class' => 'application/octet-stream',
18 'psd' => 'application/x-photoshop',
19 'so' => 'application/octet-stream',
20 'sea' => 'application/octet-stream',
21 'dll' => 'application/octet-stream',
22 'oda' => 'application/oda',
23 'pdf' => 'application/pdf',
24 'ai' => 'application/pdf',
25 'eps' => 'application/postscript',
26 'ps' => 'application/postscript',
27 'smi' => 'application/smil',
28 'smil' => 'application/smil',
29 'mif' => 'application/vnd.mif',
30 'xls' => 'application/vnd.ms-excel',
31 'ppt' => 'application/powerpoint',
32 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
33 'wbxml' => 'application/wbxml',
34 'wmlc' => 'application/wmlc',
35 'dcr' => 'application/x-director',
36 'dir' => 'application/x-director',
37 'dxr' => 'application/x-director',
38 'dvi' => 'application/x-dvi',
39 'gtar' => 'application/x-gtar',
40 'gz' => 'application/x-gzip',
41 'gzip' => 'application/x-gzip',
42 'php' => 'application/x-httpd-php',
43 'php4' => 'application/x-httpd-php',
44 'php3' => 'application/x-httpd-php',
45 'phtml' => 'application/x-httpd-php',
46 'phps' => 'application/x-httpd-php-source',
47 'js' => 'application/javascript',
48 'swf' => 'application/x-shockwave-flash',
49 'sit' => 'application/x-stuffit',
50 'tar' => 'application/x-tar',
51 'tgz' => 'application/x-tar',
52 'z' => 'application/x-compress',
53 'xhtml' => 'application/xhtml+xml',
54 'xht' => 'application/xhtml+xml',
55 'zip' => 'application/x-zip',
56 'rar' => 'application/x-rar',
57 'mid' => 'audio/midi',
58 'midi' => 'audio/midi',
59 'mpga' => 'audio/mpeg',
60 'mp2' => 'audio/mpeg',
61 'mp3' => 'audio/mpeg',
62 'aif' => 'audio/x-aiff',
63 'aiff' => 'audio/x-aiff',
64 'aifc' => 'audio/x-aiff',
65 'ram' => 'audio/x-pn-realaudio',
66 'rm' => 'audio/x-pn-realaudio',
67 'rpm' => 'audio/x-pn-realaudio-plugin',
68 'ra' => 'audio/x-realaudio',
69 'rv' => 'video/vnd.rn-realvideo',
70 'wav' => 'audio/x-wav',
71 'jpg' => 'image/jpeg',
72 'jpeg' => 'image/jpeg',
73 'jpe' => 'image/jpeg',
74 'png' => 'image/png',
75 'webp' => 'image/webp',
76 'gif' => 'image/gif',
77 'bmp' => 'image/bmp',
78 'tiff' => 'image/tiff',
79 'tif' => 'image/tiff',
80 'svg' => 'image/svg+xml',
81 'css' => 'text/css',
82 'html' => 'text/html',
83 'htm' => 'text/html',
84 'shtml' => 'text/html',
85 'txt' => 'text/plain',
86 'text' => 'text/plain',
87 'log' => 'text/plain',
88 'rtx' => 'text/richtext',
89 'rtf' => 'text/rtf',
90 'xml' => 'application/xml',
91 'xsl' => 'application/xml',
92 'mpeg' => 'video/mpeg',
93 'mpg' => 'video/mpeg',
94 'mpe' => 'video/mpeg',
95 'qt' => 'video/quicktime',
96 'mov' => 'video/quicktime',
97 'avi' => 'video/x-msvideo',
98 'movie' => 'video/x-sgi-movie',
99 'doc' => 'application/msword',
100 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
101 'dot' => 'application/msword',
102 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
103 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
104 'word' => 'application/msword',
105 'xl' => 'application/excel',
106 'eml' => 'message/rfc822',
107 'json' => 'application/json',
108 'pem' => 'application/x-x509-user-cert',
109 'p10' => 'application/x-pkcs10',
110 'p12' => 'application/x-pkcs12',
111 'p7a' => 'application/x-pkcs7-signature',
112 'p7c' => 'application/pkcs7-mime',
113 'p7m' => 'application/pkcs7-mime',
114 'p7r' => 'application/x-pkcs7-certreqresp',
115 'p7s' => 'application/pkcs7-signature',
116 'crt' => 'application/x-x509-ca-cert',
117 'crl' => 'application/pkix-crl',
118 'der' => 'application/x-x509-ca-cert',
119 'kdb' => 'application/octet-stream',
120 'pgp' => 'application/pgp',
121 'gpg' => 'application/gpg-keys',
122 'sst' => 'application/octet-stream',
123 'csr' => 'application/octet-stream',
124 'rsa' => 'application/x-pkcs7',
125 'cer' => 'application/pkix-cert',
126 '3g2' => 'video/3gpp2',
127 '3gp' => 'video/3gp',
128 'mp4' => 'video/mp4',
129 'm4a' => 'audio/x-m4a',
130 'f4v' => 'video/mp4',
131 'webm' => 'video/webm',
132 'aac' => 'audio/x-acc',
133 'm4u' => 'application/vnd.mpegurl',
134 'm3u' => 'text/plain',
135 'xspf' => 'application/xspf+xml',
136 'vlc' => 'application/videolan',
137 'wmv' => 'video/x-ms-wmv',
138 'au' => 'audio/x-au',
139 'ac3' => 'audio/ac3',
140 'flac' => 'audio/x-flac',
141 'ogg' => 'audio/ogg',
142 'kmz' => 'application/vnd.google-earth.kmz',
143 'kml' => 'application/vnd.google-earth.kml+xml',
144 'ics' => 'text/calendar',
145 'zsh' => 'text/x-scriptzsh',
146 '7zip' => 'application/x-7z-compressed',
147 'cdr' => 'application/cdr',
148 'wma' => 'audio/x-ms-wma',
149 'jar' => 'application/java-archive',
150 'sketch' => 'application/octet-stream',
151 'vsd' => 'application/vnd.ms-visio',
152 'vsdx' => 'application/vnd.ms-visio.drawing',
153 'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
154 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
155 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
156 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
157 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
158 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
159 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
160 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
161 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
162 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
163 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
164 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
165 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
166 'fb2' => 'application/xml',
167 'djvu' => 'image/vnd.djvu',
168 'epub' => 'application/epub+zip',
169 'msg' => 'message/rfc822',
170 'ott' => 'application/vnd.oasis.opendocument.text-template',
171 'otp' => 'application/vnd.oasis.opendocument.presentation-template',
172 'ots' => 'application/vnd.oasis.opendocument.spreadsheet-template',
173 'odt' => 'application/vnd.oasis.opendocument.text',
174 'odp' => 'application/vnd.oasis.opendocument.presentation',
175 'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
176 'xodt' => 'application/vnd.collabio.xodocuments.document',
177 '7z' => 'application/x-7z-compressed',
178 'bz2' => 'application/x-bzip2',
179 'mp4v' => 'video/mp4',
180 'mpg4' => 'video/mp4',
181 'ogv' => 'video/ogg',
182 'flv' => 'video/x-flv',
183 'mkv' => 'video/x-matroska',
184 'm4v' => 'video/x-m4v',
185 'h264' => 'video/h264',
186 'sql' => 'text/plain',
187 'pl' => 'text/plain',
188 'sh' => 'text/plain',
189 'ttf' => 'application/x-font-ttf',
190 'otf' => 'application/vnd.ms-opentype',
191 'eot' => 'application/vnd.ms-fontobject',
192 'woff' => 'application/font-woff',
193 'pfa' => 'application/x-font-type1',
194 'xla' => "application/vnd.ms-excel",
195 'xlb' => 'application/vnd.ms-excel',
196 'xlc' => 'application/vnd.ms-excel',
197 'xll' => 'application/vnd.ms-excel',
198 'xlm' => 'application/vnd.ms-excel',
199 'xlt' => 'application/vnd.ms-excel',
200 'xlw' => 'application/vnd.ms-excel',
201 'dbf' => 'application/vnd.ms-excel',
202 'm1v' => 'video/mpeg',
203 'm2v' => 'video/mpeg',
204 'jpgv' => 'video/jpeg',
205 'dvb' => 'video/vnd.dvb.file',
206 'fvt' => 'video/vnd.fvt',
207 'mxu' => 'video/vnd.mpegurl',
208 'pyv' => 'video/vnd.ms-playready.media.pyv',
209 'uvu' => 'video/vnd.uvvu.mp4',
210 'uvvu' => 'video/vnd.uvvu.mp4',
211 'viv' => 'video/vnd.vivo',
212 'fli' => 'video/x-fli',
213 'mk3d' => 'video/x-matroska',
214 'mks' => 'video/x-matroska',
215 'mng' => 'video/x-mng',
216 'asf' => 'video/x-ms-asf',
217 'asx' => 'video/x-ms-asf',
218 'vob' => 'video/x-ms-vob',
219 'wm' => 'video/x-ms-wm',
220 'wmx' => 'video/x-ms-wmx',
221 'wvx' => 'video/x-ms-wvx',
222 'smv' => 'video/x-smv',
223 'mp2a' => 'audio/mpeg',
224 'm2a' => 'audio/mpeg',
225 'm3a' => 'audio/mpeg',
226 'oga' => 'audio/ogg',
227 'spx' => 'audio/ogg',
228 'weba' => 'audio/webm',
229 'm3u8' => 'application/vnd.apple.mpegurl',
230 'ts' => 'video/MP2T',
231 ];
232
233 public static function getMimeTypeList()
234 {
235 return self::$mimes;
236 }
237
238 public static function getByFileExtension($extension)
239 {
240 $extension = strtolower($extension);
241 if (isset(self::$mimes[$extension]))
242 {
243 return self::$mimes[$extension];
244 }
245
246 return 'application/octet-stream';
247 }
248
249 public static function getByFilename($filename)
250 {
251 return self::getByFileExtension(getFileExtension($filename));
252 }
253
254 public static function getByFilePath($filePath)
255 {
256 $file = new IO\File($filePath);
257
258 return $file->getContentType();
259 }
260
261 public static function getByContent($content)
262 {
263 $fileInfo = new \Finfo(FILEINFO_MIME_TYPE);
264 $mimeType = $fileInfo->buffer($content);
265
266 return $mimeType?: 'application/octet-stream';
267 }
268
269 public static function isImage($mime)
270 {
271 // Attributes are possible: image/jpeg; charset=ISO-8859-1
272 $parts = explode(';', (string)$mime);
273 $mime = trim($parts[0]);
274
275 return preg_match('#^image/[a-z0-9.-]+$#i', $mime);
276 }
277
278 public static function normalize($contentType)
279 {
280 if (!is_string($contentType))
281 {
282 return 'application/octet-stream';
283 }
284
285 $ct = strtolower($contentType);
286 $ct = str_replace(array("\r", "\n", "\0"), "", $ct);
287
288 // We don't need attributes: image/jpeg; charset=ISO-8859-1
289 $parts = explode(';', $ct);
290 $ct = trim($parts[0]);
291
292 if ($ct == '')
293 {
294 $ct = 'application/octet-stream';
295 }
296 elseif (strpos($ct, "excel") !== false)
297 {
298 $ct = "application/vnd.ms-excel";
299 }
300 elseif (strpos($ct, "word") !== false && strpos($ct, "vnd.openxmlformats") === false)
301 {
302 $ct = "application/msword";
303 }
304 elseif ($ct == 'image/pjpeg' || $ct == 'image/jpg')
305 {
306 $ct = 'image/jpeg';
307 }
308
309 return $ct;
310 }
311}
static getByContent($content)
Definition mimetype.php:261
static getByFilePath($filePath)
Definition mimetype.php:254
static getByFilename($filename)
Definition mimetype.php:249
static isImage($mime)
Definition mimetype.php:269
static normalize($contentType)
Definition mimetype.php:278
static getByFileExtension($extension)
Definition mimetype.php:238