Upload via GUI (267 Dateien)
This commit is contained in:
@@ -31,11 +31,47 @@ function import_json(array $data): never
|
||||
function import_ajax_guard(): void
|
||||
{
|
||||
header('Content-Type: application/json');
|
||||
if (!hash_equals($_SESSION['csrf_token'] ?? '', (string) ($_POST['csrf_token'] ?? ''))) import_json(['ok' => false, 'error' => 'CSRF']);
|
||||
if (!hash_equals($_SESSION['csrf_token'] ?? '', (string) ($_POST['csrf_token'] ?? ''))) {
|
||||
// Ist eine Anfrage größer als post_max_size, verwirft PHP alle Formulardaten: dann fehlt auch das Token
|
||||
import_json(['ok' => false, 'error' => 'Die Sitzung ist abgelaufen oder der Upload-Teil war größer als der Server erlaubt (post_max_size ' . ini_get('post_max_size') . '). Bitte die Seite neu laden und erneut versuchen.']);
|
||||
}
|
||||
}
|
||||
|
||||
$stateDir = osticket_state_dir();
|
||||
|
||||
/** PHP-Größenangabe ("2M", "128K", "1G") in Bytes. */
|
||||
function import_ini_bytes(string $v): int
|
||||
{
|
||||
$v = trim($v);
|
||||
if ($v === '' || $v === '-1') return PHP_INT_MAX;
|
||||
$n = (float) $v;
|
||||
return (int) match (strtolower(substr($v, -1))) { 'g' => $n * 1073741824, 'm' => $n * 1048576, 'k' => $n * 1024, default => $n };
|
||||
}
|
||||
|
||||
/** Größe eines Upload-Teils: so groß wie der Server erlaubt (höchstens 5 MB, mit Reserve), damit der Upload auch bei kleinen Limits (z. B. 2 MB) klappt. */
|
||||
function import_chunk_size(): int
|
||||
{
|
||||
$limit = min(import_ini_bytes((string) ini_get('upload_max_filesize')), import_ini_bytes((string) ini_get('post_max_size')));
|
||||
$size = (int) ($limit * 0.8);
|
||||
$size = intdiv($size, 65536) * 65536;
|
||||
return max(131072, min(5 * 1048576, $size));
|
||||
}
|
||||
|
||||
/** Verständliche Meldung zu einem PHP-Upload-Fehlercode. */
|
||||
function import_upload_error(?array $file): string
|
||||
{
|
||||
$code = $file['error'] ?? UPLOAD_ERR_NO_FILE;
|
||||
$limit = ini_get('upload_max_filesize') . ' / post_max_size ' . ini_get('post_max_size');
|
||||
return match ($code) {
|
||||
UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE => 'Der Server lehnt den Upload-Teil ab, er ist größer als erlaubt (upload_max_filesize ' . $limit . '). Laden Sie die Seite neu und versuchen Sie es erneut, oder legen Sie die Datei per FTP in den DeskHub-Ordner (Option 1).',
|
||||
UPLOAD_ERR_PARTIAL => 'Der Upload-Teil kam nur unvollständig an (Verbindung unterbrochen). Bitte erneut versuchen.',
|
||||
UPLOAD_ERR_NO_FILE => 'Es kam kein Upload-Teil an.',
|
||||
UPLOAD_ERR_NO_TMP_DIR => 'Auf dem Server fehlt der temporäre Ordner für Uploads (upload_tmp_dir). Bitte den Hoster fragen oder die Datei per FTP hochladen (Option 1).',
|
||||
UPLOAD_ERR_CANT_WRITE => 'Der Server konnte den Upload-Teil nicht speichern (Speicherplatz oder Schreibrechte). Bitte die Datei per FTP hochladen (Option 1).',
|
||||
default => 'Upload fehlgeschlagen (PHP-Fehlercode ' . $code . ').',
|
||||
};
|
||||
}
|
||||
|
||||
// ── Chunked Upload ─────────────────────────────────────────────────────────
|
||||
if (($_POST['action'] ?? '') === 'upload_chunk') {
|
||||
import_ajax_guard();
|
||||
@@ -43,10 +79,12 @@ if (($_POST['action'] ?? '') === 'upload_chunk') {
|
||||
$idx = (int) ($_POST['chunk_index'] ?? 0);
|
||||
$total = (int) ($_POST['total_chunks'] ?? 1);
|
||||
$tmpFile = $stateDir . '/upload.sql';
|
||||
if (!$chunk || $chunk['error'] !== UPLOAD_ERR_OK) import_json(['ok' => false, 'error' => 'Chunk-Fehler']);
|
||||
$fh = fopen($tmpFile, $idx === 0 ? 'wb' : 'ab');
|
||||
fwrite($fh, file_get_contents($chunk['tmp_name']));
|
||||
if (!$chunk || $chunk['error'] !== UPLOAD_ERR_OK) import_json(['ok' => false, 'error' => import_upload_error($chunk)]);
|
||||
$fh = @fopen($tmpFile, $idx === 0 ? 'wb' : 'ab');
|
||||
if (!$fh) import_json(['ok' => false, 'error' => 'Die Datei konnte nicht gespeichert werden: Der Import-Ordner ist nicht beschreibbar (' . $stateDir . ').']);
|
||||
$written = fwrite($fh, (string) file_get_contents($chunk['tmp_name']));
|
||||
fclose($fh);
|
||||
if ($written === false || $written < (int) filesize($chunk['tmp_name'])) import_json(['ok' => false, 'error' => 'Die Datei konnte nicht vollständig gespeichert werden (Speicherplatz voll?).']);
|
||||
$done = ($idx + 1) >= $total;
|
||||
if ($done) {
|
||||
$_SESSION['import_sql_path'] = $tmpFile;
|
||||
@@ -64,8 +102,11 @@ if (($_POST['action'] ?? '') === 'use_path') {
|
||||
$filename = basename(trim($_POST['sql_filename'] ?? ''));
|
||||
$fullPath = realpath(__DIR__ . '/../' . $filename);
|
||||
$rootDir = realpath(__DIR__ . '/..');
|
||||
if (!$filename || !$fullPath || strpos($fullPath, $rootDir) !== 0 || !file_exists($fullPath)) {
|
||||
flash('error', 'Datei nicht gefunden: ' . $filename); redirect('/staff/import_osticket.php');
|
||||
if ($filename === '') {
|
||||
flash('error', 'Bitte den Dateinamen der SQL-Datei eintragen (das graue Beispiel im Feld ist nur ein Platzhalter).'); redirect('/staff/import_osticket.php');
|
||||
}
|
||||
if (!$fullPath || strpos($fullPath, $rootDir) !== 0 || !is_file($fullPath)) {
|
||||
flash('error', 'Datei nicht gefunden: ' . $filename . '. Sie muss im DeskHub-Hauptordner auf dem Server liegen (dort, wo auch index.php liegt).'); redirect('/staff/import_osticket.php');
|
||||
}
|
||||
$_SESSION['import_sql_path'] = $fullPath;
|
||||
$_SESSION['import_filename'] = $filename;
|
||||
@@ -170,7 +211,7 @@ require __DIR__ . '/_header.php';
|
||||
<button type="submit" class="btn">Verwenden →</button>
|
||||
</form>
|
||||
<hr style="border:none;border-top:1px solid var(--border);margin-bottom:20px">
|
||||
<p style="font-weight:600;font-size:.88rem;margin-bottom:8px">Option 2: Direkt hochladen (in 5-MB-Teilen)</p>
|
||||
<p style="font-weight:600;font-size:.88rem;margin-bottom:8px">Option 2: Direkt hochladen (in Teilen von <?= round(import_chunk_size() / 1048576, 1) ?> MB, passend zum Limit dieses Servers)</p>
|
||||
<input type="file" id="sql-file-input" accept=".sql" style="margin-bottom:10px">
|
||||
<div id="upload-progress" style="display:none;margin-bottom:10px">
|
||||
<div style="background:var(--border);border-radius:20px;height:10px;overflow:hidden;margin-bottom:5px">
|
||||
@@ -187,7 +228,7 @@ require __DIR__ . '/_header.php';
|
||||
var btn = document.getElementById('upload-btn');
|
||||
btn.disabled = true;
|
||||
document.getElementById('upload-progress').style.display = 'block';
|
||||
var chunkSize = 5*1024*1024, totalChunks = Math.ceil(file.size/chunkSize), idx = 0;
|
||||
var chunkSize = <?= import_chunk_size() ?>, totalChunks = Math.ceil(file.size/chunkSize), idx = 0;
|
||||
function next() {
|
||||
var blob = file.slice(idx*chunkSize, (idx+1)*chunkSize);
|
||||
var fd = new FormData();
|
||||
@@ -195,12 +236,13 @@ require __DIR__ . '/_header.php';
|
||||
fd.append('chunk',blob,file.name); fd.append('chunk_index',idx);
|
||||
fd.append('total_chunks',totalChunks); fd.append('filename',file.name);
|
||||
fetch('/staff/import_osticket.php',{method:'POST',body:fd})
|
||||
.then(r=>r.json()).then(d=>{
|
||||
if(!d.ok){alert('Fehler: '+d.error);btn.disabled=false;return;}
|
||||
.then(r=>r.text()).then(function(t){
|
||||
var d; try { d = JSON.parse(t); } catch(e) { d = {ok:false,error:'Der Server hat den Upload-Teil nicht angenommen (vermutlich ist er größer als das Limit post_max_size ' + <?= json_encode((string) ini_get('post_max_size')) ?> + '). Laden Sie die Seite neu und versuchen Sie es erneut, oder legen Sie die Datei per FTP in den DeskHub-Ordner (Option 1).'}; }
|
||||
if(!d.ok){alert('Fehler: '+d.error);btn.disabled=false;document.getElementById('upload-bar').style.background='#dc2626';return;}
|
||||
idx++;
|
||||
var pct=Math.round(idx/totalChunks*100);
|
||||
document.getElementById('upload-bar').style.width=pct+'%';
|
||||
document.getElementById('upload-status').textContent=pct+'% - '+Math.round(idx*5)+' MB';
|
||||
document.getElementById('upload-status').textContent=pct+'% - '+Math.round(Math.min(idx*chunkSize,file.size)/1048576)+' von '+Math.round(file.size/1048576)+' MB';
|
||||
if(d.done){
|
||||
document.getElementById('upload-bar').style.background='#22c55e';
|
||||
setTimeout(()=>{window.location.href='/staff/import_osticket.php?step=analyze';},400);
|
||||
|
||||
Reference in New Issue
Block a user