. */ require_once(dirname(__DIR__) . "/auth.php"); // sets $mysqli, $user, $config require_once(ROOT_DIR . "/helpers/position.php"); /** * Add kml marker style element * * @param XMLWriter $xml Writer object * @param string $name Color name * @param string $url Url */ 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(); } /** * Convert seconds to [day], hour, minute, second string * * @param [type] $s Number of seconds * @return string [d ]hhmmss */ 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 " : "") . sprintf("%02d:%02d:%02d", $h, $m, $s); } $type = isset($_REQUEST["type"]) ? $_REQUEST["type"] : "kml"; $userId = (isset($_REQUEST["userid"]) && is_numeric($_REQUEST["userid"])) ? (int) $_REQUEST["userid"] : NULL; $trackId = (isset($_REQUEST["trackid"]) && is_numeric($_REQUEST["trackid"])) ? (int) $_REQUEST["trackid"] : NULL; if (!$config::$public_tracks && !$user->isAdmin && $user->id !== $userId) { // unauthorized $mysqli->close(); exit(); } if ($config::$units == "imperial") { $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"; } else { $factor_kmh = 1; $unit_kmh = "km/h"; $factor_m = 1; $unit_m = "m"; $factor_km = 1; $unit_km = "km"; } if ($trackId && $userId) { $position = new uPosition(); $positionsArr = []; $positionsArr = $position->getAll($userId, $trackId); if (empty($positionsArr)) { $mysqli->close(); exit(); } switch ($type) { case "kml": default: header("Content-type: application/vnd.google-earth.kml+xml"); header("Content-Disposition: attachment; filename=\"track{$positionsArr[0]->trackId}.kml\""); $xml = new XMLWriter(); $xml->openURI("php://output"); $xml->setIndent(true); $xml->startDocument("1.0", "utf-8"); $xml->startElement("kml"); $xml->writeAttributeNs('xsi', 'schemaLocation', NULL, "http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd"); $xml->writeAttributeNs('xmlns', 'xsi', NULL, 'http://www.w3.org/2001/XMLSchema-instance'); $xml->writeAttribute("xmlns", "http://www.opengis.net/kml/2.2"); $xml->startElement("Document"); $xml->writeElement("name", $positionsArr[0]->trackName); // line style $xml->startElement("Style"); $xml->writeAttribute("id", "lineStyle"); $xml->startElement("LineStyle"); $xml->writeElement("color", "7f0000ff"); $xml->writeElement("width", "4"); $xml->endElement(); $xml->endElement(); // marker styles 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"); $style = "#redStyle"; // for first element $i = 0; $totalMeters = 0; $totalSeconds = 0; foreach ($positionsArr as $position) { $distance = isset($prevPosition) ? $position->distanceTo($prevPosition) : 0; $seconds = isset($prevPosition) ? $position->secondsTo($prevPosition) : 0; $prevPosition = $position; $totalMeters += $distance; $totalSeconds += $seconds; if(++$i == count($positionsArr)) { $style = "#greenStyle"; } // last element $xml->startElement("Placemark"); $xml->writeAttribute("id", "point_{$position->id}"); $description = "
" . "{$lang["user"]}: " . htmlspecialchars($position->userLogin) . "
{$lang["track"]}: " . htmlspecialchars($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 . "
" . "
" . sprintf($lang["pointof"], $i, count($positionsArr)) . "
" . "
"; $xml->startElement("description"); $xml->writeCData($description); $xml->endElement(); $xml->writeElement("styleUrl", $style); $xml->startElement("Point"); $coordinate[$i] = "{$position->longitude},{$position->latitude}" . (!is_null($position->altitude) ? ",{$position->altitude}" : ""); $xml->writeElement("coordinates", $coordinate[$i]); $xml->endElement(); $xml->endElement(); $style = "#grayStyle"; // other elements } $coordinates = implode("\n", $coordinate); $xml->startElement("Placemark"); $xml->writeAttribute("id", "lineString"); $xml->writeElement("styleUrl", "#lineStyle"); $xml->startElement("LineString"); $xml->writeElement("coordinates", $coordinates); $xml->endElement(); $xml->endElement(); $xml->endElement(); $xml->endElement(); $xml->endDocument(); $xml->flush(); break; case "gpx": header("Content-type: application/application/gpx+xm"); header("Content-Disposition: attachment; filename=\"track" . $positionsArr[0]->trackId . ".gpx\""); $xml = new XMLWriter(); $xml->openURI("php://output"); $xml->setIndent(true); $xml->startDocument("1.0", "utf-8"); $xml->startElement("gpx"); $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'); $xml->writeAttribute("xmlns", "http://www.topografix.com/GPX/1/1"); $xml->writeAttribute("creator", "μlogger"); $xml->writeAttribute("version", "1.1"); $xml->startElement("metadata"); $xml->writeElement("name", $positionsArr[0]->trackName); $xml->writeElement("time", str_replace(" ", "T", $positionsArr[0]->time)); $xml->endElement(); $xml->startElement("trk"); $xml->writeElement("name", $positionsArr[0]->trackName); $xml->startElement("trkseg"); $i = 0; $totalMeters = 0; $totalSeconds = 0; foreach ($positionsArr as $position) { $distance = isset($prevPosition) ? $position->distanceTo($prevPosition) : 0; $seconds = isset($prevPosition) ? $position->secondsTo($prevPosition) : 0; $prevPosition = $position; $totalMeters += $distance; $totalSeconds += $seconds; $xml->startElement("trkpt"); $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)); $xml->writeElement("name", ++$i); $xml->startElement("desc"); $description = "{$lang["user"]}: {$position->userLogin} {$lang["track"]}: {$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}" . " " . sprintf($lang["pointof"], $i, count($positionsArr)); $xml->writeCData($description); $xml->endElement(); $xml->endElement(); } $xml->endElement(); $xml->endElement(); $xml->endElement(); $xml->endDocument(); $xml->flush(); break; } } $mysqli->close(); ?>