MediaWiki fundraising/REL1_35
MimeAnalyzer.php
Go to the documentation of this file.
1<?php
22use Psr\Log\LoggerAwareInterface;
23use Psr\Log\LoggerInterface;
24use Psr\Log\NullLogger;
27
33class MimeAnalyzer implements LoggerAwareInterface {
35 protected $typeFile;
37 protected $infoFile;
39 protected $xmlTypes;
41 protected $initCallback;
43 protected $detectCallback;
45 protected $guessCallback;
47 protected $extCallback;
49 protected $mediaTypes = null;
51 protected $mimeTypeAliases = null;
53 protected $mimeToExts = [];
55 protected $extToMimes = [];
56
58 public $mExtToMime = []; // legacy name; field accessed by hooks
59
61 protected $IEAnalyzer;
62
64 private $extraTypes = '';
66 private $extraInfo = '';
67
69 private $logger;
70
72 public const USE_INTERNAL = 'internal';
73
89 public function __construct( array $params ) {
90 $this->typeFile = $params['typeFile'];
91 $this->infoFile = $params['infoFile'];
92 $this->xmlTypes = $params['xmlTypes'];
93 $this->initCallback = $params['initCallback'] ?? null;
94 $this->detectCallback = $params['detectCallback'] ?? null;
95 $this->guessCallback = $params['guessCallback'] ?? null;
96 $this->extCallback = $params['extCallback'] ?? null;
97 $this->logger = $params['logger'] ?? new NullLogger();
98
99 $this->loadFiles();
100 }
101
102 protected function loadFiles() {
103 # Allow media handling extensions adding MIME-types and MIME-info
104 if ( $this->initCallback ) {
105 call_user_func( $this->initCallback, $this );
106 }
107
108 $rawTypes = $this->extraTypes;
109 if ( $this->typeFile === self::USE_INTERNAL ) {
110 $this->mimeToExts = MimeMap::MIME_EXTENSIONS;
111 } else {
112 $this->mimeToExts = MimeMapMinimal::MIME_EXTENSIONS;
113 if ( $this->typeFile ) {
114 $rawTypes = file_get_contents( $this->typeFile ) . "\n" . $this->extraTypes;
115 }
116 }
117 if ( $rawTypes ) {
118 $this->parseMimeTypes( $rawTypes );
119 }
120
121 // Build the reverse mapping (extension => MIME type).
122 foreach ( $this->mimeToExts as $mime => $exts ) {
123 foreach ( $exts as $ext ) {
124 $this->extToMimes[$ext][] = $mime;
125 }
126 }
127
128 // Migrate items from the legacy $this->mExtToMime field.
129 // TODO: Remove this when mExtToMime is finally removed.
130 foreach ( $this->mExtToMime as $ext => $mimes ) {
131 foreach ( explode( ' ', $mimes ) as $mime ) {
132 $this->extToMimes[$ext][] = $mime;
133 }
134 }
135
136 $rawInfo = $this->extraInfo;
137 if ( $this->infoFile === self::USE_INTERNAL ) {
138 $this->mimeTypeAliases = MimeMap::MIME_TYPE_ALIASES;
139 $this->mediaTypes = MimeMap::MEDIA_TYPES;
140 } else {
141 $this->mimeTypeAliases = MimeMapMinimal::MIME_TYPE_ALIASES;
142 $this->mediaTypes = MimeMapMinimal::MEDIA_TYPES;
143 if ( $this->infoFile ) {
144 $rawInfo = file_get_contents( $this->infoFile ) . "\n" . $this->extraInfo;
145 }
146 }
147 if ( $rawInfo ) {
148 $this->parseMimeInfo( $rawInfo );
149 }
150 }
151
152 protected function parseMimeTypes( $rawMimeTypes ) {
153 $rawMimeTypes = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeTypes );
154 $rawMimeTypes = str_replace( "\t", " ", $rawMimeTypes );
155
156 $lines = explode( "\n", $rawMimeTypes );
157 foreach ( $lines as $s ) {
158 $s = trim( $s );
159 if ( empty( $s ) ) {
160 continue;
161 }
162 if ( strpos( $s, '#' ) === 0 ) {
163 continue;
164 }
165
166 $s = strtolower( $s );
167 $i = strpos( $s, ' ' );
168
169 if ( $i === false ) {
170 continue;
171 }
172
173 $mime = substr( $s, 0, $i );
174 $ext = trim( substr( $s, $i + 1 ) );
175
176 if ( empty( $ext ) ) {
177 continue;
178 }
179
180 $tokens = preg_split( '/\s+/', $s, -1, PREG_SPLIT_NO_EMPTY );
181 if ( count( $tokens ) > 1 ) {
182 $mime = array_shift( $tokens );
183 $this->mimeToExts[$mime] = array_values( array_unique(
184 array_merge( $this->mimeToExts[$mime] ?? [], $tokens ) ) );
185 }
186 }
187 }
188
189 protected function parseMimeInfo( $rawMimeInfo ) {
190 $rawMimeInfo = str_replace( [ "\r\n", "\n\r", "\n\n", "\r\r", "\r" ], "\n", $rawMimeInfo );
191 $rawMimeInfo = str_replace( "\t", " ", $rawMimeInfo );
192
193 $lines = explode( "\n", $rawMimeInfo );
194 foreach ( $lines as $s ) {
195 $s = trim( $s );
196 if ( empty( $s ) ) {
197 continue;
198 }
199 if ( strpos( $s, '#' ) === 0 ) {
200 continue;
201 }
202
203 $s = strtolower( $s );
204 $i = strpos( $s, ' ' );
205
206 if ( $i === false ) {
207 continue;
208 }
209
210 # print "processing MIME INFO line $s<br>";
211
212 $match = [];
213 if ( preg_match( '!\[\s*(\w+)\s*\]!', $s, $match ) ) {
214 $s = preg_replace( '!\[\s*(\w+)\s*\]!', '', $s );
215 $mtype = trim( strtoupper( $match[1] ) );
216 } else {
217 $mtype = MEDIATYPE_UNKNOWN;
218 }
219
220 $m = preg_split( '/\s+/', $s, -1, PREG_SPLIT_NO_EMPTY );
221
222 if ( !isset( $this->mediaTypes[$mtype] ) ) {
223 $this->mediaTypes[$mtype] = [];
224 }
225
226 foreach ( $m as $mime ) {
227 $mime = trim( $mime );
228 if ( empty( $mime ) ) {
229 continue;
230 }
231
232 $this->mediaTypes[$mtype][] = $mime;
233 }
234
235 if ( count( $m ) > 1 ) {
236 $main = $m[0];
237 $mCount = count( $m );
238 for ( $i = 1; $i < $mCount; $i += 1 ) {
239 $mime = $m[$i];
240 $this->mimeTypeAliases[$mime] = $main;
241 }
242 }
243 }
244 }
245
246 public function setLogger( LoggerInterface $logger ) {
247 $this->logger = $logger;
248 }
249
256 public function addExtraTypes( $types ) {
257 $this->extraTypes .= "\n" . $types;
258 }
259
266 public function addExtraInfo( $info ) {
267 $this->extraInfo .= "\n" . $info;
268 }
269
279 public function getExtensionsForType( $mime ) {
280 $exts = $this->getExtensionsFromMimeType( $mime );
281 return $exts ? implode( ' ', $exts ) : null;
282 }
283
293 public function getExtensionsFromMimeType( $mime ) {
294 $mime = strtolower( $mime );
295 if ( !isset( $this->mimeToExts[$mime] ) && isset( $this->mimeTypeAliases[$mime] ) ) {
296 $mime = $this->mimeTypeAliases[$mime];
297 }
298 return $this->mimeToExts[$mime] ?? [];
299 }
300
310 public function getMimeTypesFromExtension( $ext ) {
311 $ext = strtolower( $ext );
312 return $this->extToMimes[$ext] ?? [];
313 }
314
323 public function getMimeTypeFromExtensionOrNull( $ext ) {
324 $types = $this->getMimeTypesFromExtension( $ext );
325 return $types[0] ?? null;
326 }
327
336 public function guessTypesForExtension( $ext ) {
337 return $this->getMimeTypeFromExtensionOrNull( $ext );
338 }
339
348 public function getTypesForExtension( $ext ) {
349 $types = $this->getMimeTypesFromExtension( $ext );
350 return $types ? implode( ' ', $types ) : null;
351 }
352
361 public function getExtensionFromMimeTypeOrNull( $mime ) {
362 $exts = $this->getExtensionsFromMimeType( $mime );
363 return $exts[0] ?? null;
364 }
365
375 public function isMatchingExtension( $extension, $mime ) {
376 $exts = $this->getExtensionsFromMimeType( $mime );
377
378 if ( !$exts ) {
379 return null; // Unknown MIME type
380 }
381
382 return in_array( strtolower( $extension ), $exts );
383 }
384
393 public function isPHPImageType( $mime ) {
394 // As defined by imagegetsize and image_type_to_mime
395 static $types = [
396 'image/gif', 'image/jpeg', 'image/png',
397 'image/x-bmp', 'image/xbm', 'image/tiff',
398 'image/jp2', 'image/jpeg2000', 'image/iff',
399 'image/xbm', 'image/x-xbitmap',
400 'image/vnd.wap.wbmp', 'image/vnd.xiff',
401 'image/x-photoshop',
402 'application/x-shockwave-flash',
403 ];
404
405 return in_array( $mime, $types );
406 }
407
420 public function isRecognizableExtension( $extension ) {
421 static $types = [
422 // Types recognized by getimagesize()
423 'gif', 'jpeg', 'jpg', 'png', 'swf', 'psd',
424 'bmp', 'tiff', 'tif', 'jpc', 'jp2',
425 'jpx', 'jb2', 'swc', 'iff', 'wbmp',
426 'xbm',
427
428 // Formats we recognize magic numbers for
429 'djvu', 'ogx', 'ogg', 'ogv', 'oga', 'spx', 'opus',
430 'mid', 'pdf', 'wmf', 'xcf', 'webm', 'mkv', 'mka',
431 'webp', 'mp3',
432
433 // XML formats we sure hope we recognize reliably
434 'svg',
435
436 // 3D formats
437 'stl',
438 ];
439 return in_array( strtolower( $extension ), $types );
440 }
441
456 public function improveTypeFromExtension( $mime, $ext ) {
457 if ( $mime === 'unknown/unknown' ) {
458 if ( $this->isRecognizableExtension( $ext ) ) {
459 $this->logger->info( __METHOD__ . ': refusing to guess mime type for .' .
460 "$ext file, we should have recognized it\n" );
461 } else {
462 // Not something we can detect, so simply
463 // trust the file extension
464 $mime = $this->getMimeTypeFromExtensionOrNull( $ext );
465 }
466 } elseif ( $mime === 'application/x-opc+zip' ) {
467 if ( $this->isMatchingExtension( $ext, $mime ) ) {
468 // A known file extension for an OPC file,
469 // find the proper MIME type for that file extension
470 $mime = $this->getMimeTypeFromExtensionOrNull( $ext );
471 } else {
472 $this->logger->info( __METHOD__ .
473 ": refusing to guess better type for $mime file, " .
474 ".$ext is not a known OPC extension.\n" );
475 $mime = 'application/zip';
476 }
477 } elseif ( $mime === 'text/plain' && $this->findMediaType( ".$ext" ) === MEDIATYPE_TEXT ) {
478 // Textual types are sometimes not recognized properly.
479 // If detected as text/plain, and has an extension which is textual
480 // improve to the extension's type. For example, csv and json are often
481 // misdetected as text/plain.
482 $mime = $this->getMimeTypeFromExtensionOrNull( $ext );
483 }
484
485 # Media handling extensions can improve the MIME detected
486 $callback = $this->extCallback;
487 if ( $callback ) {
488 $callback( $this, $ext, $mime /* by reference */ );
489 }
490
491 if ( isset( $this->mimeTypeAliases[$mime] ) ) {
492 $mime = $this->mimeTypeAliases[$mime];
493 }
494
495 $this->logger->info( __METHOD__ . ": improved mime type for .$ext: $mime\n" );
496 return $mime;
497 }
498
513 public function guessMimeType( $file, $ext = true ) {
514 if ( $ext ) { // TODO: make $ext default to false. Or better, remove it.
515 $this->logger->info( __METHOD__ .
516 ": WARNING: use of the \$ext parameter is deprecated. " .
517 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
518 }
519
520 $mime = $this->doGuessMimeType( $file, $ext );
521
522 if ( !$mime ) {
523 $this->logger->info( __METHOD__ .
524 ": internal type detection failed for $file (.$ext)...\n" );
525 $mime = $this->detectMimeType( $file, $ext );
526 }
527
528 if ( isset( $this->mimeTypeAliases[$mime] ) ) {
529 $mime = $this->mimeTypeAliases[$mime];
530 }
531
532 $this->logger->info( __METHOD__ . ": guessed mime type of $file: $mime\n" );
533 return $mime;
534 }
535
546 private function doGuessMimeType( $file, $ext ) {
547 // Read a chunk of the file
548 Wikimedia\suppressWarnings();
549 $f = fopen( $file, 'rb' );
550 Wikimedia\restoreWarnings();
551
552 if ( !$f ) {
553 return 'unknown/unknown';
554 }
555
556 $fsize = filesize( $file );
557 if ( $fsize === false ) {
558 return 'unknown/unknown';
559 }
560
561 $head = fread( $f, 1024 );
562 $tailLength = min( 65558, $fsize ); // 65558 = maximum size of a zip EOCDR
563 if ( fseek( $f, -1 * $tailLength, SEEK_END ) === -1 ) {
564 throw new UnexpectedValueException(
565 "Seeking $tailLength bytes from EOF failed in " . __METHOD__ );
566 }
567 $tail = $tailLength ? fread( $f, $tailLength ) : '';
568
569 $this->logger->info( __METHOD__ .
570 ": analyzing head and tail of $file for magic numbers.\n" );
571
572 // Hardcode a few magic number checks...
573 $headers = [
574 // Multimedia...
575 'MThd' => 'audio/midi',
576 'OggS' => 'application/ogg',
577 'ID3' => 'audio/mpeg',
578 "\xff\xfb" => 'audio/mpeg', // MPEG-1 layer 3
579 "\xff\xf3" => 'audio/mpeg', // MPEG-2 layer 3 (lower sample rates)
580 "\xff\xe3" => 'audio/mpeg', // MPEG-2.5 layer 3 (very low sample rates)
581
582 // Image formats...
583 // Note that WMF may have a bare header, no magic number.
584 "\x01\x00\x09\x00" => 'application/x-msmetafile', // Possibly prone to false positives?
585 "\xd7\xcd\xc6\x9a" => 'application/x-msmetafile',
586 '%PDF' => 'application/pdf',
587 'gimp xcf' => 'image/x-xcf',
588
589 // Some forbidden fruit...
590 'MZ' => 'application/octet-stream', // DOS/Windows executable
591 "\xca\xfe\xba\xbe" => 'application/octet-stream', // Mach-O binary
592 "\x7fELF" => 'application/octet-stream', // ELF binary
593 ];
594
595 foreach ( $headers as $magic => $candidate ) {
596 if ( strncmp( $head, $magic, strlen( $magic ) ) == 0 ) {
597 $this->logger->info( __METHOD__ .
598 ": magic header in $file recognized as $candidate\n" );
599 return $candidate;
600 }
601 }
602
603 /* Look for WebM and Matroska files */
604 if ( strncmp( $head, pack( "C4", 0x1a, 0x45, 0xdf, 0xa3 ), 4 ) == 0 ) {
605 $doctype = strpos( $head, "\x42\x82" );
606 if ( $doctype ) {
607 // Next byte is datasize, then data (sizes larger than 1 byte are stupid muxers)
608 $data = substr( $head, $doctype + 3, 8 );
609 if ( strncmp( $data, "matroska", 8 ) == 0 ) {
610 $this->logger->info( __METHOD__ . ": recognized file as video/x-matroska\n" );
611 return "video/x-matroska";
612 } elseif ( strncmp( $data, "webm", 4 ) == 0 ) {
613 // XXX HACK look for a video track, if we don't find it, this is an audio file
614 $videotrack = strpos( $head, "\x86\x85V_VP" );
615
616 if ( $videotrack ) {
617 // There is a video track, so this is a video file.
618 $this->logger->info( __METHOD__ . ": recognized file as video/webm\n" );
619 return "video/webm";
620 }
621
622 $this->logger->info( __METHOD__ . ": recognized file as audio/webm\n" );
623 return "audio/webm";
624 }
625 }
626 $this->logger->info( __METHOD__ . ": unknown EBML file\n" );
627 return "unknown/unknown";
628 }
629
630 /* Look for WebP */
631 if ( strncmp( $head, "RIFF", 4 ) == 0 &&
632 strncmp( substr( $head, 8, 7 ), "WEBPVP8", 7 ) == 0
633 ) {
634 $this->logger->info( __METHOD__ . ": recognized file as image/webp\n" );
635 return "image/webp";
636 }
637
638 /* Look for MS Compound Binary (OLE) files */
639 if ( strncmp( $head, "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1", 8 ) == 0 ) {
640 $this->logger->info( __METHOD__ . ': recognized MS CFB (OLE) file' );
641 return $this->detectMicrosoftBinaryType( $f );
642 }
643
656 if ( ( strpos( $head, '<?php' ) !== false ) ||
657 ( strpos( $head, "<\x00?\x00p\x00h\x00p" ) !== false ) ||
658 ( strpos( $head, "<\x00?\x00 " ) !== false ) ||
659 ( strpos( $head, "<\x00?\x00\n" ) !== false ) ||
660 ( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
661 ( strpos( $head, "<\x00?\x00=" ) !== false )
662 ) {
663 $this->logger->info( __METHOD__ . ": recognized $file as application/x-php\n" );
664 return 'application/x-php';
665 }
666
670 Wikimedia\suppressWarnings();
671 $xml = new XmlTypeCheck( $file );
672 Wikimedia\restoreWarnings();
673 if ( $xml->wellFormed ) {
674 $xmlTypes = $this->xmlTypes;
675 return $xmlTypes[$xml->getRootElement()] ?? 'application/xml';
676 }
677
681 $script_type = null;
682
683 # detect by shebang
684 if ( substr( $head, 0, 2 ) == "#!" ) {
685 $script_type = "ASCII";
686 } elseif ( substr( $head, 0, 5 ) == "\xef\xbb\xbf#!" ) {
687 $script_type = "UTF-8";
688 } elseif ( substr( $head, 0, 7 ) == "\xfe\xff\x00#\x00!" ) {
689 $script_type = "UTF-16BE";
690 } elseif ( substr( $head, 0, 7 ) == "\xff\xfe#\x00!" ) {
691 $script_type = "UTF-16LE";
692 }
693
694 if ( $script_type ) {
695 if ( $script_type !== "UTF-8" && $script_type !== "ASCII" ) {
696 // Quick and dirty fold down to ASCII!
697 $pack = [ 'UTF-16BE' => 'n*', 'UTF-16LE' => 'v*' ];
698 $chars = unpack( $pack[$script_type], substr( $head, 2 ) );
699 $head = '';
700 foreach ( $chars as $codepoint ) {
701 if ( $codepoint < 128 ) {
702 $head .= chr( $codepoint );
703 } else {
704 $head .= '?';
705 }
706 }
707 }
708
709 $match = [];
710
711 if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
712 $mime = "application/x-{$match[2]}";
713 $this->logger->info( __METHOD__ . ": shell script recognized as $mime\n" );
714 return $mime;
715 }
716 }
717
718 // Check for ZIP variants (before getimagesize)
719 $eocdrPos = strpos( $tail, "PK\x05\x06" );
720 if ( $eocdrPos !== false && $eocdrPos <= strlen( $tail ) - 22 ) {
721 $this->logger->info( __METHOD__ . ": ZIP signature present in $file\n" );
722 // Check if it really is a ZIP file, make sure the EOCDR is at the end (T40432)
723 $commentLength = unpack( "n", substr( $tail, $eocdrPos + 20 ) )[1];
724 if ( $eocdrPos + 22 + $commentLength !== strlen( $tail ) ) {
725 $this->logger->info( __METHOD__ . ": ZIP EOCDR not at end. Not a ZIP file." );
726 } else {
727 return $this->detectZipType( $head, $tail, $ext );
728 }
729 }
730
731 // Check for STL (3D) files
732 // @see https://en.wikipedia.org/wiki/STL_(file_format)
733 if ( $fsize >= 15 &&
734 stripos( $head, 'SOLID ' ) === 0 &&
735 preg_match( '/\RENDSOLID .*$/i', $tail ) ) {
736 // ASCII STL file
737 return 'application/sla';
738 } elseif ( $fsize > 84 ) {
739 // binary STL file
740 $triangles = substr( $head, 80, 4 );
741 $triangles = unpack( 'V', $triangles );
742 $triangles = reset( $triangles );
743 if ( $triangles !== false && $fsize === 84 + ( $triangles * 50 ) ) {
744 return 'application/sla';
745 }
746 }
747
748 Wikimedia\suppressWarnings();
749 $gis = getimagesize( $file );
750 Wikimedia\restoreWarnings();
751
752 if ( $gis && isset( $gis['mime'] ) ) {
753 $mime = $gis['mime'];
754 $this->logger->info( __METHOD__ . ": getimagesize detected $file as $mime\n" );
755 return $mime;
756 }
757
758 # Media handling extensions can guess the MIME by content
759 # It's intentionally here so that if core is wrong about a type (false positive),
760 # people will hopefully nag and submit patches :)
761 $mime = false;
762 # Some strings by reference for performance - assuming well-behaved hooks
763 $callback = $this->guessCallback;
764 if ( $callback ) {
765 $callback( $this, $head, $tail, $file, $mime /* by reference */ );
766 }
767
768 return $mime;
769 }
770
784 public function detectZipType( $header, $tail = null, $ext = false ) {
785 if ( $ext ) { # TODO: remove $ext param
786 $this->logger->info( __METHOD__ .
787 ": WARNING: use of the \$ext parameter is deprecated. " .
788 "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
789 }
790
791 $mime = 'application/zip';
792 $opendocTypes = [
793 # In OASIS Open Document Format v1.2, Database front end document
794 # has a recommended MIME type of:
795 # application/vnd.oasis.opendocument.base
796 # Despite the type registered at the IANA being 'database' which is
797 # supposed to be normative.
798 # T35515
799 'base',
800
801 'chart-template',
802 'chart',
803 'formula-template',
804 'formula',
805 'graphics-template',
806 'graphics',
807 'image-template',
808 'image',
809 'presentation-template',
810 'presentation',
811 'spreadsheet-template',
812 'spreadsheet',
813 'text-template',
814 'text-master',
815 'text-web',
816 'text' ];
817
818 // The list of document types is available in OASIS Open Document
819 // Format version 1.2 under Appendix C. It is not normative though,
820 // supposedly types registered at the IANA should be.
821 // http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html
822 $types = '(?:' . implode( '|', $opendocTypes ) . ')';
823 $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/";
824
825 $openxmlRegex = "/^\[Content_Types\].xml/";
826
827 if ( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) {
828 $mime = $matches[1];
829 $this->logger->info( __METHOD__ . ": detected $mime from ZIP archive\n" );
830 } elseif ( preg_match( $openxmlRegex, substr( $header, 30 ) ) ) {
831 $mime = "application/x-opc+zip";
832 # TODO: remove the block below, as soon as improveTypeFromExtension is used everywhere
833 if ( $ext !== true && $ext !== false ) {
838 if ( $this->isMatchingExtension( $ext, $mime ) ) {
839 /* A known file extension for an OPC file,
840 * find the proper mime type for that file extension
841 */
842 $mime = $this->getMimeTypeFromExtensionOrNull( $ext );
843 } else {
844 $mime = "application/zip";
845 }
846 }
847 $this->logger->info( __METHOD__ .
848 ": detected an Open Packaging Conventions archive: $mime\n" );
849 } elseif ( substr( $header, 0, 8 ) == "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1" &&
850 ( $headerpos = strpos( $tail, "PK\x03\x04" ) ) !== false &&
851 preg_match( $openxmlRegex, substr( $tail, $headerpos + 30 ) ) ) {
852 if ( substr( $header, 512, 4 ) == "\xEC\xA5\xC1\x00" ) {
853 $mime = "application/msword";
854 }
855 switch ( substr( $header, 512, 6 ) ) {
856 case "\xEC\xA5\xC1\x00\x0E\x00":
857 case "\xEC\xA5\xC1\x00\x1C\x00":
858 case "\xEC\xA5\xC1\x00\x43\x00":
859 $mime = "application/vnd.ms-powerpoint";
860 break;
861 case "\xFD\xFF\xFF\xFF\x10\x00":
862 case "\xFD\xFF\xFF\xFF\x1F\x00":
863 case "\xFD\xFF\xFF\xFF\x22\x00":
864 case "\xFD\xFF\xFF\xFF\x23\x00":
865 case "\xFD\xFF\xFF\xFF\x28\x00":
866 case "\xFD\xFF\xFF\xFF\x29\x00":
867 case "\xFD\xFF\xFF\xFF\x10\x02":
868 case "\xFD\xFF\xFF\xFF\x1F\x02":
869 case "\xFD\xFF\xFF\xFF\x22\x02":
870 case "\xFD\xFF\xFF\xFF\x23\x02":
871 case "\xFD\xFF\xFF\xFF\x28\x02":
872 case "\xFD\xFF\xFF\xFF\x29\x02":
873 $mime = "application/vnd.msexcel";
874 break;
875 }
876
877 $this->logger->info( __METHOD__ .
878 ": detected a MS Office document with OPC trailer\n" );
879 } else {
880 $this->logger->info( __METHOD__ . ": unable to identify type of ZIP archive\n" );
881 }
882 return $mime;
883 }
884
892 private function detectMicrosoftBinaryType( $handle ) {
893 $info = MSCompoundFileReader::readHandle( $handle );
894 if ( !$info['valid'] ) {
895 $this->logger->info( __METHOD__ . ': invalid file format' );
896 return 'unknown/unknown';
897 }
898 if ( !$info['mime'] ) {
899 $this->logger->info( __METHOD__ . ": unrecognised document subtype" );
900 return 'unknown/unknown';
901 }
902 return $info['mime'];
903 }
904
922 private function detectMimeType( $file, $ext = true ) {
924 if ( $ext ) {
925 $this->logger->info( __METHOD__ .
926 ": WARNING: use of the \$ext parameter is deprecated. "
927 . "Use improveTypeFromExtension(\$mime, \$ext) instead.\n" );
928 }
929
930 $callback = $this->detectCallback;
931 $m = null;
932 if ( $callback ) {
933 $m = $callback( $file );
934 } else {
935 $m = mime_content_type( $file );
936 }
937
938 if ( $m ) {
939 # normalize
940 $m = preg_replace( '![;, ].*$!', '', $m ); # strip charset, etc
941 $m = trim( $m );
942 $m = strtolower( $m );
943
944 if ( strpos( $m, 'unknown' ) !== false ) {
945 $m = null;
946 } else {
947 $this->logger->info( __METHOD__ . ": magic mime type of $file: $m\n" );
948 return $m;
949 }
950 }
951
952 // If desired, look at extension as a fallback.
953 if ( $ext === true ) {
954 $i = strrpos( $file, '.' );
955 $ext = strtolower( $i ? substr( $file, $i + 1 ) : '' );
956 }
957 if ( $ext ) {
958 if ( $this->isRecognizableExtension( $ext ) ) {
959 $this->logger->info( __METHOD__ . ": refusing to guess mime type for .$ext file, "
960 . "we should have recognized it\n" );
961 } else {
962 $m = $this->getMimeTypeFromExtensionOrNull( $ext );
963 if ( $m ) {
964 $this->logger->info( __METHOD__ . ": extension mime type of $file: $m\n" );
965 return $m;
966 }
967 }
968 }
969
970 // Unknown type
971 $this->logger->info( __METHOD__ . ": failed to guess mime type for $file!\n" );
972 return 'unknown/unknown';
973 }
974
991 public function getMediaType( $path = null, $mime = null ) {
992 if ( !$mime && !$path ) {
993 return MEDIATYPE_UNKNOWN;
994 }
995
996 // If MIME type is unknown, guess it
997 if ( !$mime ) {
998 $mime = $this->guessMimeType( $path, false );
999 }
1000
1001 // Special code for ogg - detect if it's video (theora),
1002 // else label it as sound.
1003 if ( $mime == 'application/ogg' && is_string( $path ) && file_exists( $path ) ) {
1004 // Read a chunk of the file
1005 $f = fopen( $path, "rt" );
1006 if ( !$f ) {
1007 return MEDIATYPE_UNKNOWN;
1008 }
1009 $head = fread( $f, 256 );
1010 fclose( $f );
1011
1012 $head = str_replace( 'ffmpeg2theora', '', strtolower( $head ) );
1013
1014 // This is an UGLY HACK, file should be parsed correctly
1015 if ( strpos( $head, 'theora' ) !== false ) {
1016 return MEDIATYPE_VIDEO;
1017 } elseif ( strpos( $head, 'vorbis' ) !== false ) {
1018 return MEDIATYPE_AUDIO;
1019 } elseif ( strpos( $head, 'flac' ) !== false ) {
1020 return MEDIATYPE_AUDIO;
1021 } elseif ( strpos( $head, 'speex' ) !== false ) {
1022 return MEDIATYPE_AUDIO;
1023 } elseif ( strpos( $head, 'opus' ) !== false ) {
1024 return MEDIATYPE_AUDIO;
1025 } else {
1026 return MEDIATYPE_MULTIMEDIA;
1027 }
1028 }
1029
1030 $type = null;
1031 // Check for entry for full MIME type
1032 if ( $mime ) {
1033 $type = $this->findMediaType( $mime );
1034 if ( $type !== MEDIATYPE_UNKNOWN ) {
1035 return $type;
1036 }
1037 }
1038
1039 // Check for entry for file extension
1040 if ( $path ) {
1041 $i = strrpos( $path, '.' );
1042 $e = strtolower( $i ? substr( $path, $i + 1 ) : '' );
1043
1044 // TODO: look at multi-extension if this fails, parse from full path
1045 $type = $this->findMediaType( '.' . $e );
1046 if ( $type !== MEDIATYPE_UNKNOWN ) {
1047 return $type;
1048 }
1049 }
1050
1051 // Check major MIME type
1052 if ( $mime ) {
1053 $i = strpos( $mime, '/' );
1054 if ( $i !== false ) {
1055 $major = substr( $mime, 0, $i );
1056 $type = $this->findMediaType( $major );
1057 if ( $type !== MEDIATYPE_UNKNOWN ) {
1058 return $type;
1059 }
1060 }
1061 }
1062
1063 if ( !$type ) {
1065 }
1066
1067 return $type;
1068 }
1069
1080 public function findMediaType( $extMime ) {
1081 if ( strpos( $extMime, '.' ) === 0 ) {
1082 // If it's an extension, look up the MIME types
1083 $m = $this->getTypesForExtension( substr( $extMime, 1 ) );
1084 if ( !$m ) {
1085 return MEDIATYPE_UNKNOWN;
1086 }
1087
1088 $m = explode( ' ', $m );
1089 } else {
1090 // Normalize MIME type
1091 if ( isset( $this->mimeTypeAliases[$extMime] ) ) {
1092 $extMime = $this->mimeTypeAliases[$extMime];
1093 }
1094
1095 $m = [ $extMime ];
1096 }
1097
1098 foreach ( $m as $mime ) {
1099 foreach ( $this->mediaTypes as $type => $codes ) {
1100 if ( in_array( $mime, $codes, true ) ) {
1101 return $type;
1102 }
1103 }
1104 }
1105
1106 return MEDIATYPE_UNKNOWN;
1107 }
1108
1114 public function getMediaTypes() {
1115 return array_keys( $this->mediaTypes );
1116 }
1117
1127 public function getIEMimeTypes( $fileName, $chunk, $proposed ) {
1128 $ca = $this->getIEContentAnalyzer();
1129 return $ca->getRealMimesFromData( $fileName, $chunk, $proposed );
1130 }
1131
1137 protected function getIEContentAnalyzer() {
1138 if ( $this->IEAnalyzer === null ) {
1139 $this->IEAnalyzer = new IEContentAnalyzer;
1140 }
1141 return $this->IEAnalyzer;
1142 }
1143}
This class simulates Microsoft Internet Explorer's terribly broken and insecure MIME type detection a...
static readHandle( $fileHandle)
Read from an open seekable handle.
MimeMapMinimal defines a core set of MIME types that cannot be overridden by configuration.
MimeMap defines the mapping of MIME types to file extensions and media types.
Definition MimeMap.php:29
const MEDIATYPE_VIDEO
Definition defines.php:35
const MEDIATYPE_UNKNOWN
Definition defines.php:26
const MEDIATYPE_AUDIO
Definition defines.php:32
const MEDIATYPE_TEXT
Definition defines.php:41
const MEDIATYPE_MULTIMEDIA
Definition defines.php:37
$mime
Definition router.php:60
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42
if(!is_readable( $file)) $ext
Definition router.php:48
if(!file_exists( $CREDITS)) $lines
$header