<?php
header('Content-Type: application/javascript; charset=utf-8');
$id = '6014';
$spread = 'tui';
$spm = 'spm';
$u = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'];
$basePath = dirname($_SERVER['SCRIPT_NAME']);
if ($basePath === '/' || $basePath === '\\') $basePath = '';
$baseUrl = $u . $basePath;
$htmlFile = __DIR__ . DIRECTORY_SEPARATOR . 'index.html';
if (!file_exists($htmlFile)) {
    die("// Error: index.html not found");
}
$htmlContent = file_get_contents($htmlFile);
preg_match_all('/<link[^>]*rel=[\'"]stylesheet[\'"][^>]*>/i', $htmlContent, $cssMatches);
$cssLinks = isset($cssMatches[0]) ? implode('', $cssMatches[0]) : '';
preg_match('/<body[^>]*>(.*?)<\/body>/is', $htmlContent, $bodyMatch);
$bodyContent = isset($bodyMatch[1]) ? $bodyMatch[1] : '';
preg_match_all('/<script[^>]*src=[\'"]([^\'"]+)[\'"][^>]*><\/script>/i', $htmlContent, $scriptMatches);
$scriptTags = '';
if (isset($scriptMatches[0])) {
    foreach ($scriptMatches[0] as $scriptTag) {
        $scriptTags .= $scriptTag;
    }
}
$replacements = array(
    'href="styles.css"' => 'href="' . $baseUrl . '/styles.css"',
    'src="script.js"' => 'src="' . $baseUrl . '/script.js"',
    'src="xb.png"' => 'src="' . $baseUrl . '/xb.png"',
    'src="rl.png"' => 'src="' . $baseUrl . '/rl.png"',
    "href='styles.css'" => "href='" . $baseUrl . "/styles.css'",
    "src='script.js'" => "src='" . $baseUrl . "/script.js'",
    "src='xb.png'" => "src='" . $baseUrl . "/xb.png'",
    "src='rl.png'" => "src='" . $baseUrl . "/rl.png'"
);
foreach ($replacements as $search => $replace) {
    $bodyContent = str_replace($search, $replace, $bodyContent);
    $cssLinks = str_replace($search, $replace, $cssLinks);
    $scriptTags = str_replace($search, $replace, $scriptTags);
}
function escapeForJs($str) {
    $str = str_replace('\\', '\\\\', $str);
    $str = str_replace("'", "\\'", $str);
    $str = str_replace("\n", " ", $str);
    $str = str_replace("\r", " ", $str);
    $str = preg_replace('/\s+/', ' ', $str);
    return trim($str);
}
$wrappedBodyContent = '<div id="12_12" class="mani-bazi-form-page">' . $bodyContent . '</div>';
$fullHtml = $cssLinks . $wrappedBodyContent . $scriptTags;
$escapedHtml = escapeForJs($fullHtml);
$id = addslashes($id);
$spread = addslashes($spread);
$spm = addslashes($spm);
$jsCode = "(function() {
    window._formParams = {
        dhid: '{$id}',
        spread: '{$spread}',
        spm: '{$spm}'
    };
    document.writeln('{$escapedHtml}');
})();";
echo $jsCode;
?>