diff --git a/common/framework/filters/filenamefilter.php b/common/framework/filters/filenamefilter.php index 1505983f0..9d0596dc6 100644 --- a/common/framework/filters/filenamefilter.php +++ b/common/framework/filters/filenamefilter.php @@ -93,22 +93,25 @@ class FilenameFilter * Check if a file has an extension that would allow direct download. * * @param string $filename + * @param bool $include_multimedia (optional) * @return bool */ - public static function isDirectDownload($filename) + public static function isDirectDownload($filename, $include_multimedia = true) { $images = 'gif|jpe?g|png|webp'; $audios = 'mp3|wav|ogg|flac|aac'; $videos = 'mp4|webm|ogv'; $legacy = 'avi|as[fx]|flv|m4[av]|midi?|mkv|moo?v|mpe?g|qt|r[am]m?|wm[av]'; - if (preg_match(sprintf('/\.(?:%s|%s|%s|%s)$/i', $images, $audios, $videos, $legacy), $filename)) + if ($include_multimedia) { - return true; + $pattern = sprintf('/\.(?:%s|%s|%s|%s)$/i', $images, $audios, $videos, $legacy); } else { - return false; + $pattern = sprintf('/\.(?:%s)$/i', $images); } + + return preg_match($pattern, $filename) ? true : false; } } diff --git a/tests/unit/framework/filters/FilenameFilterTest.php b/tests/unit/framework/filters/FilenameFilterTest.php index 2164475ab..7c26338a6 100644 --- a/tests/unit/framework/filters/FilenameFilterTest.php +++ b/tests/unit/framework/filters/FilenameFilterTest.php @@ -91,5 +91,10 @@ class FilenameFilterTest extends \Codeception\TestCase\Test $this->assertFalse(FilenameFilter::isDirectDownload('')); $this->assertFalse(FilenameFilter::isDirectDownload('http://www.google.com')); $this->assertFalse(FilenameFilter::isDirectDownload('/')); + + $this->assertTrue(FilenameFilter::isDirectDownload('foobar.jpg', false)); + $this->assertTrue(FilenameFilter::isDirectDownload('foobar.webp', false)); + $this->assertFalse(FilenameFilter::isDirectDownload('foobar.mp4', false)); + $this->assertFalse(FilenameFilter::isDirectDownload('foobar.webm', false)); } }