*/
/*
* Require files and ensure environment.
*/
require_once __DIR__ . "/inc/functions.php";
ensureEnvironment(); // Ensure existence of system files.
$translations = loadLanguage($settings['defaultLang']); // Load translations.
ensureBackendEnv(); // Ensure session is started etc.
$pages = loadPages(); // Load overview of pages.
if (!$_SESSION['admin']) {
echo printErrorPage($translations['accessDenied']); return;
}
/*
* Load data.
*/
// Check for vars.
loadHttpToGlobals(["task", "username", "realName", "email", "password", "passwordVerify", "admin"]);
if (!isset($users)) {
$users = json_decode(file_get_contents(__DIR__ . "/../data/users.json"), true);
}
/**
* Adding new users.
*/
if (isset($task) and $task == "insert") {
$redirectURL = "./users.php?" . write_common_vars(["username", "realName", "email", "admin"]) . "#addUser";
if (!isset($admin)) $admin = false;
// Ensure all required values are set.
foreach (["username", "realName", "email", "password", "passwordVerify"] as $var) {
if (isset($$var)) continue;
$_SESSION["editHistory"] = ["changesAborted", $translations['requiredValueMissing']];
header('Location: ' . $redirectURL);
return;
}
// Check if the passwords match.
if ($password != $passwordVerify) {
$_SESSION["editHistory"] = ["changesAborted", $translations['passwordsDoNotMatch']];
header('Location: ' . $redirectURL);
return;
}
// Check if passwords is too short.
if (strlen($password) < 8) {
$_SESSION["editHistory"] = ["changesAborted", $translations['passwordTooShort']];
header('Location: ' . $redirectURL);
return;
}
// Options for hashing.
$newUser = array(
"username" => $username,
"realName" => $realName,
"email" => $email,
"password" => password_hash("$password", PASSWORD_BCRYPT, ['cost' => 12]),
"admin" => $admin,
"created" => date("Y-m-d H:i:s"),
);
$users[$username] = $newUser;
// Store the users array.
file_put_contents(__DIR__ . "/../data/users.json", json_encode($users), LOCK_EX);
$_SESSION["editHistory"] = ["changesStored", $translations['userAdded'] . " $username"];
header('Location: ./users.php#addUser');
return;
}
/*
* Output
*/
echo printBackendHead($settings, $translations['start'], $translations['start'], $settings['logo']);
echo printBackendHeader($translations['usersOverview'], $translations['helpUsers']);
echo '
';
echo printBackendNav($translations);
echo '
' . $translations['listUsers'] . '
' . $translations['addUser'] . '
' . $translations['username'] . ' |
' . $translations['realName'] . ' |
' . $translations['email'] . ' |
' . $translations['admin'] . ' |
' . $translations['options'] . ' |
';
foreach ($users as $user) {
echo '
' . $user['username'] . ' |
' . $user['realName'] . ' |
' . $user['email'] . ' |
' . $user['admin'] . ' |
|
';
}
echo '
';
echo printBackendEnd();
?>