Skip to content

Commit 6ed59df

Browse files
authored
Merge pull request #1622 from Shadow243/fix-orphaned-profiles
fix(backend): delete profile after imap or smtp server removed to avoid orphaned profiles
2 parents 003095e + 3f06166 commit 6ed59df

File tree

7 files changed

+90
-22
lines changed

7 files changed

+90
-22
lines changed

lib/framework.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/* load the framework */
1212
require APP_PATH.'lib/repository.php';
13+
require APP_PATH.'lib/searchable.php';
1314
require APP_PATH.'lib/module.php';
1415
require APP_PATH.'lib/modules.php';
1516
require APP_PATH.'lib/modules_exec.php';

lib/searchable.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
trait Searchable {
4+
/**
5+
* Return items matching $match in column $column.
6+
*
7+
* @param mixed $match
8+
* @param string $column
9+
* @param bool $returnFirst
10+
* @return array|null
11+
*/
12+
public static function getBy($match, $column = 'id', $returnFirst = false) {
13+
$results = [];
14+
foreach (static::getDataset() as $item) {
15+
if (isset($item[$column]) && $item[$column] === $match) {
16+
if ($returnFirst) {
17+
return $item;
18+
}
19+
$results[] = $item;
20+
}
21+
}
22+
return $returnFirst ? null : $results;
23+
}
24+
25+
// Each class must implement this
26+
abstract protected static function getDataset();
27+
}

lib/servers.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public static function toggle_hidden($id, $hide) {
174174
trait Hm_Server_List {
175175

176176
use Hm_Server_Modify;
177+
use Searchable;
177178
use Hm_Repository {
178179
Hm_Repository::add as repo_add;
179180
Hm_Repository::get as repo_get;
@@ -216,24 +217,11 @@ public static function dump($id = false, $full = false) {
216217
}
217218

218219
/**
219-
* Return server details matching $match in column $column.
220-
*
221-
* @param mixed $match Value to match.
222-
* @param string $column Column name to match against.
223-
* @param bool $returnFirst If true, return only the first matching server.
224-
* @return array|null Array of matches, or a single server array, or null if no match.
220+
* Get the dataset for the server list
221+
* @return array
225222
*/
226-
public static function getBy($match, $column = 'id', $returnFirst = false) {
227-
$results = [];
228-
foreach (self::$server_list as $server) {
229-
if (isset($server[$column]) && $server[$column] === $match) {
230-
if ($returnFirst) {
231-
return $server;
232-
}
233-
$results[] = $server;
234-
}
235-
}
236-
return $returnFirst ? null : $results;
223+
protected static function getDataset() {
224+
return self::$server_list;
237225
}
238226

239227
/**

modules/profiles/functions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function add_profile($name, $signature, $reply_to, $is_default, $email, $server,
99
'sig' => $signature,
1010
'rmk' => $remark,
1111
'smtp_id' => $smtp_server_id,
12+
'imap_id' => $imap_server_id,
1213
'replyto' => $reply_to,
1314
'default' => $is_default,
1415
'address' => $email,

modules/profiles/hm-profiles.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class Hm_Profiles {
1515

16-
use Hm_Repository;
16+
use Hm_Repository, Searchable;
1717

1818
private static $data = array();
1919

@@ -65,6 +65,15 @@ public static function getDefault() {
6565
return null;
6666
}
6767

68+
69+
/**
70+
* Get the dataset for the server list
71+
* @return array
72+
*/
73+
protected static function getDataset() {
74+
return self::$data;
75+
}
76+
6877
public static function createDefault($hmod) {
6978
if (! $hmod->module_is_supported('imap') || ! $hmod->module_is_supported('smtp')) {
7079
return;
@@ -84,6 +93,7 @@ public static function createDefault($hmod) {
8493
'address' => $address,
8594
'replyto' => $reply_to,
8695
'smtp_id' => $smtp_server['id'],
96+
'imap_id' => $imap_server['id'],
8797
'sig' => '',
8898
'rmk' => '',
8999
'type' => 'imap',
@@ -109,6 +119,7 @@ public static function loadLegacy($hmod) {
109119
'address' => array_key_exists('profile_address', $profile) ? $profile['profile_address'] : '',
110120
'replyto' => $profile['profile_replyto'],
111121
'smtp_id' => $profile['profile_smtp'],
122+
'imap_id' => $server['id'],
112123
'sig' => $profile['profile_sig'],
113124
'rmk' => $profile['profile_rmk'],
114125
'type' => 'imap',
@@ -120,10 +131,10 @@ public static function loadLegacy($hmod) {
120131
}
121132

122133
/**
123-
* @param string $field The name of the field to search within.
124-
* @param mixed $value The value to search for within the specified field.
125-
* @return array An array containing profiles that match the search criteria.
126-
*/
134+
* @param string $field The name of the field to search within.
135+
* @param mixed $value The value to search for within the specified field.
136+
* @return array An array containing profiles that match the search criteria.
137+
*/
127138
public static function search($field, $value) {
128139
$res = array();
129140
foreach (self::getAll() as $profile) {

modules/profiles/modules.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,44 @@ public function process() {
5656
}
5757
}
5858

59+
/**
60+
* @subpackage profile/handler
61+
*/
62+
class Hm_Handler_process_smtp_server_data_delete extends Hm_Handler_Module {
63+
public function process() {
64+
if (isset($this->request->post['smtp_delete'])) {
65+
$deleted_server_id = $this->get('deleted_server_id', '');
66+
Hm_Profiles::init($this);
67+
$servers = Hm_Profiles::getBy($deleted_server_id,'smtp_id');
68+
if ($servers) {
69+
foreach ($servers as $server) {
70+
Hm_Profiles::del($server['id']);
71+
Hm_Msgs::add("Profile {$server['name']} has been deleted since its server was removed.");
72+
}
73+
}
74+
}
75+
}
76+
}
77+
78+
/**
79+
* @subpackage profile/handler
80+
*/
81+
class Hm_Handler_process_imap_server_data_delete extends Hm_Handler_Module {
82+
public function process() {
83+
if (isset($this->request->post['imap_delete'])) {
84+
$deleted_server_id = $this->get('deleted_server_id', '');
85+
Hm_Profiles::init($this);
86+
$servers = Hm_Profiles::getBy($deleted_server_id,'imap_id');
87+
if ($servers) {
88+
foreach ($servers as $server) {
89+
Hm_Profiles::del($server['id']);
90+
Hm_Msgs::add("Profile {$server['name']} has been deleted since its server was removed.");
91+
}
92+
}
93+
}
94+
}
95+
}
96+
5997
/**
6098
* @subpackage profile/handler
6199
*/

modules/profiles/setup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
add_handler('ajax_smtp_save_draft', 'compose_profile_data', true, 'profiles', 'load_smtp_servers_from_config', 'after');
2525
add_handler('ajax_smtp_attach_file', 'compose_profile_data', true, 'profiles', 'load_smtp_servers_from_config', 'after');
2626
add_handler('servers', 'compose_profile_data', true, 'profiles', 'load_smtp_servers_from_config', 'after');
27+
add_handler('ajax_smtp_debug', 'process_smtp_server_data_delete', true, 'profiles','smtp_delete', 'after');
28+
add_handler('ajax_imap_debug', 'process_imap_server_data_delete', true, 'profiles','imap_delete', 'after');
2729

2830
return array(
2931
'allowed_pages' => array(

0 commit comments

Comments
 (0)