diff --git a/include/class-MailboxAPI.php b/include/class-MailboxAPI.php index e06fa8c..c251615 100644 --- a/include/class-MailboxAPI.php +++ b/include/class-MailboxAPI.php @@ -1,132 +1,158 @@ . // load dependend traits class_exists( 'DomainAPI' ); /** * Methods for a MailboxAPI class */ trait MailboxAPITrait { use DomainAPITrait; /** * Limit to a specific mailbox * * @param object $mailbox Mailbox * @return self */ public function whereMailbox( $mailbox ) { return $this->whereMailboxID( $mailbox->getMailboxID() ); } /** * Limit to a specific Mailbox ID * * @param int $id Mailbox ID * @return self */ public function whereMailboxID( $id ) { return $this->whereInt( $this->MAILBOX_ID, $id ); } /** * Filter a specific Mailbox username * * @param string $username Mailbox username (without domain name) * @return self */ public function whereMailboxUsername( $username ) { return $this->whereStr( 'mailbox_username', $username ); } /** * Where the Mailbox is Active (or not) * * @param boolean $active If you want the active, or the inactive * @return self */ public function whereMailboxIsActive( $active = true ) { return $this->whereInt( 'mailbox_active', $active ); } + /** + * Filter to a specific Mailbox complete e-mail address + * + * Note that you must have both the Mailbox and Domain tables + * + * @param string $address E-mail address + * @return self + */ + public function whereCompleteMailboxAddress( $address ) { + + // no valid mailbox no party + if( substr_count( $address, '@' ) === 1 ) { + + // extract the mailbox username and domain name + list( $mailbox_username, $domain_name ) = explode( '@', $address, 2 ); + } + + // check if the user input has sense + if( !$mailbox_username || !$domain_name ) { + throw new Exception( "invalid e-mail address" ); + } + + return $this->whereDomainName( $domain_name ) + ->whereMailboxUsername( $mailbox_username ); + } + /** * Join mailboxes and domain (once) * * @return self */ public function joinMailboxDomain() { if( empty( $this->joinedMailboxDomain ) ) { $this->from( 'domain' ); $this->equals( 'domain.domain_ID', 'mailbox.domain_ID' ); $this->joinedMailboxDomain = true; } return $this; } /** * Check if I can edit this mailbox * * Actually it just checks if you can edit the whole domain. * * @return boolean */ public function whereMailboxIsEditable() { return $this->whereDomainIsEditable(); } } /** * Mailbox API */ class MailboxAPI extends Query { use MailboxAPITrait; /** * Univoque Domain ID column name * * Used by DomainAPITrait */ const DOMAIN_ID = 'mailbox.domain_ID'; /** * Univoque Plan ID column name */ protected $PLAN_ID = 'domain.plan_ID'; /** * Univoque column name to the Mailbox ID */ protected $MAILBOX_ID = 'mailbox.mailbox_ID'; /** * Constructor */ public function __construct( $db = null ) { // set database and class name parent::__construct( $db, Mailbox::class ); // set database table $this->from( Mailbox::T ); } } diff --git a/scripts/mailbox/destroy.php b/scripts/mailbox/destroy.php index bd82ec8..cdd87c8 100755 --- a/scripts/mailbox/destroy.php +++ b/scripts/mailbox/destroy.php @@ -1,115 +1,94 @@ #!/usr/bin/php . /** * destroy-mailbox * * This is a command-line interface to destroy a mailbox. * * This script is designed to be run by a SYSTEM ADMINISTRATOR * with enough brain and with enough privileges to delete contents from * your MDA. * * YES, This script PERMANENTLY REMOVE ALL THE F*****G E-MAILS * of the specified mailbox and PERMANENTLY REMOVE the mailbox from * your database and YOU SHOULD HAVE A F*****G BACKUP. */ // require the framework require dirname( __FILE__ ) . '/../../load.php'; // no arguments no party if( !isset( $argv ) ) { exit( 1 ); } // check the mailbox name $mailbox_raw = $argv[1] ?? null; // no mailbox no party if( !$mailbox_raw ) { destroy_mailbox_help( "Please specify a mailbox" ); exit( 2 ); } -// mailbox username and domain name -$mailbox_username = null; -$domain_name = null; - -// no valid mailbox no party -if( substr_count( $mailbox_raw, '@' ) === 1 ) { - - // extract the mailbox username and domain name - list( $mailbox_username, $domain_name ) = explode( '@', $mailbox_raw, 2 ); -} - -// check if the user input has sense -if( !$mailbox_username || !$domain_name ) { - destroy_mailbox_help( sprintf( - "Invalid e-mail address '%s'", - $mailbox_raw - ) ); - exit( 3 ); -} - // request the mailbox $mailbox = ( new MailboxAPI() ) ->joinDomain() - ->whereDomainName( $domain_name ) - ->whereMailboxUsername( $mailbox_username ) + ->whereCompleteMailboxAddress( $mailbox_raw ) ->queryRow(); // no mailbox no party if( !$mailbox ) { echo "Cannot destroy an unexisting mailbox.\n"; exit( 4 ); } // mandatory question printf( "Are you F*****G sure to DESTROY the mailbox '%s' FOREVER? [y/n]\n", $mailbox->getMailboxAddress() ); $yes = readline(); // aborted if( $yes !== 'y' ) { echo "Aborted\n"; exit( 0 ); } // destroy this F*****G mailbox NOW MailboxDestroyer::destroy( $mailbox ); echo "Destroyed.\n"; /** * Show an help message, eventually with a custom message * * @param $message string */ function destroy_mailbox_help( $message = null ) { printf( "Usage:\n %s foo@example.com\n", $GLOBALS['argv'][0] ); // eventually spawn an error message if( $message ) { echo "\n"; printf( "Error:\n %s\n", $message ); } }