Purging users via CSV
Should you need to purge a large set of users from your Atmail system, you can use a new script to batch delete via a CSV file.
This will be included in Atmail 6.2.1 due Jan 2011 - In the meantime you can use the script below, store under:
webmail/utilities/tools/purge-users-csv.php
The usage is simple:
cd webmail/utilities/tools/
php purge-users.csv.php /path/to/userlist.txt
Where /path/to/userlist.txt contains a list of users, seperated by a newline.
This script will remove all the users database entries, clear the users maildir and purge the account from the system.
-
/**
* Purge users from the system who have not logged in
* in the last X days (where X is passed as an argument)
* to this script
*
* @author Ben Duncan
* @usage php purge-users-csv.php /path/to/csv
*/
require_once("/usr/local/atmail/webmail/utilities/nfc-bootloader.php");
// require that the argument is numeric
if (empty($_SERVER['argv'][1])) {
echo "\nUsage: php purge-users-csv.php /path/to/csvfile.txt\n\n".
"Where csvfile.txt contains a list of users seperated by a newline\n";
exit;
}
// setup api access
require_once('application/models/api.php');
$_SERVER['PHP_AUTH_USER'] = 'admin';
$api = new api( array('directApi' => 1) );
echo "Opening " . $_SERVER['argv'][1] . "\n";
$fp = fopen($_SERVER['argv'][1], "r");
while ( ($line = fgets($fp)) !== false) {
$line = trim($line);
// fetch our list of inactive accounts
$userExists = $dbAdapter->fetchOne("select Account from UserSession where Account=?", array($line));
if( !empty($userExists) ) {
echo "Deleting $line - ";
$arr = $api->userDelete($line);
if($arr['status'] == 'failed')
echo 'FAIL ' . $arr['response'] . "\n";
else
echo "OK\n";
} else {
echo "Deleting $line - FAIL ( no such user )\n";
}
}

