Merge "Hide TOC with CSS instead of JavaScript"
[lhc/web/wiklou.git] / includes / libs / mime / MimeAnalyzer.php
1 <?php
2 /**
3 * Module defining helper functions for detecting and dealing with MIME types.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22 use Psr\Log\LoggerAwareInterface;
23 use Psr\Log\LoggerInterface;
24
25 /**
26 * Implements functions related to MIME types such as detection and mapping to file extension
27 *
28 * @since 1.28
29 */
30 class MimeAnalyzer implements LoggerAwareInterface {
31 /** @var string */
32 protected $typeFile;
33 /** @var string */
34 protected $infoFile;
35 /** @var string */
36 protected $xmlTypes;
37 /** @var callable */
38 protected $initCallback;
39 /** @var callable */
40 protected $detectCallback;
41 /** @var callable */
42 protected $guessCallback;
43 /** @var callable */
44 protected $extCallback;
45 /** @var array Mapping of media types to arrays of MIME types */
46 protected $mediaTypes = null;
47 /** @var array Map of MIME type aliases */
48 protected $mimeTypeAliases = null;
49 /** @var array Map of MIME types to file extensions (as a space separated list) */
50 protected $mimetoExt = null;
51
52 /** @var array Map of file extensions types to MIME types (as a space separated list) */
53 public $mExtToMime = null; // legacy name; field accessed by hooks
54
55 /** @var IEContentAnalyzer */
56 protected $IEAnalyzer;
57
58 /** @var string Extra MIME types, set for example by media handling extensions */
59 private $extraTypes = '';
60 /** @var string Extra MIME info, set for example by media handling extensions */
61 private $extraInfo = '';
62
63 /** @var LoggerInterface */
64 private $logger;
65
66 /**
67 * Defines a set of well known MIME types
68 * This is used as a fallback to mime.types files.
69 * An extensive list of well known MIME types is provided by
70 * the file mime.types in the includes directory.
71 *
72 * This list concatenated with mime.types is used to create a MIME <-> ext
73 * map. Each line contains a MIME type followed by a space separated list of
74 * extensions. If multiple extensions for a single MIME type exist or if
75 * multiple MIME types exist for a single extension then in most cases
76 * MediaWiki assumes that the first extension following the MIME type is the
77 * canonical extension, and the first time a MIME type appears for a certain
78 * extension is considered the canonical MIME type.
79 *
80 * (Note that appending the type file list to the end of self::$wellKnownTypes
81 * sucks because you can't redefine canonical types. This could be fixed by
82 * appending self::$wellKnownTypes behind type file list, but who knows
83 * what will break? In practice this probably isn't a problem anyway -- Bryan)
84 */
85 protected static $wellKnownTypes = <<<EOT
86 application/ogg ogx ogg ogm ogv oga spx opus
87 application/pdf pdf
88 application/vnd.oasis.opendocument.chart odc
89 application/vnd.oasis.opendocument.chart-template otc
90 application/vnd.oasis.opendocument.database odb
91 application/vnd.oasis.opendocument.formula odf
92 application/vnd.oasis.opendocument.formula-template otf
93 application/vnd.oasis.opendocument.graphics odg
94 application/vnd.oasis.opendocument.graphics-template otg
95 application/vnd.oasis.opendocument.image odi
96 application/vnd.oasis.opendocument.image-template oti
97 application/vnd.oasis.opendocument.presentation odp
98 application/vnd.oasis.opendocument.presentation-template otp
99 application/vnd.oasis.opendocument.spreadsheet ods
100 application/vnd.oasis.opendocument.spreadsheet-template ots
101 application/vnd.oasis.opendocument.text odt
102 application/vnd.oasis.opendocument.text-master otm
103 application/vnd.oasis.opendocument.text-template ott
104 application/vnd.oasis.opendocument.text-web oth
105 application/javascript js
106 application/x-shockwave-flash swf
107 audio/midi mid midi kar
108 audio/mpeg mpga mpa mp2 mp3
109 audio/x-aiff aif aiff aifc
110 audio/x-wav wav
111 audio/ogg oga spx ogg opus
112 audio/opus opus ogg oga ogg spx
113 image/x-bmp bmp
114 image/gif gif
115 image/jpeg jpeg jpg jpe
116 image/png png
117 image/svg+xml svg
118 image/svg svg
119 image/tiff tiff tif
120 image/vnd.djvu djvu
121 image/x.djvu djvu
122 image/x-djvu djvu
123 image/x-portable-pixmap ppm
124 image/x-xcf xcf
125 text/plain txt
126 text/html html htm
127 video/ogg ogv ogm ogg
128 video/mpeg mpg mpeg
129 EOT;
130
131 /**
132 * Defines a set of well known MIME info entries
133 * This is used as a fallback to mime.info files.
134 * An extensive list of well known MIME types is provided by
135 * the file mime.info in the includes directory.
136 */
137 protected static $wellKnownInfo = <<<EOT
138 application/pdf [OFFICE]
139 application/vnd.oasis.opendocument.chart [OFFICE]
140 application/vnd.oasis.opendocument.chart-template [OFFICE]
141 application/vnd.oasis.opendocument.database [OFFICE]
142 application/vnd.oasis.opendocument.formula [OFFICE]
143 application/vnd.oasis.opendocument.formula-template [OFFICE]
144 application/vnd.oasis.opendocument.graphics [OFFICE]
145 application/vnd.oasis.opendocument.graphics-template [OFFICE]
146 application/vnd.oasis.opendocument.image [OFFICE]
147 application/vnd.oasis.opendocument.image-template [OFFICE]
148 application/vnd.oasis.opendocument.presentation [OFFICE]
149 application/vnd.oasis.opendocument.presentation-template [OFFICE]
150 application/vnd.oasis.opendocument.spreadsheet [OFFICE]
151 application/vnd.oasis.opendocument.spreadsheet-template [OFFICE]
152 application/vnd.oasis.opendocument.text [OFFICE]
153 application/vnd.oasis.opendocument.text-template [OFFICE]
154 application/vnd.oasis.opendocument.text-master [OFFICE]
155 application/vnd.oasis.opendocument.text-web [OFFICE]
156 application/javascript text/javascript application/x-javascript [EXECUTABLE]
157 application/x-shockwave-flash [MULTIMEDIA]
158 audio/midi [AUDIO]
159 audio/x-aiff [AUDIO]
160 audio/x-wav [AUDIO]
161 audio/mp3 audio/mpeg [AUDIO]
162 application/ogg audio/ogg video/ogg [MULTIMEDIA]
163 image/x-bmp image/x-ms-bmp image/bmp [BITMAP]
164 image/gif [BITMAP]
165 image/jpeg [BITMAP]
166 image/png [BITMAP]
167 image/svg+xml [DRAWING]
168 image/tiff [BITMAP]
169 image/vnd.djvu [BITMAP]
170 image/x-xcf [BITMAP]
171 image/x-portable-pixmap [BITMAP]
172 text/plain [TEXT]
173 text/html [TEXT]
174 video/ogg [VIDEO]
175 video/mpeg [VIDEO]
176 unknown/unknown application/octet-stream application/x-empty [UNKNOWN]
177 EOT;
178
179 /**
180 * @param array $params Configuration map, includes:
181 * - typeFile: path to file with the list of known MIME types
182 * - infoFile: path to file with the MIME type info
183 * - xmlTypes: map of root element names to XML MIME types
184 * - initCallback: initialization callback that is passed this object [optional]
185 * - detectCallback: alternative to finfo that returns the mime type for a file.
186 * For example, the callback can return the output of "file -bi". [optional]
187 * - guessCallback: callback to improve the guessed MIME type using the file data.
188 * This is intended for fixing mistakes in fileinfo or "detectCallback". [optional]
189 * - extCallback: callback to improve the guessed MIME type using the extension. [optional]
190 * - logger: PSR-3 logger [optional]
191 * @note Constructing these instances is expensive due to file reads.
192 * A service or singleton pattern should be used to avoid creating instances again and again.
193 */
194 public function __construct( array $params ) {
195 $this->typeFile = $params['typeFile'];
196 $this->infoFile = $params['infoFile'];
197 $this->xmlTypes = $params['xmlTypes'];
198 $this->initCallback = $params['initCallback'] ?? null;
199 $this->detectCallback = $params['detectCallback'] ?? null;
200 $this->guessCallback = $params['guessCallback'] ?? null;
201 $this->extCallback = $params['extCallback'] ?? null;
202 $this->logger = $params['logger'] ?? new \Psr\Log\NullLogger();
203
204 $this->loadFiles();
205 }
206
207 protected function loadFiles() {
208 /**
209 * --- load mime.types ---
210 */
211
212 # Allow media handling extensions adding MIME-types and MIME-info
213 if ( $this->initCallback ) {
214 call_user_func( $this->initCallback, $this );
215 }
216
217 $types = self::$wellKnownTypes;
218
219 $mimeTypeFile = $this->typeFile;
220 if ( $mimeTypeFile ) {
221 if ( is_file( $mimeTypeFile ) && is_readable( $mimeTypeFile ) ) {
222 $this->logger->info( __METHOD__ . ": loading mime types from $mimeTypeFile\n" );
223 $types .= "\n";
224 $types .= file_get_contents( $mimeTypeFile );
225 } else {
226 $this->logger->info( __METHOD__ . ": can't load mime types from $mimeTypeFile\n" );
227 }
228 } else {
229 $this->logger->info( __METHOD__ .
230 ": no mime types file defined, using built-ins only.\n" );
231 }
232
233 $types .= "\n" . $this->extraTypes;
234
235 $types = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $types );
236 $types = str_replace( "\t", " ", $types );
237
238 $this->mimetoExt = [];
239 $this->mExtToMime = [];
240
241 $lines = explode( "\n", $types );
242 foreach ( $lines as $s ) {
243 $s = trim( $s );
244 if ( empty( $s ) ) {
245 continue;
246 }
247 if ( strpos( $s, '#' ) === 0 ) {
248 continue;
249 }
250
251 $s = strtolower( $s );
252 $i = strpos( $s, ' ' );
253
254 if ( $i === false ) {
255 continue;
256 }
257
258 $mime = substr( $s, 0, $i );
259 $ext = trim( substr( $s, $i + 1 ) );
260
261 if ( empty( $ext ) ) {
262 continue;
263 }
264
265 if ( !empty( $this->mimetoExt[$mime] ) ) {
266 $this->mimetoExt[$mime] .= ' ' . $ext;
267 } else {
268 $this->mimetoExt[$mime] = $ext;
269 }
270
271 $extensions = explode( ' ', $ext );
272
273 foreach ( $extensions as $e ) {
274 $e = trim( $e );
275 if ( empty( $e ) ) {
276 continue;
277 }
278
279 if ( !empty( $this->mExtToMime[$e] ) ) {
280 $this->mExtToMime[$e] .= ' ' . $mime;
281 } else {
282 $this->mExtToMime[$e] = $mime;
283 }
284 }
285 }
286
287 /**
288 * --- load mime.info ---
289 */
290
291 $mimeInfoFile = $this->infoFile;
292
293 $info = self::$wellKnownInfo;
294
295 if ( $mimeInfoFile ) {
296 if ( is_file( $mimeInfoFile ) && is_readable( $mimeInfoFile ) ) {
297 $this->logger->info( __METHOD__ . ": loading mime info from $mimeInfoFile\n" );
298 $info .= "\n";
299 $info .= file_get_contents( $mimeInfoFile );
300 } else {
301 $this->logger->info( __METHOD__ . ": can't load mime info from $mimeInfoFile\n" );
302 }
303 } else {
304 $this->logger->info( __METHOD__ .
305 ": no mime info file defined, using built-ins only.\n" );
306 }
307
308 $info .= "\n" . $this->extraInfo;
309
310 $info = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $info );
311 $info = str_replace( "\t", " ", $info );
312
313 $this->mimeTypeAliases = [];
314 $this->mediaTypes = [];
315
316 $lines = explode( "\n", $info );
317 foreach ( $lines as $s ) {
318 $s = trim( $s );
319 if ( empty( $s ) ) {
320 continue;
321 }
322 if ( strpos( $s, '#' ) === 0 ) {
323 continue;
324 }
325
326 $s = strtolower( $s );
327 $i = strpos( $s, ' ' );
328
329 if ( $i === false ) {
330 continue;
331 }
332
333 # print "processing MIME INFO line $s<br>";
334
335 $match = [];
336 if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
337 $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
338 $mtype = trim( strtoupper( $match[1] ) );
339 } else {
340 $mtype = MEDIATYPE_UNKNOWN;
341 }
342
343 $m = explode( ' ', $s );
344
345 if ( !isset( $this->mediaTypes[$mtype] ) ) {
346 $this->mediaTypes[$mtype] = [];
347 }
348
349 foreach ( $m as $mime ) {
350 $mime = trim( $mime );
351 if ( empty( $mime ) ) {
352 continue;
353 }
354
355 $this->mediaTypes[$mtype][] = $mime;
356 }
357
358 if ( count( $m ) > 1 ) {
359 $main = $m[0];
360 $mCount = count( $m );
361 for ( $i = 1; $i < $mCount; $i += 1 ) {
362 $mime = $m[$i];
363 $this->mimeTypeAliases[$mime] = $main;
364 }
365 }
366 }
367 }
368
369 public function setLogger( LoggerInterface $logger ) {
370 $this->logger = $logger;
371 }
372
373 /**
374 * Adds to the list mapping MIME to file extensions.
375 * As an extension author, you are encouraged to submit patches to
376 * MediaWiki's core to add new MIME types to mime.types.
377 * @param string $types
378 */
379 public function addExtraTypes( $types ) {
380 $this->extraTypes .= "\n" . $types;
381 }
382
383 /**
384 * Adds to the list mapping MIME to media type.
385 * As an extension author, you are encouraged to submit patches to
386 * MediaWiki's core to add new MIME info to mime.info.
387 * @param string $info
388 */
389 public function addExtraInfo( $info ) {
390 $this->extraInfo .= "\n" . $info;
391 }
392
393 /**
394 * Returns a list of file extensions for a given MIME type as a space
395 * separated string or null if the MIME type was unrecognized. Resolves
396 * MIME type aliases.
397 *
398 * @param string $mime
399 * @return string|null
400 */
401 public function getExtensionsForType( $mime ) {
402 $mime = strtolower( $mime );
403
404 // Check the mime-to-ext map
405 if ( isset( $this->mimetoExt[$mime] ) ) {
406 return $this->mimetoExt[$mime];
407 }
408
409 // Resolve the MIME type to the canonical type
410 if ( isset( $this->mimeTypeAliases[$mime] ) ) {
411 $mime = $this->mimeTypeAliases[$mime];
412 if ( isset( $this->mimetoExt[$mime] ) ) {
413 return $this->mimetoExt[$mime];
414 }
415 }
416
417 return null;
418 }
419
420 /**
421 * Returns a list of MIME types for a given file extension as a space
422 * separated string or null if the extension was unrecognized.
423 *
424 * @param string $ext
425 * @return string|null
426 */
427 public function getTypesForExtension( $ext ) {
428 $ext = strtolower( $ext );
429
430 $r = $this->mExtToMime[$ext] ?? null;
431 return $r;
432 }
433
434 /**
435 * Returns a single MIME type for a given file extension or null if unknown.
436 * This is always the first type from the list returned by getTypesForExtension($ext).
437 *
438 * @param string $ext
439 * @return string|null
440 */
441 public function guessTypesForExtension( $ext ) {
442 $m = $this->getTypesForExtension( $ext );
443 if ( is_null( $m ) ) {
444 return null;
445 }
446
447 // TODO: Check if this is needed; strtok( $m, ' ' ) should be sufficient
448 $m = trim( $m );
449 $m = preg_replace( '/\s.*$/', '', $m );
450
451 return $m;
452 }
453
454 /**
455 * Tests if the extension matches the given MIME type. Returns true if a
456 * match was found, null if the MIME type is unknown, and false if the
457 * MIME type is known but no matches where found.
458 *
459 * @param string $extension
460 * @param string $mime
461 * @return bool|null
462 */
463 public function isMatchingExtension( $extension, $mime ) {
464 $ext = $this->getExtensionsForType( $mime );
465
466 if ( !$ext ) {
467 return null; // Unknown MIME type
468 }
469
470 $ext = explode( ' ', $ext );
471
472 $extension = strtolower( $extension );
473 return in_array( $extension, $ext );
474 }
475
476 /**
477 * Returns true if the MIME type is known to represent an image format
478 * supported by the PHP GD library.
479 *
480 * @param string $mime
481 *
482 * @return bool
483 */
484 public function isPHPImageType( $mime ) {
485 // As defined by imagegetsize and image_type_to_mime
486 static $types = [
487 'image/gif', 'image/jpeg', 'image/png',
488 'image/x-bmp', 'image/xbm', 'image/tiff',
489 'image/jp2', 'image/jpeg2000', 'image/iff',
490 'image/xbm', 'image/x-xbitmap',
491 'image/vnd.wap.wbmp', 'image/vnd.xiff',
492 'image/x-photoshop',
493 'application/x-shockwave-flash',
494 ];
495
496 return in_array( $mime, $types );
497 }
498
499 /**
500 * Returns true if the extension represents a type which can
501 * be reliably detected from its content. Use this to determine
502 * whether strict content checks should be applied to reject
503 * invalid uploads; if we can't identify the type we won't
504 * be able to say if it's invalid.
505 *
506 * @todo Be more accurate when using fancy MIME detector plugins;
507 * right now this is the bare minimum getimagesize() list.
508 * @param string $extension
509 * @return bool
510 */
511 function isRecognizableExtension( $extension ) {
512 static $types = [
513 // Types recognized by getimagesize()
514 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
515 'bmp', 'tiff', 'tif', 'jpc', 'jp2',
516 'jpx', 'jb2', 'swc', 'iff', 'wbmp',
517 'xbm',
518
519 // Formats we recognize magic numbers for
520 'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx', 'opus',
521 'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka',
522 'webp', 'mp3',
523
524 // XML formats we sure hope we recognize reliably
525 'svg',
526
527 // 3D formats
528 'stl',
529 ];
530 return in_array( strtolower( $extension ), $types );
531 }
532
533 /**
534 * Improves a MIME type using the file extension. Some file formats are very generic,
535 * so their MIME type is not very meaningful. A more useful MIME type can be derived
536 * by looking at the file extension. Typically, this method would be called on the
537 * result of guessMimeType().
538 *
539 * @param string $mime The MIME type, typically guessed from a file's content.
540 * @param string $ext The file extension, as taken from the file name
541 *
542 * @return string The MIME type
543 */
544 public function improveTypeFromExtension( $mime, $ext ) {
545 if ( $mime === 'unknown/unknown' ) {
546 if ( $this->isRecognizableExtension( $ext ) ) {
547 $this->logger->info( __METHOD__ . ': refusing to guess mime type for .' .
548 "$ext file, we should have recognized it\n" );
549 } else {
550 // Not something we can detect, so simply
551 // trust the file extension
552 $mime = $this->guessTypesForExtension( $ext );
553 }
554 } elseif ( $mime === 'application/x-opc+zip' ) {
555 if ( $this->isMatchingExtension( $ext, $mime ) ) {
556 // A known file extension for an OPC file,
557 // find the proper MIME type for that file extension
558 $mime = $this->guessTypesForExtension( $ext );
559 } else {
560 $this->logger->info( __METHOD__ .
561 ": refusing to guess better type for $mime file, " .
562 ".$ext is not a known OPC extension.\n" );
563 $mime = 'application/zip';
564 }
565 } elseif ( $mime === 'text/plain' && $this->findMediaType( ".$ext" ) === MEDIATYPE_TEXT ) {
566 // Textual types are sometimes not recognized properly.
567 // If detected as text/plain, and has an extension which is textual
568 // improve to the extension's type. For example, csv and json are often
569 // misdetected as text/plain.
570 $mime = $this->guessTypesForExtension( $ext );
571 }
572
573 # Media handling extensions can improve the MIME detected
574 $callback = $this->extCallback;
575 if ( $callback ) {
576 $callback( $this, $ext, $mime /* by reference */ );
577 }
578
579 if ( isset( $this->mimeTypeAliases[$mime] ) ) {
580 $mime = $this->mimeTypeAliases[$mime];
581 }
582
583 $this->logger->info( __METHOD__ . ": improved mime type for .$ext: $mime\n" );
584 return $mime;
585 }
586
587 /**
588 * MIME type detection. This uses detectMimeType to detect the MIME type
589 * of the file, but applies additional checks to determine some well known
590 * file formats that may be missed or misinterpreted by the default MIME
591 * detection (namely XML based formats like XHTML or SVG, as well as ZIP
592 * based formats like OPC/ODF files).
593 *
594 * @param string $file The file to check
595 * @param string|bool $ext The file extension, or true (default) to extract
596 * it from the filename. Set it to false to ignore the extension. DEPRECATED!
597 * Set to false, use improveTypeFromExtension($mime, $ext) later to improve MIME type.
598 *
599 * @return string The MIME type of $file
600 */
601 public function guessMimeType( $file, $ext = true ) {
602 if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
603 $this->logger->info( __METHOD__ .
604 ": WARNING: use of the \$ext parameter is deprecated. " .
605 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
606 }
607
608 $mime = $this->doGuessMimeType( $file, $ext );
609
610 if ( !$mime ) {
611 $this->logger->info( __METHOD__ .
612 ": internal type detection failed for $file (.$ext)...\n" );
613 $mime = $this->detectMimeType( $file, $ext );
614 }
615
616 if ( isset( $this->mimeTypeAliases[$mime] ) ) {
617 $mime = $this->mimeTypeAliases[$mime];
618 }
619
620 $this->logger->info( __METHOD__ . ": guessed mime type of $file: $mime\n" );
621 return $mime;
622 }
623
624 /**
625 * Guess the MIME type from the file contents.
626 *
627 * @todo Remove $ext param
628 *
629 * @param string $file
630 * @param mixed $ext
631 * @return bool|string
632 * @throws UnexpectedValueException
633 */
634 private function doGuessMimeType( $file, $ext ) {
635 // Read a chunk of the file
636 Wikimedia\suppressWarnings();
637 $f = fopen( $file, 'rb' );
638 Wikimedia\restoreWarnings();
639
640 if ( !$f ) {
641 return 'unknown/unknown';
642 }
643
644 $fsize = filesize( $file );
645 if ( $fsize === false ) {
646 return 'unknown/unknown';
647 }
648
649 $head = fread( $f, 1024 );
650 $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
651 if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
652 throw new UnexpectedValueException(
653 "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
654 }
655 $tail = $tailLength ? fread( $f, $tailLength ) : '';
656 fclose( $f );
657
658 $this->logger->info( __METHOD__ .
659 ": analyzing head and tail of $file for magic numbers.\n" );
660
661 // Hardcode a few magic number checks...
662 $headers = [
663 // Multimedia...
664 'MThd' => 'audio/midi',
665 'OggS' => 'application/ogg',
666 'ID3' => 'audio/mpeg',
667 "\xff\xfb" => 'audio/mpeg', // MPEG-1 layer 3
668 "\xff\xf3" => 'audio/mpeg', // MPEG-2 layer 3 (lower sample rates)
669 "\xff\xe3" => 'audio/mpeg', // MPEG-2.5 layer 3 (very low sample rates)
670
671 // Image formats...
672 // Note that WMF may have a bare header, no magic number.
673 "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives?
674 "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
675 '%PDF' => 'application/pdf',
676 'gimp xcf' => 'image/x-xcf',
677
678 // Some forbidden fruit...
679 'MZ' => 'application/octet-stream', // DOS/Windows executable
680 "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary
681 "\x7fELF" => 'application/octet-stream', // ELF binary
682 ];
683
684 foreach ( $headers as $magic => $candidate ) {
685 if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
686 $this->logger->info( __METHOD__ .
687 ": magic header in $file recognized as $candidate\n" );
688 return $candidate;
689 }
690 }
691
692 /* Look for WebM and Matroska files */
693 if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
694 $doctype = strpos( $head, "\x42\x82" );
695 if ( $doctype ) {
696 // Next byte is datasize, then data (sizes larger than 1 byte are stupid muxers)
697 $data = substr( $head, $doctype + 3, 8 );
698 if ( strncmp( $data, "matroska", 8 ) == 0 ) {
699 $this->logger->info( __METHOD__ . ": recognized file as video/x-matroska\n" );
700 return "video/x-matroska";
701 } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
702 // XXX HACK look for a video track, if we don't find it, this is an audio file
703 $videotrack = strpos( $head, "\x86\x85V_VP" );
704
705 if ( $videotrack ) {
706 // There is a video track, so this is a video file.
707 $this->logger->info( __METHOD__ . ": recognized file as video/webm\n" );
708 return "video/webm";
709 }
710
711 $this->logger->info( __METHOD__ . ": recognized file as audio/webm\n" );
712 return "audio/webm";
713 }
714 }
715 $this->logger->info( __METHOD__ . ": unknown EBML file\n" );
716 return "unknown/unknown";
717 }
718
719 /* Look for WebP */
720 if ( strncmp( $head, "RIFF", 4 ) == 0 &&
721 strncmp( substr( $head, 8, 7 ), "WEBPVP8", 7 ) == 0
722 ) {
723 $this->logger->info( __METHOD__ . ": recognized file as image/webp\n" );
724 return "image/webp";
725 }
726
727 /**
728 * Look for PHP. Check for this before HTML/XML... Warning: this is a
729 * heuristic, and won't match a file with a lot of non-PHP before. It
730 * will also match text files which could be PHP. :)
731 *
732 * @todo FIXME: For this reason, the check is probably useless -- an attacker
733 * could almost certainly just pad the file with a lot of nonsense to
734 * circumvent the check in any case where it would be a security
735 * problem. On the other hand, it causes harmful false positives (bug
736 * 16583). The heuristic has been cut down to exclude three-character
737 * strings like "<? ", but should it be axed completely?
738 */
739 if ( ( strpos( $head, '<?php' ) !== false ) ||
740 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
741 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
742 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
743 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
744 ( strpos( $head, "<\x00?\x00=" ) !== false )
745 ) {
746 $this->logger->info( __METHOD__ . ": recognized $file as application/x-php\n" );
747 return 'application/x-php';
748 }
749
750 /**
751 * look for XML formats (XHTML and SVG)
752 */
753 $xml = new XmlTypeCheck( $file );
754 if ( $xml->wellFormed ) {
755 $xmlTypes = $this->xmlTypes;
756 if ( isset( $xmlTypes[$xml->getRootElement()] ) ) {
757 return $xmlTypes[$xml->getRootElement()];
758 } else {
759 return 'application/xml';
760 }
761 }
762
763 /**
764 * look for shell scripts
765 */
766 $script_type = null;
767
768 # detect by shebang
769 if ( substr( $head, 0, 2 ) == "#!" ) {
770 $script_type = "ASCII";
771 } elseif ( substr( $head, 0, 5 ) == "\xef\xbb\xbf#!" ) {
772 $script_type = "UTF-8";
773 } elseif ( substr( $head, 0, 7 ) == "\xfe\xff\x00#\x00!" ) {
774 $script_type = "UTF-16BE";
775 } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
776 $script_type = "UTF-16LE";
777 }
778
779 if ( $script_type ) {
780 if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) {
781 // Quick and dirty fold down to ASCII!
782 $pack = [ 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' ];
783 $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
784 $head = '';
785 foreach ( $chars as $codepoint ) {
786 if ( $codepoint < 128 ) {
787 $head .= chr( $codepoint );
788 } else {
789 $head .= '?';
790 }
791 }
792 }
793
794 $match = [];
795
796 if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
797 $mime = "application/x-{$match[2]}";
798 $this->logger->info( __METHOD__ . ": shell script recognized as $mime\n" );
799 return $mime;
800 }
801 }
802
803 // Check for ZIP variants (before getimagesize)
804 if ( strpos( $tail, "PK\x05\x06" ) !== false ) {
805 $this->logger->info( __METHOD__ . ": ZIP header present in $file\n" );
806 return $this->detectZipType( $head, $tail, $ext );
807 }
808
809 // Check for STL (3D) files
810 // @see https://en.wikipedia.org/wiki/STL_(file_format)
811 if ( $fsize >= 15 &&
812 stripos( $head, 'SOLID ' ) === 0 &&
813 preg_match( '/\RENDSOLID .*$/i', $tail ) ) {
814 // ASCII STL file
815 return 'application/sla';
816 } elseif ( $fsize > 84 ) {
817 // binary STL file
818 $triangles = substr( $head, 80, 4 );
819 $triangles = unpack( 'V', $triangles );
820 $triangles = reset( $triangles );
821 if ( $triangles !== false && $fsize === 84 + ( $triangles * 50 ) ) {
822 return 'application/sla';
823 }
824 }
825
826 Wikimedia\suppressWarnings();
827 $gis = getimagesize( $file );
828 Wikimedia\restoreWarnings();
829
830 if ( $gis && isset( $gis['mime'] ) ) {
831 $mime = $gis['mime'];
832 $this->logger->info( __METHOD__ . ": getimagesize detected $file as $mime\n" );
833 return $mime;
834 }
835
836 # Media handling extensions can guess the MIME by content
837 # It's intentionally here so that if core is wrong about a type (false positive),
838 # people will hopefully nag and submit patches :)
839 $mime = false;
840 # Some strings by reference for performance - assuming well-behaved hooks
841 $callback = $this->guessCallback;
842 if ( $callback ) {
843 $callback( $this, $head, $tail, $file, $mime /* by reference */ );
844 };
845
846 return $mime;
847 }
848
849 /**
850 * Detect application-specific file type of a given ZIP file from its
851 * header data. Currently works for OpenDocument and OpenXML types...
852 * If can't tell, returns 'application/zip'.
853 *
854 * @param string $header Some reasonably-sized chunk of file header
855 * @param string|null $tail The tail of the file
856 * @param string|bool $ext The file extension, or true to extract it from the filename.
857 * Set it to false (default) to ignore the extension. DEPRECATED! Set to false,
858 * use improveTypeFromExtension($mime, $ext) later to improve MIME type.
859 *
860 * @return string
861 */
862 function detectZipType( $header, $tail = null, $ext = false ) {
863 if ( $ext ) { # TODO: remove $ext param
864 $this->logger->info( __METHOD__ .
865 ": WARNING: use of the \$ext parameter is deprecated. " .
866 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
867 }
868
869 $mime = 'application/zip';
870 $opendocTypes = [
871 'chart-template',
872 'chart',
873 'formula-template',
874 'formula',
875 'graphics-template',
876 'graphics',
877 'image-template',
878 'image',
879 'presentation-template',
880 'presentation',
881 'spreadsheet-template',
882 'spreadsheet',
883 'text-template',
884 'text-master',
885 'text-web',
886 'text' ];
887
888 // https://lists.oasis-open.org/archives/office/200505/msg00006.html
889 $types = '(?:' . implode( '|', $opendocTypes ) . ')';
890 $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
891
892 $openxmlRegex = "/^\[Content_Types\].xml/";
893
894 if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
895 $mime = $matches[1];
896 $this->logger->info( __METHOD__ . ": detected $mime from ZIP archive\n" );
897 } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
898 $mime = "application/x-opc+zip";
899 # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
900 if ( $ext !== true && $ext !== false ) {
901 /** This is the mode used by getPropsFromPath
902 * These MIME's are stored in the database, where we don't really want
903 * x-opc+zip, because we use it only for internal purposes
904 */
905 if ( $this->isMatchingExtension( $ext, $mime ) ) {
906 /* A known file extension for an OPC file,
907 * find the proper mime type for that file extension
908 */
909 $mime = $this->guessTypesForExtension( $ext );
910 } else {
911 $mime = "application/zip";
912 }
913 }
914 $this->logger->info( __METHOD__ .
915 ": detected an Open Packaging Conventions archive: $mime\n" );
916 } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
917 ( $headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
918 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
919 if ( substr( $header, 512, 4 ) == "\xEC\xA5\xC1\x00" ) {
920 $mime = "application/msword";
921 }
922 switch ( substr( $header, 512, 6 ) ) {
923 case "\xEC\xA5\xC1\x00\x0E\x00":
924 case "\xEC\xA5\xC1\x00\x1C\x00":
925 case "\xEC\xA5\xC1\x00\x43\x00":
926 $mime = "application/vnd.ms-powerpoint";
927 break;
928 case "\xFD\xFF\xFF\xFF\x10\x00":
929 case "\xFD\xFF\xFF\xFF\x1F\x00":
930 case "\xFD\xFF\xFF\xFF\x22\x00":
931 case "\xFD\xFF\xFF\xFF\x23\x00":
932 case "\xFD\xFF\xFF\xFF\x28\x00":
933 case "\xFD\xFF\xFF\xFF\x29\x00":
934 case "\xFD\xFF\xFF\xFF\x10\x02":
935 case "\xFD\xFF\xFF\xFF\x1F\x02":
936 case "\xFD\xFF\xFF\xFF\x22\x02":
937 case "\xFD\xFF\xFF\xFF\x23\x02":
938 case "\xFD\xFF\xFF\xFF\x28\x02":
939 case "\xFD\xFF\xFF\xFF\x29\x02":
940 $mime = "application/vnd.msexcel";
941 break;
942 }
943
944 $this->logger->info( __METHOD__ .
945 ": detected a MS Office document with OPC trailer\n" );
946 } else {
947 $this->logger->info( __METHOD__ . ": unable to identify type of ZIP archive\n" );
948 }
949 return $mime;
950 }
951
952 /**
953 * Internal MIME type detection. Detection is done using the fileinfo
954 * extension if it is available. It can be overriden by callback, which could
955 * use an external program, for example. If detection fails and $ext is not false,
956 * the MIME type is guessed from the file extension, using guessTypesForExtension.
957 *
958 * If the MIME type is still unknown, getimagesize is used to detect the
959 * MIME type if the file is an image. If no MIME type can be determined,
960 * this function returns 'unknown/unknown'.
961 *
962 * @param string $file The file to check
963 * @param string|bool $ext The file extension, or true (default) to extract it from the filename.
964 * Set it to false to ignore the extension. DEPRECATED! Set to false, use
965 * improveTypeFromExtension($mime, $ext) later to improve MIME type.
966 *
967 * @return string The MIME type of $file
968 */
969 private function detectMimeType( $file, $ext = true ) {
970 /** @todo Make $ext default to false. Or better, remove it. */
971 if ( $ext ) {
972 $this->logger->info( __METHOD__ .
973 ": WARNING: use of the \$ext parameter is deprecated. "
974 . "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
975 }
976
977 $callback = $this->detectCallback;
978 $m = null;
979 if ( $callback ) {
980 $m = $callback( $file );
981 } else {
982 $m = mime_content_type( $file );
983 }
984
985 if ( $m ) {
986 # normalize
987 $m = preg_replace( '![;, ].*$!', '', $m ); # strip charset, etc
988 $m = trim( $m );
989 $m = strtolower( $m );
990
991 if ( strpos( $m, 'unknown' ) !== false ) {
992 $m = null;
993 } else {
994 $this->logger->info( __METHOD__ . ": magic mime type of $file: $m\n" );
995 return $m;
996 }
997 }
998
999 // If desired, look at extension as a fallback.
1000 if ( $ext === true ) {
1001 $i = strrpos( $file, '.' );
1002 $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
1003 }
1004 if ( $ext ) {
1005 if ( $this->isRecognizableExtension( $ext ) ) {
1006 $this->logger->info( __METHOD__ . ": refusing to guess mime type for .$ext file, "
1007 . "we should have recognized it\n" );
1008 } else {
1009 $m = $this->guessTypesForExtension( $ext );
1010 if ( $m ) {
1011 $this->logger->info( __METHOD__ . ": extension mime type of $file: $m\n" );
1012 return $m;
1013 }
1014 }
1015 }
1016
1017 // Unknown type
1018 $this->logger->info( __METHOD__ . ": failed to guess mime type for $file!\n" );
1019 return 'unknown/unknown';
1020 }
1021
1022 /**
1023 * Determine the media type code for a file, using its MIME type, name and
1024 * possibly its contents.
1025 *
1026 * This function relies on the findMediaType(), mapping extensions and MIME
1027 * types to media types.
1028 *
1029 * @todo analyse file if need be
1030 * @todo look at multiple extension, separately and together.
1031 *
1032 * @param string|null $path Full path to the image file, in case we have to look at the contents
1033 * (if null, only the MIME type is used to determine the media type code).
1034 * @param string|null $mime MIME type. If null it will be guessed using guessMimeType.
1035 *
1036 * @return string A value to be used with the MEDIATYPE_xxx constants.
1037 */
1038 function getMediaType( $path = null, $mime = null ) {
1039 if ( !$mime && !$path ) {
1040 return MEDIATYPE_UNKNOWN;
1041 }
1042
1043 // If MIME type is unknown, guess it
1044 if ( !$mime ) {
1045 $mime = $this->guessMimeType( $path, false );
1046 }
1047
1048 // Special code for ogg - detect if it's video (theora),
1049 // else label it as sound.
1050 if ( $mime == 'application/ogg' && file_exists( $path ) ) {
1051 // Read a chunk of the file
1052 $f = fopen( $path, "rt" );
1053 if ( !$f ) {
1054 return MEDIATYPE_UNKNOWN;
1055 }
1056 $head = fread( $f, 256 );
1057 fclose( $f );
1058
1059 $head = str_replace( 'ffmpeg2theora', '', strtolower( $head ) );
1060
1061 // This is an UGLY HACK, file should be parsed correctly
1062 if ( strpos( $head, 'theora' ) !== false ) {
1063 return MEDIATYPE_VIDEO;
1064 } elseif ( strpos( $head, 'vorbis' ) !== false ) {
1065 return MEDIATYPE_AUDIO;
1066 } elseif ( strpos( $head, 'flac' ) !== false ) {
1067 return MEDIATYPE_AUDIO;
1068 } elseif ( strpos( $head, 'speex' ) !== false ) {
1069 return MEDIATYPE_AUDIO;
1070 } elseif ( strpos( $head, 'opus' ) !== false ) {
1071 return MEDIATYPE_AUDIO;
1072 } else {
1073 return MEDIATYPE_MULTIMEDIA;
1074 }
1075 }
1076
1077 $type = null;
1078 // Check for entry for full MIME type
1079 if ( $mime ) {
1080 $type = $this->findMediaType( $mime );
1081 if ( $type !== MEDIATYPE_UNKNOWN ) {
1082 return $type;
1083 }
1084 }
1085
1086 // Check for entry for file extension
1087 if ( $path ) {
1088 $i = strrpos( $path, '.' );
1089 $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
1090
1091 // TODO: look at multi-extension if this fails, parse from full path
1092 $type = $this->findMediaType( '.' . $e );
1093 if ( $type !== MEDIATYPE_UNKNOWN ) {
1094 return $type;
1095 }
1096 }
1097
1098 // Check major MIME type
1099 if ( $mime ) {
1100 $i = strpos( $mime, '/' );
1101 if ( $i !== false ) {
1102 $major = substr( $mime, 0, $i );
1103 $type = $this->findMediaType( $major );
1104 if ( $type !== MEDIATYPE_UNKNOWN ) {
1105 return $type;
1106 }
1107 }
1108 }
1109
1110 if ( !$type ) {
1111 $type = MEDIATYPE_UNKNOWN;
1112 }
1113
1114 return $type;
1115 }
1116
1117 /**
1118 * Returns a media code matching the given MIME type or file extension.
1119 * File extensions are represented by a string starting with a dot (.) to
1120 * distinguish them from MIME types.
1121 *
1122 * This function relies on the mapping defined by $this->mMediaTypes
1123 * @access private
1124 * @param string $extMime
1125 * @return int|string
1126 */
1127 function findMediaType( $extMime ) {
1128 if ( strpos( $extMime, '.' ) === 0 ) {
1129 // If it's an extension, look up the MIME types
1130 $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
1131 if ( !$m ) {
1132 return MEDIATYPE_UNKNOWN;
1133 }
1134
1135 $m = explode( ' ', $m );
1136 } else {
1137 // Normalize MIME type
1138 if ( isset( $this->mimeTypeAliases[$extMime] ) ) {
1139 $extMime = $this->mimeTypeAliases[$extMime];
1140 }
1141
1142 $m = [ $extMime ];
1143 }
1144
1145 foreach ( $m as $mime ) {
1146 foreach ( $this->mediaTypes as $type => $codes ) {
1147 if ( in_array( $mime, $codes, true ) ) {
1148 return $type;
1149 }
1150 }
1151 }
1152
1153 return MEDIATYPE_UNKNOWN;
1154 }
1155
1156 /**
1157 * Returns an array of media types (MEDIATYPE_xxx constants)
1158 *
1159 * @return array
1160 */
1161 public function getMediaTypes() {
1162 return array_keys( $this->mediaTypes );
1163 }
1164
1165 /**
1166 * Get the MIME types that various versions of Internet Explorer would
1167 * detect from a chunk of the content.
1168 *
1169 * @param string $fileName The file name (unused at present)
1170 * @param string $chunk The first 256 bytes of the file
1171 * @param string $proposed The MIME type proposed by the server
1172 * @return array
1173 */
1174 public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
1175 $ca = $this->getIEContentAnalyzer();
1176 return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
1177 }
1178
1179 /**
1180 * Get a cached instance of IEContentAnalyzer
1181 *
1182 * @return IEContentAnalyzer
1183 */
1184 protected function getIEContentAnalyzer() {
1185 if ( is_null( $this->IEAnalyzer ) ) {
1186 $this->IEAnalyzer = new IEContentAnalyzer;
1187 }
1188 return $this->IEAnalyzer;
1189 }
1190 }