aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb
diff options
context:
space:
mode:
authorMarc Alexander <admin@m-a-styles.de>2014-09-04 22:32:08 +0200
committerMarc Alexander <admin@m-a-styles.de>2014-09-05 10:56:06 +0200
commitd31ff51785f42e3de255722a32f098ab49d1489c (patch)
tree9db9c8ed1852e099a0e1586abf618c2f0a326593 /phpBB/phpbb
parent7de15bc54c14ce4fa74480f3894f9aec23dbcfba (diff)
downloadforums-d31ff51785f42e3de255722a32f098ab49d1489c.tar
forums-d31ff51785f42e3de255722a32f098ab49d1489c.tar.gz
forums-d31ff51785f42e3de255722a32f098ab49d1489c.tar.bz2
forums-d31ff51785f42e3de255722a32f098ab49d1489c.tar.xz
forums-d31ff51785f42e3de255722a32f098ab49d1489c.zip
[ticket/13031] Guess with all mimetype guessers and pick best guess
PHPBB3-13031
Diffstat (limited to 'phpBB/phpbb')
-rw-r--r--phpBB/phpbb/mimetype/guesser.php39
1 files changed, 32 insertions, 7 deletions
diff --git a/phpBB/phpbb/mimetype/guesser.php b/phpBB/phpbb/mimetype/guesser.php
index 773a1f822a..c019cb5b07 100644
--- a/phpBB/phpbb/mimetype/guesser.php
+++ b/phpBB/phpbb/mimetype/guesser.php
@@ -115,17 +115,42 @@ class guesser
return false;
}
+ $mimetype = 'application/octet-stream';
+
foreach ($this->guessers as $guesser)
{
- $mimetype = $guesser->guess($file, $file_name);
+ $mimetype_guess = $guesser->guess($file, $file_name);
- // Try to guess something that is not the fallback application/octet-stream
- if ($mimetype !== null && $mimetype !== 'application/octet-stream')
- {
- return $mimetype;
- }
+ $mimetype = $this->choose_mime_type($mimetype, $mimetype_guess);
}
// Return any mimetype if we got a result or the fallback value
- return (!empty($mimetype)) ? $mimetype : 'application/octet-stream';
+ return $mimetype;
+ }
+
+ /**
+ * Choose the best mime type based on the current mime type and the guess
+ * If a guesser returns nulls or application/octet-stream, we will keep
+ * the current guess. Guesses with a slash inside them will be favored over
+ * already existing without slashes. However, any guess that will pass the
+ * first check will always overwrite the default application/octet-stream.
+ *
+ * @param string $mime_type The current mime type
+ * @param string $guess The current mime type guess
+ *
+ * @return string The best mime type based on current mime type and guess
+ */
+ protected function choose_mime_type($mime_type, $guess)
+ {
+ if ($guess === null || $guess == 'application/octet-stream')
+ {
+ return $mime_type;
+ }
+
+ if ((strpos($mime_type, '/') === false || $mime_type == 'application/octet-stream') && strpos($guess, '/') !== false)
+ {
+ $mime_type = $guess;
+ }
+
+ return $mime_type;
}
}