Page MenuHomePhabricator

mimemagic.patch

Authored By
bzimport
Nov 21 2014, 10:23 PM
Size
2 KB
Referenced Files
None
Subscribers
None

mimemagic.patch

Index: MimeMagic.php
===================================================================
--- MimeMagic.php (revision 55559)
+++ MimeMagic.php (working copy)
@@ -474,6 +474,7 @@
* Warning: this is a heuristic, and won't match a file with a lot of non-PHP before.
* It will also match text files which could be PHP. :)
*/
+ $phpmatch = false;
if( ( strpos( $head, '<?php' ) !== false ) ||
( strpos( $head, '<? ' ) !== false ) ||
( strpos( $head, "<?\n" ) !== false ) ||
@@ -486,8 +487,19 @@
( strpos( $head, "<\x00?\x00\t" ) !== false ) ||
( strpos( $head, "<\x00?\x00=" ) !== false ) ) {
- wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
- return "application/x-php";
+ /* Initial checks can easily lead to false positives, since they're only
+ * checking for a three character string in any text or binary file
+ * Exclude files that contain 3 nulls in a row (which is present in most
+ * binary files, but not present in text files)
+ */
+ if ( strpos( $head, "\x00\x00\x00" ) === false ) {
+ wfDebug( __METHOD__ . ": recognized $file as application/x-php\n" );
+ return 'application/x-php';
+ }
+ else {
+ wfDebug( __METHOD__ . ": false identification of $file as application/x-php\n" );
+ $phpmatch = 'application/x-php';
+ }
}
/*
@@ -496,7 +508,9 @@
$xml = new XmlTypeCheck( $file );
if( $xml->wellFormed ) {
global $wgXMLMimeTypes;
- if( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
+ if ( $phpmatch ) {
+ return $phpmatch;
+ } elseif ( isset( $wgXMLMimeTypes[$xml->getRootElement()] ) ) {
return $wgXMLMimeTypes[$xml->getRootElement()];
} else {
return 'application/xml';
@@ -537,7 +551,12 @@
$match = array();
if ( preg_match( '%/?([^\s]+/)(\w+)%', $head, $match ) ) {
- $mime = "application/x-{$match[2]}";
+ if ( $phpmatch ) {
+ $mime = $phpmatch;
+ }
+ else {
+ $mime = "application/x-{$match[2]}";
+ }
wfDebug( __METHOD__.": shell script recognized as $mime\n" );
return $mime;
}
@@ -566,6 +585,10 @@
return 'image/vnd.djvu';
}
+ if ( $phpmatch ) {
+ wfDebug( __METHOD__.": failed to detect other type for file $file, default to $phpmatch\n" );
+ return $phpmatch;
+ }
return false;
}

File Metadata

Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4797
Default Alt Text
mimemagic.patch (2 KB)

Event Timeline