xaostube/public/grid.php

55 lines
1.6 KiB
PHP
Raw Normal View History

2022-06-15 22:34:33 +02:00
<?php
$db = new SQLite3('xaos.sqlite3');
$exists = $db->querySingle("SELECT TRUE FROM pragma_table_info('links')");
if (!$exists) {
$db->exec(<<<EOD
CREATE TABLE links(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
url TEXT NOT NULL,
audio BOOLEAN NOT NULL CHECK (audio IN (0, 1)),
2022-07-06 20:19:38 +02:00
video BOOLEAN NOT NULL CHECK (video IN (0, 1)),
ts NUMBER
2022-06-15 22:34:33 +02:00
);
EOD);
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$data = json_decode(file_get_contents('php://input'));
$url = $data->url;
$audio = $data->audio;
$video = $data->video;
if ($url !== null && $audio !== null && $video !== null) {
2022-07-06 20:19:38 +02:00
$stmt = $db->prepare('INSERT INTO links (url, audio, video, ts) VALUES (:url, :audio, :video, :ts);');
2022-06-15 22:34:33 +02:00
$stmt->bindValue(':url', $url);
$stmt->bindValue(':audio', $audio);
$stmt->bindValue(':video', $video);
2022-07-06 20:19:38 +02:00
$stmt->bindValue(':ts', time());
2022-06-15 22:34:33 +02:00
$result = $stmt->execute();
if (!$result) {
http_response_code(500);
die();
}
$result = $db->querySingle("SELECT last_insert_rowid()");
print_r($result);
} else {
http_response_code(400);
die();
}
} else {
$result = [];
$db_result = $db->query("SELECT * FROM links;");
while ($row = $db_result->fetchArray(SQLITE3_ASSOC)) {
$row["audio"] = $row["audio"] == 1;
$row["video"] = $row["video"] == 1;
array_push($result, $row);
}
header('Content-type: application/json; charset=utf-8');
echo json_encode($result);
}