diff --git a/include/class-Domain.php b/include/class-Domain.php index fbe3b4c..e6ea78e 100644 --- a/include/class-Domain.php +++ b/include/class-Domain.php @@ -1,77 +1,77 @@ . /** * A domain handled by the user */ class Domain extends Queried { const T = 'domain'; const UID = 'domain_name'; public function __construct() { $this->integers( 'domain_ID' ); $this->booleans( 'domain_active' ); $this->dates( 'domain_born', 'domain_expiration' ); } /** * Get domain ID * * @return int */ public function getDomainID() { return $this->get( 'domain_ID' ); } /** * Get domain name * * @return string */ public function getDomainName() { return $this->get( 'domain_name' ); } /** * Get the domain edit URl * * @return string */ public function getDomainPermalink( $absolute = false ) { return ROOT . '/domain.php/' . $this->get( 'domain_name' ); } /** * Factory mailbox from this domain * * @return MailboxFullAPI */ public function factoryMailbox() { return ( new MailboxFullAPI() )->whereDomain( $this ); } /** * Factory e-mail forward from this domain * * @return MailforwardFullAPI */ - public function factoryMailforward() { - return ( new MailforwardFullAPI() )->whereDomain( $this ); + public function factoryMailforwardfrom() { + return ( new MailforwardfromAPI() )->whereDomain( $this ); } } diff --git a/include/class-MailforwardAPI.php b/include/class-MailforwardAPI.php index 5c3c053..728dffa 100644 --- a/include/class-MailforwardAPI.php +++ b/include/class-MailforwardAPI.php @@ -1,64 +1,64 @@ . /** * E-mail forwarding API */ -class MailforwardAPI extends DomainAPI { +class MailforwardfromAPI extends DomainAPI { public function __construct() { Query::__construct(); - $this->from( Mailforward::T ); - $this->defaultClass( 'Mailforward' ); + $this->from( Mailforwardfrom::T ); + $this->defaultClass( 'Mailforwardfrom' ); } /** * Join mailforwardes and domain (once) * * @return self */ - public function joinMailforwardDomain() { - if( empty( $this->joinedMailforwardDomain ) ) { - $this->joinedMailforwardDomain = true; + public function joinMailforwardfromDomain() { + if( empty( $this->joinedMailforwardfromDomain ) ) { + $this->joinedMailforwardfromDomain = true; $this->from( 'domain' ); - $this->equals( 'domain.domain_ID', 'mailfoward.domain_ID' ); + $this->equals( 'domain.domain_ID', 'mailforwardfrom.domain_ID' ); } return $this; } /** - * Filter to a certain mail forwarding source + * Filter to a certain mail forwarding username * * @param mailforward_source string * @return self */ - public function whereMailforwardSource( $mailforward_source ) { - return $this->whereStr( 'mailfoward_source', $mailforward_source ); + public function whereMailforwardfromUsername( $username ) { + return $this->whereStr( 'mailforwardfrom_username', $username ); } /** * Filter to a certain domain ID * * @param $domain_ID int * @return self * @override */ public function whereDomainID( $domain_ID ) { - return $this->whereInt( 'mailfoward.domain_ID', $domain_ID ); + return $this->whereInt( 'mailforwardfrom.domain_ID', $domain_ID ); } } diff --git a/include/class-Mailforward.php b/include/class-Mailforwardfrom.php similarity index 67% rename from include/class-Mailforward.php rename to include/class-Mailforwardfrom.php index e214034..eaa1971 100644 --- a/include/class-Mailforward.php +++ b/include/class-Mailforwardfrom.php @@ -1,92 +1,94 @@ . -/** - * An e-mail forwarding - */ -class Mailforward extends Domain { - - const T = 'mailfoward'; - +trait MailforwardfromTrait { /** - * Get the mailforward address + * Get the mailforward from ID * - * @return string E-mail + * @return int */ - public function getMailforwardAddress() { - return sprintf( '%s@%s', - $this->getMailforwardSource(), - $this->getDomainName() - ); + public function getMailforwardfromID() { + return $this->get( 'mailforwardfrom_ID' ); } /** - * Get the mailforward source (just username) + * Get the mailforward from address * - * @return string + * @return string E-mail */ - public function getMailforwardSource() { - return $this->get( 'mailfoward_source' ); + public function getMailforwardfromAddress() { + return sprintf( '%s@%s', + $this->getMailforwardfromUsername(), + $this->getDomainName() + ); } /** - * Get the mailforward destination + * Get the mailforward from username * * @return string */ - public function getMailforwardDestination() { - return $this->get( 'mailfoward_destination' ); + public function getMailforwardfromUsername() { + return $this->get( 'mailforwardfrom_username' ); } /** * Get the mailforward permalink * * @param $absolute boolean * @return string */ - public function getMailforwardPermalink( $absolute = false ) { - return Mailforward::permalink( + public function getMailforwardfromPermalink( $absolute = false ) { + return Mailforwardfrom::permalink( $this->getDomainName(), - $this->getMailforwardSource(), + $this->getMailforwardfromUsername(), $absolute ); } +} + +/** + * An e-mail forwarding + */ +class Mailforwardfrom extends Domain { + use MailforwardfromTrait; + + const T = 'mailforwardfrom'; /** * Update the related database row */ public function update( $columns ) { query_update( self::T, $columns, sprintf( - "domain_ID = %d AND mailfoward_source = '%s'", - $this->getDomainID(), - esc_sql( $this->getMailforwardSource() ) + "mailforwardfrom_ID = %d", + $this->getMailforwardfromID() ) ); } /** * Get the mailforward permalink * * @return string */ public static function permalink( $domain, $mailforward = false, $absolute = false ) { $part = site_page( 'mailforward.php', $absolute ) . _ . $domain; if( $mailforward ) { $part .= _ . $mailforward; } return $part; } } diff --git a/include/class-MailforwardfromAPI.php b/include/class-MailforwardfromAPI.php new file mode 100644 index 0000000..be7c8eb --- /dev/null +++ b/include/class-MailforwardfromAPI.php @@ -0,0 +1,43 @@ +. + +class_exists( 'MailforwardfromAPI', true ); +class_exists( 'DomainAPI', true ); + +class MailforwardfromAPI extends DomainAPI { + + /** + * Construct + */ + public function __construct() { + parent::__construct(); + $this->from( 'mailforwardfrom' ); + $this->equals( 'mailforwardfrom.domain_ID', 'domain.domain_ID' ); + $this->defaultClass( 'Mailforwardfrom' ); + } + + /** + * Filter to a specific mailforward username + * + * @param $username string + * @return self + */ + public function whereMailforwardfromUsername( $username ) { + return $this->whereStr( 'mailforwardfrom_username', $username ); + } + +} diff --git a/include/class-Mailforwardto.php b/include/class-Mailforwardto.php new file mode 100644 index 0000000..3499a68 --- /dev/null +++ b/include/class-Mailforwardto.php @@ -0,0 +1,34 @@ +. + +/** + * An e-mail forwarding destination + */ +class Mailforwardto extends Domain { + + const T = 'mailforwardto'; + + /** + * Get the mailforward destination + * + * @return string + */ + public function getMailforwardtoAddress() { + return $this->get( 'mailforwardto_address' ); + } + +} diff --git a/include/class-MailforwardtoAPI.php b/include/class-MailforwardtoAPI.php new file mode 100644 index 0000000..0bec8e0 --- /dev/null +++ b/include/class-MailforwardtoAPI.php @@ -0,0 +1,59 @@ +. + +class MailforwardtoAPI extends Query { + + /** + * Construct + */ + public function __construct() { + parent::__construct(); + $this->from( Mailforwardto::T ); + $this->defaultClass( Mailforwardto::class ); + } + + /** + * Filter to a specific mailforwardfrom + * + * @param $mailforwardfrom object + * @return self + */ + public function whereMailforwardfrom( $mailforwardfrom ) { + return $this->whereMailforwardfromID( $mailforwardfrom->getMailforwardfromID() ); + } + + /** + * Filter to a specific mailforwardfrom + * + * @param $id int + * @return self + */ + public function whereMailforwardfromID( $id ) { + return $this->whereInt( 'mailforwardto.mailforwardfrom_ID', $id ); + } + + /** + * Filter to a specific mailforwardto address + * + * @param $address string e-mail address + * @return self + */ + public function whereMailforwardtoAddress( $address ) { + return $this->whereStr( 'mailforwardto_address', $address ); + } + +} diff --git a/include/functions.php b/include/functions.php index d15dbd7..c7c81a0 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1,191 +1,205 @@ . /** * Require a certain page from the template directory * * @param $name string page name (to be sanitized) * @param $args mixed arguments to be passed to the page scope */ function template( $template_name, $template_args = [] ) { extract( $template_args, EXTR_SKIP ); return require TEMPLATE_PATH . __ . "$template_name.php"; } /** * Get the returned text from a template * * @param $name string page name (to be sanitized) * @param $args mixed arguments to be passed to the page scope * @see template() * @return string */ function template_content( $name, $args = [] ) { ob_start(); template( $name, $args ); $text = ob_get_contents(); ob_end_clean(); return $text; } /** * Print an e-mail (safe for bots) * * @param $email string */ function email_blur( $email ) { $dot = strip_tags( __( " dot " ) ); $at = strip_tags( __( " at " ) ); $email = esc_html( $email ); echo str_replace( [ '.', '@' ], [ $dot, $at ], $email ); } /** * Send an e-mail to someone * * @param $subject string E-mail subject * @param $message string E-mail message * @param $to string E-mail recipient (from current logged-in user as default) */ function send_email( $subject, $message, $to = false ) { if( ! $to ) { if( ! is_logged() ) { die( "can't retrieve e-mail address from anon user" ); } $to = get_user( 'user_email' ); } return SMTPMail::instance() ->to( $to ) ->message( $subject, $message ) ->disconnect(); } /** * Require a certain permission * * @param $permission string An internal permission like 'edit-all-user' * @param $redirect boolean Enable or disable the redirect */ function require_permission( $permission, $redirect = true ) { if( ! has_permission( $permission ) ) { if( is_logged() ) { Header::spawn( [ 'title' => __( "Permission denied" ), ] ); Footer::spawn(); exit; } else { $login = get_menu_entry( 'login' ); $url = $login->getSitePage( URL ); if( $redirect && isset( $_SERVER[ 'REQUEST_URI' ] ) ) { $url = http_build_get_query( $url, [ 'redirect' => $_SERVER[ 'REQUEST_URI' ], ] ); } http_redirect( $url, 307 ); } } } /** * Check the POST action * * @param $action string e.g. 'save-domain' * @return bool */ function is_action( $action ) { return isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] === $action; } /** * Get URL parts from the PATH_INFO * * It spawn a "bad request" page if something goes wrong. * * @param $max int If $min is specified, this is the maximum number of parameters. When unspecified, this is the exact number of parameters. * @param $min int Mininum number of parameters. * @return array * @see https://httpd.apache.org/docs/2.4/mod/core.html#acceptpathinfo */ function url_parts( $max, $min = false ) { if( $min === false ) { $min = $max; } // split the PATH_INFO parts $parts = explode( _, $_SERVER[ 'PATH_INFO' ] ); array_shift( $parts ); // eventually spawn the "bad request" $n = count( $parts ); if( $n > $max || $n < $min ) { BadRequest::spawn( __( "unexpected URL" ) ); } // eventually fill expected fields for( $i = $n; $i < $max; $i++ ) { $parts[] = null; } return $parts; } /** * Link to an existing page from the menu * * @param $uid string E.g. 'index' * @param $args mixed Arguments */ function the_menu_link( $uid, $args = [] ) { $page = get_menu_entry( $uid ); the_link( $page->getSitePage(), $page->name, $args ); } /** * Link to a whatever page * * P.S. link() is a reserved function * * @param $url string * @param $title string * @param $args mixed Arguments */ function the_link( $url, $title, $args = [] ) { template( 'link', [ 'title' => $title, 'url' => $url, 'args' => $args, ] ); } /** * Generate a password * * @param $bytes int */ function generate_password( $bytes = 8 ) { return rtrim( base64_encode( bin2hex( openssl_random_pseudo_bytes( $bytes ) ) ), '=' ); } /** * Validate a mailbox username * * @param $mailbox string * @return bool */ function validate_mailbox_username( $mailbox ) { return 1 === preg_match( '/^[a-z][a-z0-9-_.]+$/', $mailbox ); } + +/** + * A certain value must be an e-mail + * + * @param $email string + * @return string filtered e-mail + */ +function require_email( $email ) { + $email = luser_input( $email, 128 ); + if( filter_var( $email, FILTER_VALIDATE_EMAIL ) === false ) { + BadRequest::spawn( __( "fail e-mail validation" ) ); + } + return $email; +} diff --git a/mailforward.php b/mailforward.php index 5812f1a..683c693 100644 --- a/mailforward.php +++ b/mailforward.php @@ -1,170 +1,206 @@ . /* * This is the single e-mail forwarding edit page */ // load framework require 'load.php'; // wanted informations -$domain = null; -$mailforward = null; +$domain = null; +$mailforwardfrom = null; // URL paramenters (maximum both domain and mailforward source, minimum just domain) -list( $domain_name, $mailforward_source ) = url_parts( 2, 1 ); +list( $domain_name, $mailforwardfrom_username ) = url_parts( 2, 1 ); // eventually retrieve mailforward from database -if( $mailforward_source ) { - $mailforward = ( new MailforwardFullAPI ) +if( $mailforwardfrom_username ) { + $mailforwardfrom = ( new MailforwardfromAPI ) ->select( [ 'domain.domain_ID', - 'domain.domain_name', - 'mailfoward_source', - 'mailfoward_destination', + 'domain_name', + 'mailforwardfrom.mailforwardfrom_ID', + 'mailforwardfrom_username', ] ) ->whereDomainName( $domain_name ) - ->whereMailforwardSource( $mailforward_source ) + ->whereMailforwardfromUsername( $mailforwardfrom_username ) ->whereDomainIsEditable() ->queryRow(); // 404 - $mailforward or PageNotFound::spawn(); + $mailforwardfrom or PageNotFound::spawn(); // recycle the mailforward object that has domain informations - $domain = $mailforward; + $domain = $mailforwardfrom; } // eventually retrieve domain from database if( ! $domain ) { $domain = ( new DomainAPI() ) ->select( [ 'domain.domain_ID', 'domain.domain_name', ] ) ->whereDomainName( $domain_name ) ->whereDomainIsEditable() ->queryRow(); // 404 $domain or PageNotFound::spawn(); } // save destination action if( is_action( 'mailforward-save' ) ) { // columns to be saved $changes = []; // always require destination if( empty( $_POST[ 'mailforward_destination' ] ) ) { BadRequest::spawn( __( "missing parameter" ) ); } // validate destination - $destination = luser_input( $_POST[ 'mailforward_destination' ], 128 ); - if( filter_var( $destination, FILTER_VALIDATE_EMAIL ) ) { - $changes[] = new DBCol( 'mailfoward_destination', $destination, 's' ); - } else { - BadRequest::spawn( __( "fail e-mail validation" ) ); - } + $destination = require_email( $destination ); + $changes[] = new DBCol( 'mailforward_destination', $destination, 's' ); // save source only during creation - if( ! $mailforward ) { + if( ! $mailforwardfrom ) { // require source (can be empty) if( ! isset( $_POST[ 'mailforward_source' ] ) ) { BadRequest::spawn( __( "missing parameter" ) ); } - $source = luser_input( $_POST[ 'mailforward_source' ], 128 ); - if( ! empty( $source ) && ! validate_mailbox_username( $source ) ) { - BadRequest::spawn( __( "invalid mailbox name" ) ); + $sources = luser_input( $_POST[ 'mailforward_source' ], 128 ); + $sources = explode( ',', $_POST[ 'mailforward_source' ] ); + foreach( $sources as $source ) { + if( ! empty( $source ) && ! validate_mailbox_username( $source ) ) { + BadRequest::spawn( __( "invalid mailbox name" ) ); + } + $changes[] = new DBCol( 'mailforward_source', $source, 's' ); } - $changes[] = new DBCol( 'mailfoward_source', $source, 's' ); } if( $changes ) { - if( $mailforward ) { + if( $mailforwardfrom ) { // update existing - $mailforward->update( $changes ); + $mailforwardfrom->update( $changes ); // POST/redirect/GET - http_redirect( $mailforward->getMailforwardPermalink( true ), 303 ); + http_redirect( $mailforwardfrom->getMailforwardPermalink( true ), 303 ); } else { // insert as new // check existence - $mailforward_exists = ( new MailforwardAPI ) + $mailforwardfrom_exists = ( new MailforwardAPI ) ->select( 1 ) ->whereDomain( $domain ) ->whereMailforwardSource( $source ) ->queryRow(); // die if exists - if( $mailforward_exists ) { + if( $mailforwardfrom_exists ) { BadRequest::spawn( __( "e-mail forwarding already existing" ) ); } // insert as new row - insert_row( 'mailfoward', array_merge( $changes, [ + insert_row( 'mailforward', array_merge( $changes, [ new DBCol( 'domain_ID', $domain->getDomainID(), 'd' ), ] ) ); // POST/redirect/GET http_redirect( Mailforward::permalink( $domain->getDomainName(), $source, true ), 303 ); } } } // delete action -if( $mailforward && is_action( 'mailforward-delete' ) ) { - query( sprintf( - "DELETE FROM %s WHERE domain_ID = %d AND mailfoward_source = '%s'", - T( 'mailfoward' ), - $mailforward->getDomainID(), - esc_html( $mailforward->getMailforwardSource() ) - ) ); - - // POST/redirect/GET - http_redirect( $domain->getDomainPermalink( true ), 303 ); +if( $mailforwardfrom ) { + + // action fired when deleting a whole mailforward + if( is_action( 'mailforward-delete' ) ) { + + // drop th + query( sprintf( + "DELETE FROM %s WHERE domain_ID = %d AND mailforward_source = '%s'", + T( 'mailforward' ), + $mailforwardfrom->getDomainID(), + esc_sql( $mailforwardfrom->getMailforwardSource() ) + ) ); + + // POST/redirect/GET + http_redirect( $domain->getDomainPermalink( true ), 303 ); + + } + + // action fired when adding/removing a mailforward + if( ( is_action( 'mailforwardto-add' ) || is_action( 'mailforwardto-remove' ) ) && isset( $_POST[ 'address' ] ) ) { + + $address = require_email( $_POST[ 'address' ] ); + + $existing_address = + ( new MailforwardtoAPI() ) + ->whereMailforwardfrom( $mailforwardfrom ) + ->whereMailforwardtoAddress( $address ) + ->queryRow(); + + // action fired when removing a mailforward + if( is_action( 'mailforwardto-remove' ) && $existing_address ) { + query( sprintf( + "DELETE FROM %s WHERE mailforwardfrom_ID = %d and mailforwardto_address = '%s'", + T( 'mailforwardto' ), + $mailforwardfrom->getMailforwardfromID(), + esc_sql( $address ) + ) ); + } + + // action fired when adding a mailforward + if( is_action( 'mailforwardto-add' ) && ! $existing_address ) { + insert_row( 'mailforwardto', [ + new DBCol( 'mailforwardfrom_ID', $mailforwardfrom->getMailforwardfromID(), 'd' ), + new DBCol( 'mailforwardto_address', $address, 's' ), + ] ); + } + } } // spawn header Header::spawn( [ 'title-prefix' => __( "E-mail forwarding" ), - 'title' => $mailforward - ? $mailforward->getMailforwardAddress() + 'title' => $mailforwardfrom + ? $mailforwardfrom->getMailforwardfromAddress() : __( "create" ), 'breadcrumb' => [ new MenuEntry( null, $domain->getDomainPermalink(), $domain->getDomainName() ), ], ] ); // spawn the page content template( 'mailforward', [ - 'domain' => $domain, - 'mailforward' => $mailforward, + 'domain' => $domain, + 'mailforwardfrom' => $mailforwardfrom, ] ); // spawn the footer Footer::spawn(); diff --git a/template/mailforward.php b/template/mailforward.php index 2605088..e071de5 100644 --- a/template/mailforward.php +++ b/template/mailforward.php @@ -1,62 +1,60 @@ . /* * This is the template for a single e-mail forwarding * * Called from: * mailforward.php * * Available variables: * $domain Domain object - * $mailforward Mailforward object + * $mailforwardfrom Mailforward object */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; ?>


- - getMailforwardAddress() ) ?> /> + + getMailforwardfromAddress() ) ?> /> - getMailforwardSource() ); + getMailforwardfromUsername() ); } ?> /> @getDomainName() ) ?> -

-

-
- getMailforwardDestination() ); - } - ?> /> -

-

- - - + + + + +

+ +

+ $domain, + 'mailforwardfrom' => $mailforwardfrom, + ] ) ?> diff --git a/template/mailforwards.php b/template/mailforwards.php index 365a7de..b65551f 100644 --- a/template/mailforwards.php +++ b/template/mailforwards.php @@ -1,64 +1,67 @@ . /* * This is the template for the mailboxes list * * Called from: * template/domain.php + * + * Available variables: + * $domain object */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; // domain mail forwardings -$mailforwards = $domain->factoryMailforward() +$mailforwardfroms = $domain->factoryMailforwardfrom() ->select( [ + 'mailforwardfrom.mailforwardfrom_ID', + 'mailforwardfrom_username', 'domain_name', - 'mailfoward_source', - 'mailfoward_destination', ] ) ->queryGenerator(); ?>

- valid() ): ?> + valid() ): ?>

getDomainName() ), + Mailforwardfrom::permalink( $domain->getDomainName() ), __( "Create" ) ) ?>

diff --git a/template/mailforwardto.php b/template/mailforwardto.php new file mode 100644 index 0000000..53632c0 --- /dev/null +++ b/template/mailforwardto.php @@ -0,0 +1,56 @@ +. + +/* + * This is the template for a single e-mail forwarding + * + * Called from: + * template/mailforward.php + * + * Available variables: + * $domain Domain object + * $mailforwardfrom Mailforward object + */ + +// unuseful when load directly +defined( 'BOZ_PHP' ) or die; + +$mailforwardto = + ( new MailforwardtoAPI() ) + ->whereMailforwardfrom( $mailforwardfrom ) + ->orderBy( 'mailforwardto_address' ) + ->queryGenerator(); +?> + +