Skip to content

Commit

Permalink
pdo connector implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbusRex committed Oct 11, 2023
1 parent 24a6b2f commit c9473ea
Showing 1 changed file with 63 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

// !!! 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';
$port = 3306;
$username = 'root';
$port = 5432; // Remember to change this value to match your database configuration
$username = '';
$password = '';
$database = 'bluemap';

// set this to "none" if you disabled compression on your maps
$hiresCompression = 'gzip';

// !!! END - DONT CHANGE ANYTHING AFTER THIS LINE !!!



// !!! END - DONT CHANGE ANYTHING AFTER THIS LINE !!!


// some helper functions
Expand Down Expand Up @@ -111,9 +110,12 @@ function getMimeType($path) {
$mapId = $pathParts[0];
$mapPath = explode("?", $pathParts[1], 2)[0];

// get sql-connection
$sql = new mysqli($hostname, $username, $password, $database, $port);
if ($sql->errno) error(500, "Failed to connect to Database!");
// Initialize PDO
try {
$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
if (startsWith($mapPath, "tiles/")) {
Expand All @@ -126,67 +128,72 @@ function getMimeType($path) {
$compression = $lod === 0 ? $hiresCompression : "none";

// query for tile
$statement = $sql->prepare("
SELECT t.`data`
FROM `bluemap_map_tile` t
INNER JOIN `bluemap_map` m
ON t.`map` = m.`id`
INNER JOIN `bluemap_map_tile_compression` c
ON t.`compression` = c.`id`
WHERE m.`map_id` = ?
AND t.`lod` = ?
AND t.`x` = ?
AND t.`z` = ?
AND c.`compression` = ?
");
$statement->bind_param("siiis", $mapId, $lod, $tileX, $tileZ, $compression);
$statement->execute();
if ($statement->errno) error(500, "Database query failed!");

// return result
$result = $statement->get_result();
if ($result && $line = $result->fetch_assoc()) {
if ($compression !== "none")
header("Content-Encoding: $compression");

if ($lod === 0) {
header("Content-Type: application/json");
} else {
header("Content-Type: image/png");
try {
$statement = $sql->prepare("
SELECT t.data
FROM bluemap_map_tile t
INNER JOIN bluemap_map m
ON t.map = m.id
INNER JOIN bluemap_map_tile_compression c
ON t.compression = c.id
WHERE m.map_id = :map_id
AND t.lod = :lod
AND t.x = :x
AND t.z = :z
AND c.compression = :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();

// return result
if ($line = $statement->fetch()) {
if ($compression !== "none")
header("Content-Encoding: $compression");
if ($lod === 0) {
header("Content-Type: application/json");
} else {
header("Content-Type: image/png");
}
fpassthru($line["data"]);
exit;
}

echo $line["data"];
exit;
}
} catch (PDOException $e) { error(500, "$e"); }

// empty json response if nothing found
header("Content-Type: application/json");
echo "{}";
exit;

}

// provide meta-files
$statement = $sql->prepare("
SELECT t.`value`
FROM `bluemap_map_meta` t
INNER JOIN `bluemap_map` m
ON t.`map` = m.`id`
WHERE m.`map_id` = ?
AND t.`key` = ?
");
$statement->bind_param("ss", $mapId, $mapPath);
$statement->execute();
if ($statement->errno) error(500, "Database query failed!");

$result = $statement->get_result();
if ($result && $line = $result->fetch_assoc()) {
try {
$statement = $sql->prepare("
SELECT t.value
FROM bluemap_map_meta t
INNER JOIN bluemap_map m
ON t.map = m.id
WHERE m.map_id = :map_id
AND t.key = :map_path
");
$statement->bindParam( ':map_id', $mapId, PDO::PARAM_STR );
$statement->bindParam( ':map_path', $mapPath, PDO::PARAM_STR );
$statement->setFetchMode(PDO::FETCH_ASSOC);
$statement->execute();

if ($line = $statement->fetch()) {
header("Content-Type: ".getMimeType($mapPath));
echo $line["value"];
fpassthru($line["value"]);
exit;
}
}
} catch (PDOException $e) { error(500, "$e"); }

}

// no match => 404
error(404);
error(404);

0 comments on commit c9473ea

Please sign in to comment.