From d0eaf12b3403156953172817cb5d591f99b94f85 Mon Sep 17 00:00:00 2001 From: Michael Kaufmann Date: Thu, 5 May 2022 17:26:28 +0200 Subject: [PATCH] fix checkbox-value for installation; fix session-initialization; preselect various guessable fields to ease installation Signed-off-by: Michael Kaufmann --- install/install.php | 26 +++++++----- lib/Froxlor/Install/Install.php | 44 +++++++++++++++++--- lib/formfields/install/formfield.install.php | 13 +++--- templates/Froxlor/form/formfields.html.twig | 2 +- 4 files changed, 63 insertions(+), 22 deletions(-) diff --git a/install/install.php b/install/install.php index d01ddbec..fe47b30b 100644 --- a/install/install.php +++ b/install/install.php @@ -23,6 +23,7 @@ * @license https://files.froxlor.org/misc/COPYING.txt GPLv2 */ +use Froxlor\UI\Panel\UI; use Froxlor\Install\Install; // define default theme for configurehint, etc. @@ -31,20 +32,20 @@ $_deftheme = 'Froxlor'; // validate correct php version if (version_compare("7.4.0", PHP_VERSION, ">=")) { die(view($_deftheme . '/misc/phprequirementfailed.html.twig', [ - '{{ basehref }}' => '../', - '{{ froxlor_min_version }}' => '7.4.0', - '{{ current_version }}' => PHP_VERSION, - '{{ current_year }}' => date('Y', time()), - ])); + '{{ basehref }}' => '../', + '{{ froxlor_min_version }}' => '7.4.0', + '{{ current_version }}' => PHP_VERSION, + '{{ current_year }}' => date('Y', time()), + ])); } // validate vendor autoloader if (!file_exists(dirname(__DIR__) . '/vendor/autoload.php')) { die(view($_deftheme . '/misc/vendormissinghint.html.twig', [ - '{{ basehref }}' => '../', - '{{ froxlor_install_dir }}' => dirname(__DIR__), - '{{ current_year }}' => date('Y', time()), - ])); + '{{ basehref }}' => '../', + '{{ froxlor_install_dir }}' => dirname(__DIR__), + '{{ current_year }}' => date('Y', time()), + ])); } // check installation status @@ -54,8 +55,13 @@ if (file_exists(dirname(__DIR__) . '/lib/userdata.inc.php')) { } require dirname(__DIR__) . '/vendor/autoload.php'; -require dirname(__DIR__) . '/lib/functions.php'; require dirname(__DIR__) . '/lib/tables.inc.php'; +// init twig +UI::initTwig(true); +UI::sendHeaders(); + +require dirname(__DIR__) . '/lib/functions.php'; + $installer = new Install(); $installer->handle(); diff --git a/lib/Froxlor/Install/Install.php b/lib/Froxlor/Install/Install.php index 2316b7f4..50589494 100644 --- a/lib/Froxlor/Install/Install.php +++ b/lib/Froxlor/Install/Install.php @@ -67,6 +67,10 @@ class Install // sort by distribution name asort($this->supportedOS); + // guess distribution and webserver to preselect in formfield + $guessedDistribution = $this->guessDistribution(); + $guessedWebserver = $this->guessWebserver(); + // set formfield, so we can get the fields and steps etc. $this->formfield = require dirname(__DIR__, 3) . '/lib/formfields/install/formfield.install.php'; @@ -78,10 +82,6 @@ class Install $this->phpVersion = phpversion(); $this->loadedExtensions = get_loaded_extensions(); - // init twig - UI::initTwig(true); - UI::sendHeaders(); - // set global variables UI::twig()->addGlobal('install_mode', true); UI::twig()->addGlobal('basehref', '../'); @@ -155,6 +155,7 @@ class Install if ($this->currentStep == ($this->maxSteps - 1)) { $core = new Core($_SESSION['installation']); $core->doInstall(); + // @todo no going back after this point! } // redirect user to home if the installation is done @@ -271,7 +272,7 @@ class Install if (!preg_match('/^[^\r\n\t\f\0]*$/D', $name)) { throw new Exception(lng('error.stringformaterror', ['admin_name'])); - } elseif (empty(trim($loginname)) || !preg_match('/^[a-z][a-z0-9]', $loginname)) { + } elseif (empty(trim($loginname)) || !preg_match('/^[a-z][a-z0-9]+$/', $loginname)) { throw new Exception(lng('error.loginnameiswrong', [$loginname])); } elseif (empty(trim($email)) || !Validate::validateEmail($email)) { throw new Exception(lng('error.emailiswrong', [$email])); @@ -332,4 +333,37 @@ class Install // @todo build and set $validatedData['mysql_access_host'] } + + private function guessWebserver(): ?string + { + if (strtoupper(@php_sapi_name()) == "APACHE2HANDLER" || stristr($_SERVER['SERVER_SOFTWARE'], "apache/2")) { + return 'apache24'; + } elseif (substr(strtoupper(@php_sapi_name()), 0, 8) == "LIGHTTPD" || stristr($_SERVER['SERVER_SOFTWARE'], "lighttpd")) { + return 'lighttpd'; + } elseif (substr(strtoupper(@php_sapi_name()), 0, 8) == "NGINX" || stristr($_SERVER['SERVER_SOFTWARE'], "nginx")) { + return 'nginx'; + } + return null; + } + + private function guessDistribution(): ?string + { + // set default os. + $os_dist = array( + 'VERSION_CODENAME' => 'bullseye' + ); + // read os-release + if (@file_exists('/etc/os-release')) { + $os_dist_content = file_get_contents('/etc/os-release'); + $os_dist_arr = explode("\n", $os_dist_content); + $os_dist = []; + foreach ($os_dist_arr as $os_dist_line) { + if (empty(trim($os_dist_line))) continue; + $tmp = explode("=", $os_dist_line); + $os_dist[$tmp[0]] = str_replace('"', "", trim($tmp[1])); + } + return strtolower($os_dist['VERSION_CODENAME']); + } + return null; + } } diff --git a/lib/formfields/install/formfield.install.php b/lib/formfields/install/formfield.install.php index 10855be3..f73e714e 100644 --- a/lib/formfields/install/formfield.install.php +++ b/lib/formfields/install/formfield.install.php @@ -132,7 +132,7 @@ return [ 'admin_email' => [ 'label' => lng('customer.email'), 'placeholder' => lng('customer.email'), - 'type' => 'text', + 'type' => 'email', 'mandatory' => true, 'value' => old('admin_email', null, 'installation'), ], @@ -147,20 +147,21 @@ return [ 'type' => 'select', 'mandatory' => true, 'select_var' => $this->supportedOS, + 'selected' => $guessedDistribution ], 'serverip' => [ 'label' => lng('serversettings.ipaddress.title'), 'placeholder' => lng('serversettings.ipaddress.title'), 'type' => 'text', 'mandatory' => true, - 'value' => old('serverip', null, 'installation'), + 'value' => old('serverip', $_SERVER['SERVER_ADDR'] ?? null, 'installation'), ], 'servername' => [ 'label' => lng('install.system.servername'), 'placeholder' => lng('install.system.servername'), 'type' => 'text', 'mandatory' => true, - 'value' => old('servername', null, 'installation'), + 'value' => old('servername', filter_var($_SERVER['SERVER_NAME'] ?? "", FILTER_VALIDATE_IP) ? null : $_SERVER['SERVER_NAME'], 'installation'), ], 'use_ssl' => [ 'label' => lng('serversettings.ssl.use_ssl.title'), @@ -173,7 +174,7 @@ return [ 'type' => 'select', 'mandatory' => true, 'select_var' => ['apache24' => 'Apache 2.4', 'nginx' => 'Nginx', 'lighttpd' => 'LigHTTPd'], - 'value' => old('webserver', 'apache24', 'installation'), + 'value' => old('webserver', $guessedWebserver, 'installation'), ], 'webserver_backend' => [ 'label' => lng('install.system.phpbackend'), @@ -187,14 +188,14 @@ return [ 'placeholder' => lng('admin.webserver_user'), 'type' => 'text', 'mandatory' => true, - 'value' => old('httpuser', posix_getpwuid(posix_getuid()), 'installation'), + 'value' => old('httpuser', posix_getpwuid(posix_getuid())['name'] ?? '', 'installation'), ], 'httpgroup' => [ 'label' => lng('admin.webserver_group'), 'placeholder' => lng('admin.webserver_group'), 'type' => 'text', 'mandatory' => true, - 'value' => old('httpgroup', posix_getgrgid(posix_getgid()), 'installation'), + 'value' => old('httpgroup', posix_getgrgid(posix_getgid())['name'] ?? '', 'installation'), ], 'activate_newsfeed' => [ 'label' => lng('install.system.activate_newsfeed'), diff --git a/templates/Froxlor/form/formfields.html.twig b/templates/Froxlor/form/formfields.html.twig index 318d8b8a..22e44e8b 100644 --- a/templates/Froxlor/form/formfields.html.twig +++ b/templates/Froxlor/form/formfields.html.twig @@ -71,7 +71,7 @@ {% else %}
- +
{% endif %}