diff --git a/README.md b/README.md index f85e178..f22a2e3 100644 --- a/README.md +++ b/README.md @@ -1,66 +1,69 @@ # Boz's Libre Hosting Panel This projects was started to create another _keep it simple and stupid_ libre web hosting control panel suitable for providers of everyday shared hosting services. ## Disclaimer Do not try to become a system administrator if you do not like responsibilities, if you do not want to understand your infrastructure, if you do not want to have information security paranoia, etc. ## Preamble An hosting panel is just the iceberg summit of a lot of technologies and protocols involved. Do not try to implement such project in production if you do not know what you are doing. You must gain confidence with the technologies involved. Papers: * RFC 5321 - Simple Mail Transfer Protocol * RFC 7208 - Sender Policy Framework (SPF) * RFC 6376 - DomainKeys Identified Mail (DKIM) Signatures * RFC 7489 - Domain-based Message Authentication, Reporting, and Conformance (DMARC) Software involved: * Debian GNU/Linux stable * MariaDB / MySQL * Postfix * Dovecot * PureFTPd * OpenDKIM * Apache HTTP server / nginx * PHP ## Features Let me say that I love listening to the whishlist of my costumers. Here are the most important features/TODOs: - administration of own mailboxes (thanks to Postfix and Dovecot over MariaDB) - [X] list - [X] password reset - [X] add - [X] IMAP/SMTP documentation - [ ] remove - [ ] view quota - administration of own mail aliases (thanks to Postfix and Dovecot over MariaDB) - [X] list - [X] change forward destination(s) - [X] add - [X] remove - administration of own FTP accounts (thanks to Pure-FTPd over MariaDB) - [X] list - [X] add - [X] remove - [ ] password reset - [ ] administration of own MariaDB databases - [ ] administration of User(s) - [X] password reset - [X] create a Domain for that User - [ ] change login - [ ] change e-mail +- [X] plans + - [X] limit number of mailboxes per domain + - [ ] limit number of mail forwardings ## License Copyright (C) 2018 [Valerio Bozzolan](https://boz.reyboz.it/) - _Boz Libre Hosting Panel_ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . diff --git a/include/class-Domain.php b/include/class-Domain.php index c782f21..18c5062 100644 --- a/include/class-Domain.php +++ b/include/class-Domain.php @@ -1,124 +1,131 @@ . +// load Plan trait +class_exists( 'Plan' ); + /** * Methods for a Domain class */ trait DomainTrait { + use PlanTrait; + /** * 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 * * @param boolean $absolute True for an absolute URL * @return string */ public function getDomainPermalink( $absolute = false ) { return Domain::permalink( $this->get( 'domain_name' ), $absolute ); } /** * 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 factoryMailforwardfrom() { return ( new MailforwardfromAPI() )->whereDomain( $this ); } /** * Factory FTP users from this domain * * @return FTPAPI */ public function factoryFTP() { return ( new FTPAPI() )->whereDomain( $this ); } /** * Normalize a Domain object after being retrieved from database */ protected function normalizeDomain() { $this->integers( 'domain_ID' ); $this->booleans( 'domain_active' ); $this->dates( 'domain_born', 'domain_expiration' ); + + $this->normalizePlan(); } } /** * Describe the 'domain' table */ class Domain extends Queried { use DomainTrait; /** * Table name */ const T = 'domain'; const UID = 'domain_name'; /** * Constructor */ public function __construct() { $this->normalizeDomain(); } /** * Get the domain permalink * * @param string $domain_name Domain name * @param boolean $absolute True for an absolute URL */ public static function permalink( $domain_name = null, $absolute = false ) { $url = 'domain.php'; if( $domain_name ) { $url .= _ . $domain_name; } return site_page( $url, $absolute ); } } diff --git a/include/class-DomainAPI.php b/include/class-DomainAPI.php index 3e9c26a..f4dd519 100644 --- a/include/class-DomainAPI.php +++ b/include/class-DomainAPI.php @@ -1,155 +1,160 @@ . +// load dependent traits +class_exists( 'PlanAPI' ); + /** * Methods related to a Domain class */ trait DomainAPITrait { + use PlanAPITrait; + /** * Where the domains are editable by me * * @return self */ public function whereDomainIsEditable() { if( ! has_permission( 'edit-domain-all' ) ) { $this->whereDomainUser(); } return $this; } /** * Where the Domain is Active (or not) * * @param boolean $active If you want the active, or the inactive * @return self */ public function whereDomainIsActive( $active = true ) { return $this->wheerInt( 'domain_active', $active ); } /** * Limit to a certain user (or yourself) * * @param $user_ID int * @return self */ public function whereDomainUser( $user_ID = false ) { if( $user_ID === false ) { $user_ID = get_user( 'user_ID' ); } $this->joinDomainUser(); return $this->whereInt( 'domain_user.user_ID', $user_ID ); } /** * Limit to a certian domain name * * @param $domain_name string * @return self */ public function whereDomainName( $domain_name ) { return $this->whereStr( 'domain_name', $domain_name ); } /** * Constructor from a domain ID * * @param $domain_ID int * @return self */ public function whereDomainID( $domain_ID ) { return $this->whereInt( static::DOMAIN_ID, $domain_ID ); } /** * Constructor from a Domain object * * @param $domain object * @return self */ public function whereDomain( $domain ) { return $this->whereDomainID( $domain->getDomainID() ); } /** * Order by the Domain name * * @param string $direction DESC|ASC * @return self */ public function orderByDomainName( $direction = null ) { return $this->orderBy( 'domain_name', $direction ); } /** * Join domain and users (once) * * @return self */ public function joinDomainUser() { if( empty( $this->joinedDomainUser ) ) { $this->from( 'domain_user' ); $this->equals( 'domain_user.domain_ID', 'domain.domain_ID' ); $this->joinedDomainUser = true; } return $this; } /** * Join whatever table with the domain table * * @return self */ public function joinDomain() { return $this->joinOn( 'INNER', 'domain', static::DOMAIN_ID, 'domain.domain_ID' ); } } /** * Domain API */ class DomainAPI extends Query { use DomainAPITrait; /** - * @TODO: what is this shit? Is this used? + * Univoque Domain ID column name */ - const UID = 'name'; + const DOMAIN_ID = 'domain.domain_ID'; /** - * Domain ID column name + * Univoque Plan ID column name */ - const DOMAIN_ID = 'domain.domain_ID'; + const PLAN_ID = 'domain.plan_ID'; /** * Constructor * * @param object $db Database (or NULL for the current one) */ public function __construct( $db = null ) { // set database and class name parent::__construct( $db, 'Domain' ); // set database table $this->from( Domain::T ); } } diff --git a/include/class-FTP.php b/include/class-FTP.php index 1f38ee5..a87eadd 100644 --- a/include/class-FTP.php +++ b/include/class-FTP.php @@ -1,83 +1,92 @@ . +// load dependent traits +class_exists( 'Domain' ); + /** * An FTP user */ -class FTP extends Domain { +class FTP extends Queried { + + use DomainAPITrait; + /** + * Table name + */ const T = 'ftp'; /** * Constructor * * Normalize the object obtained from the database */ public function __construct() { + $this->normalizeDomain(); $this->booleans( 'ftp_active' ); $this->integers( 'ftp_ulbandwidth', 'ftp_dlbandwidth', 'ftp_quotasize', 'ftp_quotafiles' ); } /** * Get the FTP login username * * @return string E-mail */ public function getFTPLogin() { return $this->get( 'ftp_login' ); } /** * Check if this FTP account is active * * @return bool */ public function isFTPActive() { return $this->get( 'ftp_active' ); } /** * Get the mailbox permalink * * @return string */ public function getFTPPermalink( $absolute = false ) { return FTP::permalink( $this->getDomainName(), $this->getFTPLogin() ); } /** * Get the FTP permalink from domain name and FTP login * * @param string $domain Domain name * @param string $login FTP user login * @return string */ public static function permalink( $domain, $login = null ) { $url = sprintf( '%s/%s', ROOT . '/ftp.php', $domain ); if( $login ) { $url .= "/$login"; } return $url; } } diff --git a/include/class-Mailbox.php b/include/class-Mailbox.php index 75b5d37..dae1da5 100644 --- a/include/class-Mailbox.php +++ b/include/class-Mailbox.php @@ -1,116 +1,136 @@ . // load dependent traits class_exists( 'Domain' ); -/** - * A mailbox - */ -class Mailbox extends Queried { +trait MailboxTrait { use DomainTrait; - const T = 'mailbox'; - - public function __construct() { - $this->normalizeDomain(); - $this->booleans( 'mailbox_receive' ); - } - /** * Get the mailbox username * * @return string */ public function getMailboxUsername() { return $this->get( 'mailbox_username' ); } /** * Get the mailbox address * * @return string E-mail */ public function getMailboxAddress() { return sprintf( "%s@%s", $this->get( 'mailbox_username' ), $this->get( 'domain_name' ) ); } /** * Get the mailbox permalink * * @return string */ public function getMailboxPermalink( $absolute = false ) { return Mailbox::permalink( $this->get( 'domain_name' ), $this->get( 'mailbox_username' ) ); } /** * Update this mailbox password * * @param string $password * @return string */ public function updateMailboxPassword( $password = null ) { if( ! $password ) { $password = generate_password(); } $enc_password = Mailbox::encryptPassword( $password ); // update ( new MailboxAPI() ) ->whereMailbox( $this ) ->update( [ new DBCol( 'mailbox_password', $enc_password, 's' ), ] ); return $password; } + /** + * Normalize a Mailbox after being fetched from database + */ + protected function normalizeMailbox() { + $this->normalizeDomain(); + $this->booleans( 'mailbox_receive' ); + } + +} + +/** + * A mailbox + */ +class Mailbox extends Queried { + + use MailboxTrait; + + /** + * Database table + */ + const T = 'mailbox'; + + /** + * Constructor + */ + public function __construct() { + $this->normalizeMailbox(); + } + /** * Get the mailbox permalink * * @param $domain string * @param $mailbox string * @param $absolute boolean * @return string */ public static function permalink( $domain, $mailbox = null, $absolute = false ) { $part = site_page( 'mailbox.php', $absolute ) . _ . $domain; if( $mailbox ) { $part .= _ . $mailbox; } return $part; } /** * Encrypt a password * * TODO: do not hardcode to my Dovecot configuration */ public static function encryptPassword( $password ) { $salt = bin2hex( openssl_random_pseudo_bytes( 3 ) ); return '{SHA512-CRYPT}' . crypt( $password, "$6$$salt" ); } + } diff --git a/include/class-MailboxAPI.php b/include/class-MailboxAPI.php index d50d470..8073fd4 100644 --- a/include/class-MailboxAPI.php +++ b/include/class-MailboxAPI.php @@ -1,85 +1,118 @@ . +// load dependend traits +class_exists( 'DomainAPI' ); + /** - * Mailbox API + * Methods for a MailboxAPI class */ -class MailboxAPI extends DomainAPI { +trait MailboxAPITrait { - public function __construct() { - Query::__construct(); - $this->from( Mailbox::T ); - $this->defaultClass( 'Mailbox' ); - } + use DomainAPITrait; /** * Limit to a specific mailbox * * @param object $mailbox Mailbox * @return self */ public function whereMailbox( $mailbox ) { return $this->whereDomain( $mailbox ) ->whereMaiboxUsername( $mailbox->getMailboxUsername() ); } /** * 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 ); + 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->wheerInt( 'mailbox_active', $active ); } /** * 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 + */ + const PLAN_ID = 'domain.plan_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/include/class-Plan.php b/include/class-Plan.php new file mode 100644 index 0000000..7e558b9 --- /dev/null +++ b/include/class-Plan.php @@ -0,0 +1,129 @@ +. + +// load Plan trait +class_exists( 'Plan' ); + +/** + * Methods for a Plan class + */ +trait PlanTrait { + + /** + * Get plan ID + * + * @return int + */ + public function getPlanID() { + return $this->get( 'plan_ID' ); + } + + /* + * Get plan name + * + * @return string + */ + public function getPlanName() { + return $this->get( 'plan_name' ); + } + + /** + * Get the number of FTP users of this Plan + * + * @return int|null + */ + public function getPlanFTPUsers() { + return $this->get( 'plan_ftpusers' ); + } + + /** + * Get the number of Databases of this Plan + * + * @return int|null + */ + public function getPlanDatabases() { + return $this->get( 'plan_databases' ); + } + + /** + * Get the number of Mailboxes of this Plan + * + * @return int|null + */ + public function getPlanMailboxes() { + return $this->get( 'plan_mailboxes' ); + } + + /** + * Get the plan edit URl + * + * @param boolean $absolute True for an absolute URL + * @return string + */ + public function getPlanPermalink( $absolute = false ) { + return Plan::permalink( $this->get( 'plan_name' ), $absolute ); + } + + /** + * Normalize a Plan object after being retrieved from database + */ + protected function normalizePlan() { + $this->integers( + 'plan_ID', + 'plan_ftpusers', + 'plan_databases', + 'plan_mailboxes', + 'plan_mailforwards' + ); + } + +} + +/** + * Describe the 'plan' table + */ +class Plan extends Queried { + + use PlanTrait; + + /** + * Table name + */ + const T = 'plan'; + + /** + * Constructor + */ + public function __construct() { + $this->normalizePlan(); + } + + /** + * Get the plan permalink + * + * @param string $plan_name Plan name + * @param boolean $absolute True for an absolute URL + */ + public static function permalink( $plan_name = null, $absolute = false ) { + $url = 'plan.php'; + if( $plan_name ) { + $url .= _ . $plan_name; + } + return site_page( $url, $absolute ); + } + +} diff --git a/include/class-PlanAPI.php b/include/class-PlanAPI.php new file mode 100644 index 0000000..ab578aa --- /dev/null +++ b/include/class-PlanAPI.php @@ -0,0 +1,100 @@ +. + +/** + * 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 + * + * @return self + */ + public function joinPlan() { + return $this->joinOn( 'INNER', '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/load-post.php b/load-post.php index fd22247..64972b6 100644 --- a/load-post.php +++ b/load-post.php @@ -1,102 +1,103 @@ . // include path define_default( 'INCLUDE_PATH', ABSPATH . __ . 'include' ); // template path define_default( 'TEMPLATE_PATH', ABSPATH . __ . 'template' ); // autoload classes from the /include directory spl_autoload_register( function( $name ) { // TODO: autoload classes and create DomainTrait and use in Mailbox $path = INCLUDE_PATH . __ . "class-$name.php"; if( is_file( $path ) ) { require $path; } } ); // override default user class define( 'SESSIONUSER_CLASS', 'User' ); // load common functions require INCLUDE_PATH . __ . 'functions.php'; // jquery URL // provided by the libjs-jquery package as default define_default( 'JQUERY_URL', '/javascript/jquery/jquery.min.js' ); // Bootstrap CSS/JavaScript files without trailing slash // provided by the libjs-bootstrap package as default define_default( 'BOOTSTRAP_DIR_URL', '/javascript/bootstrap' ); // path to the Net SMTP class // provided by the php-net-smtp package as default define_default( 'NET_SMTP', '/usr/share/php/Net/SMTP.php' ); // register JavaScript/CSS files register_js( 'jquery', JQUERY_URL ); register_js( 'bootstrap', BOOTSTRAP_DIR_URL . '/js/bootstrap.min.js' ); register_css( 'bootstrap', BOOTSTRAP_DIR_URL . '/css/bootstrap.min.css' ); register_css( 'custom-css', ROOT . '/content/style.css' ); // GNU Gettext i18n define( 'GETTEXT_DOMAIN', 'reyboz-hosting-panel' ); define( 'GETTEXT_DIRECTORY', 'l10n' ); define( 'GETTEXT_DEFAULT_ENCODE', CHARSET ); // UTF-8 // common strings define_default( 'SITE_NAME', "Boz Libre Hosting Panel" ); define_default( 'CONTACT_EMAIL', 'support@' . DOMAIN ); define_default( 'REPO_URL', 'https://github.com/valerio-bozzolan/boz-libre-hosting-panel' ); // limit session duration to 5 minutes (60s * 100m) define_default( 'SESSION_DURATION', 6000 ); /** * Mailbox base path * * Used by CLI scripts to calculate the current quotas. * * The mailboxes should have paths like: * MAILBOX_BASE_PATH/domain_name/user_name/ */ define_default( 'MAILBOX_BASE_PATH', '/home/vmail' ); // register web pages add_menu_entries( [ new MenuEntry( 'index', '/', __( "Dashboard" ), null, 'backend' ), new MenuEntry( 'login', 'login.php', __( "Login" ) ), new MenuEntry( 'profile', 'profile.php', __( "Profile" ) ), new MenuEntry( 'logout', 'logout.php', __( "Logout" ), null, 'read' ), new MenuEntry( 'user-list', 'user-list.php', __( "Users" ), null, 'edit-user-all' ), new MenuEntry( 'password-reset', 'password-reset.php', __( "Password reset" ) ), ] ); // permissions of a normal user register_permissions( 'user', [ 'read', 'backend', ] ); // permissions of an admin inherit_permissions( 'admin', 'user', [ 'edit-user-all', 'edit-email-all', 'edit-domain-all', + 'edit-plan-all', 'edit-ftp-all', ] ); diff --git a/template/domain.php b/template/domain.php index 412e8d9..88b4348 100644 --- a/template/domain.php +++ b/template/domain.php @@ -1,38 +1,45 @@ . /* * This is the template for the website domain dashboard page * * Called from: * domain.php * * Available variables: - * $domain object + * $domain object Domain + * $plan object Plan */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; +// pass the same arguments to the sub-templates +$args = [ + 'domain' => $domain, + 'plan' => $plan, +]; + // spawn the mailboxes list -template( 'mailboxes', [ 'domain' => $domain ] ); +template( 'mailboxes', $args ); // spawn the mail forwardings list -template( 'mailforwards', [ 'domain' => $domain ] ); +template( 'mailforwards', $args ); // spawn the ftp list -template( 'ftp-users', [ 'domain' => $domain ] ); +template( 'ftp-users', $args ); diff --git a/template/mailboxes.php b/template/mailboxes.php index f9392e8..f28b5d9 100644 --- a/template/mailboxes.php +++ b/template/mailboxes.php @@ -1,63 +1,74 @@ . /* * 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(); ?> - -

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

getDomainName() ), __( "Create" ) ) ?>

- - + + +

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

getPlanName(), + $plan->getPlanMailboxes(), + __( "Mailboxes" ) +) ) ?>

+ +getPlanMailboxes() > $count || has_permission( 'edit-email-all' ) ): ?> +

getDomainName() ), __( "Create" ) ) ?>

+ + + diff --git a/www/domain.php b/www/domain.php index 196f5cb..adbcf0f 100644 --- a/www/domain.php +++ b/www/domain.php @@ -1,89 +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() ->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 5080bd0..7e90968 100644 --- a/www/mailbox.php +++ b/www/mailbox.php @@ -1,133 +1,147 @@ . /* * 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 + + // retrieve the mailbox and its domain and its Plan $mailbox = ( new MailboxFullAPI() ) - ->select( [ - 'domain.domain_ID', - 'domain_name', - 'domain_active', - 'mailbox_username', - ] ) + ->joinPlan() ->whereDomainName( $domain_name ) - ->whereStr( 'mailbox_username', $mailbox_username ) + ->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 + + // retrieve just the domain and its Plan $domain = ( new DomainAPI() ) - ->select( [ - 'domain.domain_ID', - 'domain_name', - 'domain_active', - ] ) ->whereDomainName( $domain_name ) ->whereDomainIsEditable() + ->joinPlan() ->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' ] ) ) { - // TODO: check max. creation number in single domain props - require_permission( 'edit-email-all' ); - $_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 ) { insert_row( 'mailbox', [ new DBCol( 'mailbox_username', $_POST[ 'mailbox_username' ], 's' ), new DBCol( 'domain_ID', $domain->getDomainID(), 'd' ), ] ); } $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();