Minor changes

This commit is contained in:
2025-04-30 23:11:40 +02:00
parent 3ec40a9e07
commit 926b86f5bb
2 changed files with 107 additions and 0 deletions

105
api.php Normal file
View File

@@ -0,0 +1,105 @@
<?php
$dbuser = "peehaitchpea";
$dbpass = "ballsdeep";
$dbname = "peehaitchpea";
error_reporting(E_ALL);
ini_set("display_errors", "On");
// -----------------------------------------------------
// COMMON FUNCTIONS
// -----------------------------------------------------
date_default_timezone_set("UTC");
function query($query, $callback = null, $exceptioncallback = null) {
global $conn;
try {
$result = $conn->query($query);
} catch(mysqli_sql_exception $e) {
if($exceptioncallback !== null) $exceptioncallback($e);
return;
}
if($result === false) {
echo("SQL ERROR: " . mysqli_error($conn));
} else if($result === true) {
if($callback !== null) $callback(null, 0);
} else if($callback !== null && $result->num_rows > 0) {
$id = 0;
while($row = $result->fetch_assoc()) $callback($row, $id++);
}
}
// -----------------------------------------------------
// SQL INITIALIZATION
// -----------------------------------------------------
try {
$conn = new mysqli("localhost", $dbuser, $dbpass, $dbname);
} catch(mysqli_sql_exception $e) {
if(isset($_GET["geterrormsg"]))
die("Connection failed: " . $e);
else
die("SQL error. Try again later.");
}
if($conn->connect_error) {
if(isset($_GET["geterrormsg"]))
die("Connection failed: " . $conn->connect_error);
else
die("SQL error. Try again later.");
}
// -----------------------------------------------------
// INITIAL SETUP - CREATE NECESSARY TABLES
// -----------------------------------------------------
$tables = [];
query("SHOW TABLES;", function($row) {
global $tables;
$tables[] = $row[array_key_first($row)];
});
if(!in_array("for_moderation", $tables)) {
query("CREATE TABLE `for_moderation` (
`id` int NOT NULL AUTO_INCREMENT,
`text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
}
if(!in_array("for_stallman", $tables)) {
query("CREATE TABLE `for_stallman` (
`id` int NOT NULL AUTO_INCREMENT,
`text` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`selected` boolean NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
}
$cmd = $_GET['cmd'];
switch($cmd) {
case "newmessage":
$arr = json_decode(file_get_contents("php://input"));
foreach ($arr as $value) {
query("INSERT INTO `for_moderation`(`text`) VALUES (\"" . $conn->real_escape_string($value) . "\");");
}
break;
case "update":
query("SELECT `text` FROM `for_stallman` WHERE `selected` = true LIMIT 1;", function($row) {
echo $row["text"];
});
break;
default:
die("Loading React...");
}
?>