Import: improve error handling
This commit is contained in:
parent
33b581b47a
commit
97cfd8a723
7
auth.php
7
auth.php
@ -17,7 +17,12 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
define("ROOT_DIR", __DIR__);
|
||||
if (defined('headless')) {
|
||||
ob_get_contents();
|
||||
ob_end_clean();
|
||||
error_reporting(0);
|
||||
}
|
||||
define('ROOT_DIR', __DIR__);
|
||||
require_once(ROOT_DIR . "/helpers/config.php");
|
||||
require_once(ROOT_DIR . "/lang.php");
|
||||
require_once(ROOT_DIR . "/helpers/user.php");
|
||||
|
67
helpers/utils.php
Normal file
67
helpers/utils.php
Normal file
@ -0,0 +1,67 @@
|
||||
<?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/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Various util functions
|
||||
*/
|
||||
class uUtils {
|
||||
|
||||
/**
|
||||
* Calculate maximum allowed size of uploaded file
|
||||
* for current PHP settings
|
||||
*
|
||||
* @return int Number of bytes
|
||||
*/
|
||||
public static function getUploadMaxSize() {
|
||||
$upload_max_filesize = self::iniGetBytes('upload_max_filesize');
|
||||
$post_max_size = self::iniGetBytes('post_max_size');
|
||||
// post_max_size = 0 means unlimited size
|
||||
if ($post_max_size == 0) { $post_max_size = $upload_max_filesize; }
|
||||
$memory_limit = self::iniGetBytes('memory_limit');
|
||||
// memory_limit = -1 means no limit
|
||||
if ($memory_limit < 0) { $memory_limit = $post_max_size; }
|
||||
return min($upload_max_filesize, $post_max_size, $memory_limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of bytes from ini parameter.
|
||||
* Optionally parses shorthand byte values (G, M, B)
|
||||
*
|
||||
* @param string $iniParam Ini parameter name
|
||||
* @return int Bytes
|
||||
*/
|
||||
private static function iniGetBytes($iniParam) {
|
||||
$iniStr = ini_get($iniParam);
|
||||
$val = floatval($iniStr);
|
||||
$suffix = substr(trim($iniStr), -1);
|
||||
if (ctype_alpha($suffix)) {
|
||||
switch (strtolower($suffix)) {
|
||||
case 'g':
|
||||
$val *= 1024;
|
||||
case 'm':
|
||||
$val *= 1024;
|
||||
case 'k':
|
||||
$val *= 1024;
|
||||
}
|
||||
}
|
||||
return (int) $val;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -20,6 +20,7 @@
|
||||
require_once(__DIR__ . "/auth.php"); // sets $user
|
||||
require_once(ROOT_DIR . "/helpers/position.php");
|
||||
require_once(ROOT_DIR . "/helpers/track.php");
|
||||
require_once(ROOT_DIR . "/helpers/utils.php");
|
||||
|
||||
$displayUserId = NULL;
|
||||
$usersArr = [];
|
||||
@ -197,8 +198,8 @@
|
||||
<?php if ($user->isValid): ?>
|
||||
<div id="import">
|
||||
<u><?= $lang["import"] ?></u><br>
|
||||
<form id="importForm" enctype="multipart/form-data" method="POST">
|
||||
<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
|
||||
<form id="importForm" enctype="multipart/form-data" method="post">
|
||||
<input type="hidden" name="MAX_FILE_SIZE" value="<?= uUtils::getUploadMaxSize() ?>" />
|
||||
<input type="file" id="inputFile" name="gpx" style="display:none" onchange="importFile(this)" />
|
||||
</form>
|
||||
<a class="menulink" href="javascript:void(0);" onclick="document.getElementById('inputFile').click();">gpx</a>
|
||||
|
@ -243,12 +243,7 @@ function importFile(input){
|
||||
xhr = null;
|
||||
}
|
||||
}
|
||||
xhr.upload.onprogress = function(e) {
|
||||
console.log(e.loaded, e.total)
|
||||
}
|
||||
xhr.upload.onload = function(e) {
|
||||
console.log('file upload')
|
||||
}
|
||||
// FIXME: show progress
|
||||
xhr.open("POST", "utils/import.php", true);
|
||||
xhr.send(new FormData(input.parentElement));
|
||||
}
|
||||
|
@ -115,6 +115,7 @@ $lang["owntrackswarn"] = "Your can only edit your own tracks";
|
||||
$lang["gmauthfailure"] = "There may be problem with Google Maps API key on this page";
|
||||
$lang["gmapilink"] = "You may find more information about API keys on <a target=\"_blank\" href=\"https://developers.google.com/maps/documentation/javascript/get-api-key\">this Google webpage</a>";
|
||||
$lang["import"] = "Import track";
|
||||
$lang["iuploadfailure"] = "Uploading failed";
|
||||
$lang["iparsefailure"] = "Parsing failed";
|
||||
$lang["idatafailure"] = "No track data in imported file";
|
||||
?>
|
||||
|
@ -110,6 +110,7 @@ $lang["owntrackswarn"] = "Możesz edytować tylko swoje własne trasy";
|
||||
$lang["gmauthfailure"] = "Prawdopodobnie na tej stronie występuje problem z kluczem API Google Maps";
|
||||
$lang["gmapilink"] = "Więcej informacji o kluczach API znajdziesz <a target=\"_blank\" href=\"https://developers.google.com/maps/documentation/javascript/get-api-key\">pod tym linkiem</a>";
|
||||
$lang["import"] = "Importuj trasę";
|
||||
$lang["iuploadfailure"] = "Błąd przesyłania pliku";
|
||||
$lang["iparsefailure"] = "Błąd parsowania pliku";
|
||||
$lang["idatafailure"] = "Brak trasy w importowanym pliku";
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
define("headless", true);
|
||||
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,7 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
define("headless", true);
|
||||
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
|
||||
require_once(ROOT_DIR . "/helpers/position.php");
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
define("headless", true);
|
||||
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
|
||||
require_once(ROOT_DIR . "/helpers/track.php");
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
define("headless", true);
|
||||
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
|
||||
require_once(ROOT_DIR . "/helpers/track.php");
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
define("headless", true);
|
||||
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
|
||||
|
||||
/**
|
||||
|
@ -17,9 +17,19 @@
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
define("headless", true);
|
||||
require_once(dirname(__DIR__) . "/auth.php"); // sets $user
|
||||
require_once(ROOT_DIR . "/helpers/track.php");
|
||||
require_once(ROOT_DIR . "/helpers/position.php");
|
||||
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";
|
||||
|
||||
/**
|
||||
* Exit with xml response
|
||||
@ -49,12 +59,31 @@ if (!$user->isValid) {
|
||||
exitWithStatus(true, $lang["servererror"]);
|
||||
}
|
||||
|
||||
$sizeMax = 10 * 1024 * 1024; //FIXME: set to php limits
|
||||
if (!isset($_FILES["gpx"])) {
|
||||
$message = $lang["servererror"];
|
||||
$lastErr = error_get_last();
|
||||
if (!empty($lastErr)) {
|
||||
$message .= ": " . $lastErr["message"];
|
||||
}
|
||||
exitWithStatus(true, $message);
|
||||
}
|
||||
|
||||
$gpxFile = NULL;
|
||||
$gpxUpload = $_FILES["gpx"];
|
||||
if ($gpxUpload["error"] == UPLOAD_ERR_OK && $gpxUpload["size"] < $sizeMax) {
|
||||
$uploadErr = $gpxUpload["error"];
|
||||
if ($gpxUpload["size"] > uUtils::getUploadMaxSize() && $uploadErr == UPLOAD_ERR_OK) {
|
||||
$uploadErr = UPLOAD_ERR_FORM_SIZE;
|
||||
}
|
||||
if ($uploadErr == UPLOAD_ERR_OK) {
|
||||
$gpxFile = $gpxUpload["tmp_name"];
|
||||
$gpxName = basename($gpxUpload["name"]);
|
||||
} else {
|
||||
$message = $lang("iuploadfailure");
|
||||
if (isset($errorMessage[$uploadErr])) {
|
||||
$message .= ": " . $errorMessage[$uploadErr];
|
||||
}
|
||||
$message .= " ($uploadErr)";
|
||||
exitWithStatus(true, $message);
|
||||
}
|
||||
|
||||
$gpx = false;
|
||||
@ -73,9 +102,6 @@ if ($gpx === false) {
|
||||
if (!empty($parserMessage)) {
|
||||
$message .= ": $parserMessage";
|
||||
}
|
||||
if ($gpxUpload["error"] != UPLOAD_ERR_OK) {
|
||||
$message .= " (" . $gpxUpload["error"] . ")";
|
||||
}
|
||||
exitWithStatus(true, $message);
|
||||
}
|
||||
else if (empty($gpx->trk)) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user