prepare( 'SELECT null FROM mailbox WHERE username=? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
if ( ! $user = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$_SESSION = [];
session_regenerate_id( true );
$_SESSION[ 'csrf_token' ] = sha1( uniqid() );
$msg .= '
'.htmlspecialchars(_('It looks like your user no longer exists!')).'
';
}
}
if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
if ( $_SESSION[ 'csrf_token' ] !== $_POST[ 'csrf_token' ] ?? '' ) {
die( 'Invalid CSRF token' );
}
if ( isset( $_SESSION[ 'user_totp_pending' ] ) ) {
$db = get_db_instance();
$stmt = $db->prepare( 'SELECT totp_secret FROM mailbox WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'user_totp_pending' ] ] );
$pending = $stmt->fetch( PDO::FETCH_ASSOC );
if ( ! $pending || empty( $pending[ 'totp_secret' ] ) ) {
unset( $_SESSION[ 'user_totp_pending' ] );
$msg .= ''.htmlspecialchars(_('Incorrect username or password')).'
';
} elseif ( verify_totp_code( $pending[ 'totp_secret' ], $_POST[ 'totp_code' ] ?? '' ) ) {
$_SESSION[ 'email_user' ] = $_SESSION[ 'user_totp_pending' ];
unset( $_SESSION[ 'user_totp_pending' ] );
session_regenerate_id( true );
$_SESSION[ 'csrf_token' ] = sha1( uniqid() );
$stmt = $db->prepare( 'UPDATE mailbox SET last_login = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ time(), $_SESSION[ 'email_user' ] ] );
} else {
$msg .= ''.htmlspecialchars(_('Wrong 2FA code')).'
';
}
} elseif ( isset( $_SESSION[ '2fa_code' ] ) ) {
if ( ! empty( $_POST[ '2fa_code' ] ) && $_POST[ '2fa_code' ] === $_SESSION[ '2fa_code' ] ) {
unset( $_SESSION[ '2fa_code' ] );
unset( $_SESSION[ 'pgp_key' ] );
} else {
$msg .= ''.htmlspecialchars(_('Wrong 2FA code')).'
';
}
}
if ( ! isset( $_SESSION[ '2fa_code' ] ) && ! isset( $_SESSION[ 'user_totp_pending' ] ) && isset( $_POST[ 'action' ] ) ) {
if ( $_POST[ 'action' ] === 'logout' ) {
$_SESSION = [];
session_regenerate_id( true );
$_SESSION[ 'csrf_token' ] = sha1( uniqid() );
$msg .= ''.htmlspecialchars(_('Successfully logged out')).'
';
} elseif ( $_POST[ 'action' ] === 'login' ) {
$ok = true;
if ( ! check_captcha( $_POST[ 'challenge' ] ?? '', $_POST[ 'captcha' ] ?? '' ) ) {
$ok = false;
$msg .= ''.htmlspecialchars(_('Invalid captcha')).'
';
}
if ( empty( $_POST[ 'user' ] ) || ! preg_match( '/^([^+]+?)(@([^@]+))?$/i', $_POST[ 'user' ], $match ) ) {
$ok = false;
$msg .= ''.htmlspecialchars(_('Invalid username')).'
';
}
if ( $ok ) {
$db = get_db_instance();
$user = $match[ 1 ];
$domain = $match[ 3 ] ?? CLEARNET_SERVER;
$stmt = $db->prepare( 'SELECT target_domain FROM alias_domain WHERE alias_domain = ? AND active=1;' );
$stmt->execute( [ $domain ] );
if ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$domain = $tmp[ 'target_domain' ];
}
$stmt = $db->prepare( 'SELECT username, password, password_hash_type, tfa, pgp_key, totp_secret FROM mailbox WHERE username = ? AND active = 1;' );
$stmt->execute( [ "$user@$domain" ] );
if ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
if ( empty( $_POST[ 'pwd' ] ) || ! password_verify( $_POST[ 'pwd' ], $tmp[ 'password' ] ) ) {
$ok = false;
$msg .= ''.htmlspecialchars(_('Incorrect username or password')).'
';
} else {
// update password hash if it's using an old hashing algorithm
if ( $tmp[ 'password_hash_type' ] !== '{ARGON2ID}' ) {
$hash = password_hash( $_POST[ 'pwd' ], PASSWORD_ARGON2ID );
$stmt = $db->prepare( 'UPDATE mailbox SET password_hash_type = "{ARGON2ID}", password = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ $hash, $tmp[ 'username' ] ] );
}
if ( ! empty( $tmp[ 'totp_secret' ] ) ) {
unset( $_SESSION[ 'email_user' ] );
session_regenerate_id( true );
$_SESSION[ 'csrf_token' ] = sha1( uniqid() );
$_SESSION[ 'user_totp_pending' ] = $tmp[ 'username' ];
} else {
$_SESSION[ 'email_user' ] = $tmp[ 'username' ];
$stmt = $db->prepare( 'UPDATE mailbox SET last_login = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ time(), $_SESSION[ 'email_user' ] ] );
if ( $tmp[ 'tfa' ] ) {
$code = bin2hex( random_bytes( 3 ) );
$_SESSION[ '2fa_code' ] = $code;
$_SESSION[ 'pgp_key' ] = $tmp[ 'pgp_key' ];
}
}
}
} else {
$msg .= ''.htmlspecialchars(_('Incorrect username or password')).'
';
}
}
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_settings' ) {
$alias_goto = '';
if ( isset( $_POST[ 'alias_keep_copy' ] ) ) {
$alias_goto .= $_SESSION[ 'email_user' ] . ',';
}
if ( ! empty( $_POST[ 'alias_to' ] ) ) {
$additional = preg_split( "/[\s,]+/", $_POST[ 'alias_to' ] );
$alias_goto .= validate_email_list( $additional, $msg );
}
$alias_goto = rtrim( $alias_goto, ',' );
$stmt = $db->prepare( 'UPDATE alias SET goto = ?, enforce_tls_in = ? WHERE address = ? AND active = 1;' );
$stmt->execute( [ $alias_goto, ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), $_SESSION[ 'email_user' ] ] );
$stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), $_SESSION[ 'email_user' ] ] );
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_inactivity_period' ) {
$inactivity_period = filter_var( $_POST[ 'inactivity_period' ] ?? null, FILTER_VALIDATE_INT );
$valid_inactivity_period = $inactivity_period !== false && (
in_array( $inactivity_period, INACTIVITY_PERIOD_MONTHS, true )
|| ( ALLOW_ACCOUNT_NEVER_DELETE && $inactivity_period === 0 )
);
if ( ! $valid_inactivity_period ) {
$msg .= ''.htmlspecialchars(_('Please select a valid inactivity period.')).'
';
} else {
$stmt = $db->prepare( 'UPDATE mailbox SET inactivity_period = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ $inactivity_period, $_SESSION[ 'email_user' ] ] );
$msg .= ''.htmlspecialchars(_('Successfully updated the inactivity period.')).'
';
}
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_spam_settings' ) {
$spam_reject_score = filter_var( $_POST[ 'spam_reject_score' ] ?? null, FILTER_VALIDATE_INT );
if ( ! in_array( $spam_reject_score, SPAM_REJECT_SCORE_OPTIONS, true ) ) {
$msg .= ''.htmlspecialchars(_('Please select a valid spam reject score.')).'
';
} else {
$stmt = $db->prepare( 'UPDATE mailbox SET spam_reject_score = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ $spam_reject_score, $_SESSION[ 'email_user' ] ] );
$msg .= ''.htmlspecialchars(_('Successfully updated spam filter settings, changes can take up to 10 minutes to take effect.')).'
';
}
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_password' ) {
if ( empty( $_POST[ 'pass_update' ] ) || empty( $_POST[ 'pass_update2' ] ) || $_POST[ 'pass_update' ] !== $_POST[ 'pass_update2' ] ) {
$msg .= ''.htmlspecialchars(_('Passwords empty or don\'t match')).'
';
} else {
$hash = password_hash( $_POST[ 'pass_update' ], PASSWORD_ARGON2ID );
$stmt = $db->prepare( 'UPDATE mailbox SET password_hash_type = "{ARGON2ID}", password = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ $hash, $_SESSION[ 'email_user' ] ] );
$msg .= ''.htmlspecialchars(_('Successfully updated password')).'
';
}
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'delete_account' ) {
$msg .= ''.htmlspecialchars(_('Warning: This will permenently delete your account and all your data. Anyone can immediately register with this user again. It cannot be reversed. Are you absolutely sure?')).'
';
$msg .= '';
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'disable_account' ) {
$stmt = $db->prepare( 'SELECT inactivity_period FROM mailbox WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$inactivity_period = (int) $stmt->fetchColumn();
$disable_warning = ALLOW_ACCOUNT_NEVER_DELETE && $inactivity_period === 0
? _('Warning: This will disable your account and delete all your data. The address will not become available for registration again. It cannot be reversed. Are you absolutely sure?')
: _('Warning: This will disable your account and delete all your data. It will be available for registrations again after the selected inactivity period without a login. It cannot be reversed. Are you absolutely sure?');
$msg .= ''.htmlspecialchars($disable_warning).'
';
$msg .= '';
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'delete_account2' ) {
$stmt = $db->prepare( 'DELETE FROM alias WHERE address = ?;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$stmt = $db->prepare( 'UPDATE mailbox SET active = -2 WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$_SESSION = [];
session_regenerate_id( true );
$_SESSION[ 'csrf_token' ] = sha1( uniqid() );
$msg .= ''.htmlspecialchars(_('Successfully deleted account')).'
';
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'disable_account2' ) {
$stmt = $db->prepare( 'UPDATE alias SET active = 0 WHERE address = ?;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$stmt = $db->prepare( 'UPDATE mailbox SET active = -1 WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$_SESSION = [];
session_regenerate_id( true );
$_SESSION[ 'csrf_token' ] = sha1( uniqid() );
$msg .= ''.htmlspecialchars(_('Successfully disabled account')).'
';
} elseif ( isset( $_POST[ 'pgp_key' ] ) && ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_pgp_key' ) {
$pgp_key = trim( $_POST[ 'pgp_key' ] );
if ( empty( $pgp_key ) ) {
$msg .= ''.htmlspecialchars(_('Successfully removed the key')).'
';
$stmt = $db->prepare( 'UPDATE mailbox SET pgp_key = "", tfa = 0, pgp_verified = 0 WHERE username = ?;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
} else {
$gpg = gnupg_init();
gnupg_seterrormode( $gpg, GNUPG_ERROR_WARNING );
gnupg_setarmor( $gpg, 1 );
$imported_key = gnupg_import( $gpg, $pgp_key );
if ( ! $imported_key ) {
$msg .= ''.htmlspecialchars(_('There was an error importing the key')).'
';
} else {
$has_this_email = false;
$key_info = gnupg_keyinfo( $gpg, $imported_key[ 'fingerprint' ] );
foreach ( $key_info as $key ) {
foreach ( $key[ 'uids' ] as $uid ) {
if ( $uid[ 'email' ] === $_SESSION[ 'email_user' ] ) {
$has_this_email = true;
break;
}
}
}
if ( $has_this_email ) {
$msg .= ''.htmlspecialchars(_('Successfully imported the key')).'
';
$stmt = $db->prepare( 'UPDATE mailbox SET pgp_key = ?, tfa = 0, pgp_verified = 0 WHERE username = ?;' );
$stmt->execute( [ $pgp_key, $_SESSION[ 'email_user' ] ] );
} else {
$msg .= '' . sprintf( htmlspecialchars(_('Oops, looks like the key is missing this email address as user id. Please add your address "%s" as user ID to your pgp key or create a new key pair.')), htmlspecialchars( $_SESSION[ 'email_user' ] ) ) . '
';
}
}
}
} elseif ( isset( $_POST[ 'enable_2fa_code' ] ) && ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'enable_2fa' ) {
if ( $_POST[ 'enable_2fa_code' ] !== $_SESSION[ 'enable_2fa_code' ] ) {
$msg .= ''.htmlspecialchars(_('Sorry, the code was incorrect')).'
';
} else {
$stmt = $db->prepare( 'UPDATE mailbox SET tfa = 1, pgp_verified = 1 WHERE username = ?;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$msg .= ''.htmlspecialchars(_('Successfully enabled 2FA')).'
';
}
} elseif ( $_POST[ 'action' ] === 'disable_pgp_2fa' && ! empty( $_SESSION[ 'email_user' ] ) ) {
$stmt = $db->prepare( 'UPDATE mailbox SET tfa = 0 WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$msg .= ''.htmlspecialchars(_('Successfully disabled two-factor authentication')).'
';
} elseif ( $_POST[ 'action' ] === 'start_totp_setup' && ! empty( $_SESSION[ 'email_user' ] ) ) {
$_SESSION[ 'user_totp_setup' ] = generate_totp_secret();
} elseif ( $_POST[ 'action' ] === 'enable_totp' && ! empty( $_SESSION[ 'email_user' ] ) && ! empty( $_SESSION[ 'user_totp_setup' ] ) ) {
if ( verify_totp_code( $_SESSION[ 'user_totp_setup' ], $_POST[ 'totp_code' ] ?? '' ) ) {
$stmt = $db->prepare( 'UPDATE mailbox SET totp_secret = ? WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'user_totp_setup' ], $_SESSION[ 'email_user' ] ] );
unset( $_SESSION[ 'user_totp_setup' ] );
$msg .= ''.htmlspecialchars(_('Successfully enabled TOTP two-factor authentication')).'
';
} else {
$msg .= ''.htmlspecialchars(_('Invalid TOTP code')).'
';
}
} elseif ( $_POST[ 'action' ] === 'disable_totp' && ! empty( $_SESSION[ 'email_user' ] ) ) {
$stmt = $db->prepare( 'SELECT totp_secret FROM mailbox WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
$totp_secret = $stmt->fetchColumn();
if ( is_string( $totp_secret ) && verify_totp_code( $totp_secret, $_POST[ 'totp_code' ] ?? '' ) ) {
$stmt = $db->prepare( 'UPDATE mailbox SET totp_secret = NULL WHERE username = ? AND active = 1;' );
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
unset( $_SESSION[ 'user_totp_setup' ] );
$msg .= ''.htmlspecialchars(_('Successfully disabled TOTP two-factor authentication')).'
';
} else {
$msg .= ''.htmlspecialchars(_('Invalid TOTP code')).'
';
}
}
}
}
?>
'.htmlspecialchars(_('Your PGP key has expired, so PGP two-factor authentication was skipped for this login. Update your PGP key to restore PGP two-factor authentication.')).'';
} else {
echo $msg;
echo ''.htmlspecialchars(_('To login, please decrypt the following PGP encrypted message and confirm the code:')).'
';
echo ''.htmlspecialchars( $encrypted ).' ';
?>