summaryrefslogtreecommitdiff
path: root/includes/model/User.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/model/User.php')
-rwxr-xr-x[-rw-r--r--]includes/model/User.php189
1 files changed, 102 insertions, 87 deletions
diff --git a/includes/model/User.php b/includes/model/User.php
index 13cbc03..7d3c1e4 100644..100755
--- a/includes/model/User.php
+++ b/includes/model/User.php
@@ -1,88 +1,103 @@
-<?php
-include_once './includes/Database.php';
-
-const USER_LEVEL_MODERATOR = 1;
-
-class User
-{
- public $id;
- public $name;
- public $password;
- public $date;
- public $level = 0;
-
- private $has_value = false;
-
- // Can't use a constructor here because we have two possible ways to get the user from the database
- // and PHP does not allow function overloading.
- public function get_by_id($id)
- {
- $sql = "SELECT user_name, user_date, user_level, user_pass FROM users WHERE user_id = ?;";
- $result = Database::get()->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);
+<?php
+include_once './includes/Database.php';
+
+const USER_LEVEL_MODERATOR = 1;
+
+class User
+{
+ public $id;
+ public $name;
+ public $password;
+ public $date;
+ public $level = 0;
+
+ private $has_value = false;
+
+ // Can't use a constructor here because we have two possible ways to get the user from the database
+ // and PHP does not allow function overloading.
+ public function get_by_id($id)
+ {
+ $sql = "SELECT user_name, user_date, user_level, user_pass FROM users WHERE user_id = ?;";
+ $result = Database::get()->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