diff --git a/app/classes/user.php b/app/classes/user.php index 6082737..14b4663 100644 --- a/app/classes/user.php +++ b/app/classes/user.php @@ -33,63 +33,6 @@ class User { } - /** - * Registers a new user. - * - * @param string $username The username of the new user. - * @param string $password The password for the new user. - * - * @return bool|string True if registration is successful, error message otherwise. - */ - public function register($username, $password) { - try { - // we have two inserts, start a transaction - $this->db->beginTransaction(); - - // hash the password, don't store it plain - $hashedPassword = password_hash($password, PASSWORD_DEFAULT); - - // insert into users table - $sql = 'INSERT - INTO users (username, password) - VALUES (:username, :password)'; - $query = $this->db->prepare($sql); - $query->bindValue(':username', $username); - $query->bindValue(':password', $hashedPassword); - - // execute the first query - if (!$query->execute()) { - // rollback on error - $this->db->rollBack(); - return false; - } - - // insert the last user id into users_meta table - $sql2 = 'INSERT - INTO users_meta (user_id) - VALUES (:user_id)'; - $query2 = $this->db->prepare($sql2); - $query2->bindValue(':user_id', $this->db->lastInsertId()); - - // execute the second query - if (!$query2->execute()) { - // rollback on error - $this->db->rollBack(); - return false; - } - - // if all is OK, commit the transaction - $this->db->commit(); - return true; - - } catch (Exception $e) { - // rollback on any error - $this->db->rollBack(); - return $e->getMessage(); - } - } - - /** * Logs in a user by verifying credentials. * diff --git a/app/pages/register.php b/app/pages/register.php deleted file mode 100644 index 135c56e..0000000 --- a/app/pages/register.php +++ /dev/null @@ -1,98 +0,0 @@ -sanitizeArray($_POST, ['username', 'password', 'confirm_password', 'csrf_token']); - - // Validate CSRF token - if (!$security->verifyCsrfToken($formData['csrf_token'] ?? '')) { - throw new Exception(Feedback::get('ERROR', 'CSRF_INVALID')['message']); - } - - $validator = new Validator($formData); - $rules = [ - 'username' => [ - 'required' => true, - 'min' => 3, - 'max' => 20 - ], - 'password' => [ - 'required' => true, - 'min' => 8, - 'max' => 100 - ], - 'confirm_password' => [ - 'required' => true, - 'matches' => 'password' - ] - ]; - - $username = $formData['username'] ?? 'unknown'; - - if ($validator->validate($rules)) { - $password = $formData['password']; - - // registering - $result = $userObject->register($username, $password); - - // redirect to login - if ($result === true) { - // Get the new user's ID for logging - $userId = $userObject->getUserId($username)[0]['id']; - $logObject->insertLog($userId, "Registration: New user \"$username\" registered successfully. IP: $user_IP", 'user'); - Feedback::flash('NOTICE', 'DEFAULT', "Registration successful. You can log in now."); - header('Location: ' . htmlspecialchars($app_root)); - exit(); - // registration fail, redirect to login - } else { - $logObject->insertLog(0, "Registration: Failed registration attempt for user \"$username\". IP: $user_IP. Reason: $result", 'system'); - Feedback::flash('ERROR', 'DEFAULT', "Registration failed. $result"); - header('Location: ' . htmlspecialchars($app_root)); - exit(); - } - } else { - $error = $validator->getFirstError(); - $logObject->insertLog(0, "Registration: Failed validation for user \"" . ($username ?? 'unknown') . "\". IP: $user_IP. Reason: $error", 'system'); - Feedback::flash('ERROR', 'DEFAULT', $error); - header('Location: ' . htmlspecialchars($app_root . '?page=register')); - exit(); - } - } - } catch (Exception $e) { - $logObject->insertLog(0, "Registration: System error. IP: $user_IP. Error: " . $e->getMessage(), 'system'); - Feedback::flash('ERROR', 'DEFAULT', $e->getMessage()); - } - - // Get any new feedback messages - include '../app/helpers/feedback.php'; - - // Load the template - include '../app/templates/form-register.php'; - -// registration disabled -} else { - echo Feedback::render('NOTICE', 'DEFAULT', 'Registration is disabled', false); -} diff --git a/app/templates/form-register.php b/app/templates/form-register.php deleted file mode 100644 index fbd382c..0000000 --- a/app/templates/form-register.php +++ /dev/null @@ -1,27 +0,0 @@ - -
Enter credentials for registration:
- -