From ff2947c00c2f9956e5ba0ff34140a1871eddebc6 Mon Sep 17 00:00:00 2001 From: "Ing. Petr Suchy" Date: Fri, 7 Feb 2020 18:20:01 +0100 Subject: [PATCH] BUG #853 *edit - encryptedFields - auto encrypt and decrypt in ConfigController + possibility to unset that field --- src/lang/en_US.lang.php | 2 +- .../Config/Controller/ConfigController.php | 34 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/lang/en_US.lang.php b/src/lang/en_US.lang.php index 97c0de5f..9e80a480 100644 --- a/src/lang/en_US.lang.php +++ b/src/lang/en_US.lang.php @@ -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', diff --git a/src/psm/Module/Config/Controller/ConfigController.php b/src/psm/Module/Config/Controller/ConfigController.php index 37b71f3f..c1690a00 100644 --- a/src/psm/Module/Config/Controller/ConfigController.php +++ b/src/psm/Module/Config/Controller/ConfigController.php @@ -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);