From b0790a53458cb7150739f58b4613c5686014c50f Mon Sep 17 00:00:00 2001 From: Daniel Winzen Date: Sun, 11 Oct 2026 22:18:48 +0200 Subject: [PATCH] Add inactivity period selection for accounts --- common_config.php | 17 ++++++++++++-- cron.php | 10 +++++---- setup.php | 12 +++++++--- www/admin.php | 50 ++++++++++++++++++++++++++++++------------ www/manage_account.php | 43 ++++++++++++++++++++++++++++++++++-- www/terms.php | 2 +- 6 files changed, 108 insertions(+), 26 deletions(-) diff --git a/common_config.php b/common_config.php index e94a09a..3049fb2 100644 --- a/common_config.php +++ b/common_config.php @@ -9,10 +9,13 @@ const DBHOST = 'localhost'; // Database host const DBUSER = 'postfix'; // Database user const DBPASS = 'YOUR_PASSWORD'; // Database password const DBNAME = 'postfix'; // Database -const DBVERSION = 3; // Database schema version +const DBVERSION = 4; // Database schema version +const INACTIVITY_PERIOD_MONTHS = [1, 6, 12, 24]; // Allowed inactivity periods in months - don't forget to update get_inactivity_period_options on changes here and if an option is removed, you should also update accounts in the database mailbox table +const DEFAULT_INACTIVITY_PERIOD = 12; // Default inactivity period in months +const ALLOW_ACCOUNT_NEVER_DELETE = false; // Whether to allow users to set their inactivity period to "never delete" const PERSISTENT = true; // persistent database connection const CAPTCHA_DIFFICULTY = 1; // captcha difficulty from 0 to 4 -const RESERVED_USERNAMES = ['about', 'abuse', 'admin', 'administrator', 'billing', 'contact', 'daemon', 'ftp', 'help', 'hostmaster', 'info', 'legal', 'list', 'list-request', 'lists', 'maildaemon', 'mailerdaemon', 'mailer-daemon', 'marketing', 'media', 'news', 'newsletter', 'nobody', 'noc', 'noreply', 'no-reply', 'notification', 'notifications', 'notify', 'offer', 'offers', 'office', 'official', 'order', 'orders', 'phish', 'phishing', 'postmaster', 'root', 'sale', 'sales', 'security', 'service', 'services', 'shop', 'shopping', 'spam', 'staff', 'support', 'survey', 'system', 'team', 'teams', 'unsbubscribe', 'uucp', 'usenet', 'user', 'username', 'users', 'web', 'webmail', 'webmaster', 'webmasters', 'welcome', 'www']; // list of reserved usernames that can mot be used on public registration +const RESERVED_USERNAMES = ['about', 'abuse', 'admin', 'administrator', 'billing', 'contact', 'daemon', 'ftp', 'help', 'hostmaster', 'info', 'legal', 'list', 'list-request', 'lists', 'maildaemon', 'mailerdaemon', 'mailer-daemon', 'marketing', 'media', 'news', 'newsletter', 'nobody', 'noc', 'noreply', 'no-reply', 'notification', 'notifications', 'notify', 'offer', 'offers', 'office', 'official', 'order', 'orders', 'phish', 'phishing', 'postmaster', 'root', 'sale', 'sales', 'security', 'service', 'services', 'shop', 'shopping', 'spam', 'staff', 'support', 'survey', 'system', 'team', 'teams', 'unsubscribe', 'uucp', 'usenet', 'user', 'username', 'users', 'web', 'webmail', 'webmaster', 'webmasters', 'welcome', 'www']; // list of reserved usernames that can not be used on public registration const CANONICAL_URL = 'https://mail.danwin1210.de/'; // our preferred URL prefix for search engines const PRIVACY_POLICY_URL = '/privacy.php'; // URL to privacy policy const WEB_XMPP_URL = 'https://danwin1210.de:5281/conversejs'; // URL to Web-XMPP @@ -84,6 +87,16 @@ textdomain('mail-hosting'); require_once( __DIR__ . '/vendor/autoload.php' ); +function get_inactivity_period_options(): array +{ + return [ + 1 => _('1 month'), + 6 => _('6 months'), + 12 => _('1 year'), + 24 => _('2 years'), + ]; +} + function get_db_instance(): PDO { static $db = null; diff --git a/cron.php b/cron.php index f2c7348..3c8347a 100644 --- a/cron.php +++ b/cron.php @@ -12,10 +12,12 @@ try { } setlocale( LC_CTYPE, 'C.UTF-8' ); // make sure to use UTF-8 locale. Non UTF-8 locales can cause serious issues when handling UTF-8 file names -// mark accounts deletable that haven't been used in an entire year -$expire_time = strtotime( '-1 year' ); -$stmt = $db->prepare( 'UPDATE mailbox SET active = -2 WHERE active IN (0, 1) AND modified < ? AND (last_login < ? OR last_login IS NULL);' ); -$stmt->execute( [ date( 'Y-m-d H:i:s', $expire_time ), $expire_time ] ); +// mark accounts deletable after their selected period of inactivity +foreach ( INACTIVITY_PERIOD_MONTHS as $months ) { + $expire_time = strtotime( "-$months months" ); + $stmt = $db->prepare( 'UPDATE mailbox SET active = -2 WHERE active IN (0, 1) AND inactivity_period = ? AND inactivity_period > 0 AND modified < ? AND (last_login < ? OR last_login IS NULL);' ); + $stmt->execute( [ $months, date( 'Y-m-d H:i:s', $expire_time ), $expire_time ] ); +} // delete all associated data when deleting/disabling accounts $stmt = $db->query( 'SELECT username, local_part, domain, active FROM mailbox WHERE active IN (-1, -2);' ); diff --git a/setup.php b/setup.php index 0241fc6..d226770 100644 --- a/setup.php +++ b/setup.php @@ -36,7 +36,7 @@ $createTableStatements = [ 'dmarc_report_record_reason' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_reason` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `type` varchar(255) NOT NULL, `comment` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_reason_ibfk_1` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'dmarc_report_record_spf_result' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_spf_result` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `domain` varchar(255) NOT NULL, `result` varchar(255) NOT NULL, `scope` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_spf_result_ibfk_2` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;", 'domain_admins' => "CREATE TABLE IF NOT EXISTS `domain_admins` (`username` varchar(255) NOT NULL, `domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `id` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), KEY `username` (`username`), KEY `active` (`active`), KEY `domain` (`domain`), CONSTRAINT `domain_admins_ibfk_1` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `domain_admins_ibfk_2` FOREIGN KEY (`username`) REFERENCES `admin` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", - 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", + 'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'monitored_mailboxes' => "CREATE TABLE IF NOT EXISTS `monitored_mailboxes` (`username` varchar(255) NOT NULL, `last_login` int(11) NOT NULL, PRIMARY KEY (`username`), KEY `lastlogin` (`last_login`), CONSTRAINT `monitored_mailboxes_ibfk_1` FOREIGN KEY (`username`) REFERENCES `mailbox` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'tls_report' => "CREATE TABLE IF NOT EXISTS `tls_report` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` varchar(255) NOT NULL, `organization` varchar(255) NOT NULL, `contact` varchar(255) NOT NULL, `start` datetime NOT NULL, `end` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `report_id_organization` (`report_id`,`organization`), KEY `start_end` (`start`,`end`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", 'tls_report_policy' => "CREATE TABLE IF NOT EXISTS `tls_report_policy` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` bigint(20) unsigned NOT NULL, `policy_type` varchar(255) NOT NULL, `policy_string` text NOT NULL, `policy_domain` varchar(255) NOT NULL, `mx_host` text NOT NULL, `success` int(11) NOT NULL, `failure` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `report_id` (`report_id`), KEY `policy_domain` (`policy_domain`), CONSTRAINT `tls_report_policy_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `tls_report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `tls_report_policy_ibfk_3` FOREIGN KEY (`policy_domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;", @@ -46,12 +46,12 @@ $createTableStatements = [ try{ $stmt=$db->query("SELECT value FROM settings WHERE setting='version';"); - $version=$stmt->fetch(PDO::FETCH_NUM)[0]; + $version=(int) $stmt->fetch(PDO::FETCH_NUM)[0]; try { foreach($createTableStatements as $tableName => $statement){ $db->exec($statement); } - if ( (int) $version < 3 ) { + if ( $version < 3 ) { foreach ( [ 'admin', 'mailbox' ] as $tableName ) { $stmt = $db->query( "SHOW COLUMNS FROM `$tableName` LIKE 'totp_secret';" ); if ( ! $stmt->fetch() ) { @@ -59,6 +59,12 @@ try{ } } } + if ( $version < 4 ) { + $stmt = $db->query( "SHOW COLUMNS FROM `mailbox` LIKE 'inactivity_period';" ); + if ( ! $stmt->fetch() ) { + $db->exec( 'ALTER TABLE `mailbox` ADD COLUMN `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12 AFTER `last_login`;' ); + } + } $db->beginTransaction(); $stmt=$db->prepare("UPDATE `settings` SET `value`=? WHERE `setting`='version';"); $stmt->execute([DBVERSION]); diff --git a/www/admin.php b/www/admin.php index 2ddc748..0749372 100644 --- a/www/admin.php +++ b/www/admin.php @@ -334,20 +334,29 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $ok = check_domain_access( $_POST[ 'user' ], $msg ); } if ( $ok ) { - $alias_goto = ''; - if ( isset( $_POST[ 'alias_keep_copy' ] ) ) { - $alias_goto .= $_POST[ 'user' ] . ','; + $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 .= ''; + } else { + $alias_goto = ''; + if ( isset( $_POST[ 'alias_keep_copy' ] ) ) { + $alias_goto .= $_POST[ '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 = ?, active = ? WHERE address = ?;' ); + $stmt->execute( [ $alias_goto, ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), $_POST[ 'user' ] ] ); + $stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ?, active = ?, quota = ?, inactivity_period = ?, modified = NOW() WHERE username = ?;' ); + $stmt->execute( [ ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), DEFAULT_QUOTA, $inactivity_period, $_POST[ 'user' ] ] ); + $msg .= '
'.htmlspecialchars(_('Successfully updated mailbox.')).'
'; } - 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 = ?, active = ? WHERE address = ?;' ); - $stmt->execute( [ $alias_goto, ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), $_POST[ 'user' ] ] ); - $stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ?, active = ?, quota = ?, modified = NOW() WHERE username = ?;' ); - $stmt->execute( [ ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), DEFAULT_QUOTA, $_POST[ 'user' ] ] ); - $msg .= ''; } } elseif ( $_POST[ 'action' ] === 'save_new_mailbox' && ! empty( $_POST[ 'user' ] ) ) { $email = mb_strtolower( $_POST[ 'user' ] ); @@ -1152,7 +1161,7 @@ function send_new_mailbox(): void function send_edit_mailbox(): void { $db = get_db_instance(); - $stmt = $db->prepare( 'SELECT a.goto, m.active, m.enforce_tls_in, m.enforce_tls_out FROM alias AS a INNER JOIN mailbox AS m ON (m.username=a.address) WHERE m.username = ?;' ); + $stmt = $db->prepare( 'SELECT a.goto, m.active, m.enforce_tls_in, m.enforce_tls_out, m.inactivity_period FROM alias AS a INNER JOIN mailbox AS m ON (m.username=a.address) WHERE m.username = ?;' ); $stmt->execute( [ $_REQUEST[ 'user' ] ] ); if ( $email = $stmt->fetch( PDO::FETCH_ASSOC ) ) { $aliases = explode( ',', $email[ 'goto' ] ); @@ -1188,6 +1197,19 @@ function send_edit_mailbox(): void value="1"> +
+
+
+ +
+
diff --git a/www/manage_account.php b/www/manage_account.php index f059565..66cecd2 100644 --- a/www/manage_account.php +++ b/www/manage_account.php @@ -120,6 +120,19 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $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 .= ''; + } 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_password' ) { if ( empty( $_POST[ 'pass_update' ] ) || empty( $_POST[ 'pass_update2' ] ) || $_POST[ 'pass_update' ] !== $_POST[ 'pass_update2' ] ) { $msg .= ''; @@ -134,7 +147,13 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) { $msg .= '
'; $msg .= '
'; } elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'disable_account' ) { - $msg .= ''; + $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 .= ''; $msg .= '
'; $msg .= '
'; } elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'delete_account2' ) { @@ -356,7 +375,7 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?> $aliases = explode( ',', $tmp[ 'goto' ] ); } $aliases_to = implode( "\n", array_diff( $aliases, [ $_SESSION[ 'email_user' ] ] ) ); - $stmt = $db->prepare( 'SELECT enforce_tls_in, enforce_tls_out FROM mailbox WHERE username = ?;' ); + $stmt = $db->prepare( 'SELECT enforce_tls_in, enforce_tls_out, inactivity_period FROM mailbox WHERE username = ?;' ); $stmt->execute( [ $_SESSION[ 'email_user' ] ] ); $tls_status = $stmt->fetch( PDO::FETCH_ASSOC ); ?> @@ -397,6 +416,26 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
+
+

+

+ +
+
+
+ +
+
+ +
+

diff --git a/www/terms.php b/www/terms.php index 6845712..1e5e567 100644 --- a/www/terms.php +++ b/www/terms.php @@ -22,7 +22,7 @@ global $language, $dir, $locale;

| | | | |

  1. -
  2. +