query($sql, "i", $id); if (empty($result)) { return; } $this->id = $id; $this->name = $result[0]['user_name']; $this->password = $result[0]['user_password']; $this->date = $result[0]['user_date_registered']; $this->level = $result[0]['user_level']; $this->has_value = true; } public function get_by_name($name) { $sql = "SELECT user_id, user_date_registered, user_level, user_password 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_password']; $this->date = $result[0]['user_date_registered']; $this->level = $result[0]['user_level']; $this->has_value = true; } public function has_value(): bool { return $this->has_value; } public static function register(string $username, string $password) { $sql = "INSERT INTO users(user_name, user_password, user_date_registered) VALUES(?, ?, NOW());"; $pass_hash = password_hash($password, PASSWORD_DEFAULT); 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_password = ? WHERE user_id = ?;"; Database::get()->query($sql, "si", $pass_hash, $this->id); } public function get_threads(): array { $sql = "SELECT thread_id FROM threads WHERE thread_author = ? ORDER BY thread_last_post_date DESC"; $result = Database::get()->query($sql, "i", $this->id); $threads = array(); foreach ($result as $row) { $thread = new Thread($row['thread_id']); if ($thread->has_value()) array_push($threads, $thread); } return $threads; } public static function username_exists(string $username): bool { $sql = "SELECT * FROM users WHERE user_name = ?;"; $result = Database::get()->query($sql, "s", $username); return !empty($result); } }