Skip to content

Commit 36090e8

Browse files
committed
2 parents 5866cb5 + 0bad1b6 commit 36090e8

File tree

1 file changed

+70
-53
lines changed

1 file changed

+70
-53
lines changed
Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
// !!! SET YOUR SQL-CONNECTION SETTINGS HERE: !!!
44

5+
$driver = 'mysql'; // 'mysql' (MySQL) or 'pgsql' (PostgreSQL)
56
$hostname = '127.0.0.1';
67
$port = 3306;
78
$username = 'root';
@@ -85,6 +86,14 @@ function getMimeType($path) {
8586
return $mimeDefault;
8687
}
8788

89+
function send($data) {
90+
if (is_resource($data)) {
91+
fpassthru($data);
92+
} else {
93+
echo $data;
94+
}
95+
}
96+
8897
// determine relative request-path
8998
$root = dirname($_SERVER['PHP_SELF']);
9099
if ($root === "/" || $root === "\\") $root = "";
@@ -111,9 +120,12 @@ function getMimeType($path) {
111120
$mapId = $pathParts[0];
112121
$mapPath = explode("?", $pathParts[1], 2)[0];
113122

114-
// get sql-connection
115-
$sql = new mysqli($hostname, $username, $password, $database, $port);
116-
if ($sql->errno) error(500, "Failed to connect to Database!");
123+
// Initialize PDO
124+
try {
125+
$sql = new PDO("$driver:host=$hostname;dbname=$database", $username, $password);
126+
$sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
127+
} catch (PDOException $e ) { error(500, "Failed to connect to database"); }
128+
117129

118130
// provide map-tiles
119131
if (startsWith($mapPath, "tiles/")) {
@@ -126,67 +138,72 @@ function getMimeType($path) {
126138
$compression = $lod === 0 ? $hiresCompression : "none";
127139

128140
// query for tile
129-
$statement = $sql->prepare("
130-
SELECT t.`data`
131-
FROM `bluemap_map_tile` t
132-
INNER JOIN `bluemap_map` m
133-
ON t.`map` = m.`id`
134-
INNER JOIN `bluemap_map_tile_compression` c
135-
ON t.`compression` = c.`id`
136-
WHERE m.`map_id` = ?
137-
AND t.`lod` = ?
138-
AND t.`x` = ?
139-
AND t.`z` = ?
140-
AND c.`compression` = ?
141-
");
142-
$statement->bind_param("siiis", $mapId, $lod, $tileX, $tileZ, $compression);
143-
$statement->execute();
144-
if ($statement->errno) error(500, "Database query failed!");
145-
146-
// return result
147-
$result = $statement->get_result();
148-
if ($result && $line = $result->fetch_assoc()) {
149-
if ($compression !== "none")
150-
header("Content-Encoding: $compression");
151-
152-
if ($lod === 0) {
153-
header("Content-Type: application/json");
154-
} else {
155-
header("Content-Type: image/png");
141+
try {
142+
$statement = $sql->prepare("
143+
SELECT t.data
144+
FROM bluemap_map_tile t
145+
INNER JOIN bluemap_map m
146+
ON t.map = m.id
147+
INNER JOIN bluemap_map_tile_compression c
148+
ON t.compression = c.id
149+
WHERE m.map_id = :map_id
150+
AND t.lod = :lod
151+
AND t.x = :x
152+
AND t.z = :z
153+
AND c.compression = :compression
154+
");
155+
$statement->bindParam( ':map_id', $mapId, PDO::PARAM_STR );
156+
$statement->bindParam( ':lod', $lod, PDO::PARAM_INT );
157+
$statement->bindParam( ':x', $tileX, PDO::PARAM_INT );
158+
$statement->bindParam( ':z', $tileZ, PDO::PARAM_INT );
159+
$statement->bindParam( ':compression', $compression, PDO::PARAM_STR);
160+
$statement->setFetchMode(PDO::FETCH_ASSOC);
161+
$statement->execute();
162+
163+
// return result
164+
if ($line = $statement->fetch()) {
165+
if ($compression !== "none")
166+
header("Content-Encoding: $compression");
167+
if ($lod === 0) {
168+
header("Content-Type: application/json");
169+
} else {
170+
header("Content-Type: image/png");
171+
}
172+
send($line["data"]);
173+
exit;
156174
}
157175

158-
echo $line["data"];
159-
exit;
160-
}
176+
} catch (PDOException $e) { error(500, "Failed to fetch data"); }
161177

162178
// empty json response if nothing found
163179
header("Content-Type: application/json");
164180
echo "{}";
165181
exit;
166-
167182
}
168183

169184
// provide meta-files
170-
$statement = $sql->prepare("
171-
SELECT t.`value`
172-
FROM `bluemap_map_meta` t
173-
INNER JOIN `bluemap_map` m
174-
ON t.`map` = m.`id`
175-
WHERE m.`map_id` = ?
176-
AND t.`key` = ?
177-
");
178-
$statement->bind_param("ss", $mapId, $mapPath);
179-
$statement->execute();
180-
if ($statement->errno) error(500, "Database query failed!");
181-
182-
$result = $statement->get_result();
183-
if ($result && $line = $result->fetch_assoc()) {
184-
header("Content-Type: ".getMimeType($mapPath));
185-
echo $line["value"];
186-
exit;
187-
}
185+
try {
186+
$statement = $sql->prepare("
187+
SELECT t.value
188+
FROM bluemap_map_meta t
189+
INNER JOIN bluemap_map m
190+
ON t.map = m.id
191+
WHERE m.map_id = :map_id
192+
AND t.key = :map_path
193+
");
194+
$statement->bindParam( ':map_id', $mapId, PDO::PARAM_STR );
195+
$statement->bindParam( ':map_path', $mapPath, PDO::PARAM_STR );
196+
$statement->setFetchMode(PDO::FETCH_ASSOC);
197+
$statement->execute();
198+
199+
if ($line = $statement->fetch()) {
200+
header("Content-Type: ".getMimeType($mapPath));
201+
send($line["value"]);
202+
exit;
203+
}
204+
} catch (PDOException $e) { error(500, "Failed to fetch data"); }
188205

189206
}
190207

191208
// no match => 404
192-
error(404);
209+
error(404);

0 commit comments

Comments
 (0)