*edit - encryptedFields - auto encrypt and decrypt in ConfigController + possibility to unset that field
This commit is contained in:
Ing. Petr Suchy 2020-02-07 18:20:01 +01:00
parent 23959c4151
commit ff2947c00c
No known key found for this signature in database
GPG Key ID: 5BC9AFE53BB0E8A2
2 changed files with 27 additions and 9 deletions

View File

@ -293,7 +293,7 @@ $sm_lang = array(
'email_smtp_security_none' => 'None',
'email_smtp_username' => 'SMTP username',
'email_smtp_password' => 'SMTP password',
'email_smtp_password_description' => 'Fill only to set or change.',
//'email_smtp_password_description' => '',
'email_smtp_noauth' => 'Leave blank for no authentication',
'sms_status' => 'Allow sending text messages',
'sms_gateway' => 'Gateway to use for sending messages',

View File

@ -67,7 +67,6 @@ class ConfigController extends AbstractController
'email_smtp_host',
'email_smtp_port',
'email_smtp_username',
//'email_smtp_password', // not typical input - and saved encrypted
'sms_gateway_username',
'sms_gateway_password',
'sms_from',
@ -75,6 +74,14 @@ class ConfigController extends AbstractController
'telegram_api_token',
);
/**
* Fields for saving encrypted.
* @var array
*/
protected $encryptedFields = [
'email_smtp_password'
];
private $default_tab = 'general';
public function __construct(Database $db, \Twig_Environment $twig)
@ -177,6 +184,14 @@ class ConfigController extends AbstractController
foreach ($this->fields as $input_key) {
$tpl_data[$input_key] = (isset($config[$input_key])) ? $config[$input_key] : '';
}
// encrypted fields
foreach ($this->encryptedFields as $encryptedField) {
if (true === isset($config[$encryptedField]) && trim($config[$encryptedField])) {
$tpl_data[$encryptedField] = psm_password_decrypt($config['password_encrypt_key'], $config[$encryptedField]);
} else {
$tpl_data[$encryptedField] = '';
}
}
$tpl_data[$this->default_tab . '_active'] = 'active';
@ -204,9 +219,7 @@ class ConfigController extends AbstractController
{
if (!empty($_POST)) {
// save new config
$emailSmtpPassword = filter_input(INPUT_POST, 'email_smtp_password');
$clean = array(
$clean = array(
'language' => $_POST['language'],
'sms_gateway' => $_POST['sms_gateway'],
'alert_type' => $_POST['alert_type'],
@ -218,10 +231,7 @@ class ConfigController extends AbstractController
'log_retention_period' => intval(psm_POST('log_retention_period', 365)),
'password_encrypt_key' => psm_POST('password_encrypt_key', sha1(microtime())),
);
if ($emailSmtpPassword !== null && $emailSmtpPassword !== '') {
$clean['email_smtp_password'] = psm_password_encrypt(psm_get_conf('password_encrypt_key'), $emailSmtpPassword);
}
foreach ($this->checkboxes as $input_key) {
foreach ($this->checkboxes as $input_key) {
$clean[$input_key] = (isset($_POST[$input_key])) ? '1' : '0';
}
foreach ($this->fields as $input_key) {
@ -229,6 +239,14 @@ class ConfigController extends AbstractController
$clean[$input_key] = $_POST[$input_key];
}
}
foreach ($this->encryptedFields as $encryptedField) {
$value = filter_input(INPUT_POST, $encryptedField);
if ($value !== null && $value !== '') {
$clean[$encryptedField] = psm_password_encrypt(psm_get_conf('password_encrypt_key'), $value);
} else {
$clean[$encryptedField] = '';
}
}
$language_refresh = ($clean['language'] != psm_get_conf('language'));
foreach ($clean as $key => $value) {
psm_update_conf($key, $value);