From ecf0102fbcd340434ed8a6cbcdeb56449163a795 Mon Sep 17 00:00:00 2001 From: Bizarrus Date: Sat, 4 Nov 2023 00:33:43 +0100 Subject: [PATCH] finish user-editing --- classes/User.class.php | 25 ++-- default/admin/users.php | 21 ++-- default/admin/users/edit.php | 8 +- default/admin/users/edit/account.php | 2 +- default/email/new-password.html | 82 +++++++++++++ default/email/new-password.txt | 8 ++ handler/admin/users.php | 173 ++++++++++++++++++++++++++- 7 files changed, 292 insertions(+), 27 deletions(-) create mode 100644 default/email/new-password.html create mode 100644 default/email/new-password.txt diff --git a/classes/User.class.php b/classes/User.class.php index bca8f01..318f28b 100644 --- a/classes/User.class.php +++ b/classes/User.class.php @@ -5,21 +5,24 @@ use \fruithost\Database; class User { - private $id = null; - private $username = null; - private $email = null; - private $data = []; + private $id = null; + private $username = null; + private $email = null; + private $crypted_mail = null; + private $data = []; public function __construct() {} public function fetch(int $id) { - $result = Database::single('SELECT *, "**********" as `password` FROM `' . DATABASE_PREFIX . 'users` WHERE `id`=:id LIMIT 1', [ - 'id' => $id + $result = Database::single('SELECT *, "**********" as `password`, UPPER(SHA2(CONCAT(`id`, :salt, `email`), 512)) AS `crypted_mail` FROM `' . DATABASE_PREFIX . 'users` WHERE `id`=:id LIMIT 1', [ + 'id' => $id, + 'salt' => RESET_PASSWORD_SALT ]); - $this->id = $result->id; - $this->username = $result->username; - $this->email = $result->email; + $this->id = $result->id; + $this->username = $result->username; + $this->email = $result->email; + $this->crypted_mail = $result->crypted_mail; $this->data = Database::single('SELECT * FROM `' . DATABASE_PREFIX . 'users_data` WHERE `user_id`=:user_id LIMIT 1', [ 'user_id' => $this->id @@ -88,6 +91,10 @@ return sprintf('%s %s', $this->data->name_first, $this->data->name_last); } + public function getCryptedMail() : string | null { + return $this->crypted_mail; + } + public function getMail() : string | null { return $this->email; } diff --git a/default/admin/users.php b/default/admin/users.php index 2baf27e..4d1f93a 100644 --- a/default/admin/users.php +++ b/default/admin/users.php @@ -15,7 +15,7 @@ exit(); } ?> -
+ - - - - + + + +
diff --git a/default/admin/users/edit.php b/default/admin/users/edit.php index 03f74e5..635d70e 100644 --- a/default/admin/users/edit.php +++ b/default/admin/users/edit.php @@ -12,11 +12,13 @@ - + - + diff --git a/default/admin/users/edit/account.php b/default/admin/users/edit/account.php index 389f05c..0b7ddf4 100644 --- a/default/admin/users/edit/account.php +++ b/default/admin/users/edit/account.php @@ -13,7 +13,7 @@
- +
diff --git a/default/email/new-password.html b/default/email/new-password.html new file mode 100644 index 0000000..8079603 --- /dev/null +++ b/default/email/new-password.html @@ -0,0 +1,82 @@ + + + + + Your new password + + + +
+ fruithost +
+

Hey $USERNAME,

+

We've reset your password. Here is your new Account-Password:

+

$PASSWORD
+

Thanks,

+

The fruithost Team

+
+
+ + \ No newline at end of file diff --git a/default/email/new-password.txt b/default/email/new-password.txt new file mode 100644 index 0000000..be15da4 --- /dev/null +++ b/default/email/new-password.txt @@ -0,0 +1,8 @@ +Hey $USERNAME, + +We've reset your password. Here is your new Account-Password: +$PASSWORD + +Thanks, + +The fruithost Team \ No newline at end of file diff --git a/handler/admin/users.php b/handler/admin/users.php index 7a442ef..d5a9f90 100644 --- a/handler/admin/users.php +++ b/handler/admin/users.php @@ -2,17 +2,182 @@ use fruithost\Database; use fruithost\I18N; use fruithost\User; + use fruithost\Encryption; + use PHPMailer\PHPMailer; $users = Database::fetch('SELECT *, \'**********\' AS `password` FROM `' . DATABASE_PREFIX . 'users`'); if(isset($_POST['action'])) { - print_r($_POST['action']); - switch($_POST['action']) { + /* Save user */ case 'save': - /* Save user */ - $this->assign('success', $_POST); + $user = new User(); + $user->fetch($tab); + + switch($action) { + case 'settings': + if(isset($_POST['language'])) { + $user->setSettings('LANGUAGE', NULL, $_POST['language']); + } + + if(isset($_POST['time_format'])) { + $user->setSettings('TIME_FORMAT', NULL, $_POST['time_format']); + } + + if(isset($_POST['time_format'])) { + $user->setSettings('TIME_ZONE', NULL, $_POST['time_zone']); + } + + $this->assign('success', sprintf(I18N::get('Settings for %s was successfully updated.'), $user->getUsername())); + I18N::reload(); + break; + case 'password': + if(!empty($_POST['password_new']) && $_POST['password_new'] !== $_POST['password_repeated']) { + $template->assign('error', I18N::get('Please repeat your new password.')); + } else if(!empty($_POST['password_new']) && strlen($_POST['password_new']) < 8) { + $template->assign('error', I18N::get('The new password must contain the minimum number of 8 characters.')); + } elseif(!empty($_POST['password_new'])) { + Database::update(DATABASE_PREFIX . 'users', 'id', [ + 'id' => $user->getID(), + 'password' => strtoupper(hash('sha512', sprintf('%s%s%s', $user->getID(), MYSQL_PASSWORTD_SALT, $_POST['password_new']))), + 'lost_enabled' => 'NO', + 'lost_token' => NULL + ]); + } + + if(isset($_POST['2fa_enabled'])) { + $user->setSettings('2FA_ENABLED', NULL, 'true'); + } else { + $user->setSettings('2FA_ENABLED', NULL, 'false'); + } + + if(isset($_POST['2fa_type'])) { + $user->setSettings('2FA_TYPE', NULL, $_POST['2fa_type']); + } + + if(isset($_POST['password_recovery']) && count($_POST['password_recovery']) == 1) { + if(!filter_var($user->getMail(), FILTER_VALIDATE_EMAIL)) { + $template->assign('error', I18N::get('Can\t send recovery instructions, because the user has an invalid E-Mail address.')); + } else { + $token_raw = strtoupper(bin2hex(openssl_random_pseudo_bytes(32))); + $hash = Encryption::encrypt($token_raw, $user->getCryptedMail()); + $recovery_link = $template->url('/lost-password/' . $hash); + $password = strtoupper(bin2hex(openssl_random_pseudo_bytes(8))); + + if(array_key_exists('generate', $_POST['password_recovery'])) { + Database::update(DATABASE_PREFIX . 'users', 'id', [ + 'id' => $user->getID(), + 'password' => strtoupper(hash('sha512', sprintf('%s%s%s', $user->getID(), MYSQL_PASSWORTD_SALT, $password))), + 'lost_enabled' => 'NO', + 'lost_token' => NULL + ]); + } else { + Database::update(DATABASE_PREFIX . 'users', 'id', [ + 'id' => $user->getID(), + 'lost_enabled' => 'YES', + 'lost_time' => date('Y-m-d H:i:s', time()), + 'lost_token' => $token_raw + ]); + } + + $mail = new PHPMailer; + + if(defined('MAIL_EXTERNAL') && MAIL_EXTERNAL) { + $mail->Host = MAIL_HOSTNAME; + $mail->Port = MAIL_PORT; + $mail->SMTPAuth = true; + $mail->Username = MAIL_USERNAME; + $mail->Password = MAIL_PASSWORD; + $mail->isSMTP(); + } else { + $mail->isSendmail(); + } + + $mail->CharSet = 'utf-8'; + + if(array_key_exists('generate', $_POST['password_recovery'])) { + $mail->Subject = I18N::get('New password for your account'); + } else { + $mail->Subject = I18N::get('Reset your Password'); + } + + $mail->setFrom('no-reply@fruithost.de', 'fruithost'); + $mail->addAddress($user->getMail()); + + // @ToDo override E-Mail templates if an Theme as set!!!! + if(array_key_exists('generate', $_POST['password_recovery'])) { + $html_file = sprintf('%sdefault/email/new-password.html', PATH); + $text_file = sprintf('%sdefault/email/new-password.txt', PATH); + } else { + $html_file = sprintf('%sdefault/email/lost-password.html', PATH); + $text_file = sprintf('%sdefault/email/lost-password.txt', PATH); + } + + if(file_exists($text_file)) { + $text = str_replace([ + '$USERNAME', + '$LINK', + '$PASSWORD' + ], [ + $user->getUsername(), + $recovery_link, + $password + ], file_get_contents($text_file)); + } + + if(file_exists($html_file)) { + $mail->msgHTML(str_replace([ + '$USERNAME', + '$LINK', + '$PASSWORD' + ], [ + $user->getUsername(), + $recovery_link, + $password + ], file_get_contents($html_file)), __DIR__); + $mail->AltBody = $text; + } else { + $mail->Body = $text; + } + + if(!$mail->send()) { + $template->assign('error', $mail->ErrorInfo); + } else { + if(array_key_exists('generate', $_POST['password_recovery'])) { + $template->assign('success', I18N::get('The new generated password has been sent to users E-Mail adress!')); + } else { + $template->assign('success', I18N::get('Your password request has been sent to users E-Mail adress!')); + } + } + } + } + + $this->assign('success', sprintf(I18N::get('Security-Settings for %s was successfully updated.'), $user->getUsername())); + break; + default: + if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) { + $template->assign('error', I18N::get('Please enter a valid E-Mail address.')); + } + + Database::update(DATABASE_PREFIX . 'users_data', 'user_id', [ + 'user_id' => $user->getID(), + 'phone_number' => Encryption::encrypt($_POST['phone'], ENCRYPTION_SALT), + 'address' => Encryption::encrypt($_POST['address'], ENCRYPTION_SALT), + 'name_first' => Encryption::encrypt($_POST['name_first'], ENCRYPTION_SALT), + 'name_last' => Encryption::encrypt($_POST['name_last'], ENCRYPTION_SALT) + ]); + + Database::update(DATABASE_PREFIX . 'users', 'id', [ + 'id' => $user->getID(), + 'email' => $_POST['email'] + ]); + + /* @ToDo rename User, with all hosted files! */ + + $this->assign('success', sprintf(I18N::get('User %s was successfully edited.'), $user->getUsername())); + break; + } break; case 'delete': /* delete user */