diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..709ae90 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +nohup.out + diff --git a/api.php b/api.php new file mode 100644 index 0000000..e9f9db8 --- /dev/null +++ b/api.php @@ -0,0 +1,105 @@ +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..."); + } +?> +