<?php
// ==========================================
// WHM API Configuration (আপনার তথ্য দিন)
// ==========================================
$whm_host  = '161.248.201.242'; // আপনার সার্ভারের IP বা Hostname (যেমন: vps1.fasthostbd.click)
$whm_user  = 'deshhost';          // WHM ইউজারনেম (root বা reseller username)
$whm_token = 'VDBCQLDJZ7Z4ZUJ9DJZO4SBVE043ZKV2'; // WHM থেকে জেনারেট করা API Token

$message = '';
$status = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $cpanel_user = trim($_POST['username'] ?? '');
    $old_pass    = $_POST['old_password'] ?? '';
    $new_pass    = $_POST['new_password'] ?? '';

    if (empty($cpanel_user) || empty($old_pass) || empty($new_pass)) {
        $status = 'error';
        $message = 'দয়া করে সব ফিল্ড সঠিকভাবে পূরণ করুন।';
    } else {
        // Step 1: পুরাতন পাসওয়ার্ড যাচাই (cPanel Login API)
        $login_url = "https://{$whm_host}:2083/login/?login_only=1";
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $login_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['user' => $cpanel_user, 'pass' => $old_pass]));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $login_response = curl_exec($ch);
        curl_close($ch);

        $login_data = json_decode($login_response, true);

        // যদি পুরাতন পাসওয়ার্ড সঠিক হয় (status = 1)
        if (isset($login_data['status']) && $login_data['status'] == 1) {
            
            // Step 2: WHM API দিয়ে নতুন পাসওয়ার্ড সেট করা
            $whm_api_url = "https://{$whm_host}:2087/json-api/passwd?api.version=1&user=" . urlencode($cpanel_user) . "&password=" . urlencode($new_pass);
            
            $ch2 = curl_init();
            curl_setopt($ch2, CURLOPT_URL, $whm_api_url);
            curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
                "Authorization: whm {$whm_user}:{$whm_token}"
            ));
            curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);
            $whm_response = curl_exec($ch2);
            curl_close($ch2);

            $whm_data = json_decode($whm_response, true);

            if (isset($whm_data['metadata']['result']) && $whm_data['metadata']['result'] == 1) {
                $status = 'success';
                $message = 'আপনার cPanel পাসওয়ার্ড সফলভাবে পরিবর্তন করা হয়েছে!';
            } else {
                $status = 'error';
                $message = 'WHM API Error: পাসওয়ার্ড পরিবর্তন করা যায়নি। ইউজারনেম চেক করুন।';
            }
        } else {
            // পুরাতন পাসওয়ার্ড ভুল হলে
            $status = 'error';
            $message = 'আপনার দেওয়া বর্তমান (পুরাতন) পাসওয়ার্ডটি ভুল!';
        }
    }
}
?>

<!DOCTYPE html>
<html lang="bn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>cPanel Password Change</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        body {
            background-color: #0f172a;
            background-image: radial-gradient(circle at top right, #1e293b, #0f172a);
            color: #f8fafc;
        }
        .glass-panel {
            background: rgba(30, 41, 59, 0.7);
            backdrop-filter: blur(12px);
            -webkit-backdrop-filter: blur(12px);
            border: 1px solid rgba(255, 255, 255, 0.1);
        }
        .input-glass {
            background: rgba(15, 23, 42, 0.6);
            border: 1px solid rgba(255, 255, 255, 0.1);
            color: white;
            transition: all 0.3s ease;
        }
        .input-glass:focus {
            border-color: #3b82f6;
            outline: none;
            box-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
        }
    </style>
</head>
<body class="min-h-screen flex items-center justify-center p-4">

    <div class="glass-panel w-full max-w-md p-8 rounded-2xl shadow-2xl">
        <div class="text-center mb-8">
            <h2 class="text-2xl font-bold text-blue-400">cPanel Password Update</h2>
            <p class="text-sm text-gray-400 mt-2">আপনার বর্তমান পাসওয়ার্ড দিয়ে নতুন পাসওয়ার্ড সেট করুন</p>
        </div>

        <?php if ($message): ?>
            <div class="p-4 mb-6 rounded-lg text-sm <?php echo $status === 'success' ? 'bg-green-500/20 text-green-300 border border-green-500/30' : 'bg-red-500/20 text-red-300 border border-red-500/30'; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>

        <form method="POST" action="">
            <!-- cPanel Username -->
            <div class="mb-5">
                <label class="block text-sm font-medium text-gray-300 mb-2">cPanel ইউজারনেম</label>
                <input type="text" name="username" required placeholder="ex: user123" 
                       class="input-glass w-full px-4 py-3 rounded-lg focus:ring-2 focus:ring-blue-500">
            </div>

            <!-- Old Password -->
            <div class="mb-5">
                <label class="block text-sm font-medium text-gray-300 mb-2">পুরাতন পাসওয়ার্ড</label>
                <input type="password" name="old_password" required placeholder="বর্তমান পাসওয়ার্ড দিন" 
                       class="input-glass w-full px-4 py-3 rounded-lg focus:ring-2 focus:ring-blue-500">
            </div>

            <!-- New Password -->
            <div class="mb-6">
                <label class="block text-sm font-medium text-gray-300 mb-2">নতুন পাসওয়ার্ড</label>
                <input type="password" name="new_password" required placeholder="নতুন শক্তিশালী পাসওয়ার্ড দিন" 
                       class="input-glass w-full px-4 py-3 rounded-lg focus:ring-2 focus:ring-blue-500">
            </div>

            <!-- Submit Button -->
            <button type="submit" 
                    class="w-full bg-blue-600 hover:bg-blue-500 text-white font-bold py-3 px-4 rounded-lg transition duration-300 shadow-[0_0_15px_rgba(37,99,235,0.5)] hover:shadow-[0_0_25px_rgba(37,99,235,0.7)]">
                পরিবর্তন করুন
            </button>
        </form>
    </div>

</body>
</html>