diff --git a/include/class-Domain.php b/include/class-Domain.php index e6ea78e..cf5df76 100644 --- a/include/class-Domain.php +++ b/include/class-Domain.php @@ -1,77 +1,86 @@ . /** * 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 factoryMailforwardfrom() { return ( new MailforwardfromAPI() )->whereDomain( $this ); } + /** + * Factory FTP users from this domain + * + * @return FTPAPI + */ + public function factoryFTP() { + return ( new FTPAPI() )->whereDomain( $this ); + } + } diff --git a/include/class-FTP.php b/include/class-FTP.php new file mode 100644 index 0000000..97e9d7c --- /dev/null +++ b/include/class-FTP.php @@ -0,0 +1,84 @@ +. + +/** + * An FTP user + */ +class FTP extends Domain { + + const T = 'ftp'; + + /** + * Constructor + * + * Normalize the object obtained from the database + */ + public function __construct() { + $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 ) { + return sprintf( + '%s/%s/%s', + ROOT . '/ftp.php', + $domain, + $login + ); + } + +} diff --git a/include/class-FTPAPI.php b/include/class-FTPAPI.php new file mode 100644 index 0000000..cd47a1f --- /dev/null +++ b/include/class-FTPAPI.php @@ -0,0 +1,65 @@ +. + +/** + * FTP users API + */ +class FTPAPI extends DomainAPI { + + public function __construct() { + Query::__construct(); + $this->from( FTP::T ); + $this->defaultClass( 'FTP' ); + } + + /** + * Join FTP and domain tables (once) + * + * @return self + */ + public function joinFTPDomain() { + if( empty( $this->joinedFTPDomain ) ) { + $this->joinedFTPDomain = true; + + $this->from( 'domain' ); + $this->equals( 'domain.domain_ID', 'ftp.domain_ID' ); + } + return $this; + } + + /** + * Filter to a certain FTP login + * + * @param string $login + * @return self + */ + public function whereFTPLogin( $login ) { + return $this->whereStr( 'ftp_login', $login ); + } + + /** + * Filter to a certain domain ID + * + * @param int $id int + * @return self + * @override + */ + public function whereDomainID( $id ) { + return $this->whereInt( 'ftp.domain_ID', $id ); + } + +} diff --git a/load-post.php b/load-post.php index 34b3cdf..999a756 100644 --- a/load-post.php +++ b/load-post.php @@ -1,84 +1,85 @@ . // 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 ) { $path = INCLUDE_PATH . __ . "class-$name.php"; if( is_file( $path ) ) { require $path; } } ); // 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 ); // register web pages add_menu_entries( [ new MenuEntry( 'index', '/', __( "Dashboard" ) ), new MenuEntry( 'login', 'login.php', __( "Login" ) ), new MenuEntry( 'profile', 'profile.php', __( "Profile" ) ), new MenuEntry( 'logout', 'logout.php', __( "Logout" ) ), new MenuEntry( 'password-reset', 'password-reset.php', __( "Password reset" ) ), ] ); // permissions register_permissions( 'user', [ 'read', 'backend', ] ); inherit_permissions( 'admin', 'user', [ 'edit-user-all', 'edit-email-all', 'edit-domain-all', + 'edit-ftp-all', ] ); diff --git a/template/domain.php b/template/domain.php index 95259f3..f6e3092 100644 --- a/template/domain.php +++ b/template/domain.php @@ -1,32 +1,35 @@ . /* * This is the template for the website domain dashboard page * * Called from: * domain.php */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; // spawn the mailboxes list -template( 'mailboxes', [ 'domain' => $domain ] ); +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/ftp-access.php similarity index 61% copy from template/domain.php copy to template/ftp-access.php index 95259f3..42af6fc 100644 --- a/template/domain.php +++ b/template/ftp-access.php @@ -1,32 +1,41 @@ . /* - * This is the template for the website domain dashboard page + * This is the template for FTP instructions * * Called from: - * domain.php + * ftp.php + * + * Available variables: + * $domain Domain object + * $ftp FTP 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 ] ); +
+ ftp://getFTPLogin() ); + echo '@'; + _esc_html( $domain->getDomainName() ); + ?> +
diff --git a/template/domain.php b/template/ftp-description.php similarity index 72% copy from template/domain.php copy to template/ftp-description.php index 95259f3..82ce878 100644 --- a/template/domain.php +++ b/template/ftp-description.php @@ -1,32 +1,29 @@ . /* - * This is the template for the website domain dashboard page + * This is the template for the description of an FTP account * * Called from: - * domain.php + * template/ftp-list.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 ] ); +

diff --git a/template/ftp-users.php b/template/ftp-users.php new file mode 100644 index 0000000..e5df027 --- /dev/null +++ b/template/ftp-users.php @@ -0,0 +1,71 @@ +. + +/* + * 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() ): ?> + + + + + +

+ + + +

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

+ + + diff --git a/template/ftp.php b/template/ftp.php new file mode 100644 index 0000000..0f4feed --- /dev/null +++ b/template/ftp.php @@ -0,0 +1,57 @@ +. + +/* + * This is the template for a single FTP account + * + * Called from: + * ftp.php + * + * Available variables: + * $domain Domain object + * $ftp FTP object + */ + +// unuseful when load directly +defined( 'BOZ_PHP' ) or die; +?> + + + + $domain, + 'ftp' => $ftp, + ] ); + } ?> + +
+

+ + getFTPLogin() ) ?> /> + +
+ + + + + + + + +

+
diff --git a/www/ftp.php b/www/ftp.php new file mode 100644 index 0000000..b93724d --- /dev/null +++ b/www/ftp.php @@ -0,0 +1,154 @@ +. + +/* + * This is the single FTP account creation/edit page + */ + +// load framework +require '../load.php'; + +// wanted informations +$domain = null; +$ftp = null; + +// URL paramenters (maximum both domain and FTP login, minimum just domain) +list( $domain_name, $ftp_login ) = url_parts( 2, 1 ); + +// eventually retrieve mailforward from database +if( $ftp_login ) { + $ftp = ( new FTPAPI() ) + ->select( [ + 'domain.domain_ID', + 'domain_name', + 'ftp_login', + ] ) + ->joinFTPDomain() + ->whereDomainName( $domain_name ) + ->whereFTPLogin( $ftp_login ) + ->whereDomainIsEditable() + ->queryRow(); + + // 404 + $ftp or PageNotFound::spawn(); + + // recycle the mailforward object that has domain informations + $domain = $ftp; +} + +// 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(); +} + +if( ! $ftp ) { + // to create an FTP user, must edit all FTP users + require_permission( 'edit-ftp-all' ); +} + +// save destination action +if( is_action( 'ftp-save' ) ) { + + // save source only during creation + if( ! $ftp ) { + + // sanitize + if( ! isset( $_POST[ 'ftp_login' ] ) ) { + BadRequest::spawn( __( "missing parameter" ) ); + } + $username = luser_input( $_POST[ 'ftp_login' ], 128 ); + if( ! validate_mailbox_username( $username ) ) { + BadRequest::spawn( __( "invalid mailbox name" ) ); + } + + // check existence + $ftp_exists = ( new MailforwardfromAPI ) + ->select( 1 ) + ->whereDomain( $domain ) + ->whereFTPLogin( $source ) + ->queryRow(); + + // die if exists + if( $ftp_exists ) { + BadRequest::spawn( __( "e-mail forwarding already existing" ) ); + } + + // insert as new row + insert_row( 'ftp', [ + new DBCol( 'domain_ID', $domain->getDomainID(), 'd' ), + new DBCol( 'ftp_login', $username, 's' ), + ] ); + + // POST/redirect/GET + http_redirect( Mailforwardfrom::permalink( + $domain->getDomainName(), + $username, + true + ), 303 ); + } +} + +// delete action +if( $ftp ) { + + // action fired when deleting a whole mailforward + if( is_action( 'ftp-delete' ) ) { + + // drop th + query( sprintf( + "DELETE FROM %s WHERE domain_ID = %d AND ftp_login = '%s'", + T( 'ftp' ), + $ftp->getDomainID(), + $ftp->getFTPLogin() + ) ); + + // POST/redirect/GET + http_redirect( $domain->getDomainPermalink( true ), 303 ); + + } + +} + +// spawn header +Header::spawn( [ + 'title-prefix' => __( "FTP user" ), + 'title' => $ftp + ? $ftp->getFTPLogin() + : __( "create" ), + 'breadcrumb' => [ + new MenuEntry( null, $domain->getDomainPermalink(), $domain->getDomainName() ), + ], +] ); + +// spawn the page content +template( 'ftp', [ + 'domain' => $domain, + 'ftp' => $ftp, +] ); + +// spawn the footer +Footer::spawn();