mail-hosting/cron.php

82 lines
4.1 KiB
PHP
Raw Normal View History

2022-05-15 19:39:39 +00:00
<?php
if ( php_sapi_name() !== 'cli' ) {
exit;
}
const DBHOST_PROSODY = 'localhost'; // Database host
const DBUSER_PROSODY = 'prosody'; // Database user
const DBPASS_PROSODY = 'YOUR_PASSWORD'; // Database password
const DBNAME_PROSODY = 'prosody'; // Database
require_once 'common_config.php';
$db = get_db_instance();
try {
$db_prosody = new PDO( 'mysql:host=' . DBHOST_PROSODY . ';dbname=' . DBNAME_PROSODY, DBUSER_PROSODY, DBPASS_PROSODY, [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ] );
} catch ( PDOException $e ) {
die( 'No Connection to MySQL database!' );
}
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 ] );
// delete all associated data when deleting/disabling accounts
$stmt = $db->query( 'SELECT username, local_part, domain, active FROM mailbox WHERE active IN (-1, -2);' );
$disable = $db->prepare( 'UPDATE mailbox SET active = 0 WHERE username = ?;' );
$delete = $db->prepare( 'DELETE FROM mailbox WHERE username = ?;' );
$delete_alias = $db->prepare( 'DELETE FROM alias WHERE address = ?;' );
$delete_prosody = $db_prosody->prepare( 'DELETE FROM prosody WHERE user = ? AND host = ?;' );
$delete_prosody_archive = $db_prosody->prepare( 'DELETE FROM prosodyarchive WHERE user = ? AND host = ?;' );
while ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$domain_basename = basename( $tmp[ 'domain' ] );
$local_basename = basename( $tmp[ 'local_part' ] );
if ( ! in_array( $domain_basename, [ '..', '.', '' ], true ) && ! in_array( $local_basename, [ '..', '.', '' ], true ) ) {
$mail_files = '/var/mail/vmail/' . $domain_basename . '/' . $local_basename;
if ( file_exists( $mail_files ) ) {
exec( 'rm -r ' . escapeshellarg( $mail_files ) );
}
@unlink( '/var/local/squirrelmail/data/' . $local_basename . '@' . $domain_basename . '.pref' );
@unlink( '/var/local/squirrelmail/data/' . $local_basename . '@' . $domain_basename . '.abook' );
if ( $tmp[ 'domain' ] === 'danwin1210.de' ) {
@unlink( '/var/local/squirrelmail/data/' . $local_basename . '.pref' );
@unlink( '/var/local/squirrelmail/data/' . $local_basename . '.abook' );
@unlink( '/var/local/squirrelmail/data/' . $local_basename . '@danielas3rtn54uwmofdo3x2bsdifr47huasnmbgqzfrec5ubupvtpid.onion.pref' );
@unlink( '/var/local/squirrelmail/data/' . $local_basename . '@danielas3rtn54uwmofdo3x2bsdifr47huasnmbgqzfrec5ubupvtpid.onion.abook' );
$delete_prosody->execute( [ $tmp[ 'local_part' ], $tmp[ 'domain' ] ] );
$delete_prosody_archive->execute( [ $tmp[ 'local_part' ], $tmp[ 'domain' ] ] );
}
}
2022-12-30 12:31:57 +00:00
if ( $tmp[ 'active' ] === -2 ) {
2022-05-15 19:39:39 +00:00
$delete->execute( [ $tmp[ 'username' ] ] );
}
2022-12-30 12:31:57 +00:00
if ( $tmp[ 'active' ] === -1 ) {
2022-05-15 19:39:39 +00:00
$disable->execute( [ $tmp[ 'username' ] ] );
}
$delete_alias->execute( [ $tmp[ 'username' ] ] );
}
$stmt = $db->query( 'SELECT domain FROM domain WHERE active = -1;' );
$del_domain = $db->prepare( 'DELETE FROM domain WHERE domain = ?;' );
$del_mailbox = $db->prepare( 'DELETE FROM mailbox WHERE domain = ?;' );
$del_alias = $db->prepare( 'DELETE FROM alias WHERE domain = ?;' );
while ( $tmp = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
$domain_basename = basename( $tmp[ 'domain' ] );
if ( ! in_array( $domain_basename, [ '..', '.', '' ], true ) ) {
$del_alias->execute( [ $tmp[ 'domain' ] ] );
$del_mailbox->execute( [ $tmp[ 'domain' ] ] );
$files = glob( '/var/local/squirrelmail/data/*@' . $domain_basename . '.{pref,abook}', GLOB_BRACE );
$mail_files = '/var/mail/vmail/' . $domain_basename . '/';
if ( file_exists( $mail_files ) ) {
exec( 'rm -r ' . escapeshellarg( $mail_files ) );
}
if ( is_array( $files ) ) {
foreach ( $files as $file ) {
@unlink( $file );
}
}
$del_domain->execute( [ $tmp[ 'domain' ] ] );
}
}
// delete squirrelmail attachments older than an hour
exec( 'find /var/local/squirrelmail/attach/ -type f -cmin +60 -delete' );