Seite 1 von 1

Kleines Script - Hersteller Export / Import

Verfasst: 17 Jun 2026, 06:43
von juratextex
Dieses PHP-Script ermöglicht den einfachen Export und Import von Herstellern in Gambio GX5 über eine ZIP-Datei.

!!!!!!!!!!!!!!!!!!!!! Bitte sofort nach dem Einsatz löschen !!!!!!!!!!!!!!

🔹 Funktionen
📤 Export
Exportiert alle Hersteller in eine manufacturers.csv
Fügt alle Herstellerbilder automatisch hinzu
Erstellt eine fertige ZIP-Datei zum Download
📥 Import
Liest Herstellerdaten aus einer CSV-Datei (auch Excel-kompatibel)
Legt neue Hersteller an oder aktualisiert bestehende
Importiert automatisch die passenden Bilder
Erstellt fehlende Einträge in manufacturers_info (für Mehrsprachigkeit)

Ich hoff es hilft euch etwas


🔧 1. Script installieren
Datei z. B. als
manufacturer_impex.php speichern
In dein Gambio-Hauptverzeichnishochladen
(wo auch admin/ liegt)

Im Browser öffnen:

https://deinedomain.de/manufacturer_impex.php

🧪 6. Kontrolle im Shop

Nach Import prüfen:

Admin → Katalog → Hersteller
Frontend → Herstellerliste

👉 Falls nicht sichtbar:

Cache leeren (Gambio Admin)
Browser Cache leeren

Code: Alles auswählen

<?php
require_once('includes/application_top.php');

ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('auto_detect_line_endings', true);
set_time_limit(0);

// =========================
// CONFIG
// =========================
define('IMG_DIR', DIR_FS_CATALOG . 'images/manufacturers/');
define('TMP_DIR', DIR_FS_CATALOG . 'cache/manufacturer_impex/');

if (!file_exists(TMP_DIR)) {
    mkdir(TMP_DIR, 0777, true);
}

// =========================
// DB WRAPPER
// =========================
function db_query($sql) {
    $res = xtc_db_query($sql);
    if (!$res) {
        die("SQL Fehler: " . xtc_db_error() . "<br>Query: " . $sql);
    }
    return $res;
}

// =========================
// CSV ROBUST READER
// =========================
function readCsvRobust($file)
{
    $content = file_get_contents($file);

    $encoding = mb_detect_encoding($content, ['UTF-8', 'UTF-16', 'ISO-8859-1', 'Windows-1252'], true);

    if ($encoding === 'UTF-16') {
        $content = mb_convert_encoding($content, 'UTF-8', 'UTF-16');
    } elseif ($encoding !== 'UTF-8') {
        $content = mb_convert_encoding($content, 'UTF-8', $encoding);
    }

    $content = preg_replace('/^\xEF\xBB\xBF/', '', $content);
    $content = str_replace(["\r\n", "\r"], "\n", $content);

    $lines = explode("\n", $content);

    $delimiters = [';', ',', "\t"];
    $best = ';';
    $max = 0;

    foreach ($delimiters as $d) {
        $c = substr_count($lines[0], $d);
        if ($c > $max) {
            $max = $c;
            $best = $d;
        }
    }

    $data = [];

    foreach ($lines as $i => $line) {
        $line = trim($line);
        if ($line === '') continue;

        $row = str_getcsv($line, $best);

        if ($i === 0 && preg_match('/name/i', $row[0])) continue;

        $data[] = $row;
    }

    return $data;
}

// =========================
// IMAGE NORMALIZER (V1.3 FIX)
// =========================
function normalizeImagePath($input)
{
    $input = trim($input);
    $input = str_replace('\\', '/', $input);

    $file = basename($input);

    // 👉 FIX: IMMER manufacturers/ davor
    return 'manufacturers/' . ltrim($file, '/');
}

// =========================
// EXPORT
// =========================
if (isset($_GET['action']) && $_GET['action'] === 'export') {

    $csvFile = TMP_DIR . 'manufacturers.csv';
    $zipFile = TMP_DIR . 'manufacturers_export.zip';

    $fp = fopen($csvFile, 'w');
    fputcsv($fp, ['name', 'image'], ';');

    $res = db_query("SELECT manufacturers_name, manufacturers_image FROM manufacturers");

    while ($row = xtc_db_fetch_array($res)) {
        fputcsv($fp, $row, ';');
    }

    fclose($fp);

    $zip = new ZipArchive();
    $zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE);

    $zip->addFile($csvFile, 'manufacturers.csv');

    foreach (glob(IMG_DIR . '*') as $file) {
        $zip->addFile($file, 'images/' . basename($file));
    }

    $zip->close();

    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename="manufacturers_export.zip"');
    readfile($zipFile);
    exit;
}

// =========================
// IMPORT
// =========================
$message = "";

if (isset($_POST['import']) && isset($_FILES['zip'])) {

    $zipPath = TMP_DIR . basename($_FILES['zip']['name']);
    move_uploaded_file($_FILES['zip']['tmp_name'], $zipPath);

    $zip = new ZipArchive();
    if ($zip->open($zipPath) === TRUE) {
        $zip->extractTo(TMP_DIR);
        $zip->close();
    } else {
        die("ZIP konnte nicht geöffnet werden");
    }

    $csvFile = TMP_DIR . 'manufacturers.csv';

    if (!file_exists($csvFile)) {
        die("CSV fehlt!");
    }

    $rows = readCsvRobust($csvFile);

    $insert = 0;
    $update = 0;

    foreach ($rows as $data) {

        $name = xtc_db_input(trim($data[0] ?? ''));

        $imageRaw = $data[1] ?? '';
        $image = normalizeImagePath($imageRaw);
        $image = xtc_db_input($image);

        if ($name === '') continue;

        // check exist
        $check = db_query("
            SELECT manufacturers_id 
            FROM manufacturers 
            WHERE manufacturers_name = '$name'
            LIMIT 1
        ");

        if (xtc_db_num_rows($check) > 0) {

            $row = xtc_db_fetch_array($check);
            $id = (int)$row['manufacturers_id'];

            db_query("
                UPDATE manufacturers 
                SET manufacturers_image = '$image'
                WHERE manufacturers_id = $id
            ");

            $update++;

        } else {

            db_query("
                INSERT INTO manufacturers (manufacturers_name, manufacturers_image)
                VALUES ('$name', '$image')
            ");

            $id = xtc_db_insert_id();

            $insert++;
        }

        // manufacturers_info sicherstellen
        $info = db_query("
            SELECT manufacturers_id 
            FROM manufacturers_info 
            WHERE manufacturers_id = $id
        ");

        if (xtc_db_num_rows($info) == 0) {

            $langs = db_query("SELECT languages_id FROM languages");

            while ($l = xtc_db_fetch_array($langs)) {

                $lang_id = (int)$l['languages_id'];

                db_query("
                    INSERT INTO manufacturers_info (
                        manufacturers_id,
                        languages_id,
                        manufacturers_url
                    ) VALUES (
                        $id,
                        $lang_id,
                        ''
                    )
                ");
            }
        }

        // IMAGE COPY (robust)
        if ($image !== '') {

            $file = basename($imageRaw);

            $paths = [
                TMP_DIR . 'images/' . $file,
                TMP_DIR . 'images/manufacturers/' . $file,
                TMP_DIR . $file
            ];

            foreach ($paths as $src) {
                if (file_exists($src)) {
                    copy($src, IMG_DIR . $file);
                    break;
                }
            }
        }
    }

    $message = "✅ V1.3 Import fertig: $insert neu, $update aktualisiert";
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Hersteller Import / Export V1.3</title>
    <style>
        body { font-family: Arial; padding: 20px; }
        button { padding: 10px 15px; }
        .box { margin-bottom: 25px; }
    </style>
</head>
<body>

<h1>Hersteller Import / Export (Gambio GX5 V1.3)</h1>

<?php if ($message): ?>
    <div style="color:green;"><b><?php echo $message; ?></b></div>
<?php endif; ?>

<div class="box">
    <h2>📤 Export</h2>
    <a href="?action=export">
        <button>ZIP Export</button>
    </a>
</div>

<div class="box">
    <h2>📥 Import</h2>
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="zip" required>
        <br><br>
        <button type="submit" name="import">Import starten</button>
    </form>
</div>

<hr>

<h3>CSV Format</h3>
<pre>
name;image
Bosch;bosch.jpg
Siemens;manufacturers/siemens.jpg
</pre>

</body>
</html>

Re: Kleines Script - Hersteller Export / Import

Verfasst: 17 Jun 2026, 07:12
von Burkhard
Hallo,

danke für die Mühen.

Ich sehe aber gar keinen Zugriffsschutz? Dann sollte das Ding aber nach dem Einsatz gelöscht werden, oder? Kann man in images/manufacturers/ PHP ausführen? Weil es ja auch keine Dateitypenüberprüfung für den Upload gibt, oder sehe ich das falsch? In dem Fall könnte jeder durch Einbindung von ../../includes/configure.php den ganzen Shop übernehmen?

Re: Kleines Script - Hersteller Export / Import

Verfasst: 17 Jun 2026, 07:31
von juratextex
Sollte man sofort nach dem Einsatz löschen