diff --git a/common_config.php b/common_config.php index 7ae38f9..ebe3645 100644 --- a/common_config.php +++ b/common_config.php @@ -44,6 +44,36 @@ const INVITE_CODES_ENABLED = false; // Require invite codes for public registrat const INVITE_CODE_LIMIT = 3; // Number of invite codes a user may have generated before replacements become available const INVITE_WAIT_PERIOD_MONTHS = 1; // Months to wait after account creation and invite redemption const INVITE_CODE_EXPIRATION_MONTHS = 6; // Months after which an invite code expires and is removed from the database +const DEAD_MAN_SWITCH_ENABLED = false; // Enable staged inactivity notifications +const DEAD_MAN_SWITCH_MAILBOX = 'admin@example.com'; // Mailbox whose account activity resets the switch +const DEAD_MAN_SWITCH_TRUSTED_CONTACTS = ['trusted@example.com', 'trusted2@example.com']; // Email addresses of trusted contacts to be notified +const DEAD_MAN_SWITCH_CUSTOMER_CONTACTS = ['customer@example.com', 'customer2@example.com']; // Customer email addresses to be notified +const DEAD_MAN_SWITCH_STAGES = [ + 1 => [ + 'after' => 24 * 60 * 60, + 'recipients' => [ DEAD_MAN_SWITCH_MAILBOX ], + 'subject' => 'Dead man switch: please check in', + 'body' => "No account activity has been recorded for 24 hours. Please sign in to your monitored mailbox to reset the dead man switch.\n", + ], + 2 => [ + 'after' => 48 * 60 * 60, + 'recipients' => DEAD_MAN_SWITCH_TRUSTED_CONTACTS, + 'subject' => 'Dead man switch: please contact me', + 'body' => "No account activity has been recorded for 48 hours. Please try to contact me directly. If I am okay, signing in to " . DEAD_MAN_SWITCH_MAILBOX . " will reset the switch.\n", + ], + 3 => [ + 'after' => 72 * 60 * 60, + 'recipients' => DEAD_MAN_SWITCH_TRUSTED_CONTACTS, + 'subject' => 'Dead man switch: project handover instructions', + 'body' => "No account activity has been recorded for 72 hours. The following are my instructions for taking over my projects:\n\nEDIT_ME\n", + ], + 4 => [ + 'after' => 96 * 60 * 60, + 'recipients' => DEAD_MAN_SWITCH_CUSTOMER_CONTACTS, + 'subject' => 'Important: your contact has changed', + 'body' => "I have been unavailable for an extended period and something may have happened to me. For ongoing matters, please contact EDIT_ME.\n", + ], +]; const LANGUAGES = [ 'cs' => ['name' => 'čeština', 'locale' => 'cs_CZ', 'flag' => '🇨🇿', 'show_in_menu' => true, 'dir' => 'ltr'], diff --git a/cron.php b/cron.php index b52dbb0..aea135e 100644 --- a/cron.php +++ b/cron.php @@ -97,3 +97,4 @@ try { echo _('Error synchronizing DKIM signing keys:') . PHP_EOL; echo $exception->getMessage() . PHP_EOL; } +require_once __DIR__ . '/tools/dead_man_switch.php'; diff --git a/tools/dead_man_switch.php b/tools/dead_man_switch.php new file mode 100644 index 0000000..9d5a441 --- /dev/null +++ b/tools/dead_man_switch.php @@ -0,0 +1,83 @@ +prepare( 'SELECT `last_login`, UNIX_TIMESTAMP(`created`) AS `created_at` FROM `mailbox` WHERE `username` = ? AND `active` = 1;' ); +$stmt->execute( [ DEAD_MAN_SWITCH_MAILBOX ] ); +$monitored_mailbox = $stmt->fetch( PDO::FETCH_ASSOC ); +if ( ! $monitored_mailbox ) { + throw new RuntimeException( sprintf( _('The configured dead man switch mailbox does not exist or is inactive: %s'), DEAD_MAN_SWITCH_MAILBOX ) ); +} +$last_activity = (int) ( $monitored_mailbox['last_login'] ?? $monitored_mailbox['created_at'] ); + +$settings_key = 'dead_man_switch'; +$stmt = $db->prepare( 'SELECT `value` FROM `settings` WHERE `setting` = ?;' ); +$stmt->execute( [ $settings_key ] ); +$stored_state = $stmt->fetchColumn(); +if ( $stored_state === false ) { + $state = [ + 'mailbox' => DEAD_MAN_SWITCH_MAILBOX, + 'last_activity' => $last_activity, + 'notified_stage' => 0, + 'last_run' => 0, + ]; +} else { + $state = json_decode( $stored_state, true, 512, JSON_THROW_ON_ERROR ); + if ( !is_array($state) || $state['mailbox'] !== DEAD_MAN_SWITCH_MAILBOX || $state['last_activity'] !== $last_activity ) { + $state = [ + 'mailbox' => DEAD_MAN_SWITCH_MAILBOX, + 'last_activity' => $last_activity, + 'notified_stage' => 0, + 'last_run' => 0, + ]; + } +} +$notified_stage = $state['notified_stage']; + +$elapsed = time() - $last_activity; +$stage = $notified_stage + 1; +$notification = DEAD_MAN_SWITCH_STAGES[$stage] ?? null; +$stage_interval = $notification !== null && $stage > 1 + ? $notification['after'] - DEAD_MAN_SWITCH_STAGES[$notified_stage]['after'] + : 0; +if ( + $notification !== null + && $elapsed >= $notification['after'] + && ( $state['last_run'] === 0 || time() - $state['last_run'] >= $stage_interval ) +) { + foreach ( $notification['recipients'] as $recipient ) { + if ( ! mail( $recipient, $notification['subject'], $notification['body'], "Content-Type: text/plain; charset=UTF-8\r\n" ) ) { + throw new RuntimeException( sprintf( _('Unable to send dead man switch notification to %s.'), $recipient ) ); + } + } + $state['notified_stage'] = $stage; + $state['last_run'] = time(); +} + +$stmt = $db->prepare( 'INSERT INTO `settings` (`setting`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`);' ); +$stmt->execute( [ $settings_key, json_encode( $state, JSON_THROW_ON_ERROR ) ] );