id = $id; $this->username = $username; $this->password = $password ? password_hash($password, PASSWORD_DEFAULT) : null; $this->picture_hash = $picture_hash; $this->verified = $verified; $this->bio = $bio; $this->social_credit = $social_credit; } /** * Creates the table for the object type in the database. */ public static function CreateTable(): void { throw new SecurityFaultException("Attempt to create table on user object."); } /** * Drops the table for the object type from the database. */ public static function DropTable(): void { throw new SecurityFaultException("Attempt to drop table on user object."); } /** * Saves the object to the database. */ public function Save(): int { Hajeebtok::$Database->Query("INSERT INTO accounts (username, password, picture_hash, verified, bio, social_credit) VALUES (:username, :password, :picture_hash, :verified, :bio, :social_credit)", [ "username" => $this->username, "password" => $this->password, "picture_hash" => $this->picture_hash, "verified" => $this->verified, "bio" => $this->bio, "social_credit" => $this->social_credit ]); $id = Hajeebtok::$Database->LastInsertId(); Logger::Debug("Saved account id ($id)."); return $id; } /** * Deletes the object from the database. */ public function Delete() { Hajeebtok::$Database->Query("DELETE FROM accounts WHERE id = :id", ["id" => $this->id]); } /** * Loads the object from the database. */ public function Load() { if (empty($this->id)) { // search by username $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]); } else { // search by id $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]); } if (empty($data)) throw new AccountNotFoundException($this->id, 404); $this->id = $data["id"]; $this->username = $data["username"]; $this->password = $data["password"]; $this->picture_hash = $data["picture_hash"]; $this->verified = $data["verified"] === 1; $this->bio = $data["bio"]; $this->social_credit = $data["social_credit"]; } public function Update() { Hajeebtok::$Database->Query("UPDATE accounts SET username = :username, password = :password, picture_hash = :picture_hash, verified = :verified, bio = :bio, social_credit = :social_credit WHERE id = :id", [ "id" => $this->id, "username" => $this->username, "password" => $this->password, "picture_hash" => $this->picture_hash, "verified" => $this->verified, "bio" => $this->bio, "social_credit" => $this->social_credit ]); Logger::Debug("Updated account id ($this->id).");; } public function LoadMany(): array { if(empty($this->username)) throw new AccountNotFoundException(0, 404); $data = Hajeebtok::$Database->Query("SELECT * FROM accounts WHERE username LIKE :username", ["username" => "%$this->username%"]); if(empty($data)) throw new AccountNotFoundException(0, 404); return $data; } public function Exists(): bool { if ($this->id === null) { // search by username $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE username = :username", ["username" => $this->username]); } else { // search by id $data = Hajeebtok::$Database->Row("SELECT * FROM accounts WHERE id = :id", ["id" => $this->id]); } return !empty($data); } }