snuffler-web/inc/database.php

52 lines
1.1 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,
HANDLE TEXT NOT NULL UNIQUE,
NAME TEXT NOT NULL,
ABOUT TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS posts (
ID INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,
TIME INTEGER NOT NULL,
USERID INTEGER NOT NULL,
TEXT TEXT NOT NULL
);
INSERT OR IGNORE INTO users (ID, HANDLE, NAME, ABOUT) VALUES ('0', 'SYSTEM', 'SYSTEM', '');
";
$ret = $this->exec($sql);
}
function addUser($handle, $name, $about="") {
$sql = "INSERT INTO users (HANDLE, NAME, ABOUT) VALUES ('$handle', '$name', '$about')";
$ret = $this->exec($sql);
if(!$ret) {
die($this->lastErrorMsg());
}
}
function addPost($contents) {
$userid = 0;
$time = time();
$sql = "INSERT INTO posts (TIME, USERID, TEXT) values ('$time', '$userid', '$contents')";
$ret = $this->exec($sql);
if(!$ret) {
die($this->lastErrorMsg());
}
}
}
?>