Start adding protocol v2
This commit is contained in:
parent
ced927c306
commit
faf1eeef5f
1 changed files with 73 additions and 38 deletions
|
@ -11,53 +11,88 @@ $jsx = json_decode(file_get_contents("php://input"), true);
|
|||
|
||||
// Get operation information
|
||||
// DISCLAIMER: All php code below this point is AI Generated
|
||||
function create($DB, $jsx, $blank=false) {
|
||||
// Get UUID from MySQL and insert into sessions table
|
||||
$result = $DB->query("SELECT UUID() AS id");
|
||||
if (!$result) {
|
||||
http_response_code(500);
|
||||
echo json_encode(["error" => "Failed to generate UUID"]);
|
||||
break;
|
||||
}
|
||||
|
||||
$row = $result->fetch_assoc();
|
||||
$sessionId = $row['id'];
|
||||
|
||||
// Insert into `sessions` table
|
||||
$stmt = $DB->prepare("INSERT INTO `sessions` (`ID`, `timestamp`, 'version') VALUES (?, CURRENT_TIMESTAMP(), 0)");
|
||||
$stmt->bind_param("s", $sessionId);
|
||||
if (!$stmt->execute()) {
|
||||
http_response_code(500);
|
||||
echo json_encode(["error" => "Failed to insert into sessions"]);
|
||||
break;
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
// Prepare data as JSON and insert into `data` table
|
||||
if($jsx['type'] == "json") {
|
||||
$data = json_encode($jsx['data']);
|
||||
}else if($jsx ['type'] == "nbt") {
|
||||
$data = base64_decode($jsx['data']);
|
||||
}
|
||||
|
||||
$stmt = $DB->prepare("INSERT INTO `data` (`ID`, `SessionData`) VALUES (?, ?)");
|
||||
$null = NULL; // Required for bind_param with blob
|
||||
$stmt->bind_param("sb", $sessionId, $null); // Temporarily bind $null for blob
|
||||
|
||||
// Send the actual blob content using send_long_data
|
||||
$stmt->send_long_data(1, $data); // Index 1 refers to the second "?" in bind_param
|
||||
if (!$stmt->execute()) {
|
||||
http_response_code(500);
|
||||
echo json_encode(["error" => "Failed to insert into data"]);
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
|
||||
return $sessionId;
|
||||
}
|
||||
switch($jsx['cmd']) {
|
||||
case "create": {
|
||||
// Get UUID from MySQL and insert into sessions table
|
||||
$result = $DB->query("SELECT UUID() AS id");
|
||||
if (!$result) {
|
||||
http_response_code(500);
|
||||
echo json_encode(["error" => "Failed to generate UUID"]);
|
||||
break;
|
||||
}
|
||||
|
||||
$row = $result->fetch_assoc();
|
||||
$sessionId = $row['id'];
|
||||
|
||||
// Insert into `sessions` table
|
||||
$stmt = $DB->prepare("INSERT INTO `sessions` (`ID`, `timestamp`) VALUES (?, CURRENT_TIMESTAMP())");
|
||||
$stmt->bind_param("s", $sessionId);
|
||||
if (!$stmt->execute()) {
|
||||
http_response_code(500);
|
||||
echo json_encode(["error" => "Failed to insert into sessions"]);
|
||||
break;
|
||||
}
|
||||
$stmt->close();
|
||||
|
||||
// Prepare data as JSON and insert into `data` table
|
||||
if($jsx['type'] == "json") {
|
||||
$data = json_encode($jsx['data']);
|
||||
}else if($jsx ['type'] == "nbt") {
|
||||
$data = base64_decode($jsx['data']);
|
||||
}
|
||||
|
||||
$stmt = $DB->prepare("INSERT INTO `data` (`ID`, `SessionData`) VALUES (?, ?)");
|
||||
$null = NULL; // Required for bind_param with blob
|
||||
$stmt->bind_param("sb", $sessionId, $null); // Temporarily bind $null for blob
|
||||
|
||||
// Send the actual blob content using send_long_data
|
||||
$stmt->send_long_data(1, $data); // Index 1 refers to the second "?" in bind_param
|
||||
if (!$stmt->execute()) {
|
||||
http_response_code(500);
|
||||
echo json_encode(["error" => "Failed to insert into data"]);
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt->close();
|
||||
$session = create($DB, $jsx, false);
|
||||
|
||||
echo json_encode(["status" => "ok", "session" => $sessionId]);
|
||||
break;
|
||||
}
|
||||
// Not AI Generated
|
||||
case "createv2": {
|
||||
$session = create($DB, $jsx, true);
|
||||
|
||||
echo json_encode(["status" => "created", "session" => $sessionId]);
|
||||
break;
|
||||
}
|
||||
// Not AI Generated
|
||||
case "get_version": {
|
||||
$sessionId = $jsx['id'];
|
||||
$stmt = $DB->prepare("SELECT `version` FROM `sessions` WHERE `ID` = ?");
|
||||
$stmt->bind_param("s", $sessionId);
|
||||
$stmt->execute();
|
||||
$stmt->store_result();
|
||||
|
||||
if($stmt->num_rows === 0) {
|
||||
http_response_code(418);
|
||||
echo json_encode({"error" => "Session not found"});
|
||||
$stmt->close();
|
||||
break;
|
||||
}
|
||||
|
||||
$stmt->bind_result($version);
|
||||
$stmt->fetch();
|
||||
$stmt->close();
|
||||
|
||||
echo json_encode({"status" => "version_back", "version" => $version});
|
||||
|
||||
break;
|
||||
}
|
||||
case "get": {
|
||||
$sessionId = $jsx['id'];
|
||||
|
||||
|
@ -68,7 +103,7 @@ switch($jsx['cmd']) {
|
|||
|
||||
if ($stmt->num_rows === 0) {
|
||||
http_response_code(404);
|
||||
echo json_encode(["error" => "Session not found"]);
|
||||
echo json_encode({"error" => "Session not found"});
|
||||
$stmt->close();
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue