52 lines
1.5 KiB
PHP
52 lines
1.5 KiB
PHP
<?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)),
|
|
video BOOLEAN NOT NULL CHECK (video IN (0, 1))
|
|
);
|
|
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) {
|
|
$stmt = $db->prepare('INSERT INTO links (url, audio, video) VALUES (:url, :audio, :video);');
|
|
$stmt->bindValue(':url', $url);
|
|
$stmt->bindValue(':audio', $audio);
|
|
$stmt->bindValue(':video', $video);
|
|
$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);
|
|
}
|