<?php
// Workadventure cache destroyer

// URL rewrite rule calls us with the original filename in URL paramter "target"
if (!isset($_GET['target']))  {
    echo("No target");
    exit;
}
$target = $_GET["target"];

if (file_exists($target.".php"))   {
    // If a PHP file target.php exists: 
    //   Run that file and capture its output
    ob_start();
    include $target.".php";
    ob_end_flush();
}
else {
    // If no PHP file target.php exists:
    //   Load contents from target.json
    if (file_exists($target.".json")) 
        $data = file_get_contents($target.".json");
    else  {
        echo($target.".json"." not found.");
        exit;
    }
    
}

// Replace map exit names with timestamps: 
//   destination.php#start --> destination.number.php#start
$tstamp = round(microtime(true)*100);  # 0.01 sec resolution
$data = str_replace(".php#", ".".$tstamp.".php#", $data);  // Patch php-exits to prevent caching

header('Content-type: application/json');
echo($data);
exit;
?>