This script is a quick and handy way to delete Dspam users from your system one at a time.
Just specify the script name followed by the user you want to delete:
dspam-delete-user admin@example.com
All user data for this user is removed within Dspam, plus the user's data directory is deleted.
This script is another in-house script we created and agreed to share with others who may find it useful.
Code
#!/usr/local/bin/php -n
<?PHP
/*
Purpose
This script is designed to be used when you are running Dspam and using MySQL as Dspam's data store.
This script deletes a Dspam user from all Dspam tables and it removes the user's data directory.
The data directory is normally under /var/dspam/data/ but is configurable below.
All Dspam user's are specified in the dspam_virtual_uids table in the field "username".
Requirements
The data directory must be in this format or the user's data directory will not be removed:
/var/dspam/data/<domain>/<user>
The Dspam data root directory part of this path (/var/dspam/data/) can be configured below.
Usage
Syntax:
dspam-delete-user <Dspam Username>
The Dspam Username is usually an email address.
Example
dspam-delete-user russ@russtanner.com
If no domain is specified, domain defaults to "local". Example:
dspam-delete-user russ
- This defaults to Dspam user russ@local
Notes
Be sure to complete the Configuration section below because this script requires database credentials
in order to query and modify the database.
Author
Russ Tanner - russ@russtanner.com
VISIT: GlobalSkywatch.com
License
This script is free software. No warranty is associated with it at all.
By using it you agree to hold the author harmless and not-liable for any damage done whether the
script is used or misused.
Version History
1.0 2012-03-13 Tested by Russ Tanner on PHP 5.3.10 on CentOS 5.7.
VISIT: GlobalSkywatch.com
*/
// Configuration //
// Make configuration settings below.
// Database Credentials
// The name of the server this script is running on.
// If the database is on the same server, you will normally use "localhost".
$DBServer = 'localhost';
// The username and password used to log into the database.
$DBUsername = 'username';
$DBPassword = 'password';
// The name of the database containing Dspam's tables.
$DBDatabase = 'postfix';
// Directories //
/* (string) The absolute path to the Dspam data directory.
This is the directory containing Dspam's per-email data.
This is an absolute path so use a leading slash.
This is a directory so use a trailing slash.
*/
$DataDir = '/var/dspam/data/';
// End Configuration //
// Internal Management Globals //
/* (array) Stores passed arguments after they have been parsed.
*/
$ParsedArgs = array();
/* (string) The name of the Dspam user we are deleting.
*/
$DspamUsername = '';
// PHP Settings //
// Use utf-8 for all pages
//ini_set('default_charset', cfg::$CharSet);
// Basic settings
ini_set('display_errors', 1);
ini_set('log_errors', 1);
// Functions //
function init() {
/*
Purpose
Used to initialize this script.
Version History
1.0
*/
// Get access to the $argv variable provided by PHP.
global $argv, $ParsedArgs, $DspamUsername;
// Parse arguments and save to its global variable.
$ParsedArgs = ParseArgs($argv);
// Assign select arguments to global settings...
// The user we are deleting.
if (GetArg(0) != '') $DspamUsername = GetArg(0);
}
function DeleteUID($TableName, $UserID, $hDB) {
/*
Purpose
Executes a SQL DELETE statement for the specifield table name and user ID (UID).
Parameters
TableName (string) = The name of the table we're deleting rows from.
UID (interger) = All records with this user ID will be deleted from the table TableName.
hDB (resource) = The database connection resource necessary for queries.
*/
print "\nDeleting user data from $TableName...";
// Query the database.
$SQL = "DELETE FROM $TableName WHERE uid = $UserID";
$q = mysql_query($SQL, $hDB);
// Error Trap: Query error. Display error and exit now.
if (! $q) exit(" Error querying database:\n" . mysql_error($hDB) . "\n");
// Display the number of rows deleted.
print ' ' . mysql_affected_rows($hDB) . ' rows deleted';
}
function GetArg($ArgName, $DefValue = '') {
/*
Purpose
Used to safely retrieve arguments from the $ParsedArgs global.
This prevents PHP notices from being generated when attempting to retrieving a non-existent array element.
Parameters
ArgName (string, integer) = The name or index of the argument we want to retrieve.
DefValue (optional mixed) = The value to return if the specified argument does not exist.
Default: "" (zero-length string)
Return (string)
Returns the value of the specified argument.
Returns "" (zero-length string) if:
- The specified argument was not found.
Version History
1.0
*/
global $ParsedArgs;
// The specified argument exists. Return it.
if (array_key_exists($ArgName, $ParsedArgs)) return $ParsedArgs[$ArgName];
// The specified argument does not exist. Return the default value.
else return $DefValue;
}
function ParseArgs($argv){
array_shift($argv); $o = array();
foreach ($argv as $a){
if (substr($a,0,2) == '--'){ $eq = strpos($a,'=');
if ($eq !== false){ $o[substr($a,2,$eq-2)] = substr($a,$eq+1); }
else { $k = substr($a,2); if (!isset($o[$k])){ $o[$k] = true; } } }
else if (substr($a,0,1) == '-'){
if (substr($a,2,1) == '='){ $o[substr($a,1,1)] = substr($a,3); }
else { foreach (str_split(substr($a,1)) as $k){ if (!isset($o[$k])){ $o[$k] = true; } } } }
else { $o[] = $a; } }
return $o;
}
function SaySettings() {
/*
Purpose
Used to display all user settings.
*/
global $DspamUsername, $DataDir;
print "\nDspam Username: $DspamUsername";
print "\nData Directory: $DataDir";
return;
}
// Main //
// Initialize this script.
Init();
// Error Trap: No username specified.
if (strlen($DspamUsername) == 0) exit("\nNo username specified\n");
// Display settings.
print "\nUsing the following settings";
SaySettings();
print "\n";
// Split the email into user and domain.
list($Username, $Domain) = explode('@', $DspamUsername);
// If the domain is empty (no @ in the Dspam Username), default it to "local".
if (strlen($Domain) == 0) $Domain = 'local';
print "\nUsername: $Username\nDomain: $Domain";
// Open the database.
print "\n\nOpening database...";
// Connect to the database.
$hDB = mysql_connect($DBServer, $DBUsername, $DBPassword);
// Error Trap
if ($hDB === false) exit(" Error connecting to database:\n" . mysql_error($hDB) . "\n");
// Open the database.
$e = mysql_select_db($DBDatabase, $hDB);
// Error Trap
if (! $e) exit(" Error opening to database:\n" . mysql_error($hDB) . "\n");
print ' OK';
// Lookup the UID of the specified user.
print "\nLooking up user ID...";
// Query the database.
$SQL = sprintf("SELECT * FROM dspam_virtual_uids WHERE username = '%s'",
mysql_real_escape_string($DspamUsername, $hDB));
$q = mysql_query($SQL, $hDB);
// Error Trap: Query error. Display error and exit now.
if (! $q) exit(" Error querying database:\n" . mysql_error($hDB) . "\n");
// Error Trap: No rows were returned in the query. Display an error message and exit.
if (mysql_num_rows($q) == 0) exit(" Error: The specified user does not exist\n");
// User found. Fetch and display user ID.
$row = mysql_fetch_assoc($q);
$UserID = $row['uid'];
print " Found: $UserID";
// Delete user data from each dspam table.
DeleteUID('dspam_signature_data', $UserID, $hDB);
DeleteUID('dspam_stats', $UserID, $hDB);
DeleteUID('dspam_token_data', $UserID, $hDB);
DeleteUID('dspam_signature_data', $UserID, $hDB);
DeleteUID('dspam_virtual_uids', $UserID, $hDB);
// Delete dspam data directory.
$DirPath = $DataDir . $Domain . '/' . $Username;
print "\nDeleting user data directory $DirPath...";
// Failsafe: To prevent major accidents, we only rm if these 3 conditions are true:
// DirPath exists and is a directory
// DirPath contains at least 10 characters
// DirPath contains at least 1 forward slash
if (is_dir($DirPath) && strlen($DirPath) >= 10 && strpos($DirPath, '/') !== false) {
$cmd = "rm -r $DirPath";
exec($cmd);
//print "\ncommand: $cmd\n"; // For debugging only.
print ' OK';
}
// Display "complete" message.
print "\n\nComplete.\n\n";
?>
The Captian Today they call you "crazy". Tomorrow they call you "ahead of your time." Global Skywatch Learn about Chemtrails - You're breathing them now! OnlyTheBestHerbs.com World-class supplements Mercury Talk Why you are sick. OneUp Domains Domains, Hosting, Email 1-800-358-4278 (U.S. & Canada)