From a14508de095f1a839e86b7adc8131bceb1000ea0 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Thu, 25 Sep 2008 18:43:33 +0000 Subject: [PATCH] * Improved upload file type detection for OpenDocument formats Added a check for the magic value header in OpenDocument zip archives which specifies which subtype it is. Such files will get detected with the appropriate mime type and matching extension, so ODT etc uploads will work again where enabled. (Previously the general ZIP check and blacklist would disable them.) --- RELEASE-NOTES | 2 ++ includes/MimeMagic.php | 76 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 287178087d..fe5f83a675 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -139,6 +139,8 @@ The following extensions are migrated into MediaWiki 1.14: * (bug 12650) It is now possible to set different expiration times for different restriction types on the protection form. * (bug 8440) Allow preventing blocked users from editing their talk pages +* Improved upload file type detection for OpenDocument formats + === Bug fixes in 1.14 === diff --git a/includes/MimeMagic.php b/includes/MimeMagic.php index b21ff540e2..8383c0db43 100644 --- a/includes/MimeMagic.php +++ b/includes/MimeMagic.php @@ -11,6 +11,22 @@ define('MM_WELL_KNOWN_MIME_TYPES',<<detectZipType( $head ); } wfSuppressWarnings(); @@ -532,6 +564,48 @@ class MimeMagic { 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. * Detection is done using an external program, if $wgMimeDetectorCommand is set. -- 2.20.1