2024-09-27 05:47:54 +03:00
|
|
|
<?php
|
|
|
|
if(count(get_included_files()) ==1) {
|
|
|
|
http_response_code(403);
|
|
|
|
die("403: Forbidden");
|
|
|
|
}
|
|
|
|
class DataBase extends SQLite3 {
|
|
|
|
function __construct() {
|
|
|
|
$this->open('database.db');
|
2024-09-28 01:01:51 +03:00
|
|
|
$this->exec('PRAGMA foreign_keys=ON;');
|
|
|
|
$this->exec('PRAGMA full_column_names=ON;');
|
|
|
|
$this->exec('PRAGMA short_column_names=OFF;');
|
2024-09-27 05:47:54 +03:00
|
|
|
|
|
|
|
$sql = "
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
2024-09-28 01:01:51 +03:00
|
|
|
id INTEGER PRIMARY KEY UNIQUE,
|
2024-09-27 21:09:58 +03:00
|
|
|
pass TEXT,
|
|
|
|
email TEXT UNIQUE,
|
|
|
|
handle TEXT NOT NULL UNIQUE,
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
about TEXT
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS personas (
|
2024-09-28 01:01:51 +03:00
|
|
|
id INTEGER PRIMARY KEY UNIQUE,
|
2024-09-27 21:09:58 +03:00
|
|
|
userid INTEGER NOT NULL,
|
|
|
|
handle TEXT NOT NULL,
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
about TEXT,
|
|
|
|
colour INTEGER,
|
2024-09-28 01:01:51 +03:00
|
|
|
FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
|
2024-09-27 21:09:58 +03:00
|
|
|
UNIQUE (userid, handle)
|
2024-09-27 05:47:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS posts (
|
2024-09-28 01:01:51 +03:00
|
|
|
id INTEGER PRIMARY KEY UNIQUE,
|
2024-09-27 21:09:58 +03:00
|
|
|
time INTEGER NOT NULL,
|
|
|
|
userid INTEGER NOT NULL,
|
2024-09-28 01:01:51 +03:00
|
|
|
personaid INTEGER NOT NULL,
|
2024-09-27 21:09:58 +03:00
|
|
|
text TEXT NOT NULL,
|
2024-09-28 01:01:51 +03:00
|
|
|
FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
|
|
|
|
FOREIGN KEY (personaid) REFERENCES personas(id) ON UPDATE CASCADE ON DELETE CASCADE
|
2024-09-27 21:09:58 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE IF NOT EXISTS comments (
|
2024-09-28 01:01:51 +03:00
|
|
|
id INTEGER PRIMARY KEY UNIQUE,
|
2024-09-27 21:09:58 +03:00
|
|
|
time INTEGER NOT NULL,
|
|
|
|
userid INTEGER NOT NULL,
|
|
|
|
personaid INTEGER,
|
|
|
|
postid INTEGER NOT NULL,
|
|
|
|
text TEXT NOT NULL,
|
|
|
|
FOREIGN KEY (postid) REFERENCES posts(id),
|
2024-09-28 01:01:51 +03:00
|
|
|
FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
|
|
|
|
FOREIGN KEY (personaid) REFERENCES personas(id) ON UPDATE CASCADE ON DELETE CASCADE
|
2024-09-27 05:47:54 +03:00
|
|
|
);
|
|
|
|
|
2024-09-28 01:01:51 +03:00
|
|
|
INSERT OR IGNORE INTO users (id, handle, name, about) VALUES ('0', 'SYSTEM', 'SYSTEM', 'SYSTEM');
|
2024-09-27 05:47:54 +03:00
|
|
|
|
|
|
|
";
|
|
|
|
|
|
|
|
$ret = $this->exec($sql);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-27 21:09:58 +03:00
|
|
|
function addUser($handle, $name, $about=NULL) {
|
2024-09-28 01:01:51 +03:00
|
|
|
$id = hexdec(uniqid());
|
|
|
|
$sql = "INSERT INTO users (id, handle, name, about) VALUES ('$id', '$handle', '$name', '$about')";
|
2024-09-27 05:47:54 +03:00
|
|
|
$ret = $this->exec($sql);
|
|
|
|
if(!$ret) {
|
|
|
|
die($this->lastErrorMsg());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:01:51 +03:00
|
|
|
function addPost($text, $userid=NULL, $personaid=NULL) {
|
|
|
|
$id = hexdec(uniqid());
|
2024-09-27 05:47:54 +03:00
|
|
|
$time = time();
|
2024-09-28 01:01:51 +03:00
|
|
|
$sql = $this->prepare("INSERT INTO posts (id, time, userid, personaid, text) values ('$id', '$time', '$userid', :personaid, '$text')");
|
|
|
|
$sql->bindParam(':personaid', $personaid, SQLITE3_INTEGER);
|
|
|
|
$ret = $sql->execute();
|
|
|
|
if(!$ret) {
|
|
|
|
die($this->lastErrorMsg());
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 05:47:54 +03:00
|
|
|
|
2024-09-28 01:01:51 +03:00
|
|
|
function addPersona($userid, $handle, $name, $about=NULL, $colour=NULL) {
|
|
|
|
$id = hexdec(uniqid());
|
|
|
|
$sql = "INSERT INTO personas (id, userid, handle, name, colour) VALUES ('$id', '$userid', '$handle', '$about', '$colour');";
|
2024-09-27 21:09:58 +03:00
|
|
|
$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';";
|
2024-09-27 05:47:54 +03:00
|
|
|
$ret = $this->exec($sql);
|
|
|
|
if(!$ret) {
|
|
|
|
die($this->lastErrorMsg());
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 21:09:58 +03:00
|
|
|
|
|
|
|
function passwordVerify($userid, $password) {
|
|
|
|
$sql = "SELECT pass FROM users WHERE id='$userid';";
|
2024-09-28 01:01:51 +03:00
|
|
|
$ret = $this->query($sql)->fetchArray(SQLITE3_NUM);
|
2024-09-27 21:09:58 +03:00
|
|
|
if(!$ret) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$dbhash = $ret[0];
|
|
|
|
return password_verify($password, $dbhash);
|
|
|
|
}
|
|
|
|
|
2024-09-28 01:01:51 +03:00
|
|
|
function getPosts($userid=NULL, $personaid = NULL) {
|
|
|
|
$sql = "SELECT * FROM posts AS post LEFT JOIN users AS user ON post.userid=user.id LEFT JOIN personas AS persona ON post.personaid=persona.id;";
|
|
|
|
|
|
|
|
$ret = $this->query($sql);
|
|
|
|
$array = array();
|
|
|
|
while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
|
|
|
|
array_push($array, $row);
|
|
|
|
}
|
|
|
|
return $array;
|
2024-09-27 21:09:58 +03:00
|
|
|
}
|
2024-09-27 05:47:54 +03:00
|
|
|
}
|
|
|
|
?>
|