feat : add updater that can only list wikis

This commit is contained in:
Florian Schmitt 2024-01-09 15:40:28 +03:00
parent d9c9096d8c
commit b25aa91743
3 changed files with 367 additions and 253 deletions

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
.env .env
/vendor/* /vendor/*
yeswiki-list.csv

View file

@ -4,334 +4,371 @@ require 'vendor/autoload.php';
function checkIP($domain, $withWww = false, $noip6 = false) function checkIP($domain, $withWww = false, $noip6 = false)
{ {
if (!preg_match('/(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/', strtolower($domain))) { if (!preg_match('/(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)/', strtolower($domain))) {
throw new Exception('not valid domain : "'.$domain.'".'); throw new Exception('not valid domain : "' . $domain . '".');
}
$output = shell_exec('ping -c1 -4 ' . $domain);
preg_match_all('/PING.*\(.*((\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}).*\)/m', $output, $matches, PREG_SET_ORDER, 0);
$currentip = $matches[0][1] ?? null;
if (empty($currentip)) {
throw new Exception('the domain ' . $domain . ' has no ip v4.');
}
if ($currentip !== $_SERVER['ip4']) {
throw new Exception('the current ip v4 address of ' . $domain . ' is ' . $currentip . '. it should be ' . $_SERVER['ip4']);
}
if (!$noip6) {
$output = shell_exec('ping -c1 -6 ' . $domain);
preg_match_all('/PING.*\((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\)/m', $output, $matches, PREG_SET_ORDER, 0);
$currentip6 = $matches[0][1] ?? null;
if (empty($currentip6)) {
throw new Exception('the domain ' . $domain . ' has no ip v6.');
} }
$output = shell_exec('ping -c1 -4 '.$domain); if ($currentip6 !== $_SERVER['ip6']) {
preg_match_all('/PING.*\(.*((\b25[0-5]|\b2[0-4][0-9]|\b[01]?[0-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}).*\)/m', $output, $matches, PREG_SET_ORDER, 0); throw new Exception('the current ip v6 address of ' . $domain . ' is ' . $currentip6 . '. it should be ' . $_SERVER['ip6']);
$currentip = $matches[0][1] ?? null;
if (empty($currentip)) {
throw new Exception('the domain '.$domain.' has no ip v4.');
} }
if ($currentip !== $_SERVER['ip4']) { }
throw new Exception('the current ip v4 address of '.$domain.' is '.$currentip.'. it should be '.$_SERVER['ip4']); if ($withWww) {
} $dnsentries = dns_get_record('www.' . $domain, DNS_A + DNS_AAAA + DNS_CNAME);
if (!$noip6) { $foundCnameEntry = false;
$output = shell_exec('ping -c1 -6 '.$domain); $foundIp4 = false;
preg_match_all('/PING.*\((([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\)/m', $output, $matches, PREG_SET_ORDER, 0); $foundIp6 = false;
$currentip6 = $matches[0][1] ?? null; foreach ($dnsentries as $key => $row) {
if (empty($currentip6)) { if ($row['host'] == 'www.' . $domain && $row['type'] == 'CNAME' && $row['target'] == $domain) {
throw new Exception('the domain '.$domain.' has no ip v6.'); $foundCnameEntry = true;
} }
if ($currentip6 !== $_SERVER['ip6']) { if ($row['host'] == 'www.' . $domain && $row['type'] == 'A' && $row['ip'] == $_SERVER['ip4']) {
throw new Exception('the current ip v6 address of '.$domain.' is '.$currentip6.'. it should be '.$_SERVER['ip6']); $foundIp4 = true;
}
if (!$noip6) {
if ($row['host'] == 'www.' . $domain && $row['type'] == 'AAAA' && $row['ipv6'] == $_SERVER['ip6']) {
$foundIp6 = true;
} }
}
} }
if ($withWww) { if (!$foundCnameEntry) {
$dnsentries = dns_get_record('www.'.$domain, DNS_A + DNS_AAAA + DNS_CNAME); if (!$foundIp4) {
$foundCnameEntry = false; throw new Exception('the domain www.' . $domain . ' was not found in DNS record as a CNAME targeting ' . $domain . "\n" . ' or A entry to ' . $_SERVER['ip4']);
$foundIp4 = false; }
$foundIp6 = false; if (!$noip6 && !$foundIp6) {
foreach($dnsentries as $key => $row) { throw new Exception('the domain www.' . $domain . ' was not found in DNS record as a CNAME targeting ' . $domain . "\n" . ' or AAAA entry to ' . $_SERVER['ip6'] . '.');
if ($row['host'] == 'www.'.$domain && $row['type']=='CNAME' && $row['target'] == $domain) { }
$foundCnameEntry = true;
}
if ($row['host'] == 'www.'.$domain && $row['type']=='A' && $row['ip'] == $_SERVER['ip4']) {
$foundIp4 = true;
}
if (!$noip6) {
if ($row['host'] == 'www.'.$domain && $row['type']=='AAAA' && $row['ipv6'] == $_SERVER['ip6']) {
$foundIp6 = true;
}
}
}
if (!$foundCnameEntry) {
if (!$foundIp4) {
throw new Exception('the domain www.'.$domain.' was not found in DNS record as a CNAME targeting '.$domain."\n".' or A entry to '.$_SERVER['ip4']);
}
if (!$noip6 && !$foundIp6) {
throw new Exception('the domain www.'.$domain.' was not found in DNS record as a CNAME targeting '.$domain."\n".' or AAAA entry to '.$_SERVER['ip6'].'.');
}
}
} }
return true; }
return true;
} }
function checkIfInstalled($domain) function checkIfInstalled($domain)
{ {
exec('find /home/* -maxdepth 1 -type d | grep '.$domain, $output); exec('find /home/* -maxdepth 1 -type d | grep ' . $domain, $output);
if (!empty($output)) { if (!empty($output)) {
throw new Exception('the domain '.$domain.' was already found on the server.'); throw new Exception('the domain ' . $domain . ' was already found on the server.');
} }
} }
function checkIfUserExist($user) function checkIfUserExist($user)
{ {
exec('cut -d: -f1 /etc/passwd | grep '.$user, $output); exec('cut -d: -f1 /etc/passwd | grep ' . $user, $output);
return !empty($output); return !empty($output);
} }
function generateUserFromDomain($domain, $recursive = null) function generateUserFromDomain($domain, $recursive = null)
{ {
if ($recursive == 100) { if ($recursive == 100) {
throw new Exception('Too much users found, 100 that is too much for '.$domain); throw new Exception('Too much users found, 100 that is too much for ' . $domain);
}
$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, explode(',', $_SERVER['reservedsubdomains']))) {
if ($recursive === null) {
$recursive = 1;
} }
$user = str_split(str_replace([$_SERVER['maindomain'], '-', '.'], '', $domain), 30)[0].$recursive; $user = generateUserFromDomain($domain, $recursive + 1);
// try anthor username if user exists or if reserved name }
if (checkIfUserExist($user) || in_array($user, explode(',', $_SERVER['reservedsubdomains']))) { return $user;
if ($recursive === null) {
$recursive = 1;
}
$user = generateUserFromDomain($domain, $recursive + 1);
}
return $user;
} }
function findUserFromExistingDomain($domain) function findUserFromExistingDomain($domain)
{ {
exec('find /home/* -maxdepth 1 -type d | grep '.$domain, $output); exec('find /home/* -maxdepth 1 -type d | grep ' . $domain, $output);
if (empty($output)) { if (empty($output)) {
throw new Exception('the domain '.$domain.' was not found on the server.'); throw new Exception('the domain ' . $domain . ' was not found on the server.');
} else { } else {
return str_replace(['/home/', '/'.$domain], '', $output[0]); return str_replace(['/home/', '/' . $domain], '', $output[0]);
} }
} }
function generatePassword($length = 32, $add_dashes = false, $available_sets = 'luds') function generatePassword($length = 32, $add_dashes = false, $available_sets = 'luds')
{ {
$sets = array(); $sets = array();
if (strpos($available_sets, 'l') !== false) { if (strpos($available_sets, 'l') !== false) {
$sets[] = 'abcdefghjkmnpqrstuvwxyz'; $sets[] = 'abcdefghjkmnpqrstuvwxyz';
} }
if (strpos($available_sets, 'u') !== false) { if (strpos($available_sets, 'u') !== false) {
$sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ'; $sets[] = 'ABCDEFGHJKMNPQRSTUVWXYZ';
} }
if (strpos($available_sets, 'd') !== false) { if (strpos($available_sets, 'd') !== false) {
$sets[] = '23456789'; $sets[] = '23456789';
} }
if (strpos($available_sets, 's') !== false) { if (strpos($available_sets, 's') !== false) {
$sets[] = ';-().!?'; $sets[] = ';-().!?';
} }
$all = ''; $all = '';
$password = ''; $password = '';
foreach ($sets as $set) { foreach ($sets as $set) {
$password .= $set[array_rand(str_split($set))]; $password .= $set[array_rand(str_split($set))];
$all .= $set; $all .= $set;
} }
$all = str_split($all); $all = str_split($all);
for ($i = 0; $i < $length - count($sets); $i++) { for ($i = 0; $i < $length - count($sets); $i++) {
$password .= $all[array_rand($all)]; $password .= $all[array_rand($all)];
} }
$password = str_shuffle($password); $password = str_shuffle($password);
if (!$add_dashes) { if (!$add_dashes) {
return $password; return $password;
} }
$dash_len = floor(sqrt($length)); $dash_len = floor(sqrt($length));
$dash_str = ''; $dash_str = '';
while (strlen($password) > $dash_len) { while (strlen($password) > $dash_len) {
$dash_str .= substr($password, 0, $dash_len) . '-'; $dash_str .= substr($password, 0, $dash_len) . '-';
$password = substr($password, $dash_len); $password = substr($password, $dash_len);
} }
$dash_str .= $password; $dash_str .= $password;
return $dash_str; return $dash_str;
} }
function createSQLUserAndDatabase($user, $type) function createSQLUserAndDatabase($user, $type)
{ {
$pass = generatePassword(); $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 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 "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 "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 "FLUSH PRIVILEGES;"', $output);
$databaseModel = ($type === 'solo') ? $_SERVER['solomodel'] : $_SERVER['fermemodel']; $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); 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 // TODO: add first user and make him admin
return ['database' => $user, 'user' => $user, 'password' => $pass]; return ['database' => $user, 'user' => $user, 'password' => $pass];
} }
function removeMySQLUserAndDatabase($user) 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 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 "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 "FLUSH PRIVILEGES;"', $output);
return; return;
} }
function createUnixUserWithQuota($user, $quota) function createUnixUserWithQuota($user, $quota)
{ {
$pass = generatePassword(); $pass = generatePassword();
exec('useradd -m -p "'.$pass.'" '.$user, $output); exec('useradd -m -p "' . $pass . '" ' . $user, $output);
exec('setquota -u '.$user.' '.$quota.' '.$quota.' 0 0 -a /dev/loop0', $output); exec('setquota -u ' . $user . ' ' . $quota . ' ' . $quota . ' 0 0 -a /dev/loop0', $output);
// TODO : handle errors // TODO : handle errors
return ['user' => $user, 'password' => $pass, 'quota' => $quota]; return ['user' => $user, 'password' => $pass, 'quota' => $quota];
} }
function removeUnixUser($user) function removeUnixUser($user)
{ {
exec('deluser --remove-home '.$user, $output); exec('deluser --remove-home ' . $user, $output);
// TODO : handle errors // TODO : handle errors
return; return;
} }
function createNginxConfig($domain, $user, $herseUser, $hersePass, $nossl) function createNginxConfig($domain, $user, $herseUser, $hersePass, $nossl)
{ {
// create folder if not exists // create folder if not exists
exec('sudo -u '.$user.' mkdir -p /home'.'/'.$user.'/'.$domain); exec('sudo -u ' . $user . ' mkdir -p /home' . '/' . $user . '/' . $domain);
$nginxFile = '/etc/nginx/conf.d/'.$domain.'.conf'; $nginxFile = '/etc/nginx/conf.d/' . $domain . '.conf';
if (empty($herseUser) && empty($hersePass)) { if (empty($herseUser) && empty($hersePass)) {
// no herse needed // no herse needed
} elseif (empty($herseUser) || empty($hersePass)) { } elseif (empty($herseUser) || empty($hersePass)) {
throw new Exception('You need an username AND a password to add a herse.'); throw new Exception('You need an username AND a password to add a herse.');
} else { } else {
// add password file to domain // add password file to domain
file_put_contents( file_put_contents(
'/home'.'/'.$user.'/'.$domain.'/.htpasswd', '/home' . '/' . $user . '/' . $domain . '/.htpasswd',
$herseUser.':'.password_hash($hersePass, PASSWORD_BCRYPT) $herseUser . ':' . password_hash($hersePass, PASSWORD_BCRYPT)
); );
} }
$templates = new League\Plates\Engine(dirname(__FILE__).'/templates'); $templates = new League\Plates\Engine(dirname(__FILE__) . '/templates');
$subDomain = preg_match('/.'.$_SERVER['maindomain'].'$/isU', $domain, $matches, PREG_OFFSET_CAPTURE, 0); $subDomain = preg_match('/.' . $_SERVER['maindomain'] . '$/isU', $domain, $matches, PREG_OFFSET_CAPTURE, 0);
$isFullDomain = !preg_match('/^([a-zA-Z0-9]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9])\.)([a-zA-Z0-9]{1,2}([-a-zA-Z0-9]{0,252}[a-zA-Z0-9])?)\.([a-zA-Z]{2,63})$/isU', $domain, $matches, PREG_OFFSET_CAPTURE, 0); $isFullDomain = !preg_match('/^([a-zA-Z0-9]([-a-zA-Z0-9]{0,61}[a-zA-Z0-9])\.)([a-zA-Z0-9]{1,2}([-a-zA-Z0-9]{0,252}[a-zA-Z0-9])?)\.([a-zA-Z]{2,63})$/isU', $domain, $matches, PREG_OFFSET_CAPTURE, 0);
if (!$nossl) { if (!$nossl) {
if (!$subDomain) { if (!$subDomain) {
file_put_contents( file_put_contents(
$nginxFile, $nginxFile,
$templates->render( $templates->render(
'nginx-for-ssl-certificate', 'nginx-for-ssl-certificate',
[ [
'domain' => $domain, 'domain' => $domain,
'user' => $user, 'user' => $user,
] ]
) )
); );
exec('service nginx force-reload', $output); exec('service nginx force-reload', $output);
if ($isFullDomain) { if ($isFullDomain) {
exec('/root/.acme.sh/acme.sh --issue -d '.$domain.' -d www.'.$domain.' -k ec-384 -w /home/'.$user.'/'.$domain.'/', $output); exec('/root/.acme.sh/acme.sh --issue -d ' . $domain . ' -d www.' . $domain . ' -k ec-384 -w /home/' . $user . '/' . $domain . '/', $output);
} else { } else {
exec('/root/.acme.sh/acme.sh --issue -d '.$domain.' -k ec-384 -w /home/'.$user.'/'.$domain.'/', $output); exec('/root/.acme.sh/acme.sh --issue -d ' . $domain . ' -k ec-384 -w /home/' . $user . '/' . $domain . '/', $output);
} }
exec('mkdir -p /etc/letsencrypt/live/'.$domain, $output); exec('mkdir -p /etc/letsencrypt/live/' . $domain, $output);
exec('/root/.acme.sh/acme.sh --install-cert -d '.$domain.' --ecc \ exec('/root/.acme.sh/acme.sh --install-cert -d ' . $domain . ' --ecc \
--cert-file /etc/letsencrypt/live/'.$domain.'/cert.pem \ --cert-file /etc/letsencrypt/live/' . $domain . '/cert.pem \
--key-file /etc/letsencrypt/live/'.$domain.'/key.pem \ --key-file /etc/letsencrypt/live/' . $domain . '/key.pem \
--fullchain-file /etc/letsencrypt/live/'.$domain.'/fullchain.pem \ --fullchain-file /etc/letsencrypt/live/' . $domain . '/fullchain.pem \
--ca-file /etc/letsencrypt/live/'.$domain.'/ca.pem \ --ca-file /etc/letsencrypt/live/' . $domain . '/ca.pem \
--reloadcmd "systemctl restart nginx.service"', $output); --reloadcmd "systemctl restart nginx.service"', $output);
}
file_put_contents(
$nginxFile,
$templates->render(
'nginx-maindomain',
[
'domain' => $domain,
'user' => $user,
'herseUser' => $herseUser,
'hersePass' => $hersePass,
'subDomain' => $subDomain,
]
)
);
} else {
file_put_contents(
$nginxFile,
$templates->render(
'nginx-nossl',
[
'domain' => $domain,
'user' => $user,
'herseUser' => $herseUser,
'hersePass' => $hersePass,
'subDomain' => $subDomain,
]
)
);
} }
exec('service nginx force-reload', $output); file_put_contents(
$nginxFile,
$templates->render(
'nginx-maindomain',
[
'domain' => $domain,
'user' => $user,
'herseUser' => $herseUser,
'hersePass' => $hersePass,
'subDomain' => $subDomain,
]
)
);
} else {
file_put_contents(
$nginxFile,
$templates->render(
'nginx-nossl',
[
'domain' => $domain,
'user' => $user,
'herseUser' => $herseUser,
'hersePass' => $hersePass,
'subDomain' => $subDomain,
]
)
);
}
exec('service nginx force-reload', $output);
} }
function removeNginxConfig($domain) function removeNginxConfig($domain)
{ {
$nginxFile = '/etc/nginx/conf.d/'.$domain.'.conf'; $nginxFile = '/etc/nginx/conf.d/' . $domain . '.conf';
unlink($nginxFile); unlink($nginxFile);
exec('service nginx force-reload', $output); exec('service nginx force-reload', $output);
} }
function createPhpFpmConfig($user) function createPhpFpmConfig($user)
{ {
$phpVersion = str_replace(['php', '-fpm'], '', $_SERVER['phpservice']); $phpVersion = str_replace(['php', '-fpm'], '', $_SERVER['phpservice']);
$phpConfFile = '/etc/php/'.$phpVersion.'/fpm/pool.d/'.$user.'.conf'; $phpConfFile = '/etc/php/' . $phpVersion . '/fpm/pool.d/' . $user . '.conf';
$templates = new League\Plates\Engine(dirname(__FILE__).'/templates'); $templates = new League\Plates\Engine(dirname(__FILE__) . '/templates');
file_put_contents($phpConfFile, $templates->render('php-fpm', ['user' => $user])); file_put_contents($phpConfFile, $templates->render('php-fpm', ['user' => $user]));
exec('service '.$_SERVER['phpservice'].' reload', $output); exec('service ' . $_SERVER['phpservice'] . ' reload', $output);
} }
function removePhpFpmConfig($user) function removePhpFpmConfig($user)
{ {
$phpVersion = str_replace(['php', '-fpm'], '', $_SERVER['phpservice']); $phpVersion = str_replace(['php', '-fpm'], '', $_SERVER['phpservice']);
$phpConfFile = '/etc/php/'.$phpVersion.'/fpm/pool.d/'.$user.'.conf'; $phpConfFile = '/etc/php/' . $phpVersion . '/fpm/pool.d/' . $user . '.conf';
unlink($phpConfFile); unlink($phpConfFile);
exec('service '.$_SERVER['phpservice'].' reload', $output); exec('service ' . $_SERVER['phpservice'] . ' reload', $output);
} }
function copyYesWikiFiles($domain, $user, $dbUser, $herseUser = null, $hersePass = null, $nossl = null) function copyYesWikiFiles($domain, $user, $dbUser, $herseUser = null, $hersePass = null, $nossl = null)
{ {
$tmpFile = '/tmp/yeswiki.zip'; $tmpFile = '/tmp/yeswiki.zip';
$destDir = '/home'.'/'.$user.'/'.$domain; $destDir = '/home' . '/' . $user . '/' . $domain;
$sudo = 'sudo -u '.$user.' '; $sudo = 'sudo -u ' . $user . ' ';
exec($sudo.' mkdir -p '.$destDir, $output); exec($sudo . ' mkdir -p ' . $destDir, $output);
exec($sudo.' chown '.$user.':'.$user.' -R '.$destDir, $output); exec($sudo . ' chown ' . $user . ':' . $user . ' -R ' . $destDir, $output);
if (file_exists($tmpFile)) { if (file_exists($tmpFile)) {
unlink($tmpFile);
}
exec($sudo.'curl --insecure -o '.$tmpFile.' '.$_SERVER['source_archive_url']);
exec($sudo.'unzip '.$tmpFile.' "doryphore/*" -d '.$destDir);
exec($sudo.'mv '.$destDir.'/doryphore/* '.$destDir.'/');
exec($sudo.'rm -rf '.$destDir.'/doryphore');
unlink($tmpFile); unlink($tmpFile);
$herse = ''; }
if (!empty($herseUser) && !empty($hersePass)) { exec($sudo . 'curl --insecure -o ' . $tmpFile . ' ' . $_SERVER['source_archive_url']);
$herse = $herseUser.':'.$hersePass.'@'; exec($sudo . 'unzip ' . $tmpFile . ' "doryphore/*" -d ' . $destDir);
} exec($sudo . 'mv ' . $destDir . '/doryphore/* ' . $destDir . '/');
$curl = 'curl -X POST --insecure --data-urlencode \'config[default_language]=fr\' --data-urlencode \'config[wakka_name]='.$domain.'\' --data-urlencode \'config[root_page]=PagePrincipale\' --data-urlencode \'config[mysql_host]=localhost\' --data-urlencode \'config[mysql_database]='.$dbUser['database'].'\' --data-urlencode \'config[mysql_user]='.$dbUser['user'].'\' --data-urlencode \'config[mysql_password]='.$dbUser['password'].'\' --data-urlencode \'config[table_prefix]=yeswiki_\' --data-urlencode \'admin_name='.$_SERVER['admin_id'].'\' --data-urlencode \'admin_password='.$_SERVER['admin_password'].'\' --data-urlencode \'admin_password_conf='.$_SERVER['admin_password'].'\' --data-urlencode \'admin_email='.$_SERVER['admin_email'].'\' --data-urlencode \'config[rewrite_mode]=0\' --data-urlencode \'config[allow_raw_html]=1\' --data-urlencode \'config[allow_robots]=1\' --data-urlencode \'submit=Continue\' \'http'.($nossl ? '' : 's').'://'.$herse.$domain.'/?PagePrincipale&installAction=install\' 2>&1'; exec($sudo . 'rm -rf ' . $destDir . '/doryphore');
$output = []; unlink($tmpFile);
exec($curl, $output); $herse = '';
//echo implode("\n", $output); if (!empty($herseUser) && !empty($hersePass)) {
return; $herse = $herseUser . ':' . $hersePass . '@';
}
$curl = 'curl -X POST --insecure --data-urlencode \'config[default_language]=fr\' --data-urlencode \'config[wakka_name]=' . $domain . '\' --data-urlencode \'config[root_page]=PagePrincipale\' --data-urlencode \'config[mysql_host]=localhost\' --data-urlencode \'config[mysql_database]=' . $dbUser['database'] . '\' --data-urlencode \'config[mysql_user]=' . $dbUser['user'] . '\' --data-urlencode \'config[mysql_password]=' . $dbUser['password'] . '\' --data-urlencode \'config[table_prefix]=yeswiki_\' --data-urlencode \'admin_name=' . $_SERVER['admin_id'] . '\' --data-urlencode \'admin_password=' . $_SERVER['admin_password'] . '\' --data-urlencode \'admin_password_conf=' . $_SERVER['admin_password'] . '\' --data-urlencode \'admin_email=' . $_SERVER['admin_email'] . '\' --data-urlencode \'config[rewrite_mode]=0\' --data-urlencode \'config[allow_raw_html]=1\' --data-urlencode \'config[allow_robots]=1\' --data-urlencode \'submit=Continue\' \'http' . ($nossl ? '' : 's') . '://' . $herse . $domain . '/?PagePrincipale&installAction=install\' 2>&1';
$output = [];
exec($curl, $output);
//echo implode("\n", $output);
return;
} }
function checkHerse($herseUser, $hersePass) function checkHerse($herseUser, $hersePass)
{ {
if (empty($herseUser) && empty($hersePass)) { if (empty($herseUser) && empty($hersePass)) {
return false; // no herse needed return false; // no herse needed
} elseif (empty($herseUser) || empty($hersePass)) { } elseif (empty($herseUser) || empty($hersePass)) {
throw new Exception('You need an username AND a password to add a herse.'); throw new Exception('You need an username AND a password to add a herse.');
}
return true; // herse needed
}
function editConfigFile()
{
// change fuseau horaire
// 'contact_mail_func' => 'smtp',
// 'contact_smtp_host' => 'ssl://mail.gandi.net',
// 'contact_smtp_port' => '465',
// 'contact_smtp_user' => 'noreply@yeswiki.pro',
// 'contact_reply_to' => 'noreply@yeswiki.pro',
// 'contact_smtp_pass' => 'Ceci est 1 message automatique!',
}
function addExtensions()
{
// ferme pour l'option ferme
}
function addStatistics()
{
// requete SQL
// INSERT INTO `matomo_site` (`idsite`, `name`, `main_url`, `ts_created`, `ecommerce`, `sitesearch`, `sitesearch_keyword_parameters`, `sitesearch_category_parameters`, `timezone`, `currency`, `exclude_unknown_urls`, `excluded_ips`, `excluded_parameters`, `excluded_user_agents`, `excluded_referrers`, `group`, `type`, `keep_url_fragment`, `creator_login`) VALUES (NULL, 'Partage ton outil', 'https://partagetonoutil.fr', '2023-02-01 00:00:00', '0', '1', '', '', 'Europe/Paris', 'EUR', '0', '', '', '', '', '', 'website', '0', 'superadmin');
}
function searchWikis($path, $pattern)
{
$it = new RecursiveDirectoryIterator($path);
$list = array();
foreach (new RecursiveIteratorIterator($it) as $file) {
if (preg_match('/' . preg_quote($pattern) . '$/i', $file)) {
$wakkaConfig = [];
include_once($file);
$list[] = [
'URL' => $wakkaConfig['base_url'] ?? 'KO',
'VERSION' => $wakkaConfig['yeswiki_version'] ?? 'KO',
'RELEASE' => $wakkaConfig['yeswiki_release'] ?? 'KO',
];
} }
return true; // herse needed }
return $list;
} }
function editConfigFile() {
// change fuseau horaire
// 'contact_mail_func' => 'smtp',
// 'contact_smtp_host' => 'ssl://mail.gandi.net',
// 'contact_smtp_port' => '465',
// 'contact_smtp_user' => 'noreply@yeswiki.pro',
// 'contact_reply_to' => 'noreply@yeswiki.pro',
// 'contact_smtp_pass' => 'Ceci est 1 message automatique!',
}
function addExtensions() {
// ferme pour l'option ferme
}
function addStatistics() {
// requete SQL
// INSERT INTO `matomo_site` (`idsite`, `name`, `main_url`, `ts_created`, `ecommerce`, `sitesearch`, `sitesearch_keyword_parameters`, `sitesearch_category_parameters`, `timezone`, `currency`, `exclude_unknown_urls`, `excluded_ips`, `excluded_parameters`, `excluded_user_agents`, `excluded_referrers`, `group`, `type`, `keep_url_fragment`, `creator_login`) VALUES (NULL, 'Partage ton outil', 'https://partagetonoutil.fr', '2023-02-01 00:00:00', '0', '1', '', '', 'Europe/Paris', 'EUR', '0', '', '', '', '', '', 'website', '0', 'superadmin');
}

76
yeswiki-updater.php Executable file
View file

@ -0,0 +1,76 @@
#!/usr/bin/php
<?php
require_once('vendor/autoload.php');
require_once('utils.inc.php');
use DevCoder\DotEnv;
$climate = new League\CLImate\CLImate();
$climate->description('yeswiki-updater, find and update YesWiki like a professionnal 🌈🦄');
if (0 == posix_getuid()) {
$absolutePathToEnvFile = __DIR__ . '/.env';
if (file_exists($absolutePathToEnvFile)) {
(new DotEnv($absolutePathToEnvFile))->load();
} else {
$climate->error('ERROR : No .env file found.');
exit;
}
$climate->arguments->add([
'path' => [
'prefix' => 'p',
'longPrefix' => 'path',
'description' => 'Path to scan for wikis',
'required' => true,
'defaultValue' => '/var/www/html'
],
'output' => [
'prefix' => 'o',
'longPrefix' => 'output',
'description' => 'Output results in table format, json or csv',
'defaultValue' => 'table'
],
'update' => [
'prefix' => 'u',
'longPrefix' => 'update',
'description' => 'Update to latest version of stable yeswiki',
'defaultValue' => '0'
],
]);
$climate->arguments->parse();
$path = $climate->arguments->get('path');
if (!empty($path) && is_dir($path)) {
try {
$update = $climate->arguments->get('update');
$output = $climate->arguments->get('output');
$matches = searchWikis($path, 'wakka.config.php');
switch ($output) {
case 'table':
$climate->table($matches);
break;
case 'json':
$climate->json($matches);
break;
case 'csv':
$fileName = 'yeswiki-list.csv';
$file = fopen($fileName, "w");
foreach ($matches as $line) {
fputcsv($file, $line);
}
$climate->info($fileName . ' was created.');
break;
}
} catch (Exception $e) {
$climate->error('ERROR : ' . $e->getMessage());
}
} else {
$climate->usage();
}
} else {
$climate->error('ERROR : this script needs root privilege to run.');
exit;
}