diff --git a/auth.php b/auth.php index 9f302d4..f69c6c3 100755 --- a/auth.php +++ b/auth.php @@ -17,7 +17,12 @@ * along with this program; if not, see . */ -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"); diff --git a/helpers/utils.php b/helpers/utils.php new file mode 100644 index 0000000..8f0246c --- /dev/null +++ b/helpers/utils.php @@ -0,0 +1,67 @@ +. + */ + + /** + * 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; + } + } + +?> \ No newline at end of file diff --git a/index.php b/index.php index b25c9b4..8aa15e9 100755 --- a/index.php +++ b/index.php @@ -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 @@ isValid): ?>

-
- + +
gpx diff --git a/js/main.js b/js/main.js index d703281..54dba6a 100755 --- a/js/main.js +++ b/js/main.js @@ -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)); } diff --git a/lang/en.php b/lang/en.php index 9bada49..4e63834 100644 --- a/lang/en.php +++ b/lang/en.php @@ -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 this Google webpage"; $lang["import"] = "Import track"; +$lang["iuploadfailure"] = "Uploading failed"; $lang["iparsefailure"] = "Parsing failed"; $lang["idatafailure"] = "No track data in imported file"; ?> diff --git a/lang/pl.php b/lang/pl.php index adca967..4ea2049 100644 --- a/lang/pl.php +++ b/lang/pl.php @@ -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 pod tym linkiem"; $lang["import"] = "Importuj trasę"; +$lang["iuploadfailure"] = "Błąd przesyłania pliku"; $lang["iparsefailure"] = "Błąd parsowania pliku"; $lang["idatafailure"] = "Brak trasy w importowanym pliku"; diff --git a/utils/changepass.php b/utils/changepass.php index abaf343..387b4a3 100644 --- a/utils/changepass.php +++ b/utils/changepass.php @@ -17,6 +17,7 @@ * along with this program; if not, see . */ + define("headless", true); require_once(dirname(__DIR__) . "/auth.php"); // sets $user /** diff --git a/utils/getpositions.php b/utils/getpositions.php index b1e95a7..6724650 100755 --- a/utils/getpositions.php +++ b/utils/getpositions.php @@ -17,6 +17,7 @@ * along with this program; if not, see . */ +define("headless", true); require_once(dirname(__DIR__) . "/auth.php"); // sets $user require_once(ROOT_DIR . "/helpers/position.php"); diff --git a/utils/gettracks.php b/utils/gettracks.php index 3f15bd1..8a8a38c 100755 --- a/utils/gettracks.php +++ b/utils/gettracks.php @@ -17,6 +17,7 @@ * along with this program; if not, see . */ +define("headless", true); require_once(dirname(__DIR__) . "/auth.php"); // sets $user require_once(ROOT_DIR . "/helpers/track.php"); diff --git a/utils/handletrack.php b/utils/handletrack.php index ad7bdd1..d112efe 100644 --- a/utils/handletrack.php +++ b/utils/handletrack.php @@ -17,6 +17,7 @@ * along with this program; if not, see . */ + define("headless", true); require_once(dirname(__DIR__) . "/auth.php"); // sets $user require_once(ROOT_DIR . "/helpers/track.php"); diff --git a/utils/handleuser.php b/utils/handleuser.php index f2796c3..4084da3 100644 --- a/utils/handleuser.php +++ b/utils/handleuser.php @@ -17,6 +17,7 @@ * along with this program; if not, see . */ + define("headless", true); require_once(dirname(__DIR__) . "/auth.php"); // sets $user /** diff --git a/utils/import.php b/utils/import.php index 9450dce..0179e89 100755 --- a/utils/import.php +++ b/utils/import.php @@ -17,9 +17,19 @@ * along with this program; if not, see . */ +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)) {