0
0
mirror of https://github.com/postfixadmin/postfixadmin.git synced 2024-09-19 19:22:14 +02:00
postfixadmin/ADDITIONS/virtualmaildel.php

195 lines
5.1 KiB
PHP
Raw Normal View History

<?php
/*
Virtual Mail Delete
by George Vieira <george at citadelcomputer dot com dot au>
You can run this from your crontab with something like
0 4 * * * * vmail php -q virtualmaildel.php >/dev/null
2017-08-31 13:47:25 +02:00
Changes:
2018-01-26 15:45:57 +01:00
2017.08.31 updated to use PHP mysqli extension.
2018.02.23 removing Sieve filters if exists.
2017-09-01 10:07:34 +02:00
Tadas Ustinavičius <tadas at ring dot lt> ( https://github.com/postfixadmin/postfixadmin/pull/70 )
*/
$CONF = [];
2018-01-26 15:45:57 +01:00
// Either, uncomment this (and change to work)
//require_once('/path/to/postfixadmin/config.inc.php');
// OR uncomment this.
/*
$CONF = [
'database_host' => 'localhost',
'database_user' => 'someone',
'database_password' => 'something',
'database_name' => 'mydb'
];
*/
$MAKE_CHANGES = false; // change to true when you're happy this isn't going to trash your server.
2018-01-26 15:45:57 +01:00
if (empty($CONF)) {
die("\nPlease configure me\n\n");
}
// Where's the homedir accounts stored. (GET THIS RIGHT OTHERWISE IT THINK NONE EXIST AND DELETES ALL)
2018-01-26 15:45:57 +01:00
$homedir = '/home/virtual';
2018-01-26 15:45:57 +01:00
if (! is_dir($homedir)) {
die("Cannot find home directory for virtual mailboxes in $homedir\n");
}
//
// Recursive Delete Function
//
2021-04-13 22:19:16 +02:00
function deldir($dir)
{
$current_dir = opendir($dir);
2018-01-26 15:45:57 +01:00
while ($entryname = readdir($current_dir)) {
if (is_dir("$dir/$entryname") and ($entryname != "." and $entryname != "..")) {
2018-12-28 20:18:05 +01:00
deldir("{$dir}/{$entryname}");
} elseif ($entryname != "." and $entryname != "..") {
2018-12-28 20:18:05 +01:00
unlink("{$dir}/{$entryname}");
}
}
closedir($current_dir);
2018-12-28 20:18:05 +01:00
@rmdir($dir);
}
// --- Main Start ---
$dir = [];
//
// Get list of directories
//
2018-01-26 15:45:57 +01:00
$fr = opendir($homedir);
2017-08-31 13:47:25 +02:00
// TODO: Would glob($homedir . '/**/*/new') be somewhat quicker/shorter/less effort?
2018-01-26 15:45:57 +01:00
while (($domain = readdir($fr)) !== false) {
//
// Check if it's a dir
//
2018-02-23 21:36:18 +01:00
if ($domain == "." || $domain == ".." || filetype($homedir .'/'. $domain) != "dir") {
continue;
}
//
// Open the (assumed) DOMAIN directory
//
$ff = opendir($homedir .'/'. $domain);
while (($user = readdir($ff)) !== false) {
//
// Check for directories assuming it's a user account
//
2018-02-23 21:36:18 +01:00
if ($user == "." || $user == ".." || filetype($homedir .'/'. $domain .'/'. $user) != "dir") {
continue;
}
//
2018-02-23 21:36:18 +01:00
// if the dir 'new' exists inside then it's an account
//
if (file_exists($homedir .'/'. $domain .'/'. $user .'/'. "new")) {
$dir[$domain][$user] = "";
} else {
//
2018-02-23 21:36:18 +01:00
// Alert that the dir doesn't have a 'new' dir, possibly not an account. Leave it.
//
2018-02-23 21:36:18 +01:00
echo "UNKNOWN : " . $homedir ."/". $domain ."/". $user ."/new NOT FOUND. Possibly not an account. Leaving untouched\n";
2018-01-26 15:45:57 +01:00
}
}
2018-01-26 15:45:57 +01:00
}
//
// OK, got an array of accounts from the dir, Now connect to the DB and check them
//
2018-01-26 15:45:57 +01:00
$conx = mysqli_connect($CONF['database_host'], $CONF['database_user'], $CONF['database_password'], $CONF['database_name']);
//
// Is there a problem connecting?
//
2018-01-26 15:45:57 +01:00
if (! $conx || mysqli_connect_errno()) {
2018-12-28 20:18:05 +01:00
echo "DB connection failed." . mysqli_connect_error() . "\n";
die("Problem connecting to the database. ");
}
//
// Select all mailboxes to verify against dirs listed in array
//
$query = "SELECT * FROM mailbox";
2018-01-26 15:45:57 +01:00
$result = mysqli_query($conx, $query);
//
// Query the mailbox table
//
2018-01-26 15:45:57 +01:00
if (! $result) {
die("Failed to query mailbox table.");
}
//
// Fetch the list of results
//
2018-01-26 15:45:57 +01:00
while ($row = mysqli_fetch_assoc($result)) {
//
// Pull apart the maildir field, needed to figure out the directory structure to compare
//
2018-01-26 15:45:57 +01:00
$strip = explode("/", $row['maildir']);
//
// Unset the array if it exists. This stops it being erased later.
//
2018-01-26 15:45:57 +01:00
unset($dir[ $strip[0] ][ $strip[1] ]);
}
//
// If there are results. unset the domain too.
//
if (count($dir[$strip[0]]) == 0 and mysqli_num_rows($result) > 0) {
2018-01-26 15:45:57 +01:00
unset($dir[$strip[0]]);
}
//
// OK, time to clean up. All known users/domains have been removed from the list.
//
//
// If the array still exists (incase nothing there)
//
2018-01-26 15:45:57 +01:00
if (is_array($dir)) {
//
// Go through each dir
//
2018-01-26 15:45:57 +01:00
foreach ($dir as $key => $value) {
//
// Is this a user array?
//
2018-02-23 21:36:18 +01:00
if (!is_array($value)) {
continue;
}
//
// Go through and nuke the folders
//
foreach ($value as $user => $value2) {
// Nuke.. need any more explanations?
$path = $homedir . '/' . $key . '/' . $user;
$sieve_path = $homedir . '/.sieve/' . $key . '/' . $user;
$sieve_exists = file_exists($sieve_path);
// check if user has Sieve filters created
if ($MAKE_CHANGES) {
deldir($path);
if ($sieve_exists) {
deldir($sieve_path);
}
2018-02-23 21:36:18 +01:00
} else {
echo " - Would recursively delete : $path \n";
if ($sieve_exists) {
echo " - Would recursively delete Sieve filters : $sieve_path \n";
}
}
}
}
}
echo "Cleanup process completed\n";