PHP_AdminTool_Projekt/public/api/create_users_csv.php

111 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
session_start();
// Load config
$config = require __DIR__ . '/../../config/config.php';
// Simple login check (same as index.php)
$sessionKey = $config['security']['session_key_user'] ?? 'admin_user';
if (!isset($_SESSION[$sessionKey])) {
header('Location: ../index.php?route=login');
exit;
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ../index.php?route=createuser');
exit;
}
$delimiter = (string)($_POST['csvdelimiter'] ?? ',');
$hasHeader = (string)($_POST['hasHeader'] ?? '1');
// Two sources: uploaded file or csvcontent textarea
$csvContent = '';
if (!empty($_FILES['csvfile']['tmp_name']) && is_uploaded_file($_FILES['csvfile']['tmp_name'])) {
$csvContent = file_get_contents($_FILES['csvfile']['tmp_name']);
} else {
$csvContent = (string)($_POST['csvcontent'] ?? '');
}
if (trim($csvContent) === '') {
$_SESSION['flash_error'] = 'CSV ist leer. Bitte Datei auswählen oder Inhalt einfügen.';
header('Location: ../index.php?route=createuser');
exit;
}
// Write CSV to temp file
$tmpFile = tempnam(sys_get_temp_dir(), 'create_users_') . '.csv';
file_put_contents($tmpFile, $csvContent);
// Build payload with options
$payload = [
'input_file' => $tmpFile,
'delimiter' => $delimiter,
'has_header' => (bool)((int)$hasHeader),
'dry_run' => (bool)($config['powershell']['dry_run'] ?? false),
];
// Save options as JSON as the input to the PS script
$metaFile = tempnam(sys_get_temp_dir(), 'create_users_meta_') . '.json';
file_put_contents($metaFile, json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
$scriptDir = $config['powershell']['script_dir'] ?? __DIR__ . '/../../scripts/powershell';
$script = $scriptDir . DIRECTORY_SEPARATOR . 'create_users_csv.ps1';
$exe = $config['powershell']['exe'] ?? 'powershell';
$executionPolicy = $config['powershell']['execution_policy'] ?? 'Bypass';
$cmd = sprintf(
'%s -NoProfile -NonInteractive -ExecutionPolicy %s -File "%s" -InputFile "%s"',
$exe,
$executionPolicy,
$script,
$metaFile
);
$output = [];
$returnVar = null;
if (!file_exists($script)) {
$_SESSION['flash_error'] = 'PowerShell-Skript nicht gefunden: ' . $script;
@unlink($tmpFile);
@unlink($metaFile);
header('Location: ../index.php?route=createuser');
exit;
}
exec($cmd . ' 2>&1', $output, $returnVar);
$json = implode("\n", $output);
@unlink($tmpFile);
@unlink($metaFile);
// Optional: log the CSV script command and raw output to help debugging
@file_put_contents(__DIR__ . '/../logs/create_users_csv_output.log', date('Y-m-d H:i:s') . ' CMD: ' . $cmd . "\n" . $json . "\n\n", FILE_APPEND | LOCK_EX);
$result = null;
if ($json !== '') {
$decoded = json_decode($json, true);
if (is_array($decoded)) {
$result = $decoded;
}
}
if ($result === null) {
$_SESSION['flash_error'] = 'Unbekannter Fehler beim Ausführen des PowerShell-Skripts: ' . ($json ?: 'Keine Ausgabe');
header('Location: ../index.php?route=createuser');
exit;
}
if (!empty($result['success'])) {
$_SESSION['flash_success'] = $result['message'] ?? 'CSV verarbeitet.';
if (!empty($result['details'])) {
$_SESSION['csv_details'] = $result['details'];
}
} else {
$_SESSION['flash_error'] = $result['message'] ?? 'Fehler beim Verarbeiten der CSV.';
}
header('Location: ../index.php?route=createuser');
exit;