vpanel-stack/panel/classes/Panel/Vhost/VhostsAdd.php
2024-04-03 09:59:27 -07:00

112 lines
3.1 KiB
PHP

<?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);
/* 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;
}
$f3->set('deploy', $deploy);
}
static function get($f3) {
echo \Template::instance()->render('vhost/vhosts-add.html');
}
function post($f3) {
extract($_POST);
$messages = array();
/* validate domain */
$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";
}
} else {
$f3->reroute("/Websites/Add");
}
/* validate username & set related options */
$username = strtolower($_POST['username']);
if ($f3->get('JAILUSER') == '1') {
$jailuser = 1;
} else {
$jailuser = 0;
}
if ($username == '') {
$cmd = "$cmd -j $jailuser";
} else {
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");
}
}
if ($f3->get('WRITEUSERINFO') == '1') {
$writeuserinfo = 1;
} else {
$writeuserinfo = 0;
}
$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.";
}
$f3->set('SESSION.messages', $messages);
$f3->reroute("/Websites/$domain");
} else {
$messages[] = "Error adding website.";
$f3->set('SESSION.messages', $messages);
$f3->reroute("/Websites/Add");
}
}
}