| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This program is free software: you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation, either version 3 of the License, or
|
|---|
| 8 | (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This program is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public License
|
|---|
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 17 | */
|
|---|
| 18 |
|
|---|
| 19 | function setMapName($mbTilesFile, $mapName)
|
|---|
| 20 | {
|
|---|
| 21 | if(!is_file($mbTilesFile))
|
|---|
| 22 | return array(false, "setMapName - Map file not found: ". $mbTilesFile);
|
|---|
| 23 | if(empty($mapName) || $mapName == "")
|
|---|
| 24 | return array(false, "setMapName - Map name empty");
|
|---|
| 25 | try{
|
|---|
| 26 | $databaseConnection = new PDO("sqlite:". $mbTilesFile);
|
|---|
| 27 | $databaseConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|---|
| 28 |
|
|---|
| 29 | $query = $databaseConnection->prepare("UPDATE metadata SET value=:value WHERE name='name'");
|
|---|
| 30 | $query->bindParam(":value", $mapName);
|
|---|
| 31 | $query->execute();
|
|---|
| 32 | return array(true, "");
|
|---|
| 33 | }
|
|---|
| 34 | catch(PDOException $e){
|
|---|
| 35 | return array(false, "setMapName - Database error: ". $e->getMessage());
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | header('Content-type: application/json');
|
|---|
| 40 |
|
|---|
| 41 | $result["error"] = 1;
|
|---|
| 42 | $result["errormessage"] = "Unknown Error";
|
|---|
| 43 |
|
|---|
| 44 | if(!array_key_exists("mapName", $_POST)){
|
|---|
| 45 | $result["error"] = 1;
|
|---|
| 46 | $result["errormessage"] = "Please specify map name (no value)";
|
|---|
| 47 | echo json_encode($result);
|
|---|
| 48 | return;
|
|---|
| 49 | }
|
|---|
| 50 | $mapName = $_POST["mapName"];
|
|---|
| 51 | if(empty($mapName) || $mapName == ""){
|
|---|
| 52 | $result["error"] = 1;
|
|---|
| 53 | $result["errormessage"] = "Please specifiy map name (empty value)";
|
|---|
| 54 | echo json_encode($result);
|
|---|
| 55 | return;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | $mapDirectory = __DIR__ ."/maps";
|
|---|
| 59 | if(!array_key_exists("mapFile", $_FILES)){
|
|---|
| 60 | $result["error"] = 1;
|
|---|
| 61 | $result["errormessage"] = "No map file recieved";
|
|---|
| 62 | echo json_encode($result);
|
|---|
| 63 | return;
|
|---|
| 64 | }
|
|---|
| 65 | if (!isset($_FILES['mapFile']['error']) || is_array($_FILES['mapFile']['error'])){
|
|---|
| 66 | $result["error"] = 1;
|
|---|
| 67 | $result["errormessage"] = "Invalid parameters";
|
|---|
| 68 | echo json_encode($result);
|
|---|
| 69 | return;
|
|---|
| 70 | }
|
|---|
| 71 | switch ($_FILES['mapFile']['error']) {
|
|---|
| 72 | case UPLOAD_ERR_OK:
|
|---|
| 73 | break;
|
|---|
| 74 | case UPLOAD_ERR_NO_FILE:
|
|---|
| 75 | $result["error"] = 1;
|
|---|
| 76 | $result["errormessage"] = "No file sent";
|
|---|
| 77 | echo json_encode($result);
|
|---|
| 78 | return;
|
|---|
| 79 | case UPLOAD_ERR_INI_SIZE:
|
|---|
| 80 | case UPLOAD_ERR_FORM_SIZE:
|
|---|
| 81 | $result["error"] = 1;
|
|---|
| 82 | $result["errormessage"] = "Exceeded filesize limit.";
|
|---|
| 83 | echo json_encode($result);
|
|---|
| 84 | return;
|
|---|
| 85 | default:
|
|---|
| 86 | $result["error"] = 1;
|
|---|
| 87 | $result["errormessage"] = "Unknown PHP Error";
|
|---|
| 88 | echo json_encode($result);
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 | $mapFileName = $mapDirectory ."/". sha1_file($_FILES['mapFile']['tmp_name']) .".mbtiles";
|
|---|
| 92 | if(move_uploaded_file($_FILES['mapFile']['tmp_name'], $mapFileName)){
|
|---|
| 93 | // correct map metadata
|
|---|
| 94 | $resultRename = setMapName($mapFileName, $mapName);
|
|---|
| 95 | if($resultRename[0] === true){
|
|---|
| 96 | $result["error"] = 0;
|
|---|
| 97 | $result["errormessage"] = "";
|
|---|
| 98 | }
|
|---|
| 99 | else{
|
|---|
| 100 | $result["error"] = 1;
|
|---|
| 101 | $result["errormessage"] = $resultRename[1];
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | else {
|
|---|
| 105 | $result["error"] = 1;
|
|---|
| 106 | $result["errormessage"] = "Could not move map in place";
|
|---|
| 107 | }
|
|---|
| 108 | echo json_encode($result);
|
|---|
| 109 |
|
|---|
| 110 | ?>
|
|---|