LuminaWebsite/map_build_action.php
Maaxxs 52fea9f92d Karte: build a map from an image right on the page
Pick an image, press "Karte bauen", done - no FTP, no command line tool.
The panel sits on the setup page while no map exists and under the map as
"Karte neu bauen" afterwards.

The tiles are cut in the browser and only the finished 256px JPEGs are
uploaded. That is the point: on ordinary shared hosting a 50MB map image
runs into upload_max_filesize, cutting it server side runs into memory_limit
and max_execution_time, and GD is not installed everywhere. This way the
server needs none of it - it validates small JPEGs and stores them.

Uploads go into a side folder <key>__build that only replaces the live map
once every tile has arrived, so an abort - by button, by error or by closing
the browser - leaves the existing map untouched.

The server trusts nothing from the client: the key must match a strict
pattern, tile coordinates must lie inside 2^z on their level, and each file
is checked with getimagesize for an actual JPEG within a size limit before it
is stored.

Verified against tools/make_map_tiles.php on the same source image: same zoom
levels, same per-level tile counts (1/4/12/40), same tile names, same
manifest.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-20 14:57:11 +02:00

89 lines
3.4 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/map_build.php';
require_permission('map.manage');
// Anders als die uebrigen JSON-Endpunkte kommt hier multipart/form-data an (die Kacheln
// als Dateien), deshalb $_POST statt read_json_body().
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
json_response(false, 'Nur POST.');
}
verify_csrf();
$action = (string) ($_POST['action'] ?? '');
$key = strtolower(trim((string) ($_POST['key'] ?? '')));
if (!map_build_valid_key($key)) {
json_response(false, 'Ungültiger Kartenschlüssel (erlaubt: a-z, 0-9, - und _).');
}
try {
switch ($action) {
case 'begin':
$width = (int) ($_POST['width'] ?? 0);
$height = (int) ($_POST['height'] ?? 0);
$tileSize = (int) ($_POST['tile_size'] ?? 256);
$maxZoom = (int) ($_POST['max_zoom'] ?? 0);
$name = trim((string) ($_POST['name'] ?? ''));
if (mb_strlen($name) > 60) {
json_response(false, 'Der Name darf höchstens 60 Zeichen lang sein.');
}
map_build_begin($key, $name, $width, $height, $tileSize, $maxZoom);
json_response(true, 'Bauvorgang gestartet.', [
'expected_tiles' => map_build_tile_count($width, $height, $tileSize, $maxZoom),
]);
case 'tiles':
$state = map_build_state($key);
if ($state === null) {
json_response(false, 'Zu dieser Karte läuft kein Bauvorgang. Bitte neu starten.');
}
// Die Kacheln kommen als tile[] mit einem parallelen Feld coords[] im
// Format "z_x_y" - so bleibt die Zuordnung eindeutig, auch wenn PHP die
// Reihenfolge der Dateien anders sortieren wuerde.
$coords = is_array($_POST['coords'] ?? null) ? $_POST['coords'] : [];
$files = $_FILES['tile'] ?? null;
if (!is_array($files) || !is_array($files['tmp_name'] ?? null)) {
json_response(false, 'Es wurden keine Kacheln übertragen.');
}
$count = count($files['tmp_name']);
if ($count !== count($coords)) {
json_response(false, 'Kacheln und Koordinaten passen nicht zusammen.');
}
$written = 0;
for ($i = 0; $i < $count; $i++) {
if (preg_match('/^(\d+)_(\d+)_(\d+)$/', (string) $coords[$i], $m) !== 1) {
json_response(false, 'Ungültige Kachelkoordinate.');
}
map_build_store_tile($state, [
'error' => $files['error'][$i],
'size' => $files['size'][$i],
'tmp_name' => $files['tmp_name'][$i],
], (int) $m[1], (int) $m[2], (int) $m[3]);
$written++;
}
map_build_count($key, $written);
json_response(true, '', ['written' => $written]);
case 'finish':
$written = map_build_finish($key);
json_response(true, 'Karte fertig gebaut (' . $written . ' Kacheln).', ['tiles' => $written]);
case 'abort':
map_build_abort($key);
json_response(true, 'Bauvorgang abgebrochen, die bisherige Karte bleibt unverändert.');
default:
json_response(false, 'Unbekannte Aktion.');
}
} catch (Throwable $e) {
json_response(false, $e->getMessage());
}