diff --git a/publish/addeditor.php b/publish/addeditor.php index 5a9bd64..442354d 100644 --- a/publish/addeditor.php +++ b/publish/addeditor.php @@ -5,6 +5,8 @@ if ( ! isset($_SERVER['HTTPS'])) { require_once("../session.lib.php"); $added = FALSE; $csrf_failed = FALSE; +$hash_failed = FALSE; +$exists = FALSE; if (!isset($_SESSION['editor'])) { header("Location: https://xxx.org.za/publish/login"); return; @@ -16,11 +18,20 @@ if (!isset($_SESSION['editor'])) { $table = "editors"; $username = (string)($_POST['username'] ?? ''); - $password = password_hash((string)($_POST['password'] ?? ''), PASSWORD_DEFAULT); + $password = password_hash((string)($_POST['password'] ?? ''), password_algorithm()); $email = (string)($_POST['email'] ?? ''); + $found = $db->execute_query("SELECT username FROM $table WHERE username = ?", array($username)); $query = "INSERT INTO $table SET `username` = ?, `password` = ?, `email` = ?"; - if ($result = $db->execute_query($query, array($username, $password, $email))) { - $added = TRUE; + if ($found && $found->fetch_assoc()) { + $exists = TRUE; + } else if ($result = $db->execute_query($query, array($username, $password, $email))) { + // If the password column cut the hash short, the editor could never log in + if (password_hash_is_stored($username, $password)) { + $added = TRUE; + } else { + $db->execute_query("DELETE FROM $table WHERE username = ?", array($username)); + $hash_failed = TRUE; + } $is_admin = TRUE; } open_db('close'); @@ -54,6 +65,12 @@ if (!isset($_SESSION['editor'])) {
Your session has expired. Please try again.
+ +An editor with that username already exists.
+ + +The editor was not added: the editors.password column is too short for the password hash. Widen it to VARCHAR(255).
+