Skip to content

Commit

Permalink
don't allow duplicate servers with the same username for SMTP/IMAP/POP3
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmunro committed Apr 10, 2017
1 parent bb933c5 commit 1b81d10
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions modules/core/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,3 +556,30 @@ function start_page_opts() {
);
}

/**
* See if a host + username is already in a server list
* @param class $list class to check
* @param int $id server id to get hostname from
* @param string $user username to check for
* @return bool
*/
function in_server_list($list, $id, $user) {
$exists = false;
$server = $list::dump($id);
if (count($server) == 0) {
return false;
}
if (!array_key_exists('server', $server)) {
return false;
}
foreach ($list::dump() as $server_id => $vals) {
if ($id == $server_id) {
continue;
}
if (array_key_exists('user', $vals) && $vals['user'] == $user) {
$exists = true;
break;
}
}
return $exists;
}
5 changes: 5 additions & 0 deletions modules/imap/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,10 @@ public function process() {
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->session, $this->config, $form['imap_server_id']);
$imap = Hm_IMAP_List::connect($form['imap_server_id'], $cache, $form['imap_user'], $form['imap_pass'], true);
if ($imap->get_state() == 'authenticated') {
Expand All @@ -1314,6 +1318,7 @@ public function process() {
}
else {
Hm_Msgs::add("ERRUnable to save this server, are the username and password correct?");
Hm_IMAP_List::forget_credentials($form['imap_server_id']);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions modules/pop3/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,18 @@ public function process() {
Hm_Msgs::add('ERRUsername and Password are required to save a connection');
return;
}
if (in_server_list('Hm_POP3_List', $form['pop3_server_id'], $form['pop3_user'])) {
Hm_Msgs::add('ERRThis server and username are already configured');
return;
}
$pop3 = Hm_POP3_List::connect($form['pop3_server_id'], false, $form['pop3_user'], $form['pop3_pass'], true);
if ($pop3->state == 'authed') {
$just_saved_credentials = true;
Hm_Msgs::add("Server saved");
$this->session->record_unsaved('POP3 server saved');
}
else {
Hm_POP3_List::forget_credentials($form['pop3_server_id']);
Hm_Msgs::add("ERRUnable to save this server, are the username and password correct?");
}
}
Expand Down
5 changes: 5 additions & 0 deletions modules/smtp/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ public function process() {
Hm_Msgs::add('ERRUsername and Password are required to save a connection');
}
else {
if (in_server_list('Hm_SMTP_List', $form['smtp_server_id'], $form['smtp_user'])) {
Hm_Msgs::add('ERRThis server and username are already configured');
return;
}
$smtp = Hm_SMTP_List::connect($form['smtp_server_id'], false, $form['smtp_user'], $form['smtp_pass'], true);
if ($smtp->state == 'authed') {
$just_saved_credentials = true;
Expand All @@ -321,6 +325,7 @@ public function process() {
}
else {
Hm_Msgs::add("ERRUnable to save this server, are the username and password correct?");
Hm_SMTP_List::forget_credentials($form['smtp_server_id']);
}
}
}
Expand Down

0 comments on commit 1b81d10

Please sign in to comment.