pdo connector implementation

This commit is contained in:
AlbusRex 2023-10-11 16:25:39 +02:00
parent 24a6b2f370
commit c9473ea5b0

View File

@ -2,21 +2,20 @@
// !!! SET YOUR SQL-CONNECTION SETTINGS HERE: !!! // !!! SET YOUR SQL-CONNECTION SETTINGS HERE: !!!
$driver = 'pgsql'; //PDO driver name, check the PDO documentation for available drivers at https://www.php.net/manual/en/pdo.drivers.php. Common ones are 'pgsql' or 'mysql'.
$hostname = '127.0.0.1'; $hostname = '127.0.0.1';
$port = 3306; $port = 5432; // Remember to change this value to match your database configuration
$username = 'root'; $username = '';
$password = ''; $password = '';
$database = 'bluemap'; $database = 'bluemap';
// set this to "none" if you disabled compression on your maps // set this to "none" if you disabled compression on your maps
$hiresCompression = 'gzip'; $hiresCompression = 'gzip';
// !!! END - DONT CHANGE ANYTHING AFTER THIS LINE !!! // !!! END - DONT CHANGE ANYTHING AFTER THIS LINE !!!
// some helper functions // some helper functions
function error($code, $message = null) { function error($code, $message = null) {
global $path; global $path;
@ -111,9 +110,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, "$e"); }
// provide map-tiles // provide map-tiles
if (startsWith($mapPath, "tiles/")) { if (startsWith($mapPath, "tiles/")) {
@ -126,67 +128,72 @@ if (startsWith($path, "/maps/")) {
$compression = $lod === 0 ? $hiresCompression : "none"; $compression = $lod === 0 ? $hiresCompression : "none";
// query for tile // query for tile
$statement = $sql->prepare(" try {
SELECT t.`data` $statement = $sql->prepare("
FROM `bluemap_map_tile` t SELECT t.data
INNER JOIN `bluemap_map` m FROM bluemap_map_tile t
ON t.`map` = m.`id` INNER JOIN bluemap_map m
INNER JOIN `bluemap_map_tile_compression` c ON t.map = m.id
ON t.`compression` = c.`id` INNER JOIN bluemap_map_tile_compression c
WHERE m.`map_id` = ? ON t.compression = c.id
AND t.`lod` = ? WHERE m.map_id = :map_id
AND t.`x` = ? AND t.lod = :lod
AND t.`z` = ? AND t.x = :x
AND c.`compression` = ? AND t.z = :z
"); AND c.compression = :compression
$statement->bind_param("siiis", $mapId, $lod, $tileX, $tileZ, $compression); ");
$statement->execute(); $statement->bindParam( ':map_id', $mapId, PDO::PARAM_STR );
if ($statement->errno) error(500, "Database query failed!"); $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();
// 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) {
header("Content-Type: application/json");
if ($lod === 0) { } else {
header("Content-Type: application/json"); header("Content-Type: image/png");
} else { }
header("Content-Type: image/png"); fpassthru($line["data"]);
exit;
} }
echo $line["data"]; } catch (PDOException $e) { error(500, "$e"); }
exit;
}
// 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
$statement = $sql->prepare(" try {
SELECT t.`value` $statement = $sql->prepare("
FROM `bluemap_map_meta` t SELECT t.value
INNER JOIN `bluemap_map` m FROM bluemap_map_meta t
ON t.`map` = m.`id` INNER JOIN bluemap_map m
WHERE m.`map_id` = ? ON t.map = m.id
AND t.`key` = ? WHERE m.map_id = :map_id
"); AND t.key = :map_path
$statement->bind_param("ss", $mapId, $mapPath); ");
$statement->execute(); $statement->bindParam( ':map_id', $mapId, PDO::PARAM_STR );
if ($statement->errno) error(500, "Database query failed!"); $statement->bindParam( ':map_path', $mapPath, PDO::PARAM_STR );
$statement->setFetchMode(PDO::FETCH_ASSOC);
$statement->execute();
$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"]; fpassthru($line["value"]);
exit; exit;
} }
} catch (PDOException $e) { error(500, "$e"); }
} }
// no match => 404 // no match => 404
error(404); error(404);