$value) { $trackid = substr("".$value,0, strpos("".$value, '_')); $startdate = date("d.m.Y H:i:s", $trackid/1000); $time = filemtime("".$value); $aFiles[$trackid] = array( 'start' => $trackid, 'end' => $time, 'file' => "".$value, 'startdate' => $startdate); $links[$trackid] = ''.$startdate.' (map) '. ''.$startdate.' (kml) '. ''.$startdate.' (robot)'; } ksort($aFiles); ksort($links); if(isset($_GET['js'])) { //header("Content-type: application/json; charset=utf-8"); header("Content-type: text/javascript; charset=utf-8"); header('Content-Disposition: inline"'); echo json_encode($aFiles); } else { echo implode($links, '
'); } exit(0); } // delivering KML history file if(isset($_GET['kml']) || isset($_GET['kmz']) || isset($_GET['robot'])) { if(empty($_GET['trackid'])) { header("HTTP/1.0 406 Not Acceptable"); echo 'empty trackid'; exit(0); } $trackid = intval($_GET['trackid']); if(empty($trackid)) { header("HTTP/1.0 406 Not Acceptable"); echo 'invalid trackid'; exit(0); } //$coordinates = file_get_contents("coordinates.log"); $coordinates = file($trackid.$filename_postfix); $lastupdate = filemtime($trackid.$filename_postfix); if(isset($_GET['robot'])) { header("Content-type: text/plain; charset=utf-8"); foreach ($coordinates as $idx => $coord) { // TODO: change from lat/long/time to direction/speed // http://www.movable-type.co.uk/scripts/latlong.html list($long, $lat, $alt, $accuracy, $altitudeAccuracy, $heading, $speed, $time, $tracking) = explode("\t", $coord); // cut out inaccuracies if($accuracy > $MIN_ACCURACY) { continue; } $speed = floatval($speed); $heading = floatval($heading); $lat = floatval($lat); $long = floatval($long); if(isset($last)) { $time -= $starttime; $time /= 1000.0; $lat1 = deg2rad($last['lat']); $lat2 = deg2rad($lat); $deltaLat = $lat2 - $lat1; $deltaLong = deg2rad($long - $last['long']); $a = sin($deltaLat/2) * sin($deltaLat/2) + cos($lat1)*cos($lat2)*sin($deltaLong/2)*sin($deltaLong/2); $c = 2 * atan2(sqrt($a), sqrt(1-$a)); $distance = 6371000 * $c; $deltaTime = ($time - $last['time']); if($deltaTime < 0.5) continue; $cspeed = $distance / $deltaTime; // m/s $lat2 = deg2rad($lat); $lat1 = deg2rad($last['lat']); $y = sin($deltaLong) * cos($lat2); $x = cos($lat1)*sin($lat2) - sin($lat1)*cos($lat2)*cos($deltaLong); $cheading = rad2deg(atan2($y, $x)); if($cheading < 0) $cheading += 360; } else { $cheading = 0; $cspeed = 0; $starttime = $time; $time = 0; } if($cspeed < 0.1) { // $cspeed = 0; $cheading = 0; } $last = array('lat' => $lat, 'long' => $long, 'time' => $time); $tracking = intval($tracking); //$speed *= 3.6; // km/h $output[] = "$time\t$tracking\t$heading\t$speed\t$cheading\t$cspeed"; } } else if(isset($_GET['kml']) || isset($_GET['kmz'])) { $output = []; foreach ($coordinates as $idx => $coord) { list($long, $lat, $alt, $accuracy, $altitudeAccuracy, $heading, $speed, $time, $tracking) = explode("\t", $coord); // cut out inaccuracies if($accuracy > $MIN_ACCURACY) { continue; } if(!intval($tracking)) { continue; } $lat = floatval($lat); $long = floatval($long); $alt = floatval($alt); $line = trim("$long,$lat,$alt"); // cut out duplicates if(count($output) > 0 && trim($output[count($output) - 1]) == $line) { continue; } $output[] = $line; } } if(empty($output)) { header("HTTP/1.0 404 Not Found"); echo 'no coordinates for this track'; exit(0); } if(isset($_GET['robot'])) { $output = implode("\n", $output); header("Content-type: text/plain; charset=utf-8"); header('Content-Disposition: inline"'); print $output; exit(0); } else if(isset($_GET['kml']) || isset($_GET['kmz'])) { // see: https://developers.google.com/kml/documentation/kmlreference $output = ' Tracking your movements Your movements Your Track #track'.$trackid.' relative '. implode("\n", $output) .' '; if(!isset($_GET['debug'])) { if(isset($_GET['kmz'])) { header("Content-type: application/vnd.google-earth.kmz"); header('Content-Disposition: attachment; filename="geotrail.kmz"'); } else { header("Content-type: application/vnd.google-earth.kml+xml; charset=utf-8"); header('Content-Disposition: attachment; filename="geotrail.kml"'); } } else { header("Content-type: text/plain; charset=utf-8"); header('Content-Disposition: inline"'); } } header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.0 header("Expires: Thu, 25 Sep 1984 00:00:00 GMT"); // Date in the past header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastupdate).' GMT', true, 200); header('Content-Length: '. strlen($output)); if(isset($_GET['kmz'])) { //print gzencode($output, 9); $file = tempnam("tmp", "zip"); $zip = new ZipArchive(); $zip->open($file, ZipArchive::OVERWRITE); // Stuff with content $zip->addFromString('doc.kml', $output); //$zip->addFile('file_on_server.ext', 'second_file_name_within_archive.ext'); // Close and send to users $zip->close(); /*header('Content-Type: application/zip'); header('Content-Length: ' . filesize($file)); header('Content-Disposition: attachment; filename="file.zip"'); */ readfile($file); unlink($file); } else { print $output; } /* // Getting headers sent by the client. $headers = apache_request_headers(); // Checking if the client is validating his cache and if it is current. if ( isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) == $lastupdate)) { // Client's cache IS current, so we just respond '304 Not Modified'. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastupdate).' GMT', true, 304); } else { // Image not cached or cache outdated, we respond '200 OK' and output the image. header('Last-Modified: '.gmdate('D, d M Y H:i:s', $lastupdate).' GMT', true, 200); header('Content-Length: '. strlen($kml)); if(!isset($_GET['debug'])) { header("Content-type: application/vnd.google-earth.kml+xml; charset=utf-8"); header('Content-Disposition: attachment; filename="geotrail.kml"'); } else { header("Content-type: text/plain; charset=utf-8"); header('Content-Disposition: inline"'); } print $kml; }*/ exit(0); } // getting coordinates via GET and saving them to file if(isset($_GET['pos'])) { if(empty($_GET['lat']) || empty($_GET['long']) || empty($_GET['time']) || empty($_GET['trackid'])) { header("HTTP/1.0 406 Not Acceptable"); echo 'empty params: lat, long, time, trackid'; exit(0); } $lat = floatval($_GET['lat']); $long = floatval($_GET['long']); $alt = floatval($_GET['alt']); $accuracy = floatval($_GET['accuracy']); $altitudeAccuracy = floatval($_GET['altitudeAccuracy']); $heading = floatval($_GET['heading']); $speed = floatval($_GET['speed']); $time = intval($_GET['time']); $tracking = intval($_GET['tracking']); $trackid = intval($_GET['trackid']); if(empty($lat) || empty($long) || empty($time) || empty($trackid) || $trackid > $time) { header("HTTP/1.0 406 Not Acceptable"); echo "invalid params: lat $lat, long $long, time $time, trackid $trackid"; exit(0); } if (file_put_contents($trackid.$filename_postfix, implode("\t", array($long,$lat,$alt,$accuracy,$altitudeAccuracy,$heading,$speed,$time,$tracking)). "\n", FILE_APPEND | LOCK_EX) === false) { header("HTTP/1.0 400 Bad Request"); echo 'write error'; } else { // only send "ok" if tracking is on and when there is an accurate position header("HTTP/1.0 200 OK"); echo 'OK'; } exit(0); } ?> Geo Tracking