current_user = $current_user; $this->conn = $conn; } public function getForm($username, $bio, $password) { $this->new_username = $username; $this->new_bio = $bio; $this->new_password = $password; } public function updateUsername() { if ($this->new_username == $this->getDetails("username")) return NULL; switch(true) { case empty($this->new_username): $this->error = "Your username cannot be left blank."; break; case preg_match("/[^a-z0-9 ]/i", $this->new_username): $this->error = "Username cannot have any special characters."; break; case strlen($this->new_username) > 16: $this->error = "Username cannot be longer than 16 characters."; break; case strlen($this->new_username) < 3: $this->error = "Username cannot be shorter than 3 characters."; break; default: $stmt = $this->conn->prepare("SELECT username FROM accounts WHERE username = :username"); $stmt->bindParam(":username", $this->new_username); $stmt->execute(); if ($stmt->rowCount() > 0) $this->error = "Username has been already taken."; } if ($this->error == NULL) { $stmt = $this->conn->prepare("UPDATE accounts SET username = :username WHERE id = :id"); $stmt->bindParam(":username", $this->new_username); $stmt->bindParam(":id", $this->getDetails("id")); $stmt->execute(); session_regenerate_id(true); $_SESSION["user"] = $this->new_username; return true; } return $this->error; } public function updateBio() { if ($this->new_bio == $this->getDetails("bio")) return NULL; if (strlen($this->new_bio) > 150) $this->error = "Bio cannot be longer than 150 characters."; if ($this->error == NULL) { $stmt = $this->conn->prepare("UPDATE accounts SET bio = :bio WHERE id = :id"); $stmt->bindParam(":bio", $this->new_bio); $stmt->bindParam(":id", $this->getDetails("id")); $stmt->execute(); return true; } return $this->error; } public function updatePassword($passwordConfirm) { if (empty($this->new_password)) { return NULL; } else { if ($this->new_password != $passwordConfirm) $this->error = "Passwords don't match."; } if ($this->error == NULL) { $hashed_password = password_hash($this->new_password, PASSWORD_BCRYPT); $stmt = $this->conn->prepare("UPDATE accounts SET password = :password WHERE id = :id"); $stmt->bindParam(":password", $hashed_password); $stmt->bindParam(":id", $this->getDetails("id")); $stmt->execute(); return true; } return $this->error; } } ?>