<?php

/*
ET-Chat Copyright by SEDesign | ET-Chat v3.x.x Lizenz: CCPL | Autor: Evgeni Tcherkasski (info@s-e-d.de)
ET-Chat_T-FISH-MOD Weiterentwicklungen, Optimierungen und Ergänzungen am Code sind das geistige Eigentum von Tommy (Thomas Kuhn) und Harlekin (Bernd Witte).

Die darauf folgende Optimierungen und Ergänzungen am Code sind das Geistige Eigentum von Powermen (Christian K.) 
PHP 8/9 Ready, XML Parser, PDO Prepared Statements Get Set sicherheit usw.
*/

declare(strict_types=1);

class AdminCreateNewUser extends DbConectionMaker
{
    public function __construct()
    {
        parent::__construct();

        if (session_status() !== PHP_SESSION_ACTIVE) {
            session_start();
        }

        header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
        header('Content-Type: text/html; charset=utf-8');

        $langObj = new LangXml();
        $lang = $langObj->getLang()->admin[0]->admin_user[0] ?? (object)[];

        // -------------------------
        // Rechte prüfen
        // -------------------------
        $userPriv = $_SESSION["etchat_" . $this->_prefix . "user_priv"] ?? '';
        if (!in_array($userPriv, ["admin", "chatwache", "co_admin"], true)) {
            echo $lang->error[0]->tagData ?? 'Zugriff verweigert';
            return;
        }

        // -------------------------
        // CSRF Token (einheitlich)
        // -------------------------
        $csrfKey = 'etchat_' . $this->_prefix . 'csrf_token';
        if (empty($_SESSION[$csrfKey])) {
            $_SESSION[$csrfKey] = bin2hex(random_bytes(16));
        }
        $csrf_token = htmlspecialchars($_SESSION[$csrfKey], ENT_QUOTES, 'UTF-8');

        // -------------------------
        // Admin/Mod Benutzer laden
        // -------------------------
        $roles = ['admin', 'mod'];
        $placeholders = implode(',', array_fill(0, count($roles), '?'));

        $feld = $this->dbObj->sqlGet(
            "SELECT etchat_user_id, etchat_username, etchat_userpw, etchat_userprivilegien 
             FROM {$this->_prefix}etchat_user
             WHERE etchat_userprivilegien IN ($placeholders)",
            $roles
        );
        $this->dbObj->close();

        // -------------------------
        // Benutzerliste aufbereiten
        // -------------------------
        if (!empty($feld)) {
            $print_user_list = "<table>";
            foreach ($feld as $datasets) {
                $username = htmlspecialchars($datasets[1], ENT_QUOTES, 'UTF-8');
                $priv     = htmlspecialchars($datasets[3], ENT_QUOTES, 'UTF-8');
                $id       = (int)$datasets[0];

                $print_user_list .= "<tr>
                    <td>{$username}</td>
                    <td>(<i>{$priv}</i>)</td>
                    <td>&nbsp;&nbsp;&nbsp;</td>
                    <td><a href=\"./?AdminUserEdit&id={$id}\">" . ($lang->edit[0]->tagData ?? 'Edit') . "</a></td>
                </tr>";
            }
            $print_user_list .= "</table>";
        } else {
            $print_user_list = $lang->noadmins[0]->tagData ?? 'Keine Benutzer gefunden';
        }

        // -------------------------
        // Template
        // -------------------------
        $this->initTemplate($lang, $print_user_list, $csrf_token);
    }

    private function initTemplate(object $lang, string $print_user_list, string $csrf_token): void
    {
        include_once("styles/admin_tpl/createNewUser.tpl.html");
    }
}