You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
6.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* 邮件服务器管理工具 - 单文件版
* 用法: php admin_tool.php <命令> [参数...]
*/
// 加载配置和类
require_once __DIR__ . '/../config/database.php';
class AdminTool
{
private $db;
public function __construct()
{
$config = require __DIR__ . '/../config/database.php';
$dsn = "mysql:host={$config['host']};port={$config['port']};dbname={$config['database']}";
$this->db = new PDO($dsn, $config['username'], $config['password'], $config['options']);
}
// 主入口
public function run($args)
{
if (count($args) < 2) {
$this->showHelp();
return;
}
$command = $args[1];
switch ($command) {
case 'list':
$this->listUsers();
break;
case 'create':
$this->createUser($args);
break;
case 'enable':
case 'disable':
$this->toggleUser($args, $command === 'enable');
break;
case 'delete':
$this->deleteUser($args);
break;
case 'help':
default:
$this->showHelp();
}
}
// 显示用户列表
private function listUsers()
{
$stmt = $this->db->query("
SELECT id, username, is_admin, is_active, max_mailbox_size, created_at
FROM users ORDER BY id
");
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($users)) {
echo "暂无用户\n";
return;
}
echo "📋 用户列表 (" . count($users) . " 位)\n";
echo str_repeat("=", 70) . "\n";
foreach ($users as $user) {
printf("ID: %-4d | 邮箱: %-25s | 类型: %-6s | 状态: %-4s | 容量: %dMB | 创建: %s\n",
$user['id'],
$user['username'],
$user['is_admin'] ? '管理员' : '普通',
$user['is_active'] ? '激活' : '禁用',
$user['max_mailbox_size'],
$user['created_at']
);
}
}
// 创建用户
private function createUser($args)
{
if (count($args) < 4) {
echo "❌ 用法: php admin_tool.php create <邮箱> <密码> [管理员=0] [容量=100]\n";
echo " 示例: php admin_tool.php create user@test.com pass123 1 200\n";
return;
}
$username = $args[2];
$password = $args[3];
$isAdmin = isset($args[4]) ? (bool)$args[4] : false;
$mailboxSize = isset($args[5]) ? (int)$args[5] : 100;
// 检查用户是否存在
$check = $this->db->prepare("SELECT COUNT(*) FROM users WHERE username = ?");
$check->execute([$username]);
if ($check->fetchColumn() > 0) {
echo "❌ 用户 {$username} 已存在\n";
return;
}
// 创建用户
$stmt = $this->db->prepare("
INSERT INTO users (username, password_hash, is_admin, max_mailbox_size)
VALUES (?, ?, ?, ?)
");
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$success = $stmt->execute([$username, $hashedPassword, $isAdmin ? 1 : 0, $mailboxSize]);
if ($success) {
echo "✅ 用户创建成功: {$username}\n";
echo " 类型: " . ($isAdmin ? "管理员" : "普通用户") . "\n";
echo " 邮箱容量: {$mailboxSize}MB\n";
} else {
echo "❌ 创建失败\n";
}
}
// 启用/禁用用户
private function toggleUser($args, $enable)
{
if (count($args) < 3) {
echo "❌ 用法: php admin_tool.php " . ($enable ? "enable" : "disable") . " <用户ID>\n";
echo " 示例: php admin_tool.php " . ($enable ? "enable" : "disable") . " 2\n";
return;
}
$userId = (int)$args[2];
$status = $enable ? 1 : 0;
$action = $enable ? "启用" : "禁用";
$stmt = $this->db->prepare("UPDATE users SET is_active = ? WHERE id = ?");
$success = $stmt->execute([$status, $userId]);
if ($success && $stmt->rowCount() > 0) {
echo "✅ 用户 ID {$userId}{$action}\n";
} else {
echo "❌ 操作失败(用户可能不存在)\n";
}
}
// 删除用户
private function deleteUser($args)
{
if (count($args) < 3) {
echo " 用法: php admin_tool.php delete <用户ID>\n";
echo " 示例: php admin_tool.php delete 2\n";
return;
}
$userId = (int)$args[2];
echo " 确认删除用户 ID {$userId}(y/N): ";
$confirm = trim(fgets(STDIN));
if (strtolower($confirm) !== 'y') {
echo "操作已取消\n";
return;
}
$stmt = $this->db->prepare("DELETE FROM users WHERE id = ?");
$success = $stmt->execute([$userId]);
if ($success && $stmt->rowCount() > 0) {
echo "用户 ID {$userId} 已删除\n";
} else {
echo "删除失败(用户可能不存在)\n";
}
}
// 显示帮助
private function showHelp()
{
echo "邮件服务器管理工具\n";
echo str_repeat("=", 40) . "\n";
echo "命令列表:\n";
echo " list - 显示所有用户\n";
echo " create <邮箱> <密码> [管理员] [容量] - 创建用户\n";
echo " enable <用户ID> - 启用用户\n";
echo " disable <用户ID> - 禁用用户\n";
echo " delete <用户ID> - 删除用户\n";
echo " help - 显示此帮助\n";
echo "\n示例:\n";
echo " php admin_tool.php list\n";
echo " php admin_tool.php create user@test.com password123 0 200\n";
echo " php admin_tool.php disable 2\n";
echo " php admin_tool.php delete 3\n";
}
}
// 运行工具
$tool = new AdminTool();
$tool->run($argv);
?>