source: src/webapp/webservice/mapupload.php@ 82b4b49

0.5 app-cordova debian/bullseye debian/jessie
Last change on this file since 82b4b49 was 82b4b49, checked in by Joachim Langenbach <joachim.langenbach@…>, 9 years ago

Fixed map upload permissions

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[d7e6e9c]1<?php
2/*
[f03591a]3 Copyright (C) 2016 - 2017 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
[d7e6e9c]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*/
[6f38643]18
[f03591a]19function 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
[82b4b49]29 $columnName = "name";
30 $query = $databaseConnection->prepare("UPDATE metadata SET value=:value WHERE name=:name");
31 $query->bindParam(":name", $columnName);
[f03591a]32 $query->bindParam(":value", $mapName);
[82b4b49]33 if($query->execute() !== true){
34 return array(false, "setMapName - Could not update metadata");
35 }
[f03591a]36 return array(true, "");
37 }
38 catch(PDOException $e){
39 return array(false, "setMapName - Database error: ". $e->getMessage());
40 }
41}
42
43header('Content-type: application/json');
44
45$result["error"] = 1;
46$result["errormessage"] = "Unknown Error";
47
48if(!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"];
55if(empty($mapName) || $mapName == ""){
56 $result["error"] = 1;
57 $result["errormessage"] = "Please specifiy map name (empty value)";
58 echo json_encode($result);
59 return;
60}
[6f38643]61
[f03591a]62$mapDirectory = __DIR__ ."/maps";
[4972eea]63if(!array_key_exists("mapFile", $_FILES)){
[f03591a]64 $result["error"] = 1;
65 $result["errormessage"] = "No map file recieved";
[6f38643]66 echo json_encode($result);
67 return;
68}
[f03591a]69if (!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}
75switch ($_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";
96if(move_uploaded_file($_FILES['mapFile']['tmp_name'], $mapFileName)){
[82b4b49]97 if(!chmod($mapFileName, 0755)){
98 $result["error"] = 1;
99 $result["errormessage"] = "Could not change permissions of the map file";
[6f38643]100 }
[f03591a]101 else{
[82b4b49]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 }
[f03591a]112 }
113}
114else {
115 $result["error"] = 1;
116 $result["errormessage"] = "Could not move map in place";
[6f38643]117}
118echo json_encode($result);
119
[d7e6e9c]120?>
Note: See TracBrowser for help on using the repository browser.