2013-06-19 13:27:14 +02:00
|
|
|
<?php
|
2017-01-30 21:36:44 +01:00
|
|
|
/* μlogger
|
2013-06-19 13:27:14 +02:00
|
|
|
*
|
2017-01-30 21:36:44 +01:00
|
|
|
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
|
2013-06-19 13:27:14 +02:00
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify it under
|
2017-04-07 00:05:28 +02:00
|
|
|
* the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2013-06-19 13:27:14 +02:00
|
|
|
* (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.
|
|
|
|
*
|
2017-04-07 00:05:28 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2013-06-19 13:27:14 +02:00
|
|
|
*/
|
2017-04-07 00:05:28 +02:00
|
|
|
|
2017-04-07 16:04:59 +02:00
|
|
|
require_once("auth.php"); // sets $mysqli, $user
|
|
|
|
require_once("helpers/position.php");
|
2013-06-19 13:27:14 +02:00
|
|
|
|
2017-04-07 16:04:59 +02:00
|
|
|
function addStyle($xml, $name, $url) {
|
|
|
|
$xml->startElement("Style");
|
|
|
|
$xml->writeAttribute("id", $name."Style");
|
|
|
|
$xml->startElement("IconStyle");
|
|
|
|
$xml->writeAttribute("id", $name."Icon");
|
|
|
|
$xml->startElement("Icon");
|
|
|
|
$xml->writeElement("href", $url);
|
|
|
|
$xml->endElement();
|
|
|
|
$xml->endElement();
|
|
|
|
$xml->endElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
function toHMS($s) {
|
|
|
|
$d = floor($s / 86400);
|
|
|
|
$h = floor(($s % 86400) / 3600);
|
|
|
|
$m = floor((($s % 86400) % 3600) / 60);
|
|
|
|
$s = (($s % 86400) % 3600) % 60;
|
|
|
|
return (($d > 0) ? ($d." d ") : "").(substr("00".$h, -2)).":".(substr("00".$m, -2)).":".(substr("00".$s, -2));
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = isset($_REQUEST["type"]) ? $_REQUEST["type"] : "kml";
|
|
|
|
$userId = (isset($_REQUEST["userid"]) && is_numeric($_REQUEST["userid"])) ? $_REQUEST["userid"] : NULL;
|
|
|
|
$trackId = (isset($_REQUEST["trackid"]) && is_numeric($_REQUEST["trackid"])) ? $_REQUEST["trackid"] : NULL;
|
|
|
|
|
|
|
|
if ($config::$units == "imperial") {
|
2013-06-19 13:27:14 +02:00
|
|
|
$factor_kmh = 0.62; //to mph
|
|
|
|
$unit_kmh = "mph";
|
|
|
|
$factor_m = 3.28; // to feet
|
|
|
|
$unit_m = "ft";
|
|
|
|
$factor_km = 0.62; // to miles
|
|
|
|
$unit_km = "mi";
|
2017-04-07 16:04:59 +02:00
|
|
|
} else {
|
2013-06-19 13:27:14 +02:00
|
|
|
$factor_kmh = 1;
|
|
|
|
$unit_kmh = "km/h";
|
|
|
|
$factor_m = 1;
|
|
|
|
$unit_m = "m";
|
|
|
|
$factor_km = 1;
|
|
|
|
$unit_km = "km";
|
|
|
|
}
|
|
|
|
|
2017-04-07 16:04:59 +02:00
|
|
|
if ($trackId && $userId) {
|
|
|
|
$position = new uPosition();
|
|
|
|
$positionsArr = [];
|
|
|
|
$positionsArr = $position->getAll($userId, $trackId);
|
|
|
|
if (empty($positionsArr)) {
|
|
|
|
$mysqli->close();
|
|
|
|
exit();
|
|
|
|
}
|
2013-06-19 13:27:14 +02:00
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case "kml":
|
|
|
|
default:
|
|
|
|
header("Content-type: application/vnd.google-earth.kml+xml");
|
2017-04-07 16:04:59 +02:00
|
|
|
header("Content-Disposition: attachment; filename=\"track" . $positionsArr[0]->trackId . ".kml\"");
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml = new XMLWriter();
|
|
|
|
$xml->openURI("php://output");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->setIndent(true);
|
|
|
|
$xml->startDocument("1.0", "utf-8");
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->startElement("kml");
|
|
|
|
$xml->writeAttribute("xmlns", "http://earth.google.com/kml/2.1");
|
|
|
|
$xml->startElement("Document");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->writeElement("name", $positionsArr[0]->trackName);
|
2013-06-19 13:27:14 +02:00
|
|
|
// line style
|
|
|
|
$xml->startElement("Style");
|
|
|
|
$xml->writeAttribute("id", "lineStyle");
|
|
|
|
$xml->startElement("LineStyle");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->writeElement("color", "7f0000ff");
|
|
|
|
$xml->writeElement("width", "4");
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->endElement();
|
|
|
|
$xml->endElement();
|
|
|
|
// marker styles
|
2017-04-07 16:04:59 +02:00
|
|
|
addStyle($xml, "red", "http://maps.google.com/mapfiles/markerA.png");
|
|
|
|
addStyle($xml, "green", "http://maps.google.com/mapfiles/marker_greenB.png");
|
|
|
|
addStyle($xml, "gray", "http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_gray.png");
|
2013-06-19 13:27:14 +02:00
|
|
|
$style = "#redStyle"; // for first element
|
|
|
|
$i = 0;
|
|
|
|
$totalMeters = 0;
|
|
|
|
$totalSeconds = 0;
|
2017-04-07 16:04:59 +02:00
|
|
|
foreach ($positionsArr as $position) {
|
|
|
|
$distance = isset($prevPosition) ? $position->distanceTo($prevPosition) : 0;
|
|
|
|
$seconds = isset($prevPosition) ? $position->secondsTo($prevPosition) : 0;
|
|
|
|
$prevPosition = $position;
|
2013-06-19 13:27:14 +02:00
|
|
|
$totalMeters += $distance;
|
|
|
|
$totalSeconds += $seconds;
|
2016-10-29 14:05:13 +02:00
|
|
|
|
2017-04-07 16:04:59 +02:00
|
|
|
if(++$i == count($positionsArr)) { $style = "#greenStyle"; } // last element
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->startElement("Placemark");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->writeAttribute("id", $position->id);
|
2016-10-29 14:05:13 +02:00
|
|
|
$description =
|
2017-04-07 16:04:59 +02:00
|
|
|
"<div style=\"font-weight: bolder;padding-bottom: 10px;border-bottom: 1px solid gray;\">".
|
2017-04-07 18:37:17 +02:00
|
|
|
$lang["user"].": ".strtoupper($position->userLogin)."<br />".$lang["track"].": ".strtoupper($position->trackName).
|
2013-06-19 13:27:14 +02:00
|
|
|
"</div>".
|
|
|
|
"<div>".
|
2017-04-07 18:37:17 +02:00
|
|
|
"<div style=\"padding-top: 10px;\"><b>".$lang["time"].":</b> ".$position->time."<br />".
|
|
|
|
(!is_null($position->speed) ? "<b>".$lang["speed"].":</b> ".round($position->speed * 3.6 * $factor_kmh, 2)." ".$unit_kmh."<br />" : "").
|
|
|
|
(!is_null($position->altitude) ? "<b>".$lang["altitude"].":</b> ".round($position->altitude * $factor_m)." ".$unit_m."<br />" : "").
|
|
|
|
"<b>".$lang["ttime"].":</b> ".toHMS($totalSeconds)."<br />".
|
|
|
|
"<b>".$lang["aspeed"].":</b> ".(($totalSeconds != 0) ? round($totalMeters / $totalSeconds * 3.6 * $factor_kmh, 2) : 0)." ".$unit_kmh."<br />".
|
|
|
|
"<b>".$lang["tdistance"].":</b> ".round($totalMeters / 1000 * $factor_km, 2)." ".$unit_km."<br />"."</div>".
|
|
|
|
"<div style=\"font-size: smaller;padding-top: 10px;\">".$lang["point"]." ".$i." ".$lang["of"]." ".count($positionsArr)."</div>".
|
2013-06-19 13:27:14 +02:00
|
|
|
"</div>";
|
|
|
|
$xml->startElement("description");
|
|
|
|
$xml->writeCData($description);
|
|
|
|
$xml->endElement();
|
|
|
|
$xml->writeElement("styleUrl", $style);
|
|
|
|
$xml->startElement("Point");
|
2017-04-07 16:04:59 +02:00
|
|
|
$coordinate[$i] = $position->longitude.",".$position->latitude.(!is_null($position->altitude) ? ",".$position->altitude : "");
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->writeElement("coordinates", $coordinate[$i]);
|
|
|
|
$xml->endElement();
|
2016-10-29 14:05:13 +02:00
|
|
|
$xml->endElement();
|
2013-06-19 13:27:14 +02:00
|
|
|
$style = "#grayStyle"; // other elements
|
|
|
|
}
|
2017-04-07 16:04:59 +02:00
|
|
|
$coordinates = implode("\n", $coordinate);
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->startElement("Placemark");
|
|
|
|
$xml->writeElement("styleUrl", "#lineStyle");
|
|
|
|
$xml->startElement("LineString");
|
|
|
|
$xml->writeElement("coordinates", $coordinates);
|
|
|
|
$xml->endElement();
|
2016-10-29 14:05:13 +02:00
|
|
|
$xml->endElement();
|
|
|
|
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->endElement();
|
|
|
|
$xml->endElement();
|
2016-10-29 14:05:13 +02:00
|
|
|
$xml->endDocument();
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->flush();
|
2016-10-29 14:05:13 +02:00
|
|
|
|
2013-06-19 13:27:14 +02:00
|
|
|
break;
|
2016-10-29 14:05:13 +02:00
|
|
|
|
2013-06-19 13:27:14 +02:00
|
|
|
case "gpx":
|
|
|
|
header("Content-type: application/application/gpx+xm");
|
2017-04-07 16:04:59 +02:00
|
|
|
header("Content-Disposition: attachment; filename=\"track" . $positionsArr[0]->trackId . ".gpx\"");
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml = new XMLWriter();
|
|
|
|
$xml->openURI("php://output");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->setIndent(true);
|
|
|
|
$xml->startDocument("1.0", "utf-8");
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->startElement("gpx");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->writeAttributeNs('xsi', 'schemaLocation', NULL, "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd");
|
|
|
|
$xml->writeAttributeNs('xmlns', 'xsi', NULL, 'http://www.w3.org/2001/XMLSchema-instance');
|
2016-10-29 14:05:13 +02:00
|
|
|
$xml->writeAttribute("xmlns", "http://www.topografix.com/GPX/1/1");
|
2017-01-30 21:36:44 +01:00
|
|
|
$xml->writeAttribute("creator", "μlogger");
|
2016-10-29 14:05:13 +02:00
|
|
|
$xml->writeAttribute("version", "1.1");
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->startElement("metadata");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->writeElement("name", $positionsArr[0]->trackName);
|
|
|
|
$xml->writeElement("time", str_replace(" ", "T", $positionsArr[0]->time));
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->endElement();
|
|
|
|
$xml->startElement("trk");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->writeElement("name", $positionsArr[0]->trackName);
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->startElement("trkseg");
|
|
|
|
$i = 0;
|
|
|
|
$totalMeters = 0;
|
|
|
|
$totalSeconds = 0;
|
2017-04-07 16:04:59 +02:00
|
|
|
foreach ($positionsArr as $position) {
|
|
|
|
$distance = isset($prevPosition) ? $position->distanceTo($prevPosition) : 0;
|
|
|
|
$seconds = isset($prevPosition) ? $position->secondsTo($prevPosition) : 0;
|
|
|
|
$prevPosition = $position;
|
2013-06-19 13:27:14 +02:00
|
|
|
$totalMeters += $distance;
|
2016-10-29 14:05:13 +02:00
|
|
|
$totalSeconds += $seconds;
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->startElement("trkpt");
|
2017-04-07 16:04:59 +02:00
|
|
|
$xml->writeAttribute("lat", $position->latitude);
|
|
|
|
$xml->writeAttribute("lon", $position->longitude);
|
|
|
|
if (!is_null($position->altitude)) { $xml->writeElement("ele", $position->altitude); }
|
|
|
|
$xml->writeElement("time", str_replace(" ", "T", $position->time));
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->writeElement("name", ++$i);
|
|
|
|
$xml->startElement("desc");
|
2016-10-29 14:05:13 +02:00
|
|
|
$description =
|
2017-04-07 18:37:17 +02:00
|
|
|
$lang["user"].": ".strtoupper($position->userLogin)." ".$lang["track"].": ".strtoupper($position->trackName).
|
|
|
|
" ".$lang["time"].": ".$position->time.
|
|
|
|
(!is_null($position->speed) ? " ".$lang["speed"].": ".round($position->speed * 3.6 * $factor_kmh, 2)." ".$unit_kmh : "").
|
|
|
|
(!is_null($position->altitude) ? " ".$lang["altitude"].": ".round($position->altitude * $factor_m)." ".$unit_m : "").
|
|
|
|
" ".$lang["ttime"].": ".toHMS($totalSeconds)."".
|
|
|
|
" ".$lang["aspeed"].": ".(($totalSeconds != 0) ? round($totalMeters / $totalSeconds * 3.6 * $factor_kmh, 2) : 0)." ".$unit_kmh.
|
|
|
|
" ".$lang["tdistance"].": ".round($totalMeters / 1000 * $factor_km, 2)." ".$unit_km.
|
|
|
|
" ".$lang["point"]." ".$i." ".$lang["of"]." ".count($positionsArr);
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->writeCData($description);
|
|
|
|
$xml->endElement();
|
|
|
|
$xml->endElement();
|
2016-10-29 14:05:13 +02:00
|
|
|
}
|
2013-06-19 13:27:14 +02:00
|
|
|
$xml->endElement();
|
|
|
|
$xml->endElement();
|
|
|
|
$xml->endElement();
|
2016-10-29 14:05:13 +02:00
|
|
|
$xml->endDocument();
|
|
|
|
$xml->flush();
|
|
|
|
|
|
|
|
break;
|
2013-06-19 13:27:14 +02:00
|
|
|
}
|
2017-04-07 16:04:59 +02:00
|
|
|
|
2013-06-19 13:27:14 +02:00
|
|
|
}
|
|
|
|
$mysqli->close();
|
|
|
|
?>
|