2024-12-05 21:49:11 +03:00
#!/usr/bin/env php
2022-08-16 09:46:38 +03:00
< ? php
2024-09-23 18:51:40 +03:00
2022-08-16 09:46:38 +03:00
require_once ( 'vendor/autoload.php' );
2022-08-29 00:26:46 +03:00
require_once ( 'utils.inc.php' );
2022-08-16 09:46:38 +03:00
2024-12-05 21:49:11 +03:00
use PhpDevCommunity\DotEnv ;
use League\CLImate\CLImate ;
2022-08-29 00:26:46 +03:00
2024-12-05 21:49:11 +03:00
$climate = new CLImate ();
2022-08-16 09:46:38 +03:00
$climate -> description ( 'yeswiki-installer, install YesWiki like a professionnal 🌈🦄' );
2022-08-29 00:26:46 +03:00
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 ([
'domain' => [
'prefix' => 'd' ,
'longPrefix' => 'domain' ,
'description' => 'Domain name used for installation' ,
'required' => true ,
'defaultValue' => 'example.com'
],
'type' => [
2022-08-16 09:46:38 +03:00
'prefix' => 't' ,
'longPrefix' => 'type' ,
'description' => 'Type of installation, can be "solo" or "ferme"' ,
'required' => true ,
'defaultValue' => 'solo'
],
2022-08-29 00:26:46 +03:00
'quota' => [
2022-08-16 09:46:38 +03:00
'prefix' => 'q' ,
'longPrefix' => 'quota' ,
'description' => 'User quota for hard drive space, in bytes' ,
'required' => true ,
2022-08-29 00:26:46 +03:00
'defaultValue' => $_SERVER [ 'soloquota' ],
'castTo' => 'int'
],
'herseuser' => [
'prefix' => 'hu' ,
'longPrefix' => 'herseuser' ,
'description' => 'Username for HTTP auth barrier' ,
],
'hersepass' => [
'prefix' => 'hp' ,
'longPrefix' => 'hersepass' ,
'description' => 'Password for HTTP auth barrier' ,
],
'confirm' => [
'prefix' => 'y' ,
'longPrefix' => 'yes' ,
'description' => 'Say yes to every confirmation check (no prompt)' ,
'noValue' => true ,
2022-08-16 09:46:38 +03:00
],
2023-04-20 19:28:54 +03:00
'nossl' => [
'prefix' => 'nossl' ,
'longPrefix' => 'no-ssl-certificate' ,
'description' => 'No SSL certificate and no DNS check' ,
'noValue' => true ,
],
2023-04-21 08:22:10 +03:00
'noip6' => [
'prefix' => 'noip6' ,
'longPrefix' => 'no-ip-v6' ,
'description' => 'No ip v6 DNS check' ,
'noValue' => true ,
],
2022-08-29 00:26:46 +03:00
]);
$climate -> arguments -> parse ();
$domain = $climate -> arguments -> get ( 'domain' );
if ( ! empty ( $domain ) && $domain !== 'example.com' ) {
try {
$quota = $climate -> arguments -> get ( 'quota' );
$type = $climate -> arguments -> get ( 'type' );
$confirm = $climate -> arguments -> get ( 'confirm' );
2023-04-20 19:28:54 +03:00
$nossl = $climate -> arguments -> get ( 'nossl' );
2023-04-21 08:22:10 +03:00
$noip6 = $climate -> arguments -> get ( 'noip6' );
2022-08-29 00:26:46 +03:00
$herseUser = $climate -> arguments -> get ( 'herseuser' );
$hersePass = $climate -> arguments -> get ( 'hersepass' );
2024-09-23 18:51:40 +03:00
2023-04-21 08:57:51 +03:00
$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 );
2023-04-20 19:28:54 +03:00
if ( ! $nossl ) {
2023-04-21 10:25:37 +03:00
checkIP ( $domain , $isFullDomain , $noip6 );
2023-04-20 19:28:54 +03:00
}
2022-08-29 00:26:46 +03:00
checkIfInstalled ( $domain );
$needHerse = checkHerse ( $herseUser , $hersePass );
$user = generateUserFromDomain ( $domain );
$climate -> bold () -> underline () -> out ( 'Installation of YesWiki' );
2024-09-23 18:51:40 +03:00
$climate -> out ( 'This will install a yeswiki on <bold>' . $domain . " </bold> \n " . 'model <bold>' . $type . '</bold>, <bold>' . preg_replace ( '/000000$/' , 'Gb' , $quota ) . ' quota</bold>, with the user <bold>' . $user . '</bold>.' . " \n " . ( $needHerse ? 'An herse with user <bold>' . $herseUser . '</bold> and password <bold>' . $hersePass . '</bold> will be set up.' : '' ));
2022-08-29 00:26:46 +03:00
$input = $climate -> confirm ( 'Is it all good ?' );
if ( $confirm || $input -> confirmed ()) {
$unixUser = createUnixUserWithQuota ( $user , $quota );
2022-09-06 13:38:00 +03:00
$dbUser = createSQLUserAndDatabase ( $user , $type );
2023-04-20 19:28:54 +03:00
createNginxConfig ( $domain , $user , $herseUser , $hersePass , $nossl );
2023-02-03 22:15:42 +03:00
createPhpFpmConfig ( $user );
2023-02-03 22:08:34 +03:00
if ( $needHerse ) {
2023-04-20 19:28:54 +03:00
copyYesWikiFiles ( $domain , $user , $dbUser , $herseUser , $hersePass , $nossl );
2023-02-03 22:08:34 +03:00
} else {
2023-04-20 19:28:54 +03:00
copyYesWikiFiles ( $domain , $user , $dbUser , null , null , $nossl );
2023-02-03 22:08:34 +03:00
}
2022-08-29 00:26:46 +03:00
$climate -> shout (
2023-04-20 19:28:54 +03:00
'The yeswiki was successfully installed on <bold>http' . ( $nossl ? '' : 's' ) . '://' . $domain . '</bold>, congrats ! 🎉' . " \n "
2022-08-29 00:26:46 +03:00
. ' Unix user : <bold>' . $unixUser [ 'user' ] . '</bold> with password : <bold>' . $unixUser [ 'password' ] . '</bold> was created.' . " \n "
. 'MySQL user : <bold>' . $dbUser [ 'user' ] . '</bold> with password : <bold>' . $dbUser [ 'password' ] . '</bold> was created for database <bold>' . $dbUser [ 'database' ] . '</bold>.' . " \n "
);
2024-09-23 18:51:40 +03:00
// TODO : send log, send email
2022-08-29 00:26:46 +03:00
} else {
$climate -> info ( 'Ok, let\'s stop here...' );
}
} catch ( Exception $e ) {
$climate -> error ( 'ERROR : ' . $e -> getMessage ());
}
} else {
$climate -> usage ();
}
2022-08-16 09:46:38 +03:00
} else {
2022-08-29 00:26:46 +03:00
$climate -> error ( 'ERROR : this script needs root privilege to run.' );
exit ;
2022-08-16 09:46:38 +03:00
}