<?php
    set_magic_quotes_runtime(0);
    
    $errorResult = "{status:'failed',reason:'cannot open data file'}";
    $fh = fopen('data.json', 'a') or die($errorResult);
    flock($fh, LOCK_EX) or die($errorResult);
    
    $id = $_GET['id'];
    $label = $_GET['label'];
    $lat = $_GET['lat'];
    $lng = $_GET['lng'];
    $floor = $_GET['floor'];
    $description = $_GET['description'];
    $image = $_GET['image'];
    
    $functions = array();
    $c = 0;
    while ($_GET["function$c"] != "") {
        $functions[$c] = "'".($_GET["function$c"])."'";
        $c = $c + 1;
    }
    
    fwrite($fh, "'$id': { ");
    fwrite($fh, "'label': '$label', ");
    fwrite($fh, "'lat': $lat, ");
    fwrite($fh, "'lng': $lng, ");
    fwrite($fh, "'floor': $floor, ");
    fwrite($fh, "'functions': [ ".(join(", ", $functions))." ], ");
    fwrite($fh, "'description': '$description', ");
    fwrite($fh, "'image': '$image'");
    fwrite($fh, " },\n");
    
    fflush($fh) or die($errorResult);
    flock($fh, LOCK_UN) or die($errorResult);
    fclose($fh);
    
    echo("{status:'ok'}");
    echo($function);
?>