diff --git a/include/class-MailboxQuotaAPI.php b/include/class-MailboxQuotaAPI.php
index e04e7f9..3d5634e 100644
--- a/include/class-MailboxQuotaAPI.php
+++ b/include/class-MailboxQuotaAPI.php
@@ -1,54 +1,89 @@
.
// load dependend traits
class_exists( 'MailboxAPI' );
/**
* Methods for a MailboxQuotaAPI class
*/
trait MailboxQuotaAPITrait {
use MailboxAPITrait;
+ /**
+ * Select the MAX Mailbox quota date
+ *
+ * @return self
+ */
+ public function selectMaxMailboxQuotaDate() {
+ return $this->select( 'MAX( mailboxquota_date ) AS max_mailboxquota_date' )
+ ->groupBy( 'mailboxquota_date' );
+ }
+
+ /**
+ * Assure that this is only the more updated Mailbox quota
+ *
+ * @return self
+ */
+ public function whereMailboxQuotaIsLast() {
+
+ // subquery with a maximum constraint
+ $max = ( new MailboxQuotaAPI( null, false ) )
+ ->fromCustom( DB::instance()->getTable( 'mailboxquota', 'mailboxquota_sub' ) )
+ ->equals( 'mailboxquota.mailbox_ID', 'mailboxquota_sub.mailbox_ID' )
+ ->selectMaxMailboxQuotaDate()
+ ->getQuery();
+
+ return $this->where( sprintf( 'mailboxquota_date = (%s)', $max ) );
+ }
}
/**
* MailboxQuota API
*/
class MailboxQuotaAPI extends Query {
use MailboxQuotaAPITrait;
/**
* Univoque column name to the Mailbox ID
*/
protected $MAILBOX_ID = 'mailboxquota.mailbox_ID';
/**
* Constructor
+ *
+ * @param object $db Database
+ * @param mixed $from Set to false to avoid to use the default FROM
*/
- public function __construct( $db = null ) {
+ public function __construct( $db = null, $from = true ) {
// set database and class name
parent::__construct( $db, MailboxQuota::class );
- // set database table
- $this->from( MailboxQuota::T );
+ /**
+ * Set database table (sometime the standard alias it's not useful)
+ *
+ * See MailboxQuotaAPI class.
+ */
+ if( $from ) {
+ $this->from( MailboxQuota::T );
+ }
}
}
diff --git a/template/mailbox-stats.php b/template/mailbox-stats.php
new file mode 100644
index 0000000..2177183
--- /dev/null
+++ b/template/mailbox-stats.php
@@ -0,0 +1,57 @@
+.
+
+/*
+ * This is the template for some mailbox stats
+ *
+ * Called from:
+ * template/mailbox.php
+ *
+ * Available variables:
+ * $mailbox object
+ * $plan object
+ */
+
+// avoid to be load directly
+defined( 'BOZ_PHP' ) or die;
+
+// calculate the quota
+$quota = ( new MailboxQuotaAPI() )
+ ->select( 'mailboxquota_bytes' )
+ ->whereMailbox( $mailbox )
+ ->whereMailboxQuotaIsLast()
+ ->queryRow();
+?>
+
+
= esc_html( __( "Stats" ) ) ?>
+
+
+
+
+
+ = esc_html( __( "Size" ) ) ?> |
+ = $quota->getMailboxQuotaHumanSize() ?> |
+
+
+
+
+
+
+
+ = esc_html( __( "No stats available" ) ) ?>
+
+
diff --git a/template/mailbox.php b/template/mailbox.php
index 91c8973..86ab756 100644
--- a/template/mailbox.php
+++ b/template/mailbox.php
@@ -1,71 +1,81 @@
.
/*
* This is the template for a mailbox
*
* Called from:
- * mailbox.php
+ * www/mailbox.php
*
* Available variables:
* $mailbox object
* $domain object
+ * $plan object
* $mailbox_password string|null
*/
// unuseful when load directly
defined( 'BOZ_PHP' ) or die;
?>
+
$mailbox,
] ) ?>
+
+
+
+ $mailbox,
+ 'plan' => $plan,
+ ] ) ?>
+
= __( "Resources" ) ?>
-
= __( "How to setup your IMAP/SMTP client" ) ?>
= __( "Actions" ) ?>
diff --git a/www/mailbox.php b/www/mailbox.php
index 24803f9..203c7c4 100644
--- a/www/mailbox.php
+++ b/www/mailbox.php
@@ -1,152 +1,153 @@
.
/*
* 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( '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( '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,
+ 'plan' => $plan,
] );
// spawn the footer
Footer::spawn();