Skip to content

Commit

Permalink
Merge pull request #1311 from josaphatim/removed-save-forgot-server-b…
Browse files Browse the repository at this point in the history
…uttons

Removed forget/save actions
  • Loading branch information
kroky authored Nov 4, 2024
2 parents 77fdde8 + b34489d commit 508db58
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 298 deletions.
124 changes: 10 additions & 114 deletions modules/imap/handler_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1862,133 +1862,29 @@ class Hm_Handler_imap_connect extends Hm_Handler_Module {
*/
public function process() {
if (isset($this->request->post['imap_connect'])) {
list($success, $form) = $this->process_form(array('imap_user', 'imap_pass', 'imap_server_id'));

$sieve_enabled = false;
if ($this->module_is_supported('sievefilters') && $this->user_config->get('enable_sieve_filter_setting', DEFAULT_ENABLE_SIEVE_FILTER)) {
if (!isset($this->request->post['imap_sieve_host'])) {
foreach ($this->get('imap_servers', array()) as $index => $vals) {
if ($index == $form['imap_server_id']) {
$selected_imap = $vals;
break;
}
}
if (isset($selected_imap['sieve_config_host'])) {
$sieve_enabled = true;
$sieve_hostname = $selected_imap['sieve_config_host'];
}
} else {
$sieve_enabled = true;
$sieve_hostname = $this->request->post['imap_sieve_host'];
}
if ($sieve_enabled) {
list($success, $form) = $this->process_form(array('imap_server_id'));
$imap_details = Hm_IMAP_List::dump($form['imap_server_id'], true);
if ($success && $imap_details) {
if ($this->module_is_supported('sievefilters') && $this->user_config->get('enable_sieve_filter_setting', DEFAULT_ENABLE_SIEVE_FILTER)) {
try {
list($sieve_host, $sieve_port, $sieve_tls) = parse_sieve_config_host($sieve_hostname);
list($sieve_host, $sieve_port) = parse_sieve_config_host($imap_details['sieve_config_host']);
$client = new \PhpSieveManager\ManageSieve\Client($sieve_host, $sieve_port);
$client->connect($form['imap_user'], $form['imap_pass'], $sieve_tls, "", "PLAIN");
$client->connect($imap_details['user'], $imap_details['pass'], $imap_details['sieve_tls'], "", "PLAIN");
} catch (Exception $e) {
Hm_Msgs::add("ERRFailed to authenticate to the Sieve host");
return;
}
}
}

$imap = false;
$cache = Hm_IMAP_List::get_cache($this->cache, $form['imap_server_id']);
if ($success) {
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache, $form['imap_user'], $form['imap_pass']);
}
elseif (isset($form['imap_server_id'])) {
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache);
}
if ($imap) {
if ($imap->get_state() == 'authenticated') {
Hm_Msgs::add(sprintf("Successfully authenticated to the %s server : %s", $imap->server_type, $form['imap_user']));
}
else {
Hm_Msgs::add(sprintf("ERRFailed to authenticate to the %s server : %s", $imap->server_type, $form['imap_user']));
}
}
else {
Hm_Msgs::add('ERRUsername and password are required');
$this->out('old_form', $form);
}
}
}
}

/**
* Forget IMAP server credentials
* @subpackage imap/handler
*/
class Hm_Handler_imap_forget extends Hm_Handler_Module {
/**
* Used on the servers page to forget login information for an IMAP server
*/
public function process() {
$just_forgot_credentials = false;
if (isset($this->request->post['imap_forget'])) {
list($success, $form) = $this->process_form(array('imap_server_id'));
if ($success) {
Hm_IMAP_List::forget_credentials($form['imap_server_id']);
$just_forgot_credentials = true;
Hm_Msgs::add('Server credentials forgotten');
$this->session->record_unsaved('IMAP server credentials forgotten');
}
else {
$this->out('old_form', $form);
}
}
$this->out('just_forgot_credentials', $just_forgot_credentials);
}
}

/**
* Save a user/pass combination for an IMAP server
* @subpackage imap/handler
*/
class Hm_Handler_imap_save extends Hm_Handler_Module {
/**
* Authenticate then save the username and password for an IMAP server
*/
public function process() {
$just_saved_credentials = false;
if (isset($this->request->post['imap_save'])) {
list($success, $form) = $this->process_form(array('imap_user', 'imap_pass', 'imap_server_id'));

if (isset($this->request->post['imap_sieve_host'])) {
try {
list($sieve_host, $sieve_port, $sieve_tls) = parse_sieve_config_host($this->request->post['imap_sieve_host']);
$client = new \PhpSieveManager\ManageSieve\Client($sieve_host, $sieve_port);
$client->connect($form['imap_user'], $form['imap_pass'], $sieve_tls, "", "PLAIN");
} catch (Exception $e) {
Hm_Msgs::add("ERRFailed to authenticate to the Sieve host");
return;
}
}

if (!$success) {
Hm_Msgs::add('ERRUsername and Password are required to save a connection');
}
else {
if (in_server_list('Hm_IMAP_List', $form['imap_server_id'], $form['imap_user'])) {
Hm_Msgs::add('ERRThis server and username are already configured');
return;
}
$cache = Hm_IMAP_List::get_cache($this->cache, $form['imap_server_id']);
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache, $form['imap_user'], $form['imap_pass'], true);
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache, $imap_details['user'], $imap_details['pass']);
if (imap_authed($imap)) {
$just_saved_credentials = true;
Hm_Msgs::add("Server saved");
$this->session->record_unsaved(sprintf('%s server saved', $imap->server_type));
}
else {
Hm_Msgs::add("ERRUnable to save this server, are the username and password correct? " . $form['imap_user']);
Hm_IMAP_List::forget_credentials($form['imap_server_id']);
Hm_Msgs::add(sprintf("Successfully authenticated to the %s server : %s", $imap->server_type, $imap_details['user']));
return;
}
}
Hm_Msgs::add("ERRFailed to authenticate to IMAP server");
}
$this->out('just_saved_credentials', $just_saved_credentials);
}
}

Expand Down
4 changes: 0 additions & 4 deletions modules/imap/output_modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ protected function output() {
$disabled = isset($vals['default']) ? ' disabled': '';
if (!isset($vals['user']) || !$vals['user']) {
$res .= '<input type="submit" value="'.$this->trans('Delete').'" class="imap_delete btn btn-outline-danger btn-sm me-2 mt-3" />';
$res .= '<input type="submit" value="'.$this->trans('Save').'" class="save_imap_connection btn btn-primary btn-sm me-2 mt-3" />';
} else {
$keysToRemove = array('object', 'connected', 'default', 'nopass');
$serverDetails = array_diff_key($vals, array_flip($keysToRemove));
Expand All @@ -532,7 +531,6 @@ protected function output() {
$res .= '<input type="submit" value="'.$this->trans('Edit').'" class="edit_server_connection btn btn-outline-success btn-sm me-2 mt-3"'.$disabled.' data-server-details=\''.$this->html_safe(json_encode($serverDetails)).'\' data-id="'.$this->html_safe($serverDetails['name']).'" data-type="'.$type.'" />';
$res .= '<input type="submit" value="'.$this->trans('Test').'" class="test_imap_connect btn btn-outline-primary btn-sm me-2 mt-3" />';
$res .= '<input type="submit" value="'.$this->trans('Delete').'" class="imap_delete btn btn-outline-danger btn-sm me-2 mt-3"'.$disabled.' />';
$res .= '<input type="submit" value="'.$this->trans('Forget').'" class="forget_imap_connection btn btn-outline-warning btn-sm me-2 mt-3"'.$disabled.' />';
}

// Hide/Unhide Buttons
Expand Down Expand Up @@ -700,11 +698,9 @@ protected function output() {
// Buttons
if (!isset($vals['user']) || !$vals['user']) {
$res .= '<input type="submit" value="'.$this->trans('Delete').'" class="btn btn-outline-danger btn-sm imap_delete me-2" />';
$res .= '<input type="submit" value="'.$this->trans('Save').'" class="btn btn-outline-success btn-sm save_imap_connection me-2" />';
} else {
$res .= '<input type="submit" value="'.$this->trans('Test').'" class="btn btn-primary btn-sm test_imap_connect me-2" />';
$res .= '<input type="submit" value="'.$this->trans('Delete').'" class="btn btn-danger btn-sm imap_delete me-2" />';
$res .= '<input type="submit" value="'.$this->trans('Forget').'" class="btn btn-outline-warning btn-sm forget_imap_connection me-2" />';
}

// Hide/Unhide Button Logic
Expand Down
4 changes: 0 additions & 4 deletions modules/imap/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@
add_handler('ajax_imap_debug', 'imap_hide', true);
add_handler('ajax_imap_debug', 'imap_connect', true);
add_handler('ajax_imap_debug', 'imap_delete', true);
add_handler('ajax_imap_debug', 'imap_forget', true);
add_handler('ajax_imap_debug', 'imap_save', true);
add_handler('ajax_imap_debug', 'save_imap_cache', true);
add_handler('ajax_imap_debug', 'save_imap_servers', true);
add_handler('ajax_imap_debug', 'save_user_data', true, 'core');
Expand Down Expand Up @@ -405,8 +403,6 @@
'imap_connect' => FILTER_DEFAULT,
'imap_remember' => FILTER_VALIDATE_INT,
'imap_folder_ids' => FILTER_DEFAULT,
'imap_forget' => FILTER_DEFAULT,
'imap_save' => FILTER_DEFAULT,
'submit_imap_server' => FILTER_DEFAULT,
'submit_jmap_server' => FILTER_DEFAULT,
'new_jmap_address' => FILTER_SANITIZE_URL,
Expand Down
52 changes: 3 additions & 49 deletions modules/imap/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,69 +57,23 @@ var imap_unhide = function(event) {
imap_hide_action(form, server_id, 0);
};

var imap_forget_action = function(event) {
event.preventDefault();
Hm_Notices.hide(true);
var form = $(this).closest('.imap_connect');
var btnContainer = $(this).parent();
Hm_Ajax.request(
form.serializeArray(),
function(res) {
if (res.just_forgot_credentials) {
form.find('.credentials').prop('disabled', false);
form.find('.credentials').val('');
btnContainer.append('<input type="submit" value="Save" class="save_imap_connection btn btn-outline-secondary btn-sm me-2" />');
$('.save_imap_connection').on('click', imap_save_action);
$('.forget_imap_connection', form).hide();
Hm_Utils.set_unsaved_changes(1);
Hm_Folders.reload_folders(true);
}
},
{'imap_forget': 1}
);
};

var imap_save_action = function(event) {
event.preventDefault();
Hm_Notices.hide(true);
var form = $(this).closest('.imap_connect');
var btnContainer = $(this).parent();
Hm_Ajax.request(
form.serializeArray(),
function(res) {
if (res.just_saved_credentials) {
form.find('.credentials').attr('disabled', true);
form.find('.save_imap_connection').hide();
form.find('.imap_password').val('');
form.find('.imap_password').attr('placeholder', '[saved]');
btnContainer.append('<input type="submit" value="Forget" class="forget_imap_connection btn btn-outline-warning btn-sm me-2" />');
$('.forget_imap_connection').on('click', imap_forget_action);
Hm_Utils.set_unsaved_changes(1);
Hm_Folders.reload_folders(true);
}
},
{'imap_save': 1}
);
};

var imap_test_action = function(event) {
var imap_test_action = function(event) {
$('.imap_folder_data').empty();
event.preventDefault();
Hm_Notices.hide(true);
var form = $(this).closest('.imap_connect');
Hm_Ajax.request(
form.serializeArray(),
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_debug'},
{'name': 'imap_server_id', 'value': $('.imap_server_id', form).val()}],
false,
{'imap_connect': 1}
);
}

var imapServersPageHandler = function() {
$('.imap_delete').on('click', imap_delete_action);
$('.save_imap_connection').on('click', imap_save_action);
$('.hide_imap_connection').on('click', imap_hide);
$('.unhide_imap_connection').on('click', imap_unhide);
$('.forget_imap_connection').on('click', imap_forget_action);
$('.test_imap_connect').on('click', imap_test_action);

var dsp = Hm_Utils.get_from_local_storage('.imap_section');
Expand Down
8 changes: 4 additions & 4 deletions modules/imap_folders/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ public function process() {
$imap_account = $imap_servers[$form['imap_server_id']];
$linked_mailboxes = get_sieve_linked_mailbox($imap_account, $this);
if ($linked_mailboxes && in_array($old_folder, $linked_mailboxes)) {
list($sieve_host, $sieve_port, $sieve_tls) = parse_sieve_config_host($imap_account['sieve_config_host']);
list($sieve_host, $sieve_port) = parse_sieve_config_host($imap_account['sieve_config_host']);
try {
$client = new \PhpSieveManager\ManageSieve\Client($sieve_host, $sieve_port);
$client->connect($imap_account['user'], $imap_account['pass'], $sieve_tls, "", "PLAIN");
$client->connect($imap_account['user'], $imap_account['pass'], $imap_account['sieve_tls'], "", "PLAIN");
$script_names = array_filter(
$linked_mailboxes,
function ($value) use($old_folder) {
Expand Down Expand Up @@ -831,10 +831,10 @@ function get_sieve_linked_mailbox ($imap_account, $module) {
if (!$module->module_is_supported('sievefilters') && $module->user_config->get('enable_sieve_filter_setting', DEFAULT_ENABLE_SIEVE_FILTER)) {
return;
}
list($sieve_host, $sieve_port, $sieve_tls) = parse_sieve_config_host($imap_account['sieve_config_host']);
list($sieve_host, $sieve_port) = parse_sieve_config_host($imap_account['sieve_config_host']);
$client = new \PhpSieveManager\ManageSieve\Client($sieve_host, $sieve_port);
try {
$client->connect($imap_account['user'], $imap_account['pass'], $sieve_tls, "", "PLAIN");
$client->connect($imap_account['user'], $imap_account['pass'], $imap_account['sieve_tls'], "", "PLAIN");
$scripts = $client->listScripts();
$folders = [];
foreach ($scripts as $s) {
Expand Down
Loading

0 comments on commit 508db58

Please sign in to comment.