From f5e972c030675f46cda543e13da1b787457e070b Mon Sep 17 00:00:00 2001 From: cflip <36554078+cflip@users.noreply.github.com> Date: Wed, 23 Jun 2021 15:21:12 -0600 Subject: Add the rest of the changes --- includes/model/User.php | 189 ++++++++++++++++++++++++++---------------------- 1 file changed, 102 insertions(+), 87 deletions(-) mode change 100644 => 100755 includes/model/User.php (limited to 'includes/model/User.php') diff --git a/includes/model/User.php b/includes/model/User.php old mode 100644 new mode 100755 index 13cbc03..7d3c1e4 --- a/includes/model/User.php +++ b/includes/model/User.php @@ -1,88 +1,103 @@ -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); +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); + } + + public function get_threads(): array + { + $sql = "SELECT thread_id FROM threads WHERE thread_author = ? ORDER BY thread_date_lastpost 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; + } +} + +function username_exists(string $username): bool +{ + $sql = "SELECT * FROM users WHERE user_name = ?;"; + $result = Database::get()->query($sql, "s", $username); + + return !empty($result); } \ No newline at end of file -- cgit v1.2.3