38770-vm/api/admin_actions.php
2026-02-25 23:26:27 +00:00

63 lines
2.5 KiB
PHP

<?php
require_once __DIR__ . '/../db/config.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
die(json_encode(['success' => false, 'error' => 'Method not allowed']));
}
$action = $_POST['action'] ?? '';
try {
$db = db();
if ($action === 'add_url') {
$name = $_POST['name'] ?? '';
$url = $_POST['url'] ?? '';
if ($name && $url) {
$stmt = $db->prepare("INSERT INTO urls (name, url) VALUES (?, ?)");
$stmt->execute([$name, $url]);
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Missing parameters']);
}
} elseif ($action === 'delete_url') {
$id = $_POST['id'] ?? 0;
if ($id) {
$stmt = $db->prepare("DELETE FROM urls WHERE id = ?");
$stmt->execute([$id]);
echo json_encode(['success' => true]);
}
} elseif ($action === 'update_settings') {
$token = $_POST['telegram_bot_token'] ?? '';
$chat_id = $_POST['telegram_chat_id'] ?? '';
$stmt = $db->prepare("INSERT INTO settings (name, value) VALUES ('telegram_bot_token', ?) ON DUPLICATE KEY UPDATE value = ?");
$stmt->execute([$token, $token]);
$stmt = $db->prepare("INSERT INTO settings (name, value) VALUES ('telegram_chat_id', ?) ON DUPLICATE KEY UPDATE value = ?");
$stmt->execute([$chat_id, $chat_id]);
// Auto-register webhook for Telegram commands (/start, /status)
if (!empty($token)) {
$protocol = isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ? $_SERVER['HTTP_X_FORWARDED_PROTO'] : (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http");
$host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost');
$webhookUrl = "$protocol://$host/api/telegram_webhook.php";
$url = "https://api.telegram.org/bot$token/setWebhook?url=" . urlencode($webhookUrl);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_close($ch);
}
echo json_encode(['success' => true]);
} else {
echo json_encode(['success' => false, 'error' => 'Unknown action']);
}
} catch (Exception $e) {
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
}