<?php
/**
 * 密码哈希迁移脚本
 * 用于将管理员密码从明文迁移为 bcrypt 哈希
 *
 * 使用方式：
 * 1. 访问此脚本执行迁移
 * 2. 或者在命令行运行：php migrate_password_hash.php
 */

include("./includes/common.php");
include_once(ROOT."security_password.php");

$message = '';
$success = false;

if (isset($_GET['action']) && $_GET['action'] == 'migrate') {
    $admin_pwd = $conf['admin_pwd'] ?? '';

    if (empty($admin_pwd)) {
        $message = '管理员密码不存在';
    } elseif (is_password_hash($admin_pwd)) {
        $message = '管理员密码已经是哈希格式，无需迁移';
    } else {
        $new_hash = password_hash_bcrypt($admin_pwd);
        $result = $DB->exec("UPDATE pre_config SET v='$new_hash' WHERE k='admin_pwd'");

        if ($result) {
            $message = '迁移成功！密码已加密存储';
            $success = true;
        } else {
            $message = '迁移失败：' . $DB->error();
        }
    }
}

$current_pwd = $conf['admin_pwd'] ?? '';
$is_hashed = !empty($current_pwd) && is_password_hash($current_pwd);
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>密码迁移工具</title>
    <link href="https://s4.zstatic.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { background: #f5f5f5; padding-top: 50px; }
        .container { max-width: 600px; }
        .panel { margin-top: 20px; }
    </style>
</head>
<body>
<div class="container">
    <div class="panel panel-<?php echo $success ? 'success' : ($is_hashed ? 'info' : 'warning'); ?>">
        <div class="panel-heading">
            <h3 class="panel-title">密码哈希迁移工具</h3>
        </div>
        <div class="panel-body">
            <?php if ($message): ?>
            <div class="alert alert-<?php echo $success ? 'success' : 'info'; ?>">
                <?php echo htmlspecialchars($message); ?>
            </div>
            <?php endif; ?>

            <table class="table">
                <tr>
                    <td>当前状态：</td>
                    <td>
                        <?php if ($is_hashed): ?>
                            <span class="label label-success">已加密</span>
                        <?php else: ?>
                            <span class="label label-danger">明文存储</span>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td>密码格式：</td>
                    <td><?php echo $is_hashed ? 'BCRYPT' : 'PLAINTEXT'; ?></td>
                </tr>
                <tr>
                    <td>存储长度：</td>
                    <td><?php echo strlen($current_pwd); ?> 字符</td>
                </tr>
            </table>

            <?php if (!$is_hashed): ?>
            <a href="?action=migrate" class="btn btn-warning btn-lg btn-block"
               onclick="return confirm('确定要将管理员密码迁移为哈希格式吗？此操作不可逆。');">
                <i class="fa fa-shield"></i> 立即迁移为哈希密码
            </a>
            <?php else: ?>
            <div class="alert alert-success">
                <i class="fa fa-check-circle"></i> 密码已安全存储，无需迁移
            </div>
            <?php endif; ?>
        </div>
    </div>

    <div class="text-center text-muted" style="margin-top: 20px;">
        <p>BCRYPT 算法自动加盐，无需手动处理</p>
    </div>
</div>
</body>
</html>
