feat(cli) : separate cli, use templates WIP

This commit is contained in:
mrflos 2022-09-06 13:38:00 +03:00
parent a685758881
commit 8a62a447a8
6 changed files with 96 additions and 63 deletions

View file

@ -39,9 +39,9 @@ function generateUserFromDomain($domain, $recursive = null)
if ($recursive == 100) {
throw new Exception('Too much users found, 100 that is too much for '.$domain);
}
$user = str_split(str_replace(['yeswiki.pro', '-', '.'], '', $domain), 30)[0].$recursive;
$user = str_split(str_replace([$_SERVER['maindomain'], '-', '.'], '', $domain), 30)[0].$recursive;
// try anthor username if user exists or if reserved name
if (checkIfUserExist($user) || in_array($user, ['www', 'stats', 'mail', 'sql', 'cron', 'modelesolo', 'modeleferme'])) {
if (checkIfUserExist($user) || in_array($user, explode(',', $_SERVER['reservedsubdomains']))) {
if ($recursive === null) {
$recursive = 1;
}
@ -104,21 +104,24 @@ function generatePassword($length = 32, $add_dashes = false, $available_sets = '
return $dash_str;
}
function createSQLUserAndDatabase($user)
function createSQLUserAndDatabase($user, $type)
{
$pass = generatePassword();
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e \'CREATE DATABASE IF NOT EXISTS '.$user.';\'', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e "CREATE USER IF NOT EXISTS \''.$user.'\'@\'localhost\' IDENTIFIED BY \''.$pass.'\';"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e "GRANT ALL PRIVILEGES ON '.$user.'.* TO \''.$user.'\'@\'localhost\';"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e "FLUSH PRIVILEGES;"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -e \'CREATE DATABASE IF NOT EXISTS '.$user.';\'', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -e "CREATE USER IF NOT EXISTS \''.$user.'\'@\'localhost\' IDENTIFIED BY \''.$pass.'\';"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -e "GRANT ALL PRIVILEGES ON '.$user.'.* TO \''.$user.'\'@\'localhost\';"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -e "FLUSH PRIVILEGES;"', $output);
$databaseModel = ($type === 'solo') ? $_SERVER['solomodel'] : $_SERVER['fermemodel'];
exec('mysqldump -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -v '.$databaseModel.' | mysql -u '.$user.' -p\''.$pass.'\' -D '.$user, $output);
// TODO: add first user and make him admin
return ['database' => $user, 'user' => $user, 'password' => $pass];
}
function removeMySQLUserAndDatabase($user)
{
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e \'DROP DATABASE IF EXISTS '.$user.';\'', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e "DROP USER IF EXISTS \''.$user.'\'@\'localhost\';"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e "FLUSH PRIVILEGES;"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -e \'DROP DATABASE IF EXISTS '.$user.';\'', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -e "DROP USER IF EXISTS \''.$user.'\'@\'localhost\';"', $output);
exec('mysql -u '.$_SERVER['mysqluser'].' -p\''.$_SERVER['mysqlpassword'].'\' -e "FLUSH PRIVILEGES;"', $output);
return;
}
@ -140,28 +143,60 @@ function removeUnixUser($user)
function createNginxConfig($domain, $user, $herseUser, $hersePass)
{
// Create new Plates instance
$templates = new League\Plates\Engine('./templates');
// Render a template
echo $templates->render('nginx-yeswiki.pro', ['domain' => $domain, 'user' => $user]);
addHerse($nginxFile, $herseUser, $hersePass);
$nginxFile = '/etc/nginx/conf.d/'.$domain.'.conf';
if (empty($herseUser) && empty($hersePass)) {
// no herse needed
} elseif (empty($herseUser) || empty($hersePass)) {
throw new Exception('You need an username AND a password to add a herse.');
} else {
// add password file to domain
file_put_contents(
'/home'.'/'.$user.'/'.$domain.'/.htpasswd',
$herseUser.':'.password_hash($hersePass, PASSWORD_BCRYPT)
);
}
echo 'DIR:'.dirname(__FILE__);
$templates = new League\Plates\Engine(dirname(__FILE__).'/templates');
file_put_contents(
$nginxFile,
$templates->render(
'nginx-maindomain',
[
'domain' => $domain,
'user' => $user,
'herseUser' => $herseUser,
'hersePass' => $hersePass,
]
)
);
exec('service nginx force-reload', $output);
}
function removeNginxConfig($domain, $user)
function removeNginxConfig($domain)
{
$nginxFile = '/etc/nginx/conf.d/'.$domain.'.conf';
unlink($nginxFile);
exec('service nginx force-reload', $output);
}
function createPhpFpmConfig($user)
{
$phpVersion = str_replace(['php', '-fpm'], '', $_SERVER['phpservice']);
$phpConfFile = '/etc/php/'.$phpVersion.'/fpm/pool.d/'.$user.'.conf';
$templates = new League\Plates\Engine(dirname(__FILE__).'/templates');
file_put_contents($phpConfFile, $templates->render('php-fpm', ['user' => $user]));
exec('service '.$_SERVER['phpservice'].' reload', $output);
}
function removePhpFpmConfig($user)
{
$phpVersion = str_replace(['php', '-fpm'], '', $_SERVER['phpservice']);
$phpConfFile = '/etc/php/'.$phpVersion.'/fpm/pool.d/'.$user.'.conf';
unlink($phpConfFile);
exec('service '.$_SERVER['phpservice'].' reload', $output);
}
function copyYesWikiFiles($domain, $user, $type)
function copyYesWikiFiles($domain, $user)
{
$destDir = '/home'.'/'.$user.'/'.$domain;
exec('mkdir -p '.$destDir, $output);
@ -169,14 +204,6 @@ function copyYesWikiFiles($domain, $user, $type)
return;
}
function copyYesWikiDatabase($user, $type)
{
$databaseModel = ($type === 'solo') ? $_SERVER['solomodel'] : $_SERVER['fermemodel'];
exec('mysql -u '.$_SERVER['mysqluser'].' -p'.$_SERVER['mysqlpassword'].' -e "DUPLICATE '.$databaseModel.' TO '.$user.';"', $output);
// TODO : handle errors
return;
}
function checkHerse($herseUser, $hersePass)
{
if (empty($herseUser) && empty($hersePass)) {
@ -186,22 +213,3 @@ function checkHerse($herseUser, $hersePass)
}
return true; // herse needed
}
function addHerse(&$nginxFile, $herseUser, $hersePass)
{
if (empty($herseUser) && empty($hersePass)) {
return ; // no herse needed
} elseif (empty($herseUser) || empty($hersePass)) {
throw new Exception('You need an username AND a password to add a herse.');
} else {
//add herse to the domain
echo $nginxFile;
}
}
function removeYesWiki($domain, $user)
{
// enlever la db et le user sql
// enlever la config nginx et la conf php-fpm
// enlever le user unix et son home
}