streams/Code/Module/Settings/Account.php

126 lines
3.9 KiB
PHP
Raw Normal View History

2016-09-07 03:10:56 +00:00
<?php
2022-02-16 04:08:28 +00:00
namespace Code\Module\Settings;
2016-09-07 03:10:56 +00:00
2021-12-02 22:33:36 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Extend\Hook;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2021-12-02 22:33:36 +00:00
2021-12-02 23:02:31 +00:00
class Account
{
public function post()
{
check_form_security_token_redirectOnErr('/settings/account', 'settings_account');
Hook::call('account_settings_post', $_POST);
2021-12-02 23:02:31 +00:00
$errs = [];
$email = ((x($_POST, 'email')) ? trim(notags($_POST['email'])) : '');
$account = App::get_account();
if ($email != $account['account_email']) {
2021-12-03 03:01:39 +00:00
if (!validate_email($email)) {
2021-12-02 23:02:31 +00:00
$errs[] = t('Not valid email.');
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$adm = trim(get_config('system', 'admin_email'));
if (($adm) && (strcasecmp($email, $adm) == 0)) {
$errs[] = t('Protected email address. Cannot change to that email.');
$email = App::$account['account_email'];
}
if (!$errs) {
2021-12-03 03:01:39 +00:00
$r = q(
"update account set account_email = '%s' where account_id = %d",
2021-12-02 23:02:31 +00:00
dbesc($email),
intval($account['account_id'])
);
2021-12-03 03:01:39 +00:00
if (!$r) {
2021-12-02 23:02:31 +00:00
$errs[] = t('System failure storing new email. Please try again.');
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
}
if ($errs) {
2021-12-03 03:01:39 +00:00
foreach ($errs as $err) {
2021-12-02 23:02:31 +00:00
notice($err . EOL);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
$errs = [];
}
if ((x($_POST, 'npassword')) || (x($_POST, 'confirm'))) {
$origpass = trim($_POST['origpass']);
require_once('include/auth.php');
if (!account_verify_password($email, $origpass)) {
$errs[] = t('Password verification failed.');
}
$newpass = trim($_POST['npassword']);
$confirm = trim($_POST['confirm']);
if ($newpass != $confirm) {
$errs[] = t('Passwords do not match. Password unchanged.');
}
if ((!x($newpass)) || (!x($confirm))) {
$errs[] = t('Empty passwords are not allowed. Password unchanged.');
}
if (!$errs) {
$salt = random_string(32);
$password_encoded = hash('whirlpool', $salt . $newpass);
2021-12-03 03:01:39 +00:00
$r = q(
"update account set account_salt = '%s', account_password = '%s', account_password_changed = '%s'
2016-09-07 03:10:56 +00:00
where account_id = %d",
2021-12-02 23:02:31 +00:00
dbesc($salt),
dbesc($password_encoded),
dbesc(datetime_convert()),
intval(get_account_id())
);
2021-12-03 03:01:39 +00:00
if ($r) {
2021-12-02 23:02:31 +00:00
info(t('Password changed.') . EOL);
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
$errs[] = t('Password update failed. Please try again.');
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
}
if ($errs) {
2021-12-03 03:01:39 +00:00
foreach ($errs as $err) {
2021-12-02 23:02:31 +00:00
notice($err . EOL);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
}
goaway(z_root() . '/settings/account');
}
public function get()
{
$account_settings = "";
Hook::call('account_settings', $account_settings);
2021-12-02 23:02:31 +00:00
$email = App::$account['account_email'];
2022-02-12 20:43:29 +00:00
$tpl = Theme::get_template("settings_account.tpl");
2021-12-02 23:02:31 +00:00
$o .= replace_macros($tpl, array(
'$form_security_token' => get_form_security_token("settings_account"),
'$title' => t('Account Settings'),
'$origpass' => array('origpass', t('Current Password'), ' ', ''),
'$password1' => array('npassword', t('Enter New Password'), '', ''),
'$password2' => array('confirm', t('Confirm New Password'), '', t('Leave password fields blank unless changing')),
'$submit' => t('Submit'),
'$email' => array('email', t('Email Address:'), $email, ''),
'$removeme' => t('Remove Account'),
'$removeaccount' => t('Remove this account including all its channels'),
'$account_settings' => $account_settings
));
return $o;
}
2016-09-12 13:03:51 +00:00
}