<?php
/*
    Copyright (C) 2016 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

$title = "Web Application";
$headerJavascriptLinks = array("javascript/leaflet.js");
$headerStylesheetLinks = array("css/leaflet.css");

if(array_key_exists("mapFile", $_POST)){
	// upload new map to webservice
	if($_FILES["mapFile"]["error"] != UPLOAD_ERR_NO_FILE){
		// do some checks before using the uploaded file
		$error = false;
		switch($_FILES["mapFile"]["error"]){
			case UPLOAD_ERR_OK:
				break;
			case UPLOAD_ERR_NO_FILE:
				$errorToDisplay["title"] = "No file sent.";
				$error = true;
				break;
			case UPLOAD_ERR_INI_SIZE:
			case UPLOAD_ERR_FORM_SIZE:
				$errorToDisplay["title"] = "Exceeded filesize limit.";
				$error = true;
			default:
				$errorToDisplay["title"] = "Unknown errors.";
				$error = true;
		}
		$mapFile = sys_get_temp_dir() ."/map.mbtiles";
		if(!move_uploaded_file($_FILES["mapFile"]["tmp_name"], $mapFile)){
			$errorToDisplay["title"] = "Upload failed";
		}
		else{
			// send file to the webservice
			$upload_url = '../webservice/';
			$params = array(
				'map'=>"@$mapFile"
			);  

			$ch = curl_init();
			curl_setopt($ch, CURLOPT_VERBOSE, 1);
			curl_setopt($ch, CURLOPT_URL, $upload_url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
			$response = curl_exec($ch);
			curl_close($ch);
		}
	}
}

require_once("inc/header.php");
?>
	<h2 id="title" class="backgroundblue">GNUBoat</h2>
	<div id="display">
		<div id="speedthroughwater">
			<h2>STW</h2>
			<span id="value" class="important">-</span> kts
		</div>
		<div id="speedoverground">
			<h2>SOG</h2>
			<span id="value" class="important">-</span> kts
		</div>
		<div id="waterdepth">
			<h2>Depth</h2>
			<span id="value">-</span> m
		</div>
		<div id="watertemperature">
			<h2>Water</h2>
			<span id="value">-</span>&#176;C
		</div>
		<div id="gpsfix">
			<h2>Fix</h2>
			<span id="value">-</span>
		</div>
	</div>
	<div id="map"></div>
	<form method="post" action="./" enctype="multipart/form-data">
		<div class="formTable">
			<p>
				<label>Upload Map (mbtiles format):</label><br/>
				<input class="file" type="file" name="mapFile" value=""/>
			</p>
		</div>
		<div class="formButtons">
			<input type="submit" class="button buttonSubmit" name="buttonSendMap" value="Upload"/>
		</div>
	</form>
	<div id="shutdown">
		<p id="shutdownMessage"/>
		<form>
			<button type="button" id="shutdownButton">SHUTDOWN</button>
		</form>
	</div>
<?php
require_once("inc/footer.php");
?>
