<?php
/**
 * تست endpoint های مختلف API پنل 3x-ui
 * این فایل به شما کمک می‌کند endpoint صحیح را پیدا کنید
 */

require_once 'config.php';

$page_title = 'تست Endpoint های API';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $url = $_POST['url'] ?? '';
    $username = $_POST['username'] ?? '';
    $password = $_POST['password'] ?? '';
    
    if (empty($url) || empty($username) || empty($password)) {
        $error = 'لطفاً تمام فیلدها را پر کنید';
    } else {
        require_once 'api/XrayAPI.php';
        
        // تست endpoint های مختلف
        $endpoints_to_test = [
            'Login' => [
                '/login',
                '/xui/login',
                '/api/login',
                '/panel/login'
            ],
            'Get Inbounds' => [
                '/panel/inbound/list',
                '/xui/inbound/list',
                '/api/inbound/list',
                '/inbound/list',
                '/panel/api/inbound/list',
                '/xui/api/inbound/list'
            ],
            'System Stats' => [
                '/panel/system',
                '/xui/system',
                '/api/system',
                '/system',
                '/panel/api/system'
            ]
        ];
        
        $results = [];
        
        // تست login
        foreach ($endpoints_to_test['Login'] as $endpoint) {
            $ch = curl_init();
            curl_setopt_array($ch, [
                CURLOPT_URL => rtrim($url, '/') . $endpoint,
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => json_encode([
                    'username' => $username,
                    'password' => $password
                ]),
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTPHEADER => [
                    'Content-Type: application/json'
                ],
                CURLOPT_COOKIEJAR => sys_get_temp_dir() . '/test_cookie.txt',
                CURLOPT_COOKIEFILE => sys_get_temp_dir() . '/test_cookie.txt',
                CURLOPT_TIMEOUT => 15,
                CURLOPT_CONNECTTIMEOUT => 10,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_SSL_VERIFYHOST => 0
            ]);
            
            $response = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $error = curl_error($ch);
            curl_close($ch);
            
            $results['Login'][$endpoint] = [
                'http_code' => $httpCode,
                'success' => $httpCode === 200,
                'error' => $error,
                'response' => substr($response, 0, 200)
            ];
        }
        
        // اگر login موفق بود، تست endpoint های دیگر
        $login_success = false;
        $cookie_file = sys_get_temp_dir() . '/test_cookie.txt';
        
        foreach ($endpoints_to_test['Get Inbounds'] as $endpoint) {
            $ch = curl_init();
            curl_setopt_array($ch, [
                CURLOPT_URL => rtrim($url, '/') . $endpoint,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTPHEADER => [
                    'Content-Type: application/json'
                ],
                CURLOPT_COOKIEFILE => $cookie_file,
                CURLOPT_COOKIEJAR => $cookie_file,
                CURLOPT_TIMEOUT => 10,
                CURLOPT_CONNECTTIMEOUT => 5
            ]);
            
            $response = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $error = curl_error($ch);
            curl_close($ch);
            
            $results['Get Inbounds'][$endpoint] = [
                'http_code' => $httpCode,
                'success' => $httpCode === 200,
                'error' => $error,
                'response' => substr($response, 0, 200)
            ];
        }
        
        foreach ($endpoints_to_test['System Stats'] as $endpoint) {
            $ch = curl_init();
            curl_setopt_array($ch, [
                CURLOPT_URL => rtrim($url, '/') . $endpoint,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_HTTPHEADER => [
                    'Content-Type: application/json'
                ],
                CURLOPT_COOKIEFILE => $cookie_file,
                CURLOPT_COOKIEJAR => $cookie_file,
                CURLOPT_TIMEOUT => 10,
                CURLOPT_CONNECTTIMEOUT => 5
            ]);
            
            $response = curl_exec($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $error = curl_error($ch);
            curl_close($ch);
            
            $results['System Stats'][$endpoint] = [
                'http_code' => $httpCode,
                'success' => $httpCode === 200,
                'error' => $error,
                'response' => substr($response, 0, 200)
            ];
        }
    }
}

?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $page_title ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="card">
            <div class="card-header">
                <h4>تست Endpoint های API پنل 3x-ui</h4>
            </div>
            <div class="card-body">
                <?php if (isset($error)): ?>
                    <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
                <?php endif; ?>
                
                <form method="POST">
                    <div class="mb-3">
                        <label class="form-label">آدرس پنل (URL)</label>
                        <input type="url" name="url" class="form-control" value="<?= htmlspecialchars($_POST['url'] ?? '') ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">نام کاربری</label>
                        <input type="text" name="username" class="form-control" value="<?= htmlspecialchars($_POST['username'] ?? '') ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">رمز عبور</label>
                        <input type="password" name="password" class="form-control" required>
                    </div>
                    <button type="submit" class="btn btn-primary">تست Endpoint ها</button>
                </form>
                
                <?php if (isset($results)): ?>
                    <hr>
                    <h5>نتایج تست:</h5>
                    
                    <?php foreach ($results as $category => $endpoints): ?>
                        <h6 class="mt-4"><?= $category ?></h6>
                        <div class="table-responsive">
                            <table class="table table-sm table-bordered">
                                <thead>
                                    <tr>
                                        <th>Endpoint</th>
                                        <th>HTTP Code</th>
                                        <th>وضعیت</th>
                                        <th>پاسخ</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($endpoints as $endpoint => $result): ?>
                                        <tr class="<?= $result['success'] ? 'table-success' : 'table-danger' ?>">
                                            <td><code><?= htmlspecialchars($endpoint) ?></code></td>
                                            <td><?= $result['http_code'] ?></td>
                                            <td>
                                                <?php if ($result['success']): ?>
                                                    <span class="badge bg-success">✓ موفق</span>
                                                <?php else: ?>
                                                    <span class="badge bg-danger">✗ خطا</span>
                                                <?php endif; ?>
                                            </td>
                                            <td>
                                                <small>
                                                    <?php if ($result['error']): ?>
                                                        <span class="text-danger"><?= htmlspecialchars($result['error']) ?></span><br>
                                                    <?php endif; ?>
                                                    <?= htmlspecialchars($result['response']) ?>
                                                </small>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</body>
</html>
