From e2408fb49da8525b3689efd96e9203bd5bb65afb Mon Sep 17 00:00:00 2001 From: Jarkko Toivanen Date: Sat, 28 Sep 2024 01:01:51 +0300 Subject: [PATCH] Too many things I don't even remember :D --- inc/database.php | 60 ++++++++++++++++++++++++++++++------------------ index.php | 45 ++++++++++++++++++++++++++---------- style.css | 33 ++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 34 deletions(-) create mode 100644 style.css diff --git a/inc/database.php b/inc/database.php index 0f13124..678d634 100755 --- a/inc/database.php +++ b/inc/database.php @@ -6,11 +6,13 @@ if(count(get_included_files()) ==1) { class DataBase extends SQLite3 { function __construct() { $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 = " CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, - uuid TEXT UNIQUE, + id INTEGER PRIMARY KEY UNIQUE, pass TEXT, email TEXT UNIQUE, handle TEXT NOT NULL UNIQUE, @@ -19,41 +21,39 @@ class DataBase extends SQLite3 { ); CREATE TABLE IF NOT EXISTS personas ( - id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, - uuid TEXT UNIQUE, + id INTEGER PRIMARY KEY UNIQUE, userid INTEGER NOT NULL, handle TEXT NOT NULL, name TEXT NOT NULL, about TEXT, colour INTEGER, - FOREIGN KEY (userid) REFERENCES users(id), + FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE, UNIQUE (userid, handle) ); CREATE TABLE IF NOT EXISTS posts ( - id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, - uuid TEXT UNIQUE, + id INTEGER PRIMARY KEY UNIQUE, time INTEGER NOT NULL, userid INTEGER NOT NULL, - personaid INTEGER, + personaid INTEGER 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 ( - id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, - uuid TEXT UNIQUE, + id INTEGER PRIMARY KEY 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) + FOREIGN KEY (userid) REFERENCES users(id) ON UPDATE CASCADE ON DELETE CASCADE, + 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) { - $uuid = $this->uuidGen(); - $sql = "INSERT INTO users (handle, name, about, uuid) VALUES ('$handle', '$name', '$about', '$uuid')"; + $id = hexdec(uniqid()); + $sql = "INSERT INTO users (id, handle, name, about) VALUES ('$id', '$handle', '$name', '$about')"; $ret = $this->exec($sql); if(!$ret) { die($this->lastErrorMsg()); } } - function addPost($userid, $personaid, $contents) { + function addPost($text, $userid=NULL, $personaid=NULL) { + $id = hexdec(uniqid()); $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(); - $sql = "INSERT INTO posts (time, userid, personaid, text, uuid) values ('$time', '$userid', '$personaid', '$contents', '$uuid')"; + 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');"; $ret = $this->exec($sql); if(!$ret) { die($this->lastErrorMsg()); @@ -92,7 +101,7 @@ class DataBase extends SQLite3 { function passwordVerify($userid, $password) { $sql = "SELECT pass FROM users WHERE id='$userid';"; - $ret = $this->query($sql)->fetchArray(); + $ret = $this->query($sql)->fetchArray(SQLITE3_NUM); if(!$ret) { return false; } @@ -100,8 +109,15 @@ class DataBase extends SQLite3 { return password_verify($password, $dbhash); } - function uuidGen() { - return base64_encode(random_bytes(12)); + 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; } } ?> diff --git a/index.php b/index.php index b388092..47afd07 100755 --- a/index.php +++ b/index.php @@ -1,20 +1,23 @@ - + + Snuffler -
-
- - -

Snuffs

- - +
+

Snuffler

+ +
+
+ + +
+ lastErrorMsg()); } + +//$database->addPost("Test post", 0); + +$posts = $database->getPosts(); +//var_dump($posts); +foreach($posts as $post) { + echo '
'; + echo '' . $post["user.name"] . ''; + echo '
@' . $post["user.handle"] . ''; + echo '

' . $post["post.text"] . '

'; + echo '
' . date("D j.n.Y \k\l\o G:i", $post["post.time"]) . ''; + echo '5👍 0👎 2💬'; + echo "
"; + +} $database->close(); -?> \ No newline at end of file +?> +
+ + \ No newline at end of file diff --git a/style.css b/style.css new file mode 100644 index 0000000..f8bc043 --- /dev/null +++ b/style.css @@ -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; +} \ No newline at end of file