diff --git a/include/class-Domain.php b/include/class-Domain.php index cf5df76..5708b4e 100644 --- a/include/class-Domain.php +++ b/include/class-Domain.php @@ -1,86 +1,100 @@ . /** * 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 * + * @param boolean $absolute True for an absolute URL * @return string */ public function getDomainPermalink( $absolute = false ) { - return ROOT . '/domain.php/' . $this->get( 'domain_name' ); + 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 ); } + /** + * 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-Mailbox.php b/include/class-Mailbox.php index aa00a65..6c77b9d 100644 --- a/include/class-Mailbox.php +++ b/include/class-Mailbox.php @@ -1,97 +1,99 @@ . /** * A mailbox */ class Mailbox extends Domain { const T = 'mailbox'; public function __construct() { $this->booleans( 'mailbox_receive' ); } /** * 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 $password string * @return string */ public function updateMailboxPassword( $password = null ) { if( ! $password ) { $password = generate_password(); } $enc_password = Mailbox::encryptPassword( $password ); query_update( 'mailbox', [ new DBCol( 'mailbox_password', $enc_password, 's' ), ], sprintf( "domain_ID = %d AND mailbox_username = '%s'", $this->getDomainID(), esc_sql( $this->get( 'mailbox_username' ) ) ) ); return $password; } /** * Get the mailbox permalink * + * @param $domain string + * @param $mailbox string + * @param $absolute boolean * @return string */ - public static function permalink( $domain, $mailbox ) { - return sprintf( - '%s/%s/%s', - ROOT . '/mailbox.php', - $domain, - $mailbox - ); + 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/template/domain.php b/template/domain-create.php similarity index 69% copy from template/domain.php copy to template/domain-create.php index f6e3092..9d1a80f 100644 --- a/template/domain.php +++ b/template/domain-create.php @@ -1,35 +1,33 @@ . /* - * This is the template for the website domain dashboard page + * This is the template for the create domain form * * Called from: * domain.php */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; -// spawn the mailboxes list -template( 'mailboxes', [ 'domain' => $domain ] ); +?> -// spawn the mail forwardings list -template( 'mailforwards', [ 'domain' => $domain ] ); - -// spawn the ftp list -template( 'ftp-users', [ 'domain' => $domain ] ); +
diff --git a/template/domain.php b/template/domain.php index f6e3092..412e8d9 100644 --- a/template/domain.php +++ b/template/domain.php @@ -1,35 +1,38 @@ . /* * This is the template for the website domain dashboard page * * Called from: * domain.php + * + * Available variables: + * $domain object */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; // spawn the mailboxes list template( 'mailboxes', [ 'domain' => $domain ] ); // spawn the mail forwardings list template( 'mailforwards', [ 'domain' => $domain ] ); // spawn the ftp list template( 'ftp-users', [ 'domain' => $domain ] ); diff --git a/template/ftp-users.php b/template/ftp-users.php index e5df027..d3d88be 100644 --- a/template/ftp-users.php +++ b/template/ftp-users.php @@ -1,71 +1,70 @@ . /* * This is the template for the FTP user list * * Called from: * domain.php * * Available variables: * $domain Domain object */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; // domain FTP users $ftps = $domain->factoryFTP() ->select( [ 'ftp_login', ] ) ->queryGenerator(); ?> - valid() ): ?> - - + + valid() ): ?>getDomainName(),
$ftp->getFTPLogin()
),
$ftp->getFTPLogin()
) ?>
getDomainName() ), __( "Create" ) ) ?>
diff --git a/template/mailbox.php b/template/mailbox.php index 8bbd9a8..20798eb 100644 --- a/template/mailbox.php +++ b/template/mailbox.php @@ -1,48 +1,67 @@ . /* * This is the template for a mailbox * * Called from: * mailbox.php + * + * Available variables: + * $mailbox object + * $domain object + * $mailbox_password string|null */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; ?> - - + + - - + + + + + + + diff --git a/template/mailboxes.php b/template/mailboxes.php index 753c945..6030226 100644 --- a/template/mailboxes.php +++ b/template/mailboxes.php @@ -1,58 +1,63 @@ . /* * 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', + 'domain_name', + 'mailbox_username', + 'mailbox_receive', ] ) ->queryGenerator(); ?> - valid() ): ?> - + - + + valid() ): ?>getMailboxPermalink(),
$mailbox->getMailboxAddress()
) ?>
getDomainName() ), __( "Create" ) ) ?>
+ diff --git a/template/mailforward.php b/template/mailforward.php index ff537e5..1c139d3 100644 --- a/template/mailforward.php +++ b/template/mailforward.php @@ -1,62 +1,62 @@ . /* * This is the template for a single e-mail forwarding * * Called from: * mailforward.php * * Available variables: * $domain Domain object * $mailforwardfrom Mailforward object */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; -?> +?> $domain, 'mailforwardfrom' => $mailforwardfrom, ] ) ?> diff --git a/template/mailforwards.php b/template/mailforwards.php index b65551f..50596c5 100644 --- a/template/mailforwards.php +++ b/template/mailforwards.php @@ -1,67 +1,66 @@ . /* * 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 $mailforwardfroms = $domain->factoryMailforwardfrom() ->select( [ 'mailforwardfrom.mailforwardfrom_ID', 'mailforwardfrom_username', 'domain_name', ] ) ->queryGenerator(); ?> - valid() ): ?> - - + + valid() ): ?>getMailforwardfromPermalink(),
$mailforwardfrom->getMailforwardfromAddress()
) ?>
getDomainName() ), __( "Create" ) ) ?>
diff --git a/www/domain.php b/www/domain.php index 900b298..0f2f4bf 100644 --- a/www/domain.php +++ b/www/domain.php @@ -1,49 +1,77 @@ . /* * This is the domain edit page */ // load framework require '../load.php'; // wanted domain -list( $domain_name ) = url_parts( 1 ); +list( $domain_name ) = url_parts( 1, 0 ); -// retrieve domain -$domain = ( new DomainAPI() ) - ->whereDomainName( $domain_name ) - ->whereDomainIsEditable() - ->queryRow(); +$domain = null; -// 404? -$domain or PageNotFound::spawn(); +if( $domain_name ) { + // retrieve domain + $domain = ( new DomainAPI() ) + ->whereDomainName( $domain_name ) + ->whereDomainIsEditable() + ->queryRow(); + + // 404? + $domain or PageNotFound::spawn(); +} else { + // try to create + + require_permission( 'edit-domain-all' ); + + if( is_action( 'add-domain' ) && isset( $_POST[ 'domain_name' ] ) ) { + $domain_name = luser_input( $_POST[ 'domain_name' ], 64 ); + + // @TODO: check for duplicates + + insert_row( Domain::T, [ + new DBCol( 'domain_name', $domain_name, 's' ), + new DBCol( 'domain_active', 1, 'd' ), + new DBCol( 'domain_born', 'NOW()', '-' ), + ] ); + + // POST -> redirect -> GET + http_redirect( Domain::permalink( $domain_name, true ) ); + } +} // spawn header Header::spawn( [ 'title-prefix' => __( "Domain" ), - 'title' => $domain_name, + 'title' => $domain_name ? $domain_name : __( "Add" ), ] ); -// spawn the domain template -template( 'domain', [ - 'domain' => $domain, -] ); +if( $domain ) { + // spawn the domain template + template( 'domain', [ + 'domain' => $domain, + ] ); +} else { + // form to create the domain + template( 'domain-create' ); +} // spawn the footer Footer::spawn(); diff --git a/www/index.php b/www/index.php index 3c3a51d..1b052bb 100644 --- a/www/index.php +++ b/www/index.php @@ -1,76 +1,80 @@ . /* * This is the homepage of your hosting panel */ // load framework require '../load.php'; // require read permissions require_permission( 'read' ); // spawn header Header::spawn( [ 'breadcrumb' => false, ] ); // user domains $domains = ( new DomainAPI() ) ->select( [ 'domain.domain_ID', 'domain_name', 'domain_active', ] ) ->whereDomainIsEditable() ->orderBy( 'domain_name' ) ->queryGenerator(); ?> valid() ): ?>
domain_active ): ?>
getDomainPermalink(),
$domain->domain_name
) ?>
domain_name ) ?>