tkr/src/classes/User.php
2025-06-01 23:30:24 -04:00

55 lines
1.7 KiB
PHP

<?php
#require_once __DIR__ . '/../bootstrap.php';
#confirm_setup();
// Made this a class so it could be more obvious where config settings are coming from.
// Felt too much like magic constants in other files before.
class User {
// properties
public string $username;
public string $displayName;
public string $about;
public string $website;
public string $mood;
// load user settings from sqlite database
public static function load(): self {
$db = get_db();
// There's only ever one user. I'm just leaning into that.
$stmt = $db->query("SELECT username, display_name, about, website, mood FROM user WHERE id=1");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$u = new self();
if ($row) {
$u->username = $row['username'];
$u->displayName = $row['display_name'];
$u->about = $row['about'] ?? '';
$u->website = $row['website'] ?? '';
$u->mood = $row['mood'];
}
return $u;
}
public function save(): self {
$db = get_db();
$stmt = $db->prepare("UPDATE user SET username=?, display_name=?, about=?, website=?, mood=? WHERE id=1");
$stmt->execute([$this->username, $this->displayName, $this->about, $this->website, $this->mood]);
return self::load();
}
// Making this a separate function to avoid
// loading the password into memory
public function set_password(string $password): void {
$db = get_db();
$hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = $db->prepare("UPDATE user SET password_hash=? WHERE id=1");
$stmt->execute([$hash]);
}
}