<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
include_once ($_SERVER['DOCUMENT_ROOT'] . '/_config/functions.php');
spl_autoload_register('my_autoload');
$area                               = 'ADMINS';
$adminArea                          = 1;
include($_SERVER['DOCUMENT_ROOT'] . HOME_DIR . '/Auth/chk.auth.php');


$headline                           = '<h2>Neuer Benutzer</h2>';
$site                               = $_SERVER["HTTP_REFERER"];
$sitePrevious                       = $_SERVER["HTTP_REFERER"];

$user                               = new user();

if($_SERVER["REQUEST_METHOD"]       == "POST") {
  $userNameExist                    = $user->checkUsername($_POST['username']);
  $passwordRules                    = $user->checkPasswdComplexity($_POST['password'], 1, 8);
  
  if ($userNameExist                === false) {
    $errMsg                         = 'Benutzername ist bereits vergeben!';
    echo '<meta http-equiv="refresh" content="0; URL=' . HOME_DIR_HTML . TEMPLATE_DIR . '/users/error.php?err=' . urlencode($errMsg) . '">';
    exit();
  }
  
  if ($passwordRules                === false) {
    $errMsg                         = 'Das Passwort entspricht nicht den vogegebenen Regeln!';
    echo '<meta http-equiv="refresh" content="0; URL=' . HOME_DIR_HTML . TEMPLATE_DIR . '/users/error.php?err=' . urlencode($errMsg) . '">';
    exit();
  }
  
  $user->saveUserData($_POST);
  echo '<meta http-equiv="refresh" content="0; URL=' . $_POST['site'] . '">';

} else {
  $dropdownUserRoles                = $user->getUserRole();
  include ($_SERVER['DOCUMENT_ROOT'] . HOME_DIR . TEMPLATE_DIR . '/users/create.php');
}


