.navbar.scrolled {
background-color: rgba(255, 255, 255, 0.95) !important;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
';
// 导航栏
echo '';
// 为内容添加顶部内边距
echo '
';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username']);
$email = trim($_POST['email']);
$password = $_POST['password'];
if (empty($username) || empty($email) || empty($password)) {
$error = '用户名、邮箱和密码不能为空';
} elseif (empty($_POST['agree'])) {
$error = '必须同意 APP 审核标准才能注册';
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error = '请输入有效的邮箱地址';
} else {
// 检查数据库连接是否为 PDO 对象
if (!($conn instanceof mysqli)) {
log_error('数据库连接错误: 连接不是MySQLi实例', __FILE__, __LINE__);
$error = '数据库连接错误,请检查配置';
} else {
try {
$stmt = $conn->prepare('SELECT id FROM developers WHERE username = ? OR email = ?');
$stmt->bind_param('ss', $username, $email);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0) {
$error = '用户名或邮箱已被注册';
} else {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// 生成验证令牌
$verificationToken = bin2hex(random_bytes(32));
$insertStmt = $conn->prepare('INSERT INTO developers (username, email, password, verification_token) VALUES (?, ?, ?, ?)');
if (!$insertStmt) {
log_error('插入准备失败: ' . $conn->error, __FILE__, __LINE__);
$error = '系统错误,请稍后再试';
} else {
// 生成验证链接
$verificationLink = 'https://' . $_SERVER['HTTP_HOST'] . '/developer/verify_email.php?token=' . urlencode($verificationToken);
// 加载邮件模板
$templatePath = __DIR__ . '/../mail/verification_template.php';
if (file_exists($templatePath)) {
$templateContent = file_get_contents($templatePath);
$templateContent = str_replace('{username}', htmlspecialchars($username), $templateContent);
$templateContent = str_replace('{verification_link}', $verificationLink, $templateContent);
// 配置SMTP邮件发送
require_once '../vendor/phpmailer/phpmailer/src/PHPMailer.php';
require_once '../vendor/phpmailer/phpmailer/src/SMTP.php';
/** @var \PHPMailer\PHPMailer\PHPMailer $mail */
$mail = new \PHPMailer\PHPMailer\PHPMailer(true);
try {
$mail->isSMTP();
$mail->Host = defined('SMTP_HOST') ? SMTP_HOST : 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = defined('SMTP_USERNAME') ? SMTP_USERNAME : ''; // Ensure SMTP_USERNAME is defined in config.php
$mail->Password = defined('SMTP_PASSWORD') ? SMTP_PASSWORD : '';
$mail->SMTPSecure = defined('SMTP_ENCRYPTION') ? SMTP_ENCRYPTION : 'tls'; // Ensure SMTP_ENCRYPTION is defined in config.php
$mail->Port = defined('SMTP_PORT') ? SMTP_PORT : 587;
$mail->setFrom(defined('SMTP_FROM_EMAIL') ? SMTP_FROM_EMAIL : 'noreply@example.com', defined('SMTP_FROM_NAME') ? SMTP_FROM_NAME : 'App Store'); // Ensure SMTP_FROM_EMAIL is defined in config.php
$mail->addAddress($email, $username);
$mail->isHTML(true);
$mail->Subject = '邮箱验证 - ' . (defined('APP_STORE_NAME') ? APP_STORE_NAME : 'App Store');
$mail->Body = $templateContent;
$mail->send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
log_error('邮件发送失败: ' . $mail->ErrorInfo, __FILE__, __LINE__);
}
} else {
log_error('验证邮件模板不存在: ' . $templatePath, __FILE__, __LINE__);
}
header('Location: login.php?register_success=1&verify_email_sent=1');
exit;
}
}
} catch (PDOException $e) {
$error = '注册时发生错误,请稍后再试';
}
}
}
}
?>
开发者注册