<?
session_start();
include("functions.php");

if (count(getTerrainsList()) - 1 < $_SESSION['mapSvg']) {
    
    $img = $_POST['txt'];
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $fileData = base64_decode($img);
    //saving
    file_put_contents("resources/map/img.png", $fileData);
    
    echo "DONE";
}
else if ($_SESSION['mapSvg'] == 0){
    file_put_contents("resources/map/regionContour", $_POST['txt']);
    
    $_SESSION['mapSvg']++;

    header("Location:generateMapSvg.php");
}
else {
    file_put_contents("resources/map/" . $_SESSION['mapSvg'], $_POST['txt']);
    
    $_SESSION['mapSvg']++;

    header("Location:generateMapSvg.php");
}

exit;
?>