107 lines
2.6 KiB
PHP
Executable file
107 lines
2.6 KiB
PHP
Executable file
<?php
|
|
if(count(get_included_files()) ==1) {
|
|
http_response_code(403);
|
|
die("403: Forbidden");
|
|
}
|
|
class DataBase extends SQLite3 {
|
|
function __construct() {
|
|
$this->open('database.db');
|
|
|
|
$sql = "
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
uuid TEXT UNIQUE,
|
|
pass TEXT,
|
|
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 (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
|
|
uuid TEXT UNIQUE,
|
|
time INTEGER NOT NULL,
|
|
userid INTEGER NOT NULL,
|
|
personaid INTEGER,
|
|
text TEXT NOT NULL,
|
|
FOREIGN KEY (userid) REFERENCES users(id)
|
|
);
|
|
|
|
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');
|
|
|
|
";
|
|
|
|
$ret = $this->exec($sql);
|
|
|
|
}
|
|
|
|
function addUser($handle, $name, $about=NULL) {
|
|
$uuid = $this->uuidGen();
|
|
$sql = "INSERT INTO users (handle, name, about, uuid) VALUES ('$handle', '$name', '$about', '$uuid')";
|
|
$ret = $this->exec($sql);
|
|
if(!$ret) {
|
|
die($this->lastErrorMsg());
|
|
}
|
|
}
|
|
|
|
function addPost($userid, $personaid, $contents) {
|
|
$time = time();
|
|
|
|
$uuid = $this->uuidGen();
|
|
$sql = "INSERT INTO posts (time, userid, personaid, text, uuid) values ('$time', '$userid', '$personaid', '$contents', '$uuid')";
|
|
$ret = $this->exec($sql);
|
|
if(!$ret) {
|
|
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));
|
|
}
|
|
}
|
|
?>
|