Too many things I don't even remember :D

This commit is contained in:
Jarkko Toivanen 2024-09-28 01:01:51 +03:00
parent 5d7ac8f451
commit e2408fb49d
Signed by: jt
GPG key ID: 9151B109B73ECAD5
3 changed files with 104 additions and 34 deletions

View file

@ -6,11 +6,13 @@ if(count(get_included_files()) ==1) {
class DataBase extends SQLite3 { class DataBase extends SQLite3 {
function __construct() { function __construct() {
$this->open('database.db'); $this->open('database.db');
$this->exec('PRAGMA foreign_keys=ON;');
$this->exec('PRAGMA full_column_names=ON;');
$this->exec('PRAGMA short_column_names=OFF;');
$sql = " $sql = "
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER PRIMARY KEY UNIQUE,
uuid TEXT UNIQUE,
pass TEXT, pass TEXT,
email TEXT UNIQUE, email TEXT UNIQUE,
handle TEXT NOT NULL UNIQUE, handle TEXT NOT NULL UNIQUE,
@ -19,41 +21,39 @@ class DataBase extends SQLite3 {
); );
CREATE TABLE IF NOT EXISTS personas ( CREATE TABLE IF NOT EXISTS personas (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER PRIMARY KEY UNIQUE,
uuid TEXT UNIQUE,
userid INTEGER NOT NULL, userid INTEGER NOT NULL,
handle TEXT NOT NULL, handle TEXT NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
about TEXT, about TEXT,
colour INTEGER, colour INTEGER,
FOREIGN KEY (userid) REFERENCES users(id), FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
UNIQUE (userid, handle) 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 UNIQUE,
uuid TEXT UNIQUE,
time INTEGER NOT NULL, time INTEGER NOT NULL,
userid INTEGER NOT NULL, userid INTEGER NOT NULL,
personaid INTEGER, personaid INTEGER NOT NULL,
text TEXT NOT NULL, text TEXT NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id) FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (personaid) REFERENCES personas(id) ON UPDATE CASCADE ON DELETE CASCADE
); );
CREATE TABLE IF NOT EXISTS comments ( CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, id INTEGER PRIMARY KEY UNIQUE,
uuid TEXT UNIQUE,
time INTEGER NOT NULL, time INTEGER NOT NULL,
userid INTEGER NOT NULL, userid INTEGER NOT NULL,
personaid INTEGER, personaid INTEGER,
postid INTEGER NOT NULL, postid INTEGER NOT NULL,
text TEXT NOT NULL, text TEXT NOT NULL,
FOREIGN KEY (postid) REFERENCES posts(id), FOREIGN KEY (postid) REFERENCES posts(id),
FOREIGN KEY (userid) REFERENCES users(id), FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (personaid) REFERENCES personas(id) FOREIGN KEY (personaid) REFERENCES personas(id) ON UPDATE CASCADE ON DELETE CASCADE
); );
INSERT OR IGNORE INTO users (id, handle, name, about, uuid) VALUES ('0', 'SYSTEM', 'SYSTEM', 'SYSTEM', 'SYSTEM'); INSERT OR IGNORE INTO users (id, handle, name, about) VALUES ('0', 'SYSTEM', 'SYSTEM', 'SYSTEM');
"; ";
@ -62,19 +62,28 @@ class DataBase extends SQLite3 {
} }
function addUser($handle, $name, $about=NULL) { function addUser($handle, $name, $about=NULL) {
$uuid = $this->uuidGen(); $id = hexdec(uniqid());
$sql = "INSERT INTO users (handle, name, about, uuid) VALUES ('$handle', '$name', '$about', '$uuid')"; $sql = "INSERT INTO users (id, handle, name, about) VALUES ('$id', '$handle', '$name', '$about')";
$ret = $this->exec($sql); $ret = $this->exec($sql);
if(!$ret) { if(!$ret) {
die($this->lastErrorMsg()); die($this->lastErrorMsg());
} }
} }
function addPost($userid, $personaid, $contents) { function addPost($text, $userid=NULL, $personaid=NULL) {
$id = hexdec(uniqid());
$time = time(); $time = time();
$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());
}
}
$uuid = $this->uuidGen(); function addPersona($userid, $handle, $name, $about=NULL, $colour=NULL) {
$sql = "INSERT INTO posts (time, userid, personaid, text, uuid) values ('$time', '$userid', '$personaid', '$contents', '$uuid')"; $id = hexdec(uniqid());
$sql = "INSERT INTO personas (id, userid, handle, name, colour) VALUES ('$id', '$userid', '$handle', '$about', '$colour');";
$ret = $this->exec($sql); $ret = $this->exec($sql);
if(!$ret) { if(!$ret) {
die($this->lastErrorMsg()); die($this->lastErrorMsg());
@ -92,7 +101,7 @@ class DataBase extends SQLite3 {
function passwordVerify($userid, $password) { function passwordVerify($userid, $password) {
$sql = "SELECT pass FROM users WHERE id='$userid';"; $sql = "SELECT pass FROM users WHERE id='$userid';";
$ret = $this->query($sql)->fetchArray(); $ret = $this->query($sql)->fetchArray(SQLITE3_NUM);
if(!$ret) { if(!$ret) {
return false; return false;
} }
@ -100,8 +109,15 @@ class DataBase extends SQLite3 {
return password_verify($password, $dbhash); return password_verify($password, $dbhash);
} }
function uuidGen() { function getPosts($userid=NULL, $personaid = NULL) {
return base64_encode(random_bytes(12)); $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;
} }
} }
?> ?>

View file

@ -1,20 +1,23 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8" />
<link rel="stylesheet" href="style.css" />
<title>Snuffler</title> <title>Snuffler</title>
</head> </head>
<body> <body>
<form> <div id="centerbox">
<textarea name="text" rows="5" placeholder="Whatcha snuffin' about?"></textarea><br /> <h1>Snuffler</h1>
<select id="user" name="user"> <form id="postform">
<option value=0>SYSTEM</option> <textarea id="postformtextarea" name="text" rows="5" placeholder="Whatcha snuffin' about?"></textarea><br />
<option value=1>User</option> <div id="postformactionrow">
</select> <select id="user" name="user">
<input type="submit" id="submit" name="submit" value="Snuff!" /> <option value=0>SYSTEM</option>
<h1>Snuffs</h1> <option value=1>User</option>
</body> </select>
</html> <input type="submit" id="submit" name="submit" value="Snuff!" />
</div>
</form>
<?php <?php
@ -25,6 +28,24 @@ $database = new DataBase();
if(!$database) { if(!$database) {
die($database->lastErrorMsg()); die($database->lastErrorMsg());
} }
//$database->addPost("Test post", 0);
$posts = $database->getPosts();
//var_dump($posts);
foreach($posts as $post) {
echo '<div class="post">';
echo '<strong>' . $post["user.name"] . '</strong>';
echo '<br><small>@' . $post["user.handle"] . '</small>';
echo '<p>' . $post["post.text"] . '</p>';
echo '<hr><small>' . date("D j.n.Y \k\l\o G:i", $post["post.time"]) . '</small>';
echo '<span class="postactions">5👍 0👎 2💬</span>';
echo "</div>";
}
$database->close(); $database->close();
?> ?>
</div>
</body>
</html>

33
style.css Normal file
View file

@ -0,0 +1,33 @@
body {
background-color: #000000;
color: #ffc000;
}
hr {
border-color: #584200;
}
#centerbox {
margin: auto;
max-width: 50em;
}
#postformtextarea {
box-sizing: border-box;
width: 100%;
}
#postformactionrow {
text-align: right;
}
.post {
border-style: solid;
border-width: 1px;
padding: .5em;
/*
margin: auto;
max-width: 50em;
*/
}
.postactions {
float: right;
}