| [c455d7b] | 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 | namespace GnuBoat;
|
|---|
| 20 |
|
|---|
| 21 | require_once(__DIR__ ."/../autoloader.php");
|
|---|
| 22 |
|
|---|
| 23 | class ServiceMap extends Service
|
|---|
| 24 | {
|
|---|
| 25 | function __construct()
|
|---|
| 26 | {
|
|---|
| 27 | parent::__construct();
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| [1ba85cf] | 30 | protected function process()
|
|---|
| [c455d7b] | 31 | {
|
|---|
| 32 | header('Content-type: application/json');
|
|---|
| 33 |
|
|---|
| 34 | $result["error"] = 1;
|
|---|
| 35 | $result["errormessage"] = "Unknown Error";
|
|---|
| 36 |
|
|---|
| 37 | if(!array_key_exists("mapName", $_POST)){
|
|---|
| 38 | $result["error"] = 1;
|
|---|
| 39 | $result["errormessage"] = "Please specify map name (no value)";
|
|---|
| 40 | echo json_encode($result);
|
|---|
| 41 | return;
|
|---|
| 42 | }
|
|---|
| 43 | $mapName = $_POST["mapName"];
|
|---|
| 44 | if(empty($mapName) || $mapName == ""){
|
|---|
| 45 | $result["error"] = 1;
|
|---|
| 46 | $result["errormessage"] = "Please specifiy map name (empty value)";
|
|---|
| 47 | echo json_encode($result);
|
|---|
| 48 | return;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | $mapDirectory = __DIR__ ."/../../maps";
|
|---|
| 52 | if(!array_key_exists("mapFile", $_FILES)){
|
|---|
| 53 | $result["error"] = 1;
|
|---|
| 54 | $result["errormessage"] = "No map file recieved";
|
|---|
| 55 | echo json_encode($result);
|
|---|
| 56 | return;
|
|---|
| 57 | }
|
|---|
| 58 | if (!isset($_FILES['mapFile']['error']) || is_array($_FILES['mapFile']['error'])){
|
|---|
| 59 | $result["error"] = 1;
|
|---|
| 60 | $result["errormessage"] = "Invalid parameters";
|
|---|
| 61 | echo json_encode($result);
|
|---|
| 62 | return;
|
|---|
| 63 | }
|
|---|
| 64 | switch ($_FILES['mapFile']['error']) {
|
|---|
| 65 | case UPLOAD_ERR_OK:
|
|---|
| 66 | break;
|
|---|
| 67 | case UPLOAD_ERR_NO_FILE:
|
|---|
| 68 | $result["error"] = 1;
|
|---|
| 69 | $result["errormessage"] = "No file sent";
|
|---|
| 70 | echo json_encode($result);
|
|---|
| 71 | return;
|
|---|
| 72 | case UPLOAD_ERR_INI_SIZE:
|
|---|
| 73 | case UPLOAD_ERR_FORM_SIZE:
|
|---|
| 74 | $result["error"] = 1;
|
|---|
| 75 | $result["errormessage"] = "Exceeded filesize limit.";
|
|---|
| 76 | echo json_encode($result);
|
|---|
| 77 | return;
|
|---|
| 78 | default:
|
|---|
| 79 | $result["error"] = 1;
|
|---|
| 80 | $result["errormessage"] = "Unknown PHP Error";
|
|---|
| 81 | echo json_encode($result);
|
|---|
| 82 | return;
|
|---|
| 83 | }
|
|---|
| 84 | $mapFileName = $mapDirectory ."/". sha1_file($_FILES['mapFile']['tmp_name']) .".mbtiles";
|
|---|
| 85 | if(move_uploaded_file($_FILES['mapFile']['tmp_name'], $mapFileName)){
|
|---|
| 86 | if(!chmod($mapFileName, 0755)){
|
|---|
| 87 | $result["error"] = 1;
|
|---|
| 88 | $result["errormessage"] = "Could not change permissions of the map file";
|
|---|
| 89 | }
|
|---|
| 90 | else{
|
|---|
| 91 | // correct map metadata
|
|---|
| 92 | $resultRename = $this->setMapName($mapFileName, $mapName);
|
|---|
| 93 | if($resultRename[0] === true){
|
|---|
| 94 | $result["error"] = 0;
|
|---|
| 95 | $result["errormessage"] = "";
|
|---|
| 96 | }
|
|---|
| 97 | else{
|
|---|
| 98 | $result["error"] = 1;
|
|---|
| 99 | $result["errormessage"] = $resultRename[1];
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | else {
|
|---|
| 104 | $result["error"] = 1;
|
|---|
| 105 | $result["errormessage"] = "Could not move map in place";
|
|---|
| 106 | }
|
|---|
| 107 | echo json_encode($result);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | private function setMapName($mbTilesFile, $mapName)
|
|---|
| 111 | {
|
|---|
| 112 | if(!is_file($mbTilesFile))
|
|---|
| 113 | return array(false, "setMapName - Map file not found: ". $mbTilesFile);
|
|---|
| 114 | if(empty($mapName) || $mapName == "")
|
|---|
| 115 | return array(false, "setMapName - Map name empty");
|
|---|
| 116 | try{
|
|---|
| 117 | $databaseConnection = new \PDO("sqlite:". $mbTilesFile);
|
|---|
| 118 | $databaseConnection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|---|
| 119 |
|
|---|
| 120 | $columnName = "name";
|
|---|
| 121 | $query = $databaseConnection->prepare("UPDATE metadata SET value=:value WHERE name=:name");
|
|---|
| 122 | $query->bindParam(":name", $columnName);
|
|---|
| 123 | $query->bindParam(":value", $mapName);
|
|---|
| 124 | if($query->execute() !== true){
|
|---|
| 125 | return array(false, "setMapName - Could not update metadata");
|
|---|
| 126 | }
|
|---|
| 127 | return array(true, "");
|
|---|
| 128 | }
|
|---|
| 129 | catch(\PDOException $e){
|
|---|
| 130 | return array(false, "setMapName - Database error: ". $e->getMessage());
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | ?>
|
|---|