query($sql, "i", $id); if (empty($result)) { return; } $this->id = $id; $this->name = $result[0]['user_name']; $this->password = $result[0]['user_pass']; $this->date = $result[0]['user_date']; $this->level = $result[0]['user_level']; $this->has_value = true; } public function get_by_name($name) { $sql = "SELECT user_id, user_date, user_level, user_pass FROM users WHERE user_name = ?"; $result = Database::get()->query($sql, "s", $name); if (empty($result)) { return; } $this->id = $result[0]['user_id']; $this->name = $name; $this->password = $result[0]['user_pass']; $this->date = $result[0]['user_date']; $this->level = $result[0]['user_level']; $this->has_value = true; } public function has_value() { return $this->has_value; } public static function register(string $username, string $pass_hash) { $sql = "INSERT INTO users(user_name, user_pass, user_date, user_level) VALUES(?, ?, NOW(), 0);"; Database::get()->query($sql, "ss", $username, $pass_hash); } public function change_password(string $pass_hash) { if (!Session::get()->is_signed_in()) { trigger_error('You are not signed in.'); return; } if (Session::get()->get_current_user()->id != $this->id) { trigger_error("You can't change another user's password."); return; } $sql = "UPDATE users SET user_pass = ? WHERE user_id = ?;"; Database::get()->query($sql, "si", $pass_hash, $this->id); } } function username_exists(string $username): bool { $sql = "SELECT * FROM users WHERE user_name = ?;"; $result = Database::get()->query($sql, "s", $username); return !empty($result); }