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-05-09 15:25:16 +02:00
|
|
|
define("headless", true);
|
2017-05-09 09:06:23 +02:00
|
|
|
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
|
|
|
|
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");
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
if (!$user->isValid) {
|
2017-05-09 18:18:15 +02:00
|
|
|
uUtils::exitWithError($lang["servererror"]);
|
2017-05-09 09:06:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 15:25:16 +02:00
|
|
|
if (!isset($_FILES["gpx"])) {
|
|
|
|
$message = $lang["servererror"];
|
|
|
|
$lastErr = error_get_last();
|
|
|
|
if (!empty($lastErr)) {
|
|
|
|
$message .= ": " . $lastErr["message"];
|
|
|
|
}
|
2017-05-09 18:18:15 +02:00
|
|
|
uUtils::exitWithError($message);
|
2017-05-09 15:25:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 09:06:23 +02:00
|
|
|
$gpxFile = NULL;
|
|
|
|
$gpxUpload = $_FILES["gpx"];
|
2017-05-09 15:25:16 +02:00
|
|
|
$uploadErr = $gpxUpload["error"];
|
|
|
|
if ($gpxUpload["size"] > uUtils::getUploadMaxSize() && $uploadErr == UPLOAD_ERR_OK) {
|
|
|
|
$uploadErr = UPLOAD_ERR_FORM_SIZE;
|
|
|
|
}
|
|
|
|
if ($uploadErr == UPLOAD_ERR_OK) {
|
2017-05-09 09:06:23 +02:00
|
|
|
$gpxFile = $gpxUpload["tmp_name"];
|
|
|
|
$gpxName = basename($gpxUpload["name"]);
|
2017-05-09 15:25:16 +02:00
|
|
|
} else {
|
|
|
|
$message = $lang("iuploadfailure");
|
|
|
|
if (isset($errorMessage[$uploadErr])) {
|
|
|
|
$message .= ": " . $errorMessage[$uploadErr];
|
|
|
|
}
|
|
|
|
$message .= " ($uploadErr)";
|
2017-05-09 18:18:15 +02:00
|
|
|
uUtils::exitWithError($message);
|
2017-05-09 09:06:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$gpx = false;
|
|
|
|
libxml_use_internal_errors(true);
|
|
|
|
if ($gpxFile && file_exists($gpxFile)) {
|
|
|
|
$gpx = simplexml_load_file($gpxFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-09 16:00:02 +02:00
|
|
|
$trackCnt = 0;
|
|
|
|
foreach ($gpx->trk as $trk) {
|
|
|
|
$trackName = empty($trk->name) ? $gpxName : $trk->name->__toString();
|
|
|
|
$metaName = empty($gpx->metadata->name) ? NULL : $gpx->metadata->name->__toString();
|
|
|
|
$track = new uTrack();
|
|
|
|
$trackId = $track->add($user->id, $trackName, $metaName);
|
|
|
|
if ($trackId === false) {
|
2017-05-09 18:18:15 +02:00
|
|
|
uUtils::exitWithError($lang["servererror"]);
|
2017-05-09 16:00:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-05-09 09:06:23 +02:00
|
|
|
|
2017-05-09 16:00:02 +02:00
|
|
|
$position = new uPosition();
|
|
|
|
foreach($trk->trkseg as $segment) {
|
|
|
|
foreach($segment->trkpt as $point) {
|
|
|
|
$ret = $position->add($user->id, $trackId,
|
|
|
|
strtotime($point->time), $point["lat"], $point["lon"], $point->ele,
|
|
|
|
NULL, NULL, NULL, "gps", NULL, NULL);
|
|
|
|
if ($ret === false) {
|
2017-05-09 18:18:15 +02:00
|
|
|
uUtils::exitWithError($lang["servererror"]);
|
2017-05-09 16:00:02 +02:00
|
|
|
}
|
2017-05-09 09:06:23 +02:00
|
|
|
}
|
|
|
|
}
|
2017-05-09 16:00:02 +02:00
|
|
|
$trackCnt++;
|
2017-05-09 09:06:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-09 16:00:02 +02:00
|
|
|
// return track id and tracks count
|
2017-05-09 18:18:15 +02:00
|
|
|
uUtils::exitWithSuccess([ "trackid" => $trackId, "trackcnt" => $trackCnt ]);
|
2017-05-09 09:06:23 +02:00
|
|
|
|
|
|
|
?>
|