<?php
if(file_exists("../data/spotrep-".strtoupper(base64_decode($_GET['d'])).".txt")) {
	$dfile = "../data/spotrep-".strtoupper(base64_decode($_GET['d'])).".txt";
	$dcall = base64_decode($_GET['d']);
} else {
	$dfile = "../data.txt";
	$dcall = "ALL";
}
if (isset($_GET['ms'])) {
	echo md5_file($dfile);
	die();
}
?>
<!DOCTYPE html>
<html> <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>SpotRep2 Map - K9RCP</title>
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <style>

* { margin: 0; padding: 0; }
  #mapid { height: 100%; width: 100%; position: absolute !important; }
  body {
   height: 100%;
   overflow: hidden;
   width: 100%;
}
  </style>
  <!-- Google Font: Source Sans Pro -->
  <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
 <link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css"
   integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
   crossorigin=""/>
    <script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js"
   integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
   crossorigin=""></script>
</head>
<body>
    	 <div style="background-color: black;" id="mapid" width="600px" height="600px"></div>
         <script>
<?php
if(isset($_COOKIE['mapzoom']) && isset($_COOKIE['mapcenter'])) { ?>
var map = L.map('mapid').setView([<?php echo str_replace(array("LatLng(", ")"), "", $_COOKIE['mapcenter']); ?>], <?php echo $_COOKIE['mapzoom']; ?>);
<?php } else { ?>
var map = L.map('mapid').setView([45.290899, -122.976504], 8);

<?php } ?>

L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
    attribution: 'SpotRep Mapping by K9RCP', // Update this with your mapping attribution
    maxZoom: 18,
    id: 'mapbox/streets-v11',
    tileSize: 512,
    zoomOffset: -1,
    accessToken: '' // You will need to get a accessToken from your mapping provider.
}).addTo(map);

map.addEventListener('mousemove', function(ev) {
	document.cookie = "mapcenter="+map.getCenter();
	document.cookie = "mapzoom="+map.getZoom();
});


	var LeafIcon = L.Icon.extend({
		options: {
			iconSize:     [18, 18],
		}
	});



<?php

$rawdata = explode("\n", file_get_contents($dfile));

foreach ($rawdata as &$dataraw2) {
	if(strlen($dataraw2) > 3) {
		$data = explode("|", $dataraw2);
        	echo "L.marker([".str_replace("\n", "", explode("=", $data[5])[1]).", ".str_replace("\n", "", explode("=", $data[6])[1])."]).bindPopup('<b>Callsign: </b>".str_replace("\n", "", explode("=", $data[4])[1])." <br /><br />";
		echo "<b>Subject: </b>".str_replace("\n", "", explode("=", $data[2])[1])." <br />";

		for ($i = 5; $i < (count($data) - 0); $i++) {
			echo trim(str_replace(array("\n"."\r"), "", "<b>".htmlspecialchars(str_replace(array(":T", ":M"), "", explode("=", $data[$i])[0])).": </b>".htmlspecialchars(str_replace(array("'", ")", ",","(","\n"), "", explode("=", $data[$i])[1]))))."<br />";
		}
		echo "').addTo(map);\n";
	}
}
?>

var updateXml;

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer") {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        ro = new XMLHttpRequest();
    }
    return ro;
}
var xmlHttp = createRequestObject();
var lastapidata = null;
var lastimagedata = null;
function runapipull() {
    xmlHttp.open('get', '?d=<?php echo str_replace("=", "", base64_encode($dcall)); ?>&ms=' + new Date().getTime());
    xmlHttp.onreadystatechange = readapidata;
    xmlHttp.send(null);
}

function readapidata() {
    if (xmlHttp.readyState == 4) {
		data = xmlHttp.responseText;

		if (data != "<?php echo md5_file($dfile); ?>") {
			location.reload();
	        }
		clearInterval(updateXml);
		updateXml = setInterval( function(){ runapipull(); }, 5000 );
        //setTimeout("runapipull()", 15000);
    }
}
runapipull();
        </script>

      </div>


</body>
</html>