source: src/webapp/webservice/inc/GnuBoat/ServiceMap.php@ 096625e

debian/bullseye
Last change on this file since 096625e was 0662b2d, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

Improved webservice class to use TrackItem in ServiceData.php and added some tests

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[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
19namespace GnuBoat;
20
21require_once(__DIR__ ."/../autoloader.php");
22
23class ServiceMap extends Service
24{
25 function __construct()
26 {
27 parent::__construct();
28 }
29
[1ba85cf]30 protected function process()
[c455d7b]31 {
[0662b2d]32 if(!array_key_exists("mapName", $_POST))
33 return $this->error("Please specify map name (no value)");
[c455d7b]34
35 $mapName = $_POST["mapName"];
[0662b2d]36 if(empty($mapName) || $mapName == "")
37 return $this->error("Please specifiy map name (empty value)");
[c455d7b]38
39 $mapDirectory = __DIR__ ."/../../maps";
[0662b2d]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
[c455d7b]46 switch ($_FILES['mapFile']['error']) {
47 case UPLOAD_ERR_OK:
48 break;
49 case UPLOAD_ERR_NO_FILE:
[0662b2d]50 return $this->error("No file sent");
[c455d7b]51 case UPLOAD_ERR_INI_SIZE:
52 case UPLOAD_ERR_FORM_SIZE:
[0662b2d]53 return $this->error("Exceeded filesize limit.");
[c455d7b]54 default:
[0662b2d]55 return $this->error("Unknown PHP Error");
[c455d7b]56 }
57 $mapFileName = $mapDirectory ."/". sha1_file($_FILES['mapFile']['tmp_name']) .".mbtiles";
58 if(move_uploaded_file($_FILES['mapFile']['tmp_name'], $mapFileName)){
[0662b2d]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]);
[c455d7b]67 }
[0662b2d]68 else
69 return $this->error("Could not move map in place");
[c455d7b]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?>
Note: See TracBrowser for help on using the repository browser.