From 0722f46c6e5e1a1bae85c24379ff061025679c1d Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Thu, 6 Feb 2014 16:06:04 +0100 Subject: Simplify valid_user() and valid_username() Signed-off-by: Lukas Fleischer --- web/lib/acctfuncs.inc.php | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) (limited to 'web') diff --git a/web/lib/acctfuncs.inc.php b/web/lib/acctfuncs.inc.php index 49d7d7c..f705574 100644 --- a/web/lib/acctfuncs.inc.php +++ b/web/lib/acctfuncs.inc.php @@ -537,17 +537,14 @@ function is_ipbanned() { * @return bool True if username meets criteria, otherwise false */ function valid_username($user) { - if (!empty($user)) { - if ( strlen($user) >= USERNAME_MIN_LEN && - strlen($user) <= USERNAME_MAX_LEN ) { - $user = strtolower($user); - if ( preg_match("/^[a-z0-9]+[.\-_]?[a-z0-9]+$/", $user) ) { - return true; - } - } + if (strlen($user) < USERNAME_MIN_LEN || + strlen($user) > USERNAME_MAX_LEN) { + return false; + } else if (!preg_match("/^[a-z0-9]+[.\-_]?[a-z0-9]+$/", $user)) { + return false; } - return false; + return true; } /** @@ -558,21 +555,17 @@ function valid_username($user) { * @return string|void Return user ID if in database, otherwise void */ function valid_user($user) { - /* if ( $user = valid_username($user) ) { */ - - $dbh = DB::connect(); - - if ( $user ) { - $q = "SELECT ID FROM Users "; - $q.= "WHERE Username = " . $dbh->quote($user); + if ($user) { + $dbh = DB::connect(); + $q = "SELECT ID FROM Users WHERE "; + $q.= "Username = " . $dbh->quote($user); $result = $dbh->query($q); if ($result) { $row = $result->fetch(PDO::FETCH_NUM); return $row[0]; } } - return; } /** -- cgit v1.2.3-54-g00ecf