Database on steroids

This commit is contained in:
Jarkko Toivanen 2024-09-27 21:09:58 +03:00
parent ced9eba276
commit 5d7ac8f451
Signed by: jt
GPG key ID: 9151B109B73ECAD5

View file

@ -9,20 +9,51 @@ class DataBase extends SQLite3 {
$sql = " $sql = "
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
ID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
HANDLE TEXT NOT NULL UNIQUE, uuid TEXT UNIQUE,
NAME TEXT NOT NULL, pass TEXT,
ABOUT TEXT NOT NULL email TEXT UNIQUE,
handle TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
about TEXT
);
CREATE TABLE IF NOT EXISTS personas (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
uuid TEXT UNIQUE,
userid INTEGER NOT NULL,
handle TEXT NOT NULL,
name TEXT NOT NULL,
about TEXT,
colour INTEGER,
FOREIGN KEY (userid) REFERENCES users(id),
UNIQUE (userid, handle)
); );
CREATE TABLE IF NOT EXISTS posts ( CREATE TABLE IF NOT EXISTS posts (
ID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
TIME INTEGER NOT NULL, uuid TEXT UNIQUE,
USERID INTEGER NOT NULL, time INTEGER NOT NULL,
TEXT TEXT NOT NULL userid INTEGER NOT NULL,
personaid INTEGER,
text TEXT NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id)
); );
INSERT OR IGNORE INTO users (ID, HANDLE, NAME, ABOUT) VALUES ('0', 'SYSTEM', 'SYSTEM', ''); CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
uuid TEXT UNIQUE,
time INTEGER NOT NULL,
userid INTEGER NOT NULL,
personaid INTEGER,
postid INTEGER NOT NULL,
text TEXT NOT NULL,
FOREIGN KEY (postid) REFERENCES posts(id),
FOREIGN KEY (userid) REFERENCES users(id),
FOREIGN KEY (personaid) REFERENCES personas(id)
);
INSERT OR IGNORE INTO users (id, handle, name, about, uuid) VALUES ('0', 'SYSTEM', 'SYSTEM', 'SYSTEM', 'SYSTEM');
"; ";
@ -30,23 +61,47 @@ class DataBase extends SQLite3 {
} }
function addUser($handle, $name, $about="") { function addUser($handle, $name, $about=NULL) {
$sql = "INSERT INTO users (HANDLE, NAME, ABOUT) VALUES ('$handle', '$name', '$about')"; $uuid = $this->uuidGen();
$sql = "INSERT INTO users (handle, name, about, uuid) VALUES ('$handle', '$name', '$about', '$uuid')";
$ret = $this->exec($sql); $ret = $this->exec($sql);
if(!$ret) { if(!$ret) {
die($this->lastErrorMsg()); die($this->lastErrorMsg());
} }
} }
function addPost($contents) { function addPost($userid, $personaid, $contents) {
$userid = 0;
$time = time(); $time = time();
$sql = "INSERT INTO posts (TIME, USERID, TEXT) values ('$time', '$userid', '$contents')"; $uuid = $this->uuidGen();
$sql = "INSERT INTO posts (time, userid, personaid, text, uuid) values ('$time', '$userid', '$personaid', '$contents', '$uuid')";
$ret = $this->exec($sql); $ret = $this->exec($sql);
if(!$ret) { if(!$ret) {
die($this->lastErrorMsg()); die($this->lastErrorMsg());
} }
} }
function passwordSet($userid, $password=NULL) {
$hash = empty($password) ? NULL : password_hash($password, PASSWORD_DEFAULT);
$sql = "UPDATE USERS SET pass='$hash' WHERE id='$userid';";
$ret = $this->exec($sql);
if(!$ret) {
die($this->lastErrorMsg());
}
}
function passwordVerify($userid, $password) {
$sql = "SELECT pass FROM users WHERE id='$userid';";
$ret = $this->query($sql)->fetchArray();
if(!$ret) {
return false;
}
$dbhash = $ret[0];
return password_verify($password, $dbhash);
}
function uuidGen() {
return base64_encode(random_bytes(12));
}
} }
?> ?>