diff --git a/include/class-Footer.php b/include/class-Footer.php index a78d6cb..895f5c7 100644 --- a/include/class-Footer.php +++ b/include/class-Footer.php @@ -1,31 +1,33 @@ . /** * The footer of the web page */ class Footer { /** * Spawn the footer to standard output * * @param $args array arguments */ public static function spawn() { - template( 'footer', Header::$args ); + template( 'footer', [ + 'args' => & Header::$args, + ] ); } } diff --git a/include/class-Mailbox.php b/include/class-Mailbox.php index be565a6..aa00a65 100644 --- a/include/class-Mailbox.php +++ b/include/class-Mailbox.php @@ -1,106 +1,97 @@ . /** * 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 = Mailbox::generatePassword(); + $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 * * @return string */ public static function permalink( $domain, $mailbox ) { return sprintf( '%s/%s/%s', ROOT . '/mailbox.php', $domain, $mailbox ); } /** * 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" ); } - - /** - * Generate a password - * - * TODO: do not hardcode to a certain way - */ - public static function generatePassword() { - return rtrim( base64_encode( bin2hex( openssl_random_pseudo_bytes( 8 ) ) ), '=' ); - } } diff --git a/include/functions.php b/include/functions.php index f6f32ff..2d108c0 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1,138 +1,163 @@ . /** * Require a certain page from the template directory * * @param $name string page name (to be sanitized) * @param $args mixed arguments to be passed to the page scope */ -function template( $name, $args = [] ) { - extract( $args ); - require TEMPLATE_PATH . __ . "$name.php"; +function template( $template_name, $template_args = [] ) { + extract( $template_args, EXTR_SKIP ); + return require TEMPLATE_PATH . __ . "$template_name.php"; +} + +/** + * Get the returned text from a template + * + * @param $name string page name (to be sanitized) + * @param $args mixed arguments to be passed to the page scope + * @see template() + * @return string + */ +function template_content( $name, $args = [] ) { + ob_start(); + template( $name, $args ); + $text = ob_get_contents(); + ob_end_clean(); + return $text; } /** * Print an e-mail (safe for bots) * * @param $email string */ function email_blur( $email ) { $dot = strip_tags( __( " dot " ) ); $at = strip_tags( __( " at " ) ); $email = esc_html( $email ); echo str_replace( [ '.', '@' ], [ $dot, $at ], $email ); } /** * Send an e-mail to someone * * @param $subject string E-mail subject * @param $message string E-mail message * @param $to string E-mail recipient (from current logged-in user as default) */ function send_email( $subject, $message, $to = false ) { if( ! $to ) { if( ! is_logged() ) { die( "can't retrieve e-mail address from anon user" ); } $to = get_user( 'user_email' ); } return SMTPMail::instance() ->to( $to ) ->message( $subject, $message ) ->disconnect(); } /** * Require a certain permission * * @param $permission string An internal permission like 'edit-all-user' * @param $redirect boolean Enable or disable the redirect */ function require_permission( $permission, $redirect = true ) { if( ! has_permission( $permission ) ) { if( is_logged() ) { Header::spawn( [ 'title' => __( "Permission denied" ), ] ); Footer::spawn(); exit; } else { $login = get_menu_entry( 'login' ); $url = $login->getSitePage( URL ); if( $redirect && isset( $_SERVER[ 'REQUEST_URI' ] ) ) { $url = http_build_get_query( $url, [ 'redirect' => $_SERVER[ 'REQUEST_URI' ], ] ); } http_redirect( $url, 307 ); } } } /** * Check the POST action * * @param $action string e.g. 'save-domain' * @return bool */ function is_action( $action ) { return isset( $_POST[ 'action' ] ) && $_POST[ 'action' ] === $action; } /** * Get URL parts * * @param $n */ function url_parts( $n ) { $parts = explode( _, $_SERVER[ 'PATH_INFO' ] ); array_shift( $parts ); if( count( $parts ) !== $n ) { BadRequest::spawn(); } return $parts; } /** * Link to an existing page from the menu * * @param $uid string E.g. 'index' * @param $args mixed Arguments */ -function the_menu_link( $uid ) { +function the_menu_link( $uid, $args = [] ) { $page = get_menu_entry( $uid ); the_link( $page->getSitePage(), $page->name, $args ); } /** * Link to a whatever page * * P.S. link() is a reserved function * * @param $url string * @param $title string * @param $args mixed Arguments */ function the_link( $url, $title, $args = [] ) { template( 'link', [ 'title' => $title, 'url' => $url, 'args' => $args, ] ); } + +/** + * Generate a password + * + * @param $bytes int + */ +function generate_password( $bytes = 8 ) { + return rtrim( base64_encode( bin2hex( openssl_random_pseudo_bytes( $bytes ) ) ), '=' ); +} diff --git a/load-post.php b/load-post.php index bb5f345..904d7dd 100644 --- a/load-post.php +++ b/load-post.php @@ -1,80 +1,81 @@ . // 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' ); // 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', ] ); diff --git a/profile.php b/profile.php new file mode 100644 index 0000000..844db56 --- /dev/null +++ b/profile.php @@ -0,0 +1,83 @@ +. + +/* + * This is the user profile page + */ + +// load framework +require 'load.php'; + +// must be logged +require_permission( 'read' ); + +// myself +$user = get_user(); + +// edit whatever user +if( isset( $_SERVER[ 'PATH_INFO' ] ) ) { + require_permission( 'edit-all-user' ); + + list( $user_uid ) = url_parts( 1 ); + $user = Sessionuser::factoryFromUID( $user_uid ) + ->queryRow(); + + if( ! $user ) { + PageNotFound::spaqn(); + } +} + +// spawn header +Header::spawn( [ + 'title' => __( "Profile" ), +] ); + +// user e-mail +$email = $user->get( 'user_email' ); + +// handle send user password action +if( is_action( 'send-user-password' ) ) { + + // generate + $password = generate_password(); + + // e-mail body + $mail_content = template_content( 'mail-user-password', [ + 'email' => $email, + 'name' => $user->get( 'user_name' ), + 'uid' => $user->get( 'user_uid' ), + 'surname' => $user->get( 'user_surname' ), + 'password' => $password, + ] ); + + // update + query_update( 'user', [ + new DBCol( 'user_password', Sessionuser::encryptPassword( $password ), 's' ), + ], sprintf( + 'user_ID = %d', + $user->get( 'user_ID' ) + ) ); + + // send + send_email( __( "Password reset" ), $mail_content, $email ); +} + +// spawn the profile page +template( 'profile', [ 'email' => $email ] ); + +// spawn footer +Footer::spawn(); diff --git a/template/footer.php b/template/footer.php index 9a5124f..d0a3fac 100644 --- a/template/footer.php +++ b/template/footer.php @@ -1,77 +1,80 @@ . /* * This is the template for the website footer * * Called from: * include/class-Footer.php + * + * Available variables: + * $args array */ // unuseful when load directly defined( 'BOZ_PHP' ) or die; load_module( 'footer' ); ?>