central-ssh/api.php
2025-06-30 17:09:10 +00:00

71 lines
1.6 KiB
PHP

<?php
// Configuration
$password = "SuperSecretPassword"; // Change this!
$appendFile = "authorized_keys.txt";
// Get posted data
$inputText = $_POST['text'] ?? null;
$inputPassword = $_POST['password'] ?? null;
$deleteLine = isset($_POST['delete_line']) ? intval($_POST['delete_line']) : null;
if (!$inputPassword) {
http_response_code(400);
echo "Missing 'password' parameter.";
exit;
}
// Validate password
if ($inputPassword !== $password) {
http_response_code(401);
echo "Invalid password.";
exit;
}
// Handle deletion
if ($deleteLine !== null) {
if (!file_exists($appendFile)) {
http_response_code(404);
echo "File not found.";
exit;
}
$lines = file($appendFile, FILE_IGNORE_NEW_LINES);
if ($deleteLine < 0 || $deleteLine >= count($lines)) {
http_response_code(400);
echo "Invalid line number.";
exit;
}
// Remove specified line
array_splice($lines, $deleteLine, 1);
// Save back to file
$result = file_put_contents($appendFile, implode(PHP_EOL, $lines) . PHP_EOL);
if ($result === false) {
http_response_code(500);
echo "Failed to update file.";
exit;
}
echo "Line deleted successfully.";
exit;
}
// Handle appending
if (!$inputText) {
http_response_code(400);
echo "Missing 'text' parameter for appending.";
exit;
}
$fileHandle = fopen($appendFile, "a");
if ($fileHandle === false) {
http_response_code(500);
echo "Failed to open file.";
exit;
}
fwrite($fileHandle, $inputText . PHP_EOL);
fclose($fileHandle);
echo "Text appended successfully.";