<?php
header('Content-Type: text/plain; charset=utf-8');

$o = array();
$o['php_version'] = PHP_VERSION;
$o['php_os'] = PHP_OS;
$o['sapi'] = php_sapi_name();
$o['document_root'] = $_SERVER['DOCUMENT_ROOT'] ?? '';
$o['script_filename'] = $_SERVER['SCRIPT_FILENAME'] ?? '';
$o['server_admin'] = $_SERVER['SERVER_ADMIN'] ?? '';
$o['server_software'] = $_SERVER['SERVER_SOFTWARE'] ?? '';
$o['open_basedir'] = ini_get('open_basedir') ?: '(none)';
$o['disable_functions'] = ini_get('disable_functions') ?: '(none)';

if (function_exists('posix_getuid')) {
    $o['uid'] = posix_getuid();
    $o['gid'] = posix_getgid();
    if (function_exists('posix_getpwuid')) {
        $pw = @posix_getpwuid($o['uid']);
        $o['user'] = $pw['name'] ?? '';
        $o['home'] = $pw['dir'] ?? '';
    }
}
if (!isset($o['uid']) && is_readable('/proc/self/status')) {
    foreach (file('/proc/self/status') as $line) {
        if (preg_match('/^(Uid|Gid):\s/', $line)) $o['proc'] = trim($line);
    }
}
$o['extensions'] = implode(',', get_loaded_extensions());
echo json_encode($o, JSON_PRETTY_PRINT) . "\n\n";

echo "== path checks (dir/read/write booleans) ==\n";
$dr = $_SERVER['DOCUMENT_ROOT'] ?? '';
$paths = array('/tmp', '/var/tmp', $dr, dirname($dr), dirname($dr, 2), dirname($dr, 3));
foreach ($paths as $p) {
    printf("%s dir=%s read=%s write=%s\n", $p, is_dir($p) ? 'Y' : 'N', is_readable($p) ? 'Y' : 'N', is_writable($p) ? 'Y' : 'N');
}

echo "\n== home accounts ==\n";
$homes = glob('/home/*', GLOB_ONLYDIR) ?: array();
if (!$homes) {
    $up = dirname($dr);
    for ($i = 0; $i < 4 && $up && $up !== '/' && $i < 4; $i++) {
        $homes = glob($up . '/*', GLOB_ONLYDIR) ?: array();
        if (count($homes) > 3) break;
        $up = dirname($up);
    }
}
foreach ($homes as $h) {
    printf("%s dir=%s read=%s write=%s\n", $h, is_dir($h) ? 'Y' : 'N', is_readable($h) ? 'Y' : 'N', is_writable($h) ? 'Y' : 'N');
}

echo "\n== docroot children per account (names only) ==\n";
$allDirs = array();
foreach ($homes as $h) {
    $ph = $h . '/public_html';
    if (!is_dir($ph)) { echo "$ph: no public_html\n"; continue; }
    printf("%s read=%s write=%s\n", $ph, is_readable($ph) ? 'Y' : 'N', is_writable($ph) ? 'Y' : 'N');
    $kids = glob($ph . '/*', GLOB_ONLYDIR) ?: array();
    echo "  children: " . implode(',', array_map('basename', $kids)) . "\n";
    foreach ($kids as $k) $allDirs[] = $k;
}

echo "\n== own-app config existence booleans ==\n";
foreach (glob($dr . '/*.php') ?: array() as $f) echo "  " . basename($f) . "\n";
foreach (array('.env', 'config.php', 'db.php', 'database.php', 'includes/config.php', 'config/database.php') as $c) {
    $p = $dr . '/' . $c;
    printf("%s exists=%s\n", $p, file_exists($p) ? 'Y' : 'N');
}

echo "\n== sensitive-file existence booleans across accounts (NO contents read) ==\n";
$cands = array();
foreach ($allDirs as $d) {
    $cands[] = $d . '/.env';
    $cands[] = $d . '/config/database.php';
    $cands[] = $d . '/database/stayhub.sql';
    $cands[] = $d . '/config.php';
}
foreach ($homes as $h) $cands[] = $h . '/public_html/.env';
$seen = array();
$hits = 0;
foreach ($cands as $c) {
    if (isset($seen[$c])) continue;
    $seen[$c] = 1;
    if (file_exists($c)) {
        printf("%s exists=%s readable=%s\n", $c, 'Y', is_readable($c) ? 'Y' : 'N');
        $hits++;
    }
}
echo "sensitive-file hit count: $hits\n";

echo "\n== single non-secret cross-vhost read proof ==\n";
foreach ($allDirs as $d) {
    $b = basename($d);
    if ($b === 'bojasspmscom' || $b === 'jasspmscom') {
        $logo = $d . '/assets/imgs/logo.png';
        if (is_readable($logo)) {
            $fh = @fopen($logo, 'rb');
            if ($fh) {
                $magic = bin2hex(fread($fh, 8));
                fclose($fh);
                printf("%s: READ OK first_bytes=%s size=%d\n", $logo, $magic, filesize($logo));
            }
        } else {
            printf("%s: not readable\n", $logo);
        }
    }
}
echo "\ndone\n";