| 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 |
|
|---|
| 30 | protected function process()
|
|---|
| 31 | {
|
|---|
| 32 | if(!array_key_exists("mapName", $_POST))
|
|---|
| 33 | return $this->error("Please specify map name (no value)");
|
|---|
| 34 |
|
|---|
| 35 | $mapName = $_POST["mapName"];
|
|---|
| 36 | if(empty($mapName) || $mapName == "")
|
|---|
| 37 | return $this->error("Please specifiy map name (empty value)");
|
|---|
| 38 |
|
|---|
| 39 | $mapDirectory = __DIR__ ."/../../maps";
|
|---|
| 40 | if(!array_key_exists("mapFile", $_FILES))
|
|---|
| 41 | return $this->error("No map file recieved");
|
|---|
| 42 |
|
|---|
| 43 | if (!isset($_FILES['mapFile']['error']) || is_array($_FILES['mapFile']['error']))
|
|---|
| 44 | return $this->error("Invalid parameters");
|
|---|
| 45 |
|
|---|
| 46 | switch ($_FILES['mapFile']['error']) {
|
|---|
| 47 | case UPLOAD_ERR_OK:
|
|---|
| 48 | break;
|
|---|
| 49 | case UPLOAD_ERR_NO_FILE:
|
|---|
| 50 | return $this->error("No file sent");
|
|---|
| 51 | case UPLOAD_ERR_INI_SIZE:
|
|---|
| 52 | case UPLOAD_ERR_FORM_SIZE:
|
|---|
| 53 | return $this->error("Exceeded filesize limit.");
|
|---|
| 54 | default:
|
|---|
| 55 | return $this->error("Unknown PHP Error");
|
|---|
| 56 | }
|
|---|
| 57 | $mapFileName = $mapDirectory ."/". sha1_file($_FILES['mapFile']['tmp_name']) .".mbtiles";
|
|---|
| 58 | if(move_uploaded_file($_FILES['mapFile']['tmp_name'], $mapFileName)){
|
|---|
| 59 | if(!chmod($mapFileName, 0755))
|
|---|
| 60 | return $this->error("Could not change permissions of the map file");
|
|---|
| 61 | // correct map metadata
|
|---|
| 62 | $resultRename = $this->setMapName($mapFileName, $mapName);
|
|---|
| 63 | if($resultRename[0] === true)
|
|---|
| 64 | return $this->output(["error" => 0, "errormessage" => ""]);
|
|---|
| 65 |
|
|---|
| 66 | return $this->error($resultRename[1]);
|
|---|
| 67 | }
|
|---|
| 68 | else
|
|---|
| 69 | return $this->error("Could not move map in place");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | private function setMapName($mbTilesFile, $mapName)
|
|---|
| 73 | {
|
|---|
| 74 | if(!is_file($mbTilesFile))
|
|---|
| 75 | return array(false, "setMapName - Map file not found: ". $mbTilesFile);
|
|---|
| 76 | if(empty($mapName) || $mapName == "")
|
|---|
| 77 | return array(false, "setMapName - Map name empty");
|
|---|
| 78 | try{
|
|---|
| 79 | $databaseConnection = new \PDO("sqlite:". $mbTilesFile);
|
|---|
| 80 | $databaseConnection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|---|
| 81 |
|
|---|
| 82 | $columnName = "name";
|
|---|
| 83 | $query = $databaseConnection->prepare("UPDATE metadata SET value=:value WHERE name=:name");
|
|---|
| 84 | $query->bindParam(":name", $columnName);
|
|---|
| 85 | $query->bindParam(":value", $mapName);
|
|---|
| 86 | if($query->execute() !== true){
|
|---|
| 87 | return array(false, "setMapName - Could not update metadata");
|
|---|
| 88 | }
|
|---|
| 89 | return array(true, "");
|
|---|
| 90 | }
|
|---|
| 91 | catch(\PDOException $e){
|
|---|
| 92 | return array(false, "setMapName - Database error: ". $e->getMessage());
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | ?>
|
|---|