This commit is contained in:
Lukas Rieger (Blue) 2023-11-21 16:16:01 +01:00
commit 36090e8545
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2

View File

@ -2,6 +2,7 @@
// !!! SET YOUR SQL-CONNECTION SETTINGS HERE: !!! // !!! SET YOUR SQL-CONNECTION SETTINGS HERE: !!!
$driver = 'mysql'; // 'mysql' (MySQL) or 'pgsql' (PostgreSQL)
$hostname = '127.0.0.1'; $hostname = '127.0.0.1';
$port = 3306; $port = 3306;
$username = 'root'; $username = 'root';
@ -85,6 +86,14 @@ function getMimeType($path) {
return $mimeDefault; return $mimeDefault;
} }
function send($data) {
if (is_resource($data)) {
fpassthru($data);
} else {
echo $data;
}
}
// determine relative request-path // determine relative request-path
$root = dirname($_SERVER['PHP_SELF']); $root = dirname($_SERVER['PHP_SELF']);
if ($root === "/" || $root === "\\") $root = ""; if ($root === "/" || $root === "\\") $root = "";
@ -111,9 +120,12 @@ if (startsWith($path, "/maps/")) {
$mapId = $pathParts[0]; $mapId = $pathParts[0];
$mapPath = explode("?", $pathParts[1], 2)[0]; $mapPath = explode("?", $pathParts[1], 2)[0];
// get sql-connection // Initialize PDO
$sql = new mysqli($hostname, $username, $password, $database, $port); try {
if ($sql->errno) error(500, "Failed to connect to Database!"); $sql = new PDO("$driver:host=$hostname;dbname=$database", $username, $password);
$sql->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e ) { error(500, "Failed to connect to database"); }
// provide map-tiles // provide map-tiles
if (startsWith($mapPath, "tiles/")) { if (startsWith($mapPath, "tiles/")) {
@ -126,65 +138,70 @@ if (startsWith($path, "/maps/")) {
$compression = $lod === 0 ? $hiresCompression : "none"; $compression = $lod === 0 ? $hiresCompression : "none";
// query for tile // query for tile
try {
$statement = $sql->prepare(" $statement = $sql->prepare("
SELECT t.`data` SELECT t.data
FROM `bluemap_map_tile` t FROM bluemap_map_tile t
INNER JOIN `bluemap_map` m INNER JOIN bluemap_map m
ON t.`map` = m.`id` ON t.map = m.id
INNER JOIN `bluemap_map_tile_compression` c INNER JOIN bluemap_map_tile_compression c
ON t.`compression` = c.`id` ON t.compression = c.id
WHERE m.`map_id` = ? WHERE m.map_id = :map_id
AND t.`lod` = ? AND t.lod = :lod
AND t.`x` = ? AND t.x = :x
AND t.`z` = ? AND t.z = :z
AND c.`compression` = ? AND c.compression = :compression
"); ");
$statement->bind_param("siiis", $mapId, $lod, $tileX, $tileZ, $compression); $statement->bindParam( ':map_id', $mapId, PDO::PARAM_STR );
$statement->bindParam( ':lod', $lod, PDO::PARAM_INT );
$statement->bindParam( ':x', $tileX, PDO::PARAM_INT );
$statement->bindParam( ':z', $tileZ, PDO::PARAM_INT );
$statement->bindParam( ':compression', $compression, PDO::PARAM_STR);
$statement->setFetchMode(PDO::FETCH_ASSOC);
$statement->execute(); $statement->execute();
if ($statement->errno) error(500, "Database query failed!");
// return result // return result
$result = $statement->get_result(); if ($line = $statement->fetch()) {
if ($result && $line = $result->fetch_assoc()) {
if ($compression !== "none") if ($compression !== "none")
header("Content-Encoding: $compression"); header("Content-Encoding: $compression");
if ($lod === 0) { if ($lod === 0) {
header("Content-Type: application/json"); header("Content-Type: application/json");
} else { } else {
header("Content-Type: image/png"); header("Content-Type: image/png");
} }
send($line["data"]);
echo $line["data"];
exit; exit;
} }
} catch (PDOException $e) { error(500, "Failed to fetch data"); }
// empty json response if nothing found // empty json response if nothing found
header("Content-Type: application/json"); header("Content-Type: application/json");
echo "{}"; echo "{}";
exit; exit;
} }
// provide meta-files // provide meta-files
try {
$statement = $sql->prepare(" $statement = $sql->prepare("
SELECT t.`value` SELECT t.value
FROM `bluemap_map_meta` t FROM bluemap_map_meta t
INNER JOIN `bluemap_map` m INNER JOIN bluemap_map m
ON t.`map` = m.`id` ON t.map = m.id
WHERE m.`map_id` = ? WHERE m.map_id = :map_id
AND t.`key` = ? AND t.key = :map_path
"); ");
$statement->bind_param("ss", $mapId, $mapPath); $statement->bindParam( ':map_id', $mapId, PDO::PARAM_STR );
$statement->bindParam( ':map_path', $mapPath, PDO::PARAM_STR );
$statement->setFetchMode(PDO::FETCH_ASSOC);
$statement->execute(); $statement->execute();
if ($statement->errno) error(500, "Database query failed!");
$result = $statement->get_result(); if ($line = $statement->fetch()) {
if ($result && $line = $result->fetch_assoc()) {
header("Content-Type: ".getMimeType($mapPath)); header("Content-Type: ".getMimeType($mapPath));
echo $line["value"]; send($line["value"]);
exit; exit;
} }
} catch (PDOException $e) { error(500, "Failed to fetch data"); }
} }