#!/usr/local/bin/php -n / The Dspam data root directory part of this path (/var/dspam/data/) can be configured below. Usage Syntax: dspam-delete-user 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 on PHP 5.3.10. 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(); // 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..."; // Say status. // 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'; // Say status. // 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"; ?>