use PhpHelper::parseArrayToPhpFile in new MysqlServer API command; show asterisk for mandatory fields in formfields; add ssl-flag for ssl-enabled ip in installation

Signed-off-by: Michael Kaufmann <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann
2022-05-02 10:00:59 +02:00
parent 00a3424f65
commit c4a2d87d70
8 changed files with 44 additions and 50 deletions

View File

@@ -159,7 +159,7 @@ if ($page == 'overview' || $page == 'emails') {
'formdata' => $email_add_data['emails_add'] 'formdata' => $email_add_data['emails_add']
]); ]);
} else { } else {
Response::standardError('noemaildomainaddedyet'); Response::standardError('emails.noemaildomainaddedyet');
} }
} }
} else { } else {

View File

@@ -29,6 +29,7 @@ use Exception;
use PDO; use PDO;
use PDOException; use PDOException;
use Froxlor\Froxlor; use Froxlor\Froxlor;
use Froxlor\PhpHelper;
use Froxlor\Api\ApiCommand; use Froxlor\Api\ApiCommand;
use Froxlor\Api\ResourceEntity; use Froxlor\Api\ResourceEntity;
use Froxlor\Database\Database; use Froxlor\Database\Database;
@@ -171,8 +172,6 @@ class MysqlServer extends ApiCommand implements ResourceEntity
throw new Exception('Cannot delete first/default mysql-server'); throw new Exception('Cannot delete first/default mysql-server');
} }
// @todo check whether the server is in use by any customer
// get all data from lib/userdata // get all data from lib/userdata
require Froxlor::getInstallDir() . "/lib/userdata.inc.php"; require Froxlor::getInstallDir() . "/lib/userdata.inc.php";
@@ -180,6 +179,12 @@ class MysqlServer extends ApiCommand implements ResourceEntity
throw new Exception('Mysql server not found', 404); throw new Exception('Mysql server not found', 404);
} }
// check whether the server is in use by any customer
$result_ms = $this->databasesOnServer(true, $dbserver);
if ($result_ms > 0) {
Response::standardError('mysqlserverstillhasdbs', '', true);
}
unset($sql_root[$dbserver]); unset($sql_root[$dbserver]);
$this->generateNewUserData($sql, $sql_root); $this->generateNewUserData($sql, $sql_root);
@@ -288,51 +293,36 @@ class MysqlServer extends ApiCommand implements ResourceEntity
* @access admin, customer * @access admin, customer
* @return string json-encoded array count * @return string json-encoded array count
*/ */
public function databasesOnServer() public function databasesOnServer(bool $internal_all = false, int $dbserver = 0)
{ {
$dbserver = $this->getParam('mysql_server'); if ($internal_all) {
$customer_ids = $this->getAllowedCustomerIds(); $result_stmt = Database::prepare("
SELECT COUNT(*) num_dbs FROM `" . TABLE_PANEL_DATABASES . "`
$result_stmt = Database::prepare(" WHERE `dbserver` = :dbserver
SELECT COUNT(*) num_dbs FROM `" . TABLE_PANEL_DATABASES . "` ");
WHERE `customerid` IN (" . implode(", ", $customer_ids) . ") AND `dbserver` = :dbserver"); $result = Database::pexecute_first($result_stmt, ['dbserver' => $dbserver], true, true);
$result = Database::pexecute_first($result_stmt, ['dbserver' => $dbserver], true, true); return (int) $result['num_dbs'];
return $this->response(['count' => $result['num_dbs']]); } else {
$dbserver = $this->getParam('mysql_server');
$customer_ids = $this->getAllowedCustomerIds();
$result_stmt = Database::prepare("
SELECT COUNT(*) num_dbs FROM `" . TABLE_PANEL_DATABASES . "`
WHERE `customerid` IN (" . implode(", ", $customer_ids) . ") AND `dbserver` = :dbserver
");
$result = Database::pexecute_first($result_stmt, ['dbserver' => $dbserver], true, true);
return $this->response(['count' => $result['num_dbs']]);
}
} }
/**
* write new userdata.inc.php file
*/
private function generateNewUserData(array $sql, array $sql_root) private function generateNewUserData(array $sql, array $sql_root)
{ {
$content = '<?php' . PHP_EOL; $content = PhpHelper::parseArrayToPhpFile(
$content .= '//automatically generated userdata.inc.php for Froxlor' . PHP_EOL; ['sql' => $sql, 'sql_root' => $sql_root],
$content .= '$sql[\'host\']=\'' . $sql['host'] . '\';' . PHP_EOL; 'automatically generated userdata.inc.php for froxlor'
$content .= '$sql[\'user\']=\'' . $sql['user'] . '\';' . PHP_EOL; );
$content .= '$sql[\'password\']=\'' . $sql['password'] . '\';' . PHP_EOL;
$content .= '$sql[\'db\']=\'' . $sql['db'] . '\';' . PHP_EOL;
foreach ($sql_root as $index => $sqlroot_data) {
$content .= '// database server #' . ($index + 1) . PHP_EOL;
foreach ($sqlroot_data as $field => $value) {
// ssl-fields
if (is_array($value)) {
foreach ($value as $vfield => $vvalue) {
if ($vfield == 'verifyServerCertificate') {
$content .= '$sql_root[' . (int)$index . '][\'' . $field . '\'][\'' . $vfield . '\'] = ' . ($vvalue ? 'true' : 'false') . ';' . PHP_EOL;
} else {
$content .= '$sql_root[' . (int)$index . '][\'' . $field . '\'][\'' . $vfield . '\'] = \'' . $vvalue . '\';' . PHP_EOL;
}
}
} else {
if ($field == 'password') {
$content .= '$sql_root[' . (int)$index . '][\'' . $field . '\'] = <<<EOP
' . $value . '
EOP;' . PHP_EOL;
} else {
$content .= '$sql_root[' . (int)$index . '][\'' . $field . '\'] = \'' . $value . '\';' . PHP_EOL;
}
}
}
}
$content .= '$sql[\'debug\']=' . ($sql['debug'] ? 'true' : 'false') . ';' . PHP_EOL;
$content .= '?>' . PHP_EOL;
file_put_contents(Froxlor::getInstallDir() . "/lib/userdata.inc.php", $content); file_put_contents(Froxlor::getInstallDir() . "/lib/userdata.inc.php", $content);
} }
} }

View File

@@ -462,13 +462,15 @@ class Core
`port` = :serverport, `port` = :serverport,
`namevirtualhost_statement` = :nvh, `namevirtualhost_statement` = :nvh,
`vhostcontainer` = '1', `vhostcontainer` = '1',
`vhostcontainer_servername_statement` = '1' `vhostcontainer_servername_statement` = '1',
`ssl` = :ssl
"); ");
$nvh = $this->validatedData['webserver'] == 'apache2' ? '1' : '0'; $nvh = $this->validatedData['webserver'] == 'apache2' ? '1' : '0';
$stmt->execute([ $stmt->execute([
'nvh' => $nvh, 'nvh' => $nvh,
'serverip' => $this->validatedData['serverip'], 'serverip' => $this->validatedData['serverip'],
'serverport' => 80 'serverport' => 80,
'ssl' => 0
]); ]);
$defaultip = $db_user->lastInsertId(); $defaultip = $db_user->lastInsertId();
@@ -477,7 +479,8 @@ class Core
$stmt->execute([ $stmt->execute([
'nvh' => $this->validatedData['webserver'] == 'apache2' ? '1' : '0', 'nvh' => $this->validatedData['webserver'] == 'apache2' ? '1' : '0',
'serverip' => $this->validatedData['serverip'], 'serverip' => $this->validatedData['serverip'],
'serverport' => 443 'serverport' => 443,
'ssl' => 1
]); ]);
$defaultsslip = $db_user->lastInsertId(); $defaultsslip = $db_user->lastInsertId();
} }

View File

@@ -210,8 +210,6 @@ class PhpHelper
public static function loadConfigArrayDir() public static function loadConfigArrayDir()
{ {
global $lng, $theme;
// we now use dynamic function parameters // we now use dynamic function parameters
// so we can read from more than one directory // so we can read from more than one directory
// and still be valid for old calls // and still be valid for old calls

View File

@@ -1158,6 +1158,7 @@ Vielen Dank, Ihr Administrator',
'title' => 'Tabellenspalten verwalten', 'title' => 'Tabellenspalten verwalten',
'description' => 'Hier können die angezeigten Tabellenspalten angepasst werden', 'description' => 'Hier können die angezeigten Tabellenspalten angepasst werden',
], ],
'mandatoryfield' => 'Pflichtfeld',
], ],
'phpfpm' => [ 'phpfpm' => [
'vhost_httpuser' => 'Lokaler Benutzer für PHP-FPM (Froxlor-Vhost)', 'vhost_httpuser' => 'Lokaler Benutzer für PHP-FPM (Froxlor-Vhost)',

View File

@@ -1498,6 +1498,7 @@ Yours sincerely, your administrator',
'title' => 'Manage table columns', 'title' => 'Manage table columns',
'description' => 'Here you can customize the visible columns', 'description' => 'Here you can customize the visible columns',
], ],
'mandatoryfield' => 'Field is mandatory',
], ],
'phpfpm' => [ 'phpfpm' => [
'vhost_httpuser' => 'Local user to use for PHP-FPM (Froxlor vHost)', 'vhost_httpuser' => 'Local user to use for PHP-FPM (Froxlor vHost)',

View File

@@ -43,5 +43,6 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
<span class="text-danger">*</span> {{ lng('panel.mandatoryfield') }}
</form> </form>
{% endmacro %} {% endmacro %}

View File

@@ -7,7 +7,7 @@
{% if em %} {% if em %}
<mark> <mark>
{% endif %} {% endif %}
{{ field.label.title|raw }} {{ field.label.title|raw }}{% if field.mandatory is defined and field.mandatory %}<span class="text-danger">*</span>{% endif %}
{% if em %} {% if em %}
</mark> </mark>
{% endif %} {% endif %}
@@ -19,7 +19,7 @@
{% if em %} {% if em %}
<mark> <mark>
{% endif %} {% endif %}
{{ field.label|raw }} {{ field.label|raw }}{% if field.mandatory is defined and field.mandatory %}<span class="text-danger">*</span>{% endif %}
{% if em %} {% if em %}
</mark> </mark>
{% endif %} {% endif %}