X-Git-Url: https://git.p6c8.net/jirafeau/jirafeau.git/blobdiff_plain/08b9270797ef139d44343187cfa545a145021d83..refs/merge-requests/39/head:/lib/functions.php diff --git a/lib/functions.php b/lib/functions.php index 8bfa61b..7f03b5f 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -229,6 +229,30 @@ function jirafeau_ini_to_bytes($value) return $bytes; } +/** + * returns the configured disallowed extensions, lower-cased and without + * a leading dot + * @param $cfg jirafeau config + * @returns array of normalized extensions + */ +function jirafeau_disallowed_extensions($cfg) +{ + if (empty($cfg['disallowed_extensions'])) { + return array(); + } + + // tolerate a leading dot in the config, e.g. '.jpg' instead of 'jpg' + $extensions = array(); + foreach ($cfg['disallowed_extensions'] as $extension) { + $extension = ltrim(strtolower(trim($extension)), '.'); + if ($extension !== '') { + $extensions[] = $extension; + } + } + + return $extensions; +} + /** * checks if a file name's extension is allowed to be uploaded * @param $cfg jirafeau config @@ -237,14 +261,24 @@ function jirafeau_ini_to_bytes($value) */ function jirafeau_is_extension_allowed($cfg, $filename) { - if (empty($cfg['disallowed_extensions'])) { + $extensions = jirafeau_disallowed_extensions($cfg); + if (empty($extensions)) { return true; } - $extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); - $disallowed = array_map('strtolower', $cfg['disallowed_extensions']); + // trailing dots and whitespace are dropped on storage or by the client OS + $filename = strtolower(rtrim($filename, " \t\n\r\0\v.")); - return !in_array($extension, $disallowed); + // suffix match so that compound extensions such as 'tar.gz' work too; + // 'gz' still covers 'archive.tar.gz' + foreach ($extensions as $extension) { + $suffix = '.' . $extension; + if (substr($filename, -strlen($suffix)) === $suffix) { + return false; + } + } + + return true; } /**