parent
75308793ba
commit
fe37b27513
@ -0,0 +1,240 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../config/database.php';
|
||||
require_once __DIR__ . '/../src/storage/Database.php';
|
||||
require_once __DIR__ . '/../src/storage/EmailRepository.php';
|
||||
require_once __DIR__ . '/../src/utils/Security.php';
|
||||
|
||||
session_start();
|
||||
|
||||
// 身份验证
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
$emailRepo = new EmailRepository();
|
||||
$message = '';
|
||||
$error = '';
|
||||
|
||||
// 处理删除邮件
|
||||
if (isset($_GET['delete'])) {
|
||||
$emailId = (int)$_GET['delete'];
|
||||
if ($emailRepo->delete($emailId)) {
|
||||
$message = "邮件删除成功";
|
||||
} else {
|
||||
$error = "删除失败";
|
||||
}
|
||||
}
|
||||
|
||||
// 处理标记已读
|
||||
if (isset($_GET['mark_read'])) {
|
||||
$emailId = (int)$_GET['mark_read'];
|
||||
if ($emailRepo->markAsRead($emailId)) {
|
||||
$message = "邮件已标记为已读";
|
||||
}
|
||||
}
|
||||
|
||||
// 获取邮件列表
|
||||
$isAdmin = $_SESSION['is_admin'] ?? false;
|
||||
$userId = $_SESSION['user_id'];
|
||||
|
||||
// 分页参数
|
||||
$page = isset($_GET['page']) ? max(1, (int)$_GET['page']) : 1;
|
||||
$perPage = 20;
|
||||
$offset = ($page - 1) * $perPage;
|
||||
|
||||
// 获取邮件
|
||||
if ($isAdmin) {
|
||||
$emails = $emailRepo->getAll($perPage, $offset);
|
||||
$totalEmails = $emailRepo->getCount();
|
||||
} else {
|
||||
$emails = $emailRepo->getInbox($userId, $perPage, $offset);
|
||||
$totalEmails = $emailRepo->getCount($userId);
|
||||
}
|
||||
|
||||
$totalPages = ceil($totalEmails / $perPage);
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>邮件管理 - 邮件服务器</title>
|
||||
<meta charset="UTF-8">
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background: #f5f5f5; }
|
||||
.header { background: #007bff; color: white; padding: 15px; margin: -20px -20px 20px -20px; }
|
||||
.menu { background: white; padding: 10px; margin-bottom: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
||||
.menu a { margin-right: 15px; text-decoration: none; color: #007bff; }
|
||||
.container { background: white; padding: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
||||
.message { background: #d4edda; color: #155724; padding: 12px; border-radius: 5px; margin-bottom: 20px; }
|
||||
.error { background: #f8d7da; color: #721c24; padding: 12px; border-radius: 5px; margin-bottom: 20px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
|
||||
th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
|
||||
th { background: #f8f9fa; font-weight: 600; }
|
||||
tr:hover { background: #f8f9fa; }
|
||||
.btn { padding: 6px 12px; border: none; border-radius: 4px; cursor: pointer; text-decoration: none; display: inline-block; }
|
||||
.btn-primary { background: #007bff; color: white; }
|
||||
.btn-danger { background: #dc3545; color: white; }
|
||||
.btn-success { background: #28a745; color: white; }
|
||||
.btn-small { padding: 4px 8px; font-size: 12px; }
|
||||
.badge { padding: 4px 8px; border-radius: 3px; font-size: 12px; font-weight: 500; }
|
||||
.badge-read { background: #6c757d; color: white; }
|
||||
.badge-unread { background: #007bff; color: white; }
|
||||
.email-unread { font-weight: bold; }
|
||||
.pagination { margin-top: 20px; text-align: center; }
|
||||
.pagination a { display: inline-block; padding: 8px 12px; margin: 0 4px; text-decoration: none; border: 1px solid #ddd; border-radius: 4px; }
|
||||
.pagination a:hover { background: #f8f9fa; }
|
||||
.pagination .current { background: #007bff; color: white; border-color: #007bff; }
|
||||
.email-preview { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); overflow: auto; }
|
||||
.modal-content { background: white; margin: 50px auto; padding: 20px; width: 80%; max-width: 800px; border-radius: 5px; }
|
||||
.close { float: right; font-size: 28px; font-weight: bold; cursor: pointer; }
|
||||
.email-body { white-space: pre-wrap; background: #f8f9fa; padding: 15px; border-radius: 5px; margin-top: 10px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<h1>邮件服务器管理后台</h1>
|
||||
<div>欢迎, <?php echo htmlspecialchars($_SESSION['username']); ?>
|
||||
(<a href="logout.php" style="color: white;">退出</a>)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="menu">
|
||||
<a href="index.php">仪表盘</a>
|
||||
<?php if ($isAdmin): ?>
|
||||
<a href="users.php">用户管理</a>
|
||||
<?php endif; ?>
|
||||
<a href="emails.php">邮件管理</a>
|
||||
<a href="filters.php">过滤规则</a>
|
||||
<a href="logs.php">系统日志</a>
|
||||
<?php if ($isAdmin): ?>
|
||||
<a href="settings.php">系统设置</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<h2>邮件管理 <?php if ($isAdmin): ?>(全部邮件)<?php else: ?>(我的收件箱)<?php endif; ?></h2>
|
||||
|
||||
<?php if ($message): ?>
|
||||
<div class="message"><?php echo $message; ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($error): ?>
|
||||
<div class="error"><?php echo $error; ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<p>共 <?php echo $totalEmails; ?> 封邮件</p>
|
||||
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>发件人</th>
|
||||
<th>收件人</th>
|
||||
<th>主题</th>
|
||||
<th>状态</th>
|
||||
<th>时间</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php if (empty($emails)): ?>
|
||||
<tr>
|
||||
<td colspan="7" style="text-align: center; padding: 40px;">
|
||||
暂无邮件
|
||||
</td>
|
||||
</tr>
|
||||
<?php else: ?>
|
||||
<?php foreach ($emails as $email): ?>
|
||||
<tr class="<?php echo $email['is_read'] ? '' : 'email-unread'; ?>">
|
||||
<td><?php echo $email['id']; ?></td>
|
||||
<td><?php echo htmlspecialchars($email['sender_name'] ?? $email['sender'] ?? '未知'); ?></td>
|
||||
<td><?php echo htmlspecialchars($email['recipient_name'] ?? $email['recipient'] ?? '未知'); ?></td>
|
||||
<td class="email-preview">
|
||||
<a href="#" onclick="viewEmail(<?php echo htmlspecialchars(json_encode($email)); ?>); return false;">
|
||||
<?php echo htmlspecialchars($email['subject'] ?? '(无主题)'); ?>
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
<?php if ($email['is_read']): ?>
|
||||
<span class="badge badge-read">已读</span>
|
||||
<?php else: ?>
|
||||
<span class="badge badge-unread">未读</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo $email['created_at']; ?></td>
|
||||
<td>
|
||||
<a href="#" onclick="viewEmail(<?php echo htmlspecialchars(json_encode($email)); ?>); return false;" class="btn btn-primary btn-small">查看</a>
|
||||
<?php if (!$email['is_read']): ?>
|
||||
<a href="?mark_read=<?php echo $email['id']; ?>" class="btn btn-success btn-small">标记已读</a>
|
||||
<?php endif; ?>
|
||||
<a href="?delete=<?php echo $email['id']; ?>" class="btn btn-danger btn-small" onclick="return confirm('确定要删除此邮件吗?');">删除</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<!-- 分页 -->
|
||||
<?php if ($totalPages > 1): ?>
|
||||
<div class="pagination">
|
||||
<?php if ($page > 1): ?>
|
||||
<a href="?page=<?php echo $page - 1; ?>">上一页</a>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php for ($i = 1; $i <= $totalPages; $i++): ?>
|
||||
<?php if ($i == $page): ?>
|
||||
<span class="current"><?php echo $i; ?></span>
|
||||
<?php else: ?>
|
||||
<a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
|
||||
<?php endif; ?>
|
||||
<?php endfor; ?>
|
||||
|
||||
<?php if ($page < $totalPages): ?>
|
||||
<a href="?page=<?php echo $page + 1; ?>">下一页</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<!-- 查看邮件模态框 -->
|
||||
<div id="emailModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<span class="close" onclick="closeEmailModal()">×</span>
|
||||
<h3 id="email-subject">邮件详情</h3>
|
||||
<div>
|
||||
<strong>发件人:</strong><span id="email-sender"></span><br>
|
||||
<strong>收件人:</strong><span id="email-recipient"></span><br>
|
||||
<strong>时间:</strong><span id="email-time"></span><br>
|
||||
<strong>主题:</strong><span id="email-subject-text"></span>
|
||||
</div>
|
||||
<div class="email-body" id="email-body"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function viewEmail(email) {
|
||||
document.getElementById('email-subject').textContent = email.subject || '(无主题)';
|
||||
document.getElementById('email-subject-text').textContent = email.subject || '(无主题)';
|
||||
document.getElementById('email-sender').textContent = email.sender_name || email.sender || '未知';
|
||||
document.getElementById('email-recipient').textContent = email.recipient_name || email.recipient || '未知';
|
||||
document.getElementById('email-time').textContent = email.created_at;
|
||||
document.getElementById('email-body').textContent = email.body || '(无内容)';
|
||||
document.getElementById('emailModal').style.display = 'block';
|
||||
}
|
||||
|
||||
function closeEmailModal() {
|
||||
document.getElementById('emailModal').style.display = 'none';
|
||||
}
|
||||
|
||||
window.onclick = function(event) {
|
||||
var modal = document.getElementById('emailModal');
|
||||
if (event.target == modal) {
|
||||
closeEmailModal();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
// 清除所有会话数据
|
||||
$_SESSION = array();
|
||||
|
||||
// 销毁会话cookie
|
||||
if (isset($_COOKIE[session_name()])) {
|
||||
setcookie(session_name(), '', time()-3600, '/');
|
||||
}
|
||||
|
||||
// 销毁会话
|
||||
session_destroy();
|
||||
|
||||
// 重定向到登录页面
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
|
||||
@ -0,0 +1,292 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../config/database.php';
|
||||
require_once __DIR__ . '/../src/storage/Database.php';
|
||||
require_once __DIR__ . '/../src/storage/UserRepository.php';
|
||||
require_once __DIR__ . '/../src/utils/Validator.php';
|
||||
require_once __DIR__ . '/../src/utils/Security.php';
|
||||
|
||||
session_start();
|
||||
|
||||
// 身份验证
|
||||
if (!isset($_SESSION['user_id'])) {
|
||||
header('Location: index.php');
|
||||
exit;
|
||||
}
|
||||
|
||||
// 检查管理员权限
|
||||
if (!$_SESSION['is_admin']) {
|
||||
die('权限不足:只有管理员可以访问此页面');
|
||||
}
|
||||
|
||||
$userRepo = new UserRepository();
|
||||
$message = '';
|
||||
$error = '';
|
||||
|
||||
// 处理创建用户
|
||||
if (isset($_POST['create_user'])) {
|
||||
$username = trim($_POST['username'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
$isAdmin = isset($_POST['is_admin']) ? 1 : 0;
|
||||
$isActive = isset($_POST['is_active']) ? 1 : 0;
|
||||
|
||||
$usernameValidation = Validator::validateUsername($username);
|
||||
if (!$usernameValidation['valid']) {
|
||||
$error = implode('<br>', $usernameValidation['errors']);
|
||||
} else {
|
||||
if (!Validator::validateEmailDomain($username, 'test.com')) {
|
||||
$error = "邮箱域名必须是 @test.com";
|
||||
} else {
|
||||
$passwordValidation = Validator::validatePassword($password, 6);
|
||||
if (!$passwordValidation['valid']) {
|
||||
$error = implode('<br>', $passwordValidation['errors']);
|
||||
} else {
|
||||
try {
|
||||
if ($userRepo->usernameExists($username)) {
|
||||
$error = "用户名已存在";
|
||||
} else {
|
||||
$userRepo->create($username, $password, $isAdmin, $isActive);
|
||||
$message = "用户创建成功";
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
$error = "创建失败: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理更新用户
|
||||
if (isset($_POST['update_user'])) {
|
||||
$userId = (int)$_POST['user_id'];
|
||||
$data = [];
|
||||
|
||||
if (!empty($_POST['new_password'])) {
|
||||
$passwordValidation = Validator::validatePassword($_POST['new_password'], 6);
|
||||
if (!$passwordValidation['valid']) {
|
||||
$error = implode('<br>', $passwordValidation['errors']);
|
||||
} else {
|
||||
$data['password'] = $_POST['new_password'];
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['is_admin'])) {
|
||||
$data['is_admin'] = (int)$_POST['is_admin'];
|
||||
}
|
||||
|
||||
if (isset($_POST['is_active'])) {
|
||||
$data['is_active'] = (int)$_POST['is_active'];
|
||||
}
|
||||
|
||||
if (empty($error) && !empty($data)) {
|
||||
if ($userRepo->update($userId, $data)) {
|
||||
$message = "用户更新成功";
|
||||
} else {
|
||||
$error = "更新失败";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理删除用户
|
||||
if (isset($_GET['delete'])) {
|
||||
$userId = (int)$_GET['delete'];
|
||||
if ($userId != $_SESSION['user_id']) { // 不能删除自己
|
||||
if ($userRepo->delete($userId)) {
|
||||
$message = "用户删除成功";
|
||||
} else {
|
||||
$error = "删除失败";
|
||||
}
|
||||
} else {
|
||||
$error = "不能删除自己的账号";
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有用户
|
||||
$users = $userRepo->getAll();
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>用户管理 - 邮件服务器</title>
|
||||
<meta charset="UTF-8">
|
||||
<style>
|
||||
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background: #f5f5f5; }
|
||||
.header { background: #007bff; color: white; padding: 15px; margin: -20px -20px 20px -20px; }
|
||||
.menu { background: white; padding: 10px; margin-bottom: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
||||
.menu a { margin-right: 15px; text-decoration: none; color: #007bff; }
|
||||
.container { background: white; padding: 20px; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
|
||||
.message { background: #d4edda; color: #155724; padding: 12px; border-radius: 5px; margin-bottom: 20px; }
|
||||
.error { background: #f8d7da; color: #721c24; padding: 12px; border-radius: 5px; margin-bottom: 20px; }
|
||||
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
|
||||
th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
|
||||
th { background: #f8f9fa; font-weight: 600; }
|
||||
tr:hover { background: #f8f9fa; }
|
||||
.btn { padding: 6px 12px; border: none; border-radius: 4px; cursor: pointer; text-decoration: none; display: inline-block; }
|
||||
.btn-primary { background: #007bff; color: white; }
|
||||
.btn-danger { background: #dc3545; color: white; }
|
||||
.btn-success { background: #28a745; color: white; }
|
||||
.btn-small { padding: 4px 8px; font-size: 12px; }
|
||||
.form-group { margin-bottom: 15px; }
|
||||
.form-group label { display: block; margin-bottom: 5px; font-weight: 500; }
|
||||
.form-group input, .form-group select { width: 100%; padding: 8px; border: 1px solid #ddd; border-radius: 4px; }
|
||||
.form-inline { display: flex; gap: 10px; align-items: flex-end; }
|
||||
.form-inline .form-group { flex: 1; margin-bottom: 0; }
|
||||
.badge { padding: 4px 8px; border-radius: 3px; font-size: 12px; font-weight: 500; }
|
||||
.badge-admin { background: #ffc107; color: #000; }
|
||||
.badge-active { background: #28a745; color: white; }
|
||||
.badge-inactive { background: #6c757d; color: white; }
|
||||
.modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); }
|
||||
.modal-content { background: white; margin: 50px auto; padding: 20px; width: 500px; border-radius: 5px; }
|
||||
.close { float: right; font-size: 28px; font-weight: bold; cursor: pointer; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<h1>邮件服务器管理后台</h1>
|
||||
<div>欢迎, <?php echo htmlspecialchars($_SESSION['username']); ?>
|
||||
(<a href="logout.php" style="color: white;">退出</a>)
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="menu">
|
||||
<a href="index.php">仪表盘</a>
|
||||
<a href="users.php">用户管理</a>
|
||||
<a href="emails.php">邮件管理</a>
|
||||
<a href="filters.php">过滤规则</a>
|
||||
<a href="logs.php">系统日志</a>
|
||||
<a href="settings.php">系统设置</a>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<h2>用户管理</h2>
|
||||
|
||||
<?php if ($message): ?>
|
||||
<div class="message"><?php echo $message; ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($error): ?>
|
||||
<div class="error"><?php echo $error; ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<!-- 创建用户表单 -->
|
||||
<h3>创建新用户</h3>
|
||||
<form method="POST" class="form-inline">
|
||||
<div class="form-group">
|
||||
<label>邮箱地址</label>
|
||||
<input type="email" name="username" placeholder="user@test.com" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>密码</label>
|
||||
<input type="password" name="password" placeholder="至少6个字符" required minlength="6">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>管理员</label>
|
||||
<input type="checkbox" name="is_admin" value="1">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>激活</label>
|
||||
<input type="checkbox" name="is_active" value="1" checked>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button type="submit" name="create_user" class="btn btn-primary">创建用户</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<!-- 用户列表 -->
|
||||
<h3>用户列表 (<?php echo count($users); ?>)</h3>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>用户名</th>
|
||||
<th>角色</th>
|
||||
<th>状态</th>
|
||||
<th>创建时间</th>
|
||||
<th>操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($users as $user): ?>
|
||||
<tr>
|
||||
<td><?php echo $user['id']; ?></td>
|
||||
<td><?php echo htmlspecialchars($user['username']); ?></td>
|
||||
<td>
|
||||
<?php if ($user['is_admin']): ?>
|
||||
<span class="badge badge-admin">管理员</span>
|
||||
<?php else: ?>
|
||||
<span>普通用户</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td>
|
||||
<?php if ($user['is_active']): ?>
|
||||
<span class="badge badge-active">激活</span>
|
||||
<?php else: ?>
|
||||
<span class="badge badge-inactive">禁用</span>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<td><?php echo $user['created_at']; ?></td>
|
||||
<td>
|
||||
<a href="#" onclick="editUser(<?php echo htmlspecialchars(json_encode($user)); ?>); return false;" class="btn btn-primary btn-small">编辑</a>
|
||||
<?php if ($user['id'] != $_SESSION['user_id']): ?>
|
||||
<a href="?delete=<?php echo $user['id']; ?>" class="btn btn-danger btn-small" onclick="return confirm('确定要删除此用户吗?');">删除</a>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- 编辑用户模态框 -->
|
||||
<div id="editModal" class="modal">
|
||||
<div class="modal-content">
|
||||
<span class="close" onclick="closeModal()">×</span>
|
||||
<h3>编辑用户</h3>
|
||||
<form method="POST">
|
||||
<input type="hidden" name="user_id" id="edit_user_id">
|
||||
<div class="form-group">
|
||||
<label>用户名</label>
|
||||
<input type="text" id="edit_username" readonly style="background: #f5f5f5;">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>新密码(留空则不修改)</label>
|
||||
<input type="password" name="new_password" placeholder="留空则不修改">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>
|
||||
<input type="checkbox" name="is_admin" id="edit_is_admin" value="1"> 管理员
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>
|
||||
<input type="checkbox" name="is_active" id="edit_is_active" value="1"> 激活
|
||||
</label>
|
||||
</div>
|
||||
<button type="submit" name="update_user" class="btn btn-success">保存</button>
|
||||
<button type="button" onclick="closeModal()" class="btn">取消</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function editUser(user) {
|
||||
document.getElementById('edit_user_id').value = user.id;
|
||||
document.getElementById('edit_username').value = user.username;
|
||||
document.getElementById('edit_is_admin').checked = user.is_admin == 1;
|
||||
document.getElementById('edit_is_active').checked = user.is_active == 1;
|
||||
document.getElementById('editModal').style.display = 'block';
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
document.getElementById('editModal').style.display = 'none';
|
||||
}
|
||||
|
||||
window.onclick = function(event) {
|
||||
var modal = document.getElementById('editModal');
|
||||
if (event.target == modal) {
|
||||
closeModal();
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* 测试用户注册功能
|
||||
* 用法: php scripts/test_register.php
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../config/database.php';
|
||||
require_once __DIR__ . '/../src/storage/Database.php';
|
||||
require_once __DIR__ . '/../src/storage/UserRepository.php';
|
||||
require_once __DIR__ . '/../src/utils/Validator.php';
|
||||
require_once __DIR__ . '/../src/utils/Security.php';
|
||||
|
||||
echo "=== 用户注册功能测试 ===\n\n";
|
||||
|
||||
try {
|
||||
$userRepo = new UserRepository();
|
||||
|
||||
// 测试1: 验证邮箱格式
|
||||
echo "测试1: 验证邮箱格式\n";
|
||||
$testEmails = [
|
||||
'valid@test.com' => true,
|
||||
'invalid-email' => false,
|
||||
'test@test.com' => true,
|
||||
'user@wrong.com' => false,
|
||||
];
|
||||
|
||||
foreach ($testEmails as $email => $expected) {
|
||||
$isValid = Validator::validateEmail($email);
|
||||
$domainValid = Validator::validateEmailDomain($email, 'test.com');
|
||||
$result = $isValid && ($expected ? $domainValid : !$domainValid);
|
||||
echo " {$email}: " . ($result ? "✓" : "✗") . "\n";
|
||||
}
|
||||
|
||||
// 测试2: 验证密码强度
|
||||
echo "\n测试2: 验证密码强度\n";
|
||||
$testPasswords = [
|
||||
'12345' => false, // 太短
|
||||
'123456' => true, // 符合最小长度
|
||||
'password123' => true,
|
||||
];
|
||||
|
||||
foreach ($testPasswords as $password => $expected) {
|
||||
$validation = Validator::validatePassword($password, 6);
|
||||
$result = $validation['valid'] === $expected;
|
||||
echo " '{$password}': " . ($result ? "✓" : "✗") . "\n";
|
||||
if (!$validation['valid']) {
|
||||
echo " 错误: " . implode(', ', $validation['errors']) . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// 测试3: 检查用户名是否存在
|
||||
echo "\n测试3: 检查用户名是否存在\n";
|
||||
$existingUser = $userRepo->findByUsername('admin@test.com');
|
||||
if ($existingUser) {
|
||||
echo " admin@test.com 存在: ✓\n";
|
||||
} else {
|
||||
echo " admin@test.com 不存在: ✗\n";
|
||||
}
|
||||
|
||||
// 测试4: 创建测试用户(如果不存在)
|
||||
echo "\n测试4: 创建测试用户\n";
|
||||
$testUsername = 'testuser@test.com';
|
||||
|
||||
if ($userRepo->usernameExists($testUsername)) {
|
||||
echo " 测试用户已存在,跳过创建\n";
|
||||
} else {
|
||||
try {
|
||||
$newUser = $userRepo->create($testUsername, 'test123456', false, true);
|
||||
echo " 创建用户成功: ✓\n";
|
||||
echo " 用户ID: {$newUser['id']}\n";
|
||||
echo " 用户名: {$newUser['username']}\n";
|
||||
echo " 是否管理员: " . ($newUser['is_admin'] ? '是' : '否') . "\n";
|
||||
} catch (Exception $e) {
|
||||
echo " 创建用户失败: ✗ - " . $e->getMessage() . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// 测试5: 验证密码
|
||||
echo "\n测试5: 验证密码\n";
|
||||
$testUser = $userRepo->findByUsername($testUsername);
|
||||
if ($testUser) {
|
||||
$verified = $userRepo->verifyPassword($testUsername, 'test123456');
|
||||
if ($verified) {
|
||||
echo " 密码验证成功: ✓\n";
|
||||
} else {
|
||||
echo " 密码验证失败: ✗\n";
|
||||
}
|
||||
}
|
||||
|
||||
// 测试6: 获取所有用户
|
||||
echo "\n测试6: 获取用户列表\n";
|
||||
$users = $userRepo->getAll(10);
|
||||
echo " 用户总数: " . count($users) . "\n";
|
||||
foreach ($users as $user) {
|
||||
echo " - {$user['username']} (ID: {$user['id']}, " .
|
||||
($user['is_admin'] ? '管理员' : '普通用户') . ", " .
|
||||
($user['is_active'] ? '激活' : '禁用') . ")\n";
|
||||
}
|
||||
|
||||
echo "\n=== 测试完成 ===\n";
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo "错误: " . $e->getMessage() . "\n";
|
||||
echo "堆栈跟踪:\n" . $e->getTraceAsString() . "\n";
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue