From b9b85843572bf283f48285001e276ba7e61b63f6 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sun, 22 Feb 2009 13:37:51 +0100 Subject: updated to MediaWiki 1.14.0 --- includes/MimeMagic.php | 90 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 4 deletions(-) (limited to 'includes/MimeMagic.php') diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php index e33b1c0a..4797752d 100644 --- a/includes/MimeMagic.php +++ b/includes/MimeMagic.php @@ -11,6 +11,22 @@ define('MM_WELL_KNOWN_MIME_TYPES',<<wellFormed ) { global $wgXMLMimeTypes; - if( isset( $wgXMLMimeTypes[$xml->rootElement] ) ) { - return $wgXMLMimeTypes[$xml->rootElement]; + if( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) { + return $wgXMLMimeTypes[$xml->getRootElement()]; } else { return 'application/xml'; } @@ -509,6 +543,12 @@ class MimeMagic { } } + // Check for ZIP (before getimagesize) + if ( strpos( $tail, "PK\x05\x06" ) !== false ) { + wfDebug( __METHOD__.": ZIP header present at end of $file\n" ); + return $this->detectZipType( $head ); + } + wfSuppressWarnings(); $gis = getimagesize( $file ); wfRestoreWarnings(); @@ -517,8 +557,6 @@ class MimeMagic { $mime = $gis['mime']; wfDebug( __METHOD__.": getimagesize detected $file as $mime\n" ); return $mime; - } else { - return false; } // Also test DjVu @@ -527,6 +565,50 @@ class MimeMagic { wfDebug( __METHOD__.": detected $file as image/vnd.djvu\n" ); return 'image/vnd.djvu'; } + + return false; + } + + /** + * Detect application-specific file type of a given ZIP file from its + * header data. Currently works for OpenDocument types... + * If can't tell, returns 'application/zip'. + * + * @param string $header Some reasonably-sized chunk of file header + * @return string + */ + function detectZipType( $header ) { + $opendocTypes = array( + 'chart', + 'chart-template', + 'formula', + 'formula-template', + 'graphics', + 'graphics-template', + 'image', + 'image-template', + 'presentation', + 'presentation-template', + 'spreadsheet', + 'spreadsheet-template', + 'text', + 'text-template', + 'text-master', + 'text-web' ); + + // http://lists.oasis-open.org/archives/office/200505/msg00006.html + $types = '(?:' . implode( '|', $opendocTypes ) . ')'; + $opendocRegex = "/^mimetype(application\/vnd\.oasis\.opendocument\.$types)/"; + wfDebug( __METHOD__.": $opendocRegex\n" ); + + if( preg_match( $opendocRegex, substr( $header, 30 ), $matches ) ) { + $mime = $matches[1]; + wfDebug( __METHOD__.": detected $mime from ZIP archive\n" ); + return $mime; + } else { + wfDebug( __METHOD__.": unable to identify type of ZIP archive\n" ); + return 'application/zip'; + } } /** Internal mime type detection, please use guessMimeType() for application code instead. -- cgit v1.2.3-54-g00ecf