From ee6706a4ab8207fc2c007fc2069505b24873e780 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Sat, 8 Jun 2013 17:11:28 +0200 Subject: mfile/new_id(): rewrite as loop and make min/max parameters Signed-off-by: Florian Pritz --- application/models/mfile.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'application/models') diff --git a/application/models/mfile.php b/application/models/mfile.php index 0dd8d7b1e..27f05e46b 100644 --- a/application/models/mfile.php +++ b/application/models/mfile.php @@ -16,23 +16,29 @@ class Mfile extends CI_Model { } // Returns an unused ID - function new_id() + function new_id($min = 3, $max = 6) { static $id_blacklist = NULL; - $id = random_alphanum(3,6); - if ($id_blacklist == NULL) { $id_blacklist = scandir(FCPATH); $id_blacklist[] = "file"; $id_blacklist[] = "user"; } - if ($this->id_exists($id) || in_array($id, $id_blacklist)) { - return $this->new_id(); - } else { + $max_tries = 100; + + for ($try = 0; $try < $max_tries; $try++) { + $id = random_alphanum($min, $max); + + if ($this->id_exists($id) || in_array($id, $id_blacklist)) { + continue; + } + return $id; } + + show_error("Failed to find unused ID after $max_tries tries."); } function id_exists($id) -- cgit v1.2.3-24-g4f1b