snuffler-web/inc/database.php

64 lines
1.3 KiB
PHP
Raw Normal View History

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');
$sql = "
CREATE TABLE users (
ID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
HANDLE TEXT NOT NULL UNIQUE,
NAME TEXT NOT NULL,
ABOUT TEXT NOT NULL
);
CREATE TABLE posts (
ID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
TIME INTEGER NOT NULL,
USERID INTEGER NOT NULL,
TEXT TEXT NOT NULL
);
INSERT INTO users (ID, HANDLE, NAME, ABOUT) VALUES ('0', 'SYSTEM', 'SYSTEM', '');
";
$ret = $this->exec($sql);
if(!$ret) {
echo $this->lastErrorMsg();
} else {
echo "Created required tables\n";
}
echo "Initialized database\n";
}
function addUser($handle, $name, $about="") {
$sql = "INSERT INTO users (HANDLE, NAME, ABOUT) VALUES ('$handle', '$name', '$about')";
$ret = $this->exec($sql);
if(!$ret) {
echo $this->lastErrorMsg();
} else {
echo "User added\n";
}
}
function addPost($contents) {
$userid = 0;
$time = time();
$sql = "INSERT INTO posts (TIME, USERID, TEXT) values ('$time', '$userid', '$contents')";
$ret = $this->exec($sql);
if(!$ret) {
echo $this->lastErrorMsg();
} else {
echo "Post added\n";
}
}
}
?>