From 82ae4c6587c81211fe384c4b8bc607026dd018a7 Mon Sep 17 00:00:00 2001 From: WubTheCaptain Date: Wed, 16 Sep 2015 00:28:14 +1000 Subject: Rework generate_name() logic Adds a new config option POMF_FILES_LENGTH. This will allow the user to adjust the length of filenames. Defaults to 6. The filename generator never used character z in new filenames. There are 27 characters in a-z, but the old code uses a magic number of 26 characters. This bug was introduced in commit 5b77a4eaab418836820b2a0bad7c88f0b88136a0. Instead of relying on magic numbers like with mt_rand(), conveniently use arrays with array_rand() instead. More characters can be added with array_merge() in future if desired. --- static/upload.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'static/upload.php') diff --git a/static/upload.php b/static/upload.php index 8aac02c..6e54ae1 100644 --- a/static/upload.php +++ b/static/upload.php @@ -23,8 +23,9 @@ function generate_name ($file) { global $doubledots; // We start at N retries, and --N until we give up - $tries = POMF_FILES_RETRIES; - $ext = pathinfo($file->name, PATHINFO_EXTENSION); + $tries = POMF_FILES_RETRIES; + $length = POMF_FILES_LENGTH; + $ext = pathinfo($file->name, PATHINFO_EXTENSION); // Check if extension is a double-dot extension and, if true, override $ext $revname = strrev($file->name); @@ -39,11 +40,10 @@ function generate_name ($file) { // Iterate until we reach the maximum number of retries if ($tries-- == 0) throw new Exception('Gave up trying to find an unused name', 500); - $chars = 'abcdefghijklmnopqrstuvwxyz'; + $chars = range('a', 'z'); $name = ''; - for ($i = 0; $i < 6; $i++) { - $name .= $chars[mt_rand(0, 25)]; - // $chars string length is hardcoded, should use a variable to store it? + for ($i = 0; $i < $length; $i++) { + $name .= $chars[array_rand($chars)]; } // Add the extension to the file name -- cgit v1.2.3