prepare("SELECT id, login, password FROM users WHERE login = ? LIMIT 1"); $stmt->bind_param('s', $login); $stmt->execute(); $stmt->bind_result($this->id, $this->login, $this->hash); if ($stmt->fetch()) { $this->isValid = true; } $stmt->close(); $config = new uConfig(); if (!empty($config::$admin_user) && $config::$admin_user == $this->login) { $this->isAdmin = true; } } } public function add($login, $hash) { $userid = false; if (!empty($login) && !empty($hash)) { $sql = "INSERT INTO users (login, password) VALUES (?, ?)"; $stmt = self::$db->prepare($sql); $stmt->bind_param('ss', $login, $hash); $stmt->execute(); if (!self::$db->error && !$stmt->errno) { $userid = self::$db->insert_id; } $stmt->close(); } return $userid; } public function setPass($hash) { $ret = false; $sql = "UPDATE users SET password = ? WHERE login = ?"; $stmt = self::$db->prepare($sql); $stmt->bind_param('ss', $hash, $this->login); $stmt->execute(); if (!self::$db->error && !$stmt->errno) { $ret = true; } $stmt->close(); return $ret; } public function validPassword($password) { return password_verify($password, $this->hash); } public function storeInSession() { $_SESSION['user'] = $this; } public function getFromSession() { if (isset($_SESSION['user'])) { $sessionUser = $_SESSION['user']; $this->id = $sessionUser->id; $this->login = $sessionUser->login; $this->hash = $sessionUser->hash; $this->isAdmin = $sessionUser->isAdmin; $this->isValid = $sessionUser->isValid; } } public function listAll() { $query = "SELECT id, login FROM users ORDER BY login"; $result = self::$db->query($query); if ($result === false) { return false; } $userArr = []; while ($row = $result->fetch_assoc()) { $userArr[$row['id']] = $row['login']; } $result->close(); return $userArr; } } ?>