Add invite code system

This commit is contained in:
danwin1210 committed 2026-10-25 15:32:20 +01:00
1 parent 11fe088b64
commit e8b44ac1a9
7 files changed
+205 -11

No files matched your search

+52 -3
View File
@@ -109,7 +109,14 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Invalid TOTP code')).'</div>';
}
} elseif ( ! empty( $_SESSION[ 'email_admin_user' ] ) ) {
if ( $_POST[ 'action' ] === 'update_alias' ) {
if ( INVITE_CODES_ENABLED && $_POST[ 'action' ] === 'create_admin_invite_code' ) {
$invite_code = create_admin_invite_code( $db, $_SESSION[ 'email_admin_user' ] );
if( $invite_code === null ) {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Unable to create invite code.')).'</div>';
} else {
$msg .= '<div class="green" role="status">'.sprintf(htmlspecialchars(_('Invite code created: %s')), '<code>'.htmlspecialchars( $invite_code ).'</code>').'</div>';
}
} elseif ( $_POST[ 'action' ] === 'update_alias' ) {
$alias_goto = '';
if ( isset( $_POST[ 'alias_keep_copy' ] ) ) {
$alias_goto .= $_SESSION[ 'email_admin_user' ] . ',';
@@ -523,6 +530,9 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
?> | <a href="?action=admins"><?php echo htmlspecialchars(_('Manage admins')); ?></a><?php
?> | <a href="?action=alias_domains"><?php echo htmlspecialchars(_('Manage alias domains')); ?></a><?php
}
if ( INVITE_CODES_ENABLED ) {
?> | <a href="?action=invite_codes"><?php echo htmlspecialchars(_('Manage invite codes')); ?></a><?php
}
?> | <a href="?action=edit_admin"><?php echo htmlspecialchars(_('Manage your admin account')); ?></a><?php
?> | <a href="?action=domains"><?php echo htmlspecialchars(_('Manage domains')); ?></a><?php
?> | <a href="?action=alias"><?php echo htmlspecialchars(_('Manage aliases')); ?></a><?php
@@ -537,7 +547,7 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
<input type="hidden" name="action" value="verify_admin_totp">
<div class="row">
<div class="col"><label for="totp_code"><?php echo htmlspecialchars(_('Authentication code')); ?></label></div>
<div class="col"><input type="text" name="totp_code" id="totp_code" inputmode="numeric" pattern="[0-9]{6}" autocomplete="one-time-code" required></div>
<div class="col"><input type="text" name="totp_code" id="totp_code" inputmode="numeric" pattern="[0-9]{6}" autocomplete="one-time-code" required autofocus></div>
</div>
<button type="submit"><?php echo htmlspecialchars(_('Confirm')); ?></button>
</form>
@@ -546,7 +556,7 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
<div class="row">
<div class="col"><label for="user"><?php echo htmlspecialchars(_('Username')); ?></label></div>
<div class="col"><input type="text" name="user" id="user" autocomplete="username" required></div>
<div class="col"><input type="text" name="user" id="user" autocomplete="username" required autofocus></div>
</div>
<div class="row">
<div class="col"><label for="pwd"><?php echo htmlspecialchars(_('Password')); ?></label></div>
@@ -562,6 +572,8 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
<?php } else {
if ( empty( $_REQUEST[ 'action' ] ) || in_array( $_REQUEST[ 'action' ], [ 'login', 'verify_admin_totp' ], true ) ) {
?><p><?php echo htmlspecialchars(_('Welcome to the admin management interface. You can configure your domain(s) and accounts here. Please select an option from the menu.')); ?></p><?php
} elseif ( INVITE_CODES_ENABLED && in_array( $_REQUEST[ 'action' ], [ 'invite_codes', 'create_admin_invite_code' ], true ) ) {
send_manage_invite_codes();
} elseif ( in_array( $_REQUEST[ 'action' ], [ 'admins', 'delete_admin2' ], true ) && $_SESSION[ 'email_admin_superadmin' ] ) {
send_manage_admins();
} elseif ( in_array( $_REQUEST[ 'action' ], [ 'domains', 'delete_domain2' ], true ) ) {
@@ -613,6 +625,43 @@ function sync_admin_dkim_keys_alert(): string
}
}
function send_manage_invite_codes(): void
{
if ( ! INVITE_CODES_ENABLED ) {
return;
}
$db = get_db_instance();
$page = get_admin_list_page();
$offset = ( $page - 1 ) * ADMIN_LIST_PAGE_SIZE;
$total = (int) $db->query( 'SELECT COUNT(*) FROM invite_codes;' )->fetchColumn();
$totalPages = max( 1, (int) ceil( $total / ADMIN_LIST_PAGE_SIZE ) );
$stmt = $db->prepare( 'SELECT code, generated_by, generated_by_admin, created_at, redeemed_by, redeemed_at FROM invite_codes ORDER BY created_at DESC, id DESC LIMIT :limit OFFSET :offset;' );
$stmt->bindValue( ':limit', ADMIN_LIST_PAGE_SIZE, PDO::PARAM_INT );
$stmt->bindValue( ':offset', $offset, PDO::PARAM_INT );
$stmt->execute();
?>
<h2><?php echo htmlspecialchars(_('Invite codes')); ?></h2>
<form class="form_limit" action="admin.php" method="post">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
<button name="action" value="create_admin_invite_code" type="submit"><?php echo htmlspecialchars(_('Generate invite code')); ?></button>
</form>
<?php
while ( $invite = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$creator = $invite[ 'generated_by' ] ?? $invite[ 'generated_by_admin' ];
?>
<p><code><?php echo htmlspecialchars( $invite[ 'code' ] ); ?></code> —
<?php echo htmlspecialchars(_('Created by')); ?> <?php echo htmlspecialchars( $creator ); ?> (<?php echo htmlspecialchars( $invite[ 'created_at' ] ); ?>)
<?php if ( $invite[ 'redeemed_by' ] !== null ) { ?>
— <?php echo htmlspecialchars(_('Used by')); ?> <?php echo htmlspecialchars( $invite[ 'redeemed_by' ] ); ?> (<?php echo htmlspecialchars( $invite[ 'redeemed_at' ] ); ?>)
<?php } else { ?>
— <?php echo htmlspecialchars(_('Not yet used')); ?>
<?php } ?>
</p>
<?php
}
render_admin_pagination( 'invite_codes', $page, $totalPages );
}
function get_admin_list_page(): int
{
$page = isset( $_GET[ 'page' ] ) ? (int) $_GET[ 'page' ] : 1;
+43 -3
View File
@@ -106,6 +106,13 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Incorrect username or password')).'</div>';
}
}
} elseif ( INVITE_CODES_ENABLED && INVITE_CODE_LIMIT > 0 && ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'create_invite_code' ) {
$invite_code = create_user_invite_code( $db, $_SESSION[ 'email_user' ] );
if ( $invite_code === null ) {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('No invite code is available yet. New accounts must wait before generating codes, and each used code can be replaced after the waiting period.')).'</div>';
} else {
$msg .= '<div class="green" role="status">'.sprintf(htmlspecialchars(_('Invite code created: %s')), '<code>'.htmlspecialchars( $invite_code ).'</code>').'</div>';
}
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_settings' ) {
$alias_goto = '';
if ( isset( $_POST[ 'alias_keep_copy' ] ) ) {
@@ -310,7 +317,7 @@ if ( isset( $_SESSION[ 'user_totp_pending' ] ) ) {
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
<div class="row">
<div class="col"><label for="totp_code"><?php echo htmlspecialchars(_('Authentication code')); ?></label></div>
<div class="col"><input type="text" name="totp_code" id="totp_code" inputmode="numeric" pattern="[0-9]{6}" autocomplete="one-time-code" required></div>
<div class="col"><input type="text" name="totp_code" id="totp_code" inputmode="numeric" pattern="[0-9]{6}" autocomplete="one-time-code" required autofocus></div>
</div>
<button type="submit"><?php echo htmlspecialchars(_('Confirm')); ?></button>
</form>
@@ -352,7 +359,7 @@ if ( isset( $_SESSION[ 'user_totp_pending' ] ) ) {
<form class="form_limit" action="manage_account.php" method="post">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
<div class="row">
<div class="col"><input type="text" name="2fa_code" aria-label="<?php echo htmlspecialchars(_('2FA code')); ?>"></div>
<div class="col"><input type="text" name="2fa_code" aria-label="<?php echo htmlspecialchars(_('2FA code')); ?>" autofocus></div>
<div class="col">
<button type="submit"><?php echo htmlspecialchars(_('Confirm')); ?></button>
</div>
@@ -387,7 +394,7 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
<div class="row">
<div class="col"><label for="user"><?php echo htmlspecialchars(_('Username')); ?></label></div>
<div class="col"><input type="text" name="user" id="user" autocomplete="username" required
<div class="col"><input type="text" name="user" id="user" autocomplete="username" required autofocus
value="<?php echo htmlspecialchars( $_POST[ 'user' ] ?? '' ); ?>"></div>
</div>
<div class="row">
@@ -450,6 +457,39 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
</div>
</form>
<?php if ( INVITE_CODES_ENABLED && INVITE_CODE_LIMIT > 0 ) {
$invite_status = get_user_invite_status( $db, $_SESSION[ 'email_user' ] );
$inviteStmt = $db->prepare( 'SELECT code, created_at, redeemed_by, redeemed_at FROM invite_codes WHERE generated_by = ? ORDER BY created_at DESC;' );
$inviteStmt->execute( [ $_SESSION[ 'email_user' ] ] );
?>
<h2><?php echo htmlspecialchars(_('Invite codes')); ?></h2>
<p><?php
if ( $invite_status[ 'available' ] > 0 ) {
printf( htmlspecialchars(_('You can create %d invite code(s).')), $invite_status[ 'available' ] );
} elseif ( ! $invite_status[ 'eligible' ] ) {
printf( htmlspecialchars(_('You can generate up to %d invite codes after your account has been active for %d month(s).')), INVITE_CODE_LIMIT, INVITE_WAIT_PERIOD_MONTHS );
} else {
printf( htmlspecialchars(_('You can create more invite codes %d month(s) after one of your existing codes is used.')), INVITE_WAIT_PERIOD_MONTHS );
}
?></p>
<?php if ( $invite_status[ 'available' ] > 0 ) { ?>
<form class="form_limit" action="manage_account.php" method="post">
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
<button name="action" value="create_invite_code" type="submit"><?php echo htmlspecialchars(_('Generate invite code')); ?></button>
</form>
<?php }
while ( $invite = $inviteStmt->fetch( PDO::FETCH_ASSOC ) ) { ?>
<p><code><?php echo htmlspecialchars( $invite[ 'code' ] ); ?></code> —
<?php echo htmlspecialchars(_('Created')); ?>: <?php echo htmlspecialchars( $invite[ 'created_at' ] ); ?>
<?php if ( $invite[ 'redeemed_by' ] !== null ) { ?>
— <?php echo htmlspecialchars(_('Used by')); ?> <?php echo htmlspecialchars( $invite[ 'redeemed_by' ] ); ?> (<?php echo htmlspecialchars( $invite[ 'redeemed_at' ] ); ?>)
<?php } else { ?>
— <?php echo htmlspecialchars(_('Not yet used')); ?>
<?php } ?>
</p>
<?php } ?>
<?php } ?>
<h2><?php echo htmlspecialchars(_('Identity addresses')); ?></h2>
<p><?php echo htmlspecialchars(_('Identity addresses receive mail in this mailbox and can be used as a sender for this account. They must use your mailbox domain.')); ?></p>
<?php
+50 -4
View File
@@ -26,6 +26,20 @@ if ( isset( $_POST[ 'user' ] ) ) {
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Invalid captcha')).'</div>';
}
$db = get_db_instance();
$invite_code = is_string( $_POST[ 'invite_code' ] ?? null ) ? strtolower( trim( $_POST[ 'invite_code' ] ) ) : '';
if ( INVITE_CODES_ENABLED ) {
if ( $invite_code === '' ) {
$ok = false;
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('A valid invite code is required.')).'</div>';
} else {
$stmt = $db->prepare( 'SELECT null FROM invite_codes WHERE code = ? AND redeemed_by IS NULL;' );
$stmt->execute( [ $invite_code ] );
if ( ! $stmt->fetchColumn() ) {
$ok = false;
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('The invite code is invalid or has already been used.')).'</div>';
}
}
}
if ( ! preg_match( '/^([^+\/\'"]+?)(@([^@]+))?$/iu', $_POST[ 'user' ], $match ) ) {
$ok = false;
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Invalid username. It may not contain a +, \', " or /.')).'</div>';
@@ -67,7 +81,21 @@ if ( isset( $_POST[ 'user' ] ) ) {
}
if ( $ok ) {
$hash = password_hash( $_POST[ 'pwd' ], PASSWORD_ARGON2ID );
$db->beginTransaction();
if ( INVITE_CODES_ENABLED ) {
$db->beginTransaction();
$stmt = $db->prepare( 'SELECT id FROM invite_codes WHERE code = ? AND redeemed_by IS NULL FOR UPDATE;' );
$stmt->execute( [ $invite_code ] );
$invite_id = $stmt->fetchColumn();
if ( ! $invite_id ) {
$db->rollBack();
$ok = false;
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('The invite code is invalid or has already been used.')).'</div>';
}
} else {
$db->beginTransaction();
}
}
if ( $ok ) {
$stmt = $db->prepare( 'INSERT INTO alias (address, goto, domain, created, modified) VALUES (?, ?, ?, NOW(), NOW());' );
$stmt->execute( [ "$user@$domain", "$user@$domain", $domain ] );
$stmt = $db->prepare( 'INSERT INTO mailbox (username, password, quota, local_part, domain, created, modified, password_hash_type, openpgpkey_wkd, feedback_id) VALUES(?, ?, ?, ?, ?, NOW(), NOW(), ?, ?, ?);' );
@@ -75,8 +103,20 @@ if ( isset( $_POST[ 'user' ] ) ) {
if ( ! create_clearnet_onion_identity( $db, "$user@$domain", $msg ) ) {
$db->rollBack();
} else {
$db->commit();
$msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully created new mailbox!')).'</div>';
if ( INVITE_CODES_ENABLED ) {
$stmt = $db->prepare( 'UPDATE invite_codes SET redeemed_by = ?, redeemed_at = NOW() WHERE id = ? AND redeemed_by IS NULL;' );
$stmt->execute( [ "$user@$domain", $invite_id ] );
if ( $stmt->rowCount() !== 1 ) {
$db->rollBack();
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('The invite code is invalid or has already been used.')).'</div>';
} else {
$db->commit();
$msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully created new mailbox!')).'</div>';
}
} else {
$db->commit();
$msg .= '<div class="green" role="alert">'.htmlspecialchars(_('Successfully created new mailbox!')).'</div>';
}
}
}
}
@@ -112,8 +152,14 @@ if( ! REGISTRATION_ENABLED ) {
<form class="form_limit" action="register.php" method="post"><input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ] ?>">
<div class="row">
<div class="col"><label for="user"><?php echo htmlspecialchars(_('Username')); ?></label></div>
<div class="col"><input type="text" name="user" id="user" autocomplete="username" required value="<?php echo htmlspecialchars( $_POST[ 'user' ] ?? '' ); ?>"></div>
<div class="col"><input type="text" name="user" id="user" autocomplete="username" required value="<?php echo htmlspecialchars( $_POST[ 'user' ] ?? '' ); ?>" autofocus></div>
</div>
<?php if ( INVITE_CODES_ENABLED ) { ?>
<div class="row">
<div class="col"><label for="invite_code"><?php echo htmlspecialchars(_('Invite code')); ?></label></div>
<div class="col"><input type="text" name="invite_code" id="invite_code" autocomplete="off" required value="<?php echo htmlspecialchars( $_POST[ 'invite_code' ] ?? '' ); ?>"></div>
</div>
<?php } ?>
<div class="row">
<div class="col"><label for="pwd"><?php echo htmlspecialchars(_('Password')); ?></label></div>
<div class="col"><input type="password" name="pwd" id="pwd" autocomplete="new-password" required></div>