ulogger-server/utils/import.php

134 lines
4.7 KiB
PHP
Raw Normal View History

2017-05-09 09:06:23 +02:00
<?php
/* μlogger
*
* Copyright(C) 2017 Bartek Fabiszewski (www.fabiszewski.net)
*
* This 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/>.
*/
2017-08-25 13:59:19 +02:00
require_once(dirname(__DIR__) . "/helpers/auth.php");
2017-05-09 09:06:23 +02:00
require_once(ROOT_DIR . "/helpers/track.php");
require_once(ROOT_DIR . "/helpers/position.php");
2017-05-09 15:25:16 +02:00
require_once(ROOT_DIR . "/helpers/utils.php");
2019-02-25 10:04:09 +01:00
require_once(ROOT_DIR . "/helpers/config.php");
require_once(ROOT_DIR . "/helpers/lang.php");
2017-08-25 13:59:19 +02:00
$auth = new uAuth();
2017-05-09 15:25:16 +02:00
2019-02-25 10:04:09 +01:00
$lang = (new uLang(uConfig::$lang))->getStrings();
2018-11-09 12:42:35 +01:00
$uploadErrors = [];
2017-05-09 15:25:16 +02:00
$uploadErrors[UPLOAD_ERR_INI_SIZE] = "The uploaded file exceeds the upload_max_filesize directive in php.ini";
$uploadErrors[UPLOAD_ERR_FORM_SIZE] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
$uploadErrors[UPLOAD_ERR_PARTIAL] = "The uploaded file was only partially uploaded";
$uploadErrors[UPLOAD_ERR_NO_FILE] = "No file was uploaded";
$uploadErrors[UPLOAD_ERR_NO_TMP_DIR] = "Missing a temporary folder";
$uploadErrors[UPLOAD_ERR_CANT_WRITE] = "Failed to write file to disk";
$uploadErrors[UPLOAD_ERR_EXTENSION] = "A PHP extension stopped the file upload";
2017-05-09 09:06:23 +02:00
2017-08-25 13:59:19 +02:00
if (!$auth->isAuthenticated()) {
uUtils::exitWithError($lang["private"]);
2017-05-09 09:06:23 +02:00
}
2019-07-12 21:50:21 +02:00
try {
$fileMeta = uUtils::requireFile("gpx");
} catch (ErrorException $ee) {
2017-05-09 15:25:16 +02:00
$message = $lang["servererror"];
2019-07-12 21:50:21 +02:00
$message .= ": {$ee->getMessage()}";
2017-05-09 18:18:15 +02:00
uUtils::exitWithError($message);
2019-07-12 21:50:21 +02:00
} catch (Exception $e) {
2018-11-09 12:42:35 +01:00
$message = $lang["iuploadfailure"];
2019-07-12 21:50:21 +02:00
$message .= ": {$ee->getMessage()}";
2017-05-09 18:18:15 +02:00
uUtils::exitWithError($message);
2017-05-09 09:06:23 +02:00
}
2019-07-12 21:50:21 +02:00
$gpxFile = $fileMeta[uUpload::META_TMP_NAME];
$gpxName = basename($fileMeta[uUpload::META_NAME]);
2017-05-09 09:06:23 +02:00
libxml_use_internal_errors(true);
2019-07-12 21:50:21 +02:00
$gpx = simplexml_load_file($gpxFile);
unlink($gpxFile);
2017-05-09 09:06:23 +02:00
if ($gpx === false) {
$message = $lang["iparsefailure"];
$parserMessages = [];
foreach(libxml_get_errors() as $parseError) {
$parserMessages[] = $parseError->message;
}
$parserMessage = implode(", ", $parserMessages);
if (!empty($parserMessage)) {
$message .= ": $parserMessage";
}
2017-05-09 18:18:15 +02:00
uUtils::exitWithError($message);
2017-05-09 09:06:23 +02:00
}
2019-12-05 22:46:58 +01:00
else if ($gpx->getName() !== "gpx") {
2017-09-19 22:03:09 +02:00
uUtils::exitWithError($lang["iparsefailure"]);
}
2017-05-09 09:06:23 +02:00
else if (empty($gpx->trk)) {
2017-05-09 18:18:15 +02:00
uUtils::exitWithError($lang["idatafailure"]);
2017-05-09 09:06:23 +02:00
}
2019-12-05 22:46:58 +01:00
$trackList = [];
2017-05-09 16:00:02 +02:00
foreach ($gpx->trk as $trk) {
2017-09-19 22:03:09 +02:00
$trackName = empty($trk->name) ? $gpxName : (string) $trk->name;
$metaName = empty($gpx->metadata->name) ? NULL : (string) $gpx->metadata->name;
2017-08-25 13:59:19 +02:00
$trackId = uTrack::add($auth->user->id, $trackName, $metaName);
2017-05-09 16:00:02 +02:00
if ($trackId === false) {
2017-05-09 18:18:15 +02:00
uUtils::exitWithError($lang["servererror"]);
2017-05-09 16:00:02 +02:00
break;
}
$track = new uTrack($trackId);
2017-09-19 22:03:09 +02:00
$posCnt = 0;
2017-05-09 09:06:23 +02:00
2017-05-09 16:00:02 +02:00
foreach($trk->trkseg as $segment) {
foreach($segment->trkpt as $point) {
2019-12-05 22:46:58 +01:00
if (!isset($point["lat"], $point["lon"])) {
2017-09-19 22:03:09 +02:00
$track->delete();
uUtils::exitWithError($lang["iparsefailure"]);
}
$time = isset($point->time) ? strtotime($point->time) : 1;
$altitude = isset($point->ele) ? (double) $point->ele : NULL;
2019-07-12 21:50:21 +02:00
$comment = isset($point->desc) && !empty($point->desc) ? (string) $point->desc : NULL;
2017-06-24 15:09:44 +02:00
$speed = NULL;
$bearing = NULL;
$accuracy = NULL;
$provider = "gps";
if (!empty($point->extensions)) {
// parse ulogger extensions
2019-12-05 22:46:58 +01:00
$ext = $point->extensions->children('ulogger', true);
if (count($ext->speed)) { $speed = (double) $ext->speed; }
if (count($ext->bearing)) { $bearing = (double) $ext->bearing; }
if (count($ext->accuracy)) { $accuracy = (int) $ext->accuracy; }
if (count($ext->provider)) { $provider = (string) $ext->provider; }
2017-06-24 15:09:44 +02:00
}
$ret = $track->addPosition($auth->user->id,
$time, (double) $point["lat"], (double) $point["lon"], $altitude,
2019-07-12 21:50:21 +02:00
$speed, $bearing, $accuracy, $provider, $comment, NULL);
2017-05-09 16:00:02 +02:00
if ($ret === false) {
$track->delete();
2017-05-09 18:18:15 +02:00
uUtils::exitWithError($lang["servererror"]);
2017-05-09 16:00:02 +02:00
}
2017-09-19 22:03:09 +02:00
$posCnt++;
2017-05-09 09:06:23 +02:00
}
}
2017-09-19 22:03:09 +02:00
if ($posCnt) {
2019-12-05 22:46:58 +01:00
array_unshift($trackList, [ "id" => $track->id, "name" => $track->name ]);
2017-09-19 22:03:09 +02:00
} else {
$track->delete();
}
2017-05-09 09:06:23 +02:00
}
2019-12-05 22:46:58 +01:00
header("Content-type: application/json");
echo json_encode($trackList);
?>