diff --git a/include/class-PlanAPI.php b/include/class-PlanAPI.php index ab578aa..65a4480 100644 --- a/include/class-PlanAPI.php +++ b/include/class-PlanAPI.php @@ -1,100 +1,101 @@ . /** * Methods related to a Plan class */ trait PlanAPITrait { /** * Limit to a specific Plan by its UID * * @param $plan_name string * @return self */ public function wherePlanUID( $uid ) { return $this->whereStr( 'plan_uid', $plan_uid ); } /** * Constructor from a plan ID * * @param $plan_ID int * @return self */ public function wherePlanID( $plan_ID ) { return $this->whereInt( static::PLAN_ID, $plan_ID ); } /** * Constructor from a Plan object * * @param $plan object * @return self */ public function wherePlan( $plan ) { return $this->wherePlanID( $plan->getPlanID() ); } /** * Order by the Plan name * * @param string $direction DESC|ASC * @return self */ public function orderByPlanName( $direction = null ) { return $this->orderBy( 'plan_name', $direction ); } /** * Join whatever table with the plan table * + * @param string $type Join type * @return self */ - public function joinPlan() { - return $this->joinOn( 'INNER', 'plan', static::PLAN_ID, 'plan.plan_ID' ); + public function joinPlan( $type = 'INNER' ) { + return $this->joinOn( $type, 'plan', static::PLAN_ID, 'plan.plan_ID' ); } } /** * Plan API */ class PlanAPI extends Query { use PlanAPITrait; /** * Plan ID column name */ const PLAN_ID = 'plan.plan_ID'; /** * Constructor * * @param object $db Database (or NULL for the current one) */ public function __construct( $db = null ) { // set database connection and default class name for the results parent::__construct( $db, 'Plan' ); // select from this database table $this->from( Plan::T ); } } diff --git a/template/mailboxes.php b/template/mailboxes.php index f28b5d9..15d8684 100644 --- a/template/mailboxes.php +++ b/template/mailboxes.php @@ -1,74 +1,76 @@ . /* * This is the template for the mailboxes list * * Called from: * template/domain.php */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; // domain mailboxes $mailboxes = $domain->factoryMailbox() ->select( [ 'domain_name', 'mailbox_username', 'mailbox_receive', ] ) ->queryGenerator(); // total number of mailboxes $count = DB::instance()->affectedRows(); ?>
- + null, +] ) ?> valid() ): ?>= HTML::a(
$mailbox->getMailboxPermalink(),
- $mailbox->getMailboxAddress()
+ esc_html( $mailbox->getMailboxAddress() )
) ?>
= esc_html( sprintf( __( "Your Plan \"%s\" allows %s %s." ), $plan->getPlanName(), $plan->getPlanMailboxes(), __( "Mailboxes" ) ) ) ?>
getPlanMailboxes() > $count || has_permission( 'edit-email-all' ) ): ?>getDomainName() ), __( "Create" ) ) ?>
diff --git a/www/domain.php b/www/domain.php index adbcf0f..e7e3747 100644 --- a/www/domain.php +++ b/www/domain.php @@ -1,91 +1,91 @@ . /* * This is the domain edit page */ // load framework require '../load.php'; // wanted domain list( $domain_name ) = url_parts( 1, 0 ); $domain = null; if( $domain_name ) { // retrieve domain $domain = ( new DomainAPI() ) ->whereDomainName( $domain_name ) ->whereDomainIsEditable() - ->joinPlan() + ->joinPlan( 'LEFT' ) ->queryRow(); // 404? $domain or PageNotFound::spawn(); } else { // try to create require_permission( 'edit-domain-all' ); if( is_action( 'add-domain' ) && isset( $_POST[ 'domain_name' ] ) ) { // trim and normalize to max length $domain_name = luser_input( $_POST[ 'domain_name' ], 64 ); // existing domain $existing = ( new DomainAPI() ) ->whereDomainName( $domain_name ) ->queryRow(); // go to the existing one if( $existing ) { http_redirect( $existing->getDomainPermalink() ); } // insert this new domain insert_row( Domain::T, [ new DBCol( 'domain_name', $domain_name, 's' ), new DBCol( 'domain_active', 1, 'd' ), new DBCol( 'domain_born', 'NOW()', '-' ), ] ); // go to the new domain http_redirect( Domain::permalink( $domain_name, true ) ); } } // spawn header Header::spawn( [ 'uid' => false, 'title-prefix' => __( "Domain" ), 'title' => $domain_name ? $domain_name : __( "Add" ), ] ); if( $domain ) { // spawn the domain template template( 'domain', [ 'domain' => $domain, 'plan' => $domain, ] ); } else { // form to create the domain template( 'domain-create' ); } // spawn the footer Footer::spawn(); diff --git a/www/mailbox.php b/www/mailbox.php index 7e90968..24803f9 100644 --- a/www/mailbox.php +++ b/www/mailbox.php @@ -1,147 +1,152 @@ . /* * This is the mailbox edit page */ // load framework require '../load.php'; // wanted domain and mailbox username list( $domain_name, $mailbox_username ) = url_parts( 2, 1 ); $domain = null; $mailbox = null; $plan = null; $mailbox_password = null; if( $mailbox_username ) { // retrieve the mailbox and its domain and its Plan $mailbox = ( new MailboxFullAPI() ) - ->joinPlan() + ->joinPlan( 'LEFT' ) ->whereDomainName( $domain_name ) ->whereMailboxUsername( $mailbox_username ) ->whereMailboxIsEditable() ->queryRow(); // 404? $mailbox or PageNotFound::spawn(); // the mailbox has the domain stuff $domain = $mailbox; // the mailbox has the Plan stuff $plan = $mailbox; } else { // retrieve just the domain and its Plan $domain = ( new DomainAPI() ) ->whereDomainName( $domain_name ) ->whereDomainIsEditable() - ->joinPlan() + ->joinPlan( 'LEFT' ) ->queryRow(); // 404? $domain or PageNotFound::spawn(); $plan = $domain; } // does the user want to create a Mailbox? if( !$mailbox ) { // count the actual number of Domain Mailbox(es) $mailbox_count = (int) ( new MailboxAPI() ) ->select( 'COUNT(*) count' ) ->whereDomain( $domain ) ->queryValue( 'count' ); // check if I can add another Mailbox if( $mailbox_count >= $plan->getPlanMailboxes() && !has_permission( 'edit-email-all' ) ) { BadRequest::spawn( __( "Your Plan does not allow this action" ), 401 ); } } /* * Change the mailbox password */ if( $mailbox && is_action( 'mailbox-password-reset' ) ) { $mailbox_password = $mailbox->updateMailboxPassword(); } /* * Create the mailbox */ if( !$mailbox && is_action( 'mailbox-create' ) && isset( $_POST[ 'mailbox_username' ] ) ) { $_POST[ 'mailbox_username' ] = luser_input( $_POST[ 'mailbox_username' ], 64 ); $mailbox = ( new MailboxFullAPI() ) ->select( [ 'domain.domain_ID', 'domain_name', 'mailbox_username', ] ) ->whereDomainName( $domain_name ) ->whereStr( 'mailbox_username', $_POST[ 'mailbox_username' ] ) ->queryRow(); if( !$mailbox ) { + // assign a damn temporary password + $mailbox_password = generate_password(); + $mailbox_password_safe = Mailbox::encryptPassword( $mailbox_password ); + insert_row( 'mailbox', [ new DBCol( 'mailbox_username', $_POST[ 'mailbox_username' ], 's' ), new DBCol( 'domain_ID', $domain->getDomainID(), 'd' ), + new DBCol( 'mailbox_password', $mailbox_password_safe, 's' ), ] ); } $mailbox = ( new MailboxFullAPI() ) ->select( [ 'domain.domain_ID', 'domain_name', 'mailbox_username', ] ) ->whereDomainName( $domain_name ) ->whereStr( 'mailbox_username', $_POST[ 'mailbox_username' ] ) ->queryRow(); if( $mailbox ) { http_redirect( $mailbox->getMailboxPermalink( true ) ); } } // spawn header Header::spawn( [ 'uid' => false, 'title-prefix' => __( "Mailbox" ), 'title' => $mailbox ? $mailbox->getMailboxAddress() : __( "create" ), 'breadcrumb' => [ new MenuEntry( null, $domain->getDomainPermalink(), $domain->getDomainName() ), ], ] ); // spawn the page content template( 'mailbox', [ 'mailbox' => $mailbox, 'mailbox_password' => $mailbox_password, 'domain' => $domain, ] ); // spawn the footer Footer::spawn();