2023-03-30 10:14:46 -07:00
< ? php
/**
* vpanel - stack
* https :// git . stack - source . com / msb / vpanel - stack
* Copyright ( c ) 2022 Matthew Saunders Brown < matthewsaundersbrown @ gmail . com >
* GNU General Public License v3 . 0 + ( see COPYING or https :// www . gnu . org / licenses / gpl - 3.0 . txt )
*/
namespace Panel\Vhost ;
class VhostsAdd extends \Panel\Vhost {
function beforeRoute ( $f3 ) {
parent :: beforeRoute ( $f3 );
2023-06-19 11:53:16 -07:00
/* get system defaults for new websites */
$deploy = array ();
$deploy [ 'username' ] = '' ;
if ( $users_array = $f3 -> call ( '\Panel::vGet' , array ( " vhost-user-get.sh -c " , FALSE ))) {
$deploy [ 'users_array' ] = $users_array ;
} else {
$deploy [ 'users_array' ] = array ();
}
if ( $f3 -> get ( 'WRITEUSERINFO' ) == '1' ) {
$deploy [ 'writeuserinfo' ] = 1 ;
} else {
$deploy [ 'writeuserinfo' ] = 0 ;
}
if ( $f3 -> get ( 'SHOWWRITEINFO' ) == '1' ) {
$deploy [ 'showwriteinfo' ] = 1 ;
} else {
$deploy [ 'showwriteinfo' ] = 0 ;
}
2024-04-15 10:41:09 -07:00
2023-06-19 11:53:16 -07:00
$f3 -> set ( 'deploy' , $deploy );
2023-03-30 10:14:46 -07:00
2024-04-15 10:41:09 -07:00
/* check if VHOSTLIMIT is set */
if ( is_numeric ( $f3 -> get ( 'VHOSTLIMIT' ))) {
if ( $f3 -> get ( 'VHOSTLIMIT' ) > 0 ) {
$f3 -> set ( 'vhostlimited' , TRUE );
if ( $vhosts_array = $f3 -> call ( '\Panel::vGet' , array ( " vhost-get.sh -c " , FALSE ))) {
$f3 -> set ( 'vhostscount' , count ( $vhosts_array ));
}
}
}
2023-03-30 10:14:46 -07:00
}
static function get ( $f3 ) {
2024-04-15 10:41:09 -07:00
/* check VHOSTLIMIT */
if ( $f3 -> get ( 'vhostlimited' ) && ( $f3 -> get ( 'vhostscount' ) >= $f3 -> get ( 'VHOSTLIMIT' ))) {
echo \Template :: instance () -> render ( 'vhost/vhosts-add-max.html' );
} else {
echo \Template :: instance () -> render ( 'vhost/vhosts-add.html' );
}
2023-03-30 10:14:46 -07:00
}
function post ( $f3 ) {
extract ( $_POST );
$messages = array ();
/* validate domain */
2023-06-19 11:53:16 -07:00
$domain = strtolower ( $_POST [ 'domain' ]);
$domain = preg_replace ( '/^www\./' , '' , $domain );
if ( $f3 -> call ( '\Panel::validateDomain' , $domain )) {
if ( $f3 -> call ( '\Panel::verifyVhostDomainExists' , $domain )) {
$messages = $f3 -> get ( 'SESSION.messages' );
$messages [] = " $domain already exists on this server. " ;
$f3 -> set ( 'SESSION.messages' , $messages );
$f3 -> reroute ( " /Websites/Add " );
} else {
$cmd = " /usr/local/bin/vhost-deploy.sh -d $domain " ;
}
2023-03-30 10:14:46 -07:00
} else {
2023-06-19 11:53:16 -07:00
$f3 -> reroute ( " /Websites/Add " );
2023-03-30 10:14:46 -07:00
}
2023-06-19 11:53:16 -07:00
/* validate username & set related options */
$username = strtolower ( $_POST [ 'username' ]);
if ( $f3 -> get ( 'JAILUSER' ) == '1' ) {
$jailuser = 1 ;
2023-03-30 10:14:46 -07:00
} else {
2023-06-19 11:53:16 -07:00
$jailuser = 0 ;
2023-03-30 10:14:46 -07:00
}
2023-06-19 11:53:16 -07:00
if ( $username == '' ) {
$cmd = " $cmd -j $jailuser " ;
2023-03-30 10:14:46 -07:00
} else {
2023-06-19 11:53:16 -07:00
if ( $f3 -> call ( '\Panel::vGet' , array ( " vhost-user-get.sh -u $username -c " , FALSE ))) {
$cmd = " $cmd -u $username " ;
} else {
$messages = $f3 -> get ( 'SESSION.messages' );
$messages [] = " Error validating username $username . " ;
$f3 -> set ( 'SESSION.messages' , $messages );
$f3 -> reroute ( " /Websites/Add " );
2023-03-30 10:14:46 -07:00
}
}
2023-06-19 11:53:16 -07:00
if ( $f3 -> get ( 'WRITEUSERINFO' ) == '1' ) {
$writeuserinfo = 1 ;
2023-03-30 10:14:46 -07:00
} else {
2023-06-19 11:53:16 -07:00
$writeuserinfo = 0 ;
2023-03-30 10:14:46 -07:00
}
2023-06-19 11:53:16 -07:00
$cmd = " $cmd -w $writeuserinfo " ;
exec ( " $cmd " , $output , $result_code );
if ( $result_code == 0 ) {
$messages [] = " Success: Website $domain added. " ;
if ( $username == '' && $jailuser == 1 ) {
$messages [] = " Note: New user is being jailed. Setting up the jail environment takes about a minute to complete and is being run in the background now. " ;
}
2023-03-30 10:14:46 -07:00
$f3 -> set ( 'SESSION.messages' , $messages );
2024-04-03 09:59:27 -07:00
$f3 -> reroute ( " /Websites/ $domain " );
2023-03-30 10:14:46 -07:00
} else {
2023-06-19 11:53:16 -07:00
$messages [] = " Error adding website. " ;
$f3 -> set ( 'SESSION.messages' , $messages );
$f3 -> reroute ( " /Websites/Add " );
2023-03-30 10:14:46 -07:00
}
2023-06-19 11:53:16 -07:00
2023-03-30 10:14:46 -07:00
}
}