\n";
if (file_put_contents($tokenFile, $content)) {
chmod($tokenFile, 0600);
header('Location: config_add.php?pass=' . urlencode($apiPass));
exit;
} else {
$setupError = '保存失败,请检查目录权限!';
}
}
}
// 如果需要设置密码,显示设置页面
if ($needSetup) {
?>n
初始设置 - VPS Hub
❌
💡 提示:此密码用于保护配置管理页面的访问权限。
密码要求:至少8位,只能包含字母(a-z,A-Z)和数字(0-9)
403, 'msg' => '访问密码错误'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
exit;
}
// 处理添加配置
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['add_config'])) {
$apiLabel = trim($_POST['api_label']);
$siteType = trim($_POST['site_type']);
$siteUrl = trim($_POST['site_url']);
$account = trim($_POST['account']);
$apiKey = trim($_POST['api_key']);
$autoMonitor = isset($_POST['auto_monitor']) ? 1 : 0;
if (empty($apiLabel) || empty($siteType) || empty($siteUrl) || empty($account) || empty($apiKey)) {
$message = '❌ API标识、网站类型、网站链接、账户和密钥不能为空!
';
} else {
// 验证URL格式:不允许包含路径
$parsedUrl = parse_url($siteUrl);
if (!$parsedUrl || !isset($parsedUrl['scheme']) || !isset($parsedUrl['host'])) {
$message = '❌ 网站链接格式错误!请输入完整的URL(例如: https://www.example.com)
';
} elseif (isset($parsedUrl['path']) && $parsedUrl['path'] !== '/') {
$message = '❌ 网站链接严禁包含路径部分!只允许输入根域名(例如: https://www.example.com),不要添加 /v1、/api 等路径
';
} else {
$db = getVpsDB();
// 检查API标识是否已存在
$existingConfig = $db->queryOne('SELECT id FROM configs WHERE api_label = ?', [$apiLabel]);
if ($existingConfig) {
$message = '❌ API标识 "' . htmlspecialchars($apiLabel) . '" 已存在,请使用其他标识!
';
} else {
$configId = $db->insert(
'INSERT INTO configs (api_label, site_type, site_url, account, api_key, auto_monitor) VALUES (?, ?, ?, ?, ?, ?)',
[$apiLabel, $siteType, $siteUrl, $account, $apiKey, $autoMonitor]
);
if ($configId) {
$message = '✅ 配置添加成功!ID: ' . $configId . '
';
// 如果是首次添加配置,提示运行install.sh
$configCount = $db->queryOne('SELECT COUNT(*) as count FROM configs')['count'];
if ($configCount == 1) {
$message .= '💡 这是第一个配置,请运行 app/install.sh 安装监控服务
';
// 尝试自动执行install.sh(需要PHP有执行权限)
$installScript = __DIR__ . '/app/install.sh';
if (file_exists($installScript)) {
// 检查exec函数是否可用
if (!function_exists('exec')) {
$message .= '⚠️ exec函数被禁用,无法自动安装监控服务。请手动执行: sudo bash app/install.sh
';
} else {
// 先设置可执行权限
chmod($installScript, 0755);
// 执行安装脚本
$output = [];
$returnVar = 0;
exec("bash {$installScript} 2>&1", $output, $returnVar);
if ($returnVar === 0) {
$message .= '✅ 监控服务已自动安装并启动
';
} else {
$message .= '⚠️ 自动安装失败,请手动执行: sudo bash app/install.sh
';
// 输出错误信息以便调试
if (!empty($output)) {
$message .= '查看错误详情
' . htmlspecialchars(implode("\n", $output)) . ' ';
}
}
}
}
}
// 自动刷新VPS列表
if ($siteType === 'mofang') {
refreshVpsListForConfig($configId);
$message .= '✅ 已自动获取VPS列表
';
}
} else {
$message = '❌ 配置添加失败!
';
}
}
}
}
} elseif (isset($_POST['delete_config'])) {
$configId = intval($_POST['config_id']);
$db = getVpsDB();
$db->execute('DELETE FROM configs WHERE id = ?', [$configId]);
$message = '✅ 配置已删除
';
}
}
// 获取所有配置
$db = getVpsDB();
$configs = $db->query('SELECT * FROM configs ORDER BY id');
?>
配置管理 - VPS Hub