| 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 | $columnName = "name";
|
|---|
| 30 | $query = $databaseConnection->prepare("UPDATE metadata SET value=:value WHERE name=:name");
|
|---|
| 31 | $query->bindParam(":name", $columnName);
|
|---|
| 32 | $query->bindParam(":value", $mapName);
|
|---|
| 33 | if($query->execute() !== true){
|
|---|
| 34 | return array(false, "setMapName - Could not update metadata");
|
|---|
| 35 | }
|
|---|
| 36 | return array(true, "");
|
|---|
| 37 | }
|
|---|
| 38 | catch(PDOException $e){
|
|---|
| 39 | return array(false, "setMapName - Database error: ". $e->getMessage());
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | header('Content-type: application/json');
|
|---|
| 44 |
|
|---|
| 45 | $result["error"] = 1;
|
|---|
| 46 | $result["errormessage"] = "Unknown Error";
|
|---|
| 47 |
|
|---|
| 48 | if(!array_key_exists("mapName", $_POST)){
|
|---|
| 49 | $result["error"] = 1;
|
|---|
| 50 | $result["errormessage"] = "Please specify map name (no value)";
|
|---|
| 51 | echo json_encode($result);
|
|---|
| 52 | return;
|
|---|
| 53 | }
|
|---|
| 54 | $mapName = $_POST["mapName"];
|
|---|
| 55 | if(empty($mapName) || $mapName == ""){
|
|---|
| 56 | $result["error"] = 1;
|
|---|
| 57 | $result["errormessage"] = "Please specifiy map name (empty value)";
|
|---|
| 58 | echo json_encode($result);
|
|---|
| 59 | return;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | $mapDirectory = __DIR__ ."/maps";
|
|---|
| 63 | if(!array_key_exists("mapFile", $_FILES)){
|
|---|
| 64 | $result["error"] = 1;
|
|---|
| 65 | $result["errormessage"] = "No map file recieved";
|
|---|
| 66 | echo json_encode($result);
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 | if (!isset($_FILES['mapFile']['error']) || is_array($_FILES['mapFile']['error'])){
|
|---|
| 70 | $result["error"] = 1;
|
|---|
| 71 | $result["errormessage"] = "Invalid parameters";
|
|---|
| 72 | echo json_encode($result);
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 | switch ($_FILES['mapFile']['error']) {
|
|---|
| 76 | case UPLOAD_ERR_OK:
|
|---|
| 77 | break;
|
|---|
| 78 | case UPLOAD_ERR_NO_FILE:
|
|---|
| 79 | $result["error"] = 1;
|
|---|
| 80 | $result["errormessage"] = "No file sent";
|
|---|
| 81 | echo json_encode($result);
|
|---|
| 82 | return;
|
|---|
| 83 | case UPLOAD_ERR_INI_SIZE:
|
|---|
| 84 | case UPLOAD_ERR_FORM_SIZE:
|
|---|
| 85 | $result["error"] = 1;
|
|---|
| 86 | $result["errormessage"] = "Exceeded filesize limit.";
|
|---|
| 87 | echo json_encode($result);
|
|---|
| 88 | return;
|
|---|
| 89 | default:
|
|---|
| 90 | $result["error"] = 1;
|
|---|
| 91 | $result["errormessage"] = "Unknown PHP Error";
|
|---|
| 92 | echo json_encode($result);
|
|---|
| 93 | return;
|
|---|
| 94 | }
|
|---|
| 95 | $mapFileName = $mapDirectory ."/". sha1_file($_FILES['mapFile']['tmp_name']) .".mbtiles";
|
|---|
| 96 | if(move_uploaded_file($_FILES['mapFile']['tmp_name'], $mapFileName)){
|
|---|
| 97 | if(!chmod($mapFileName, 0755)){
|
|---|
| 98 | $result["error"] = 1;
|
|---|
| 99 | $result["errormessage"] = "Could not change permissions of the map file";
|
|---|
| 100 | }
|
|---|
| 101 | else{
|
|---|
| 102 | // correct map metadata
|
|---|
| 103 | $resultRename = setMapName($mapFileName, $mapName);
|
|---|
| 104 | if($resultRename[0] === true){
|
|---|
| 105 | $result["error"] = 0;
|
|---|
| 106 | $result["errormessage"] = "";
|
|---|
| 107 | }
|
|---|
| 108 | else{
|
|---|
| 109 | $result["error"] = 1;
|
|---|
| 110 | $result["errormessage"] = $resultRename[1];
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | else {
|
|---|
| 115 | $result["error"] = 1;
|
|---|
| 116 | $result["errormessage"] = "Could not move map in place";
|
|---|
| 117 | }
|
|---|
| 118 | echo json_encode($result);
|
|---|
| 119 |
|
|---|
| 120 | ?>
|
|---|