|
2 | 2 |
|
3 | 3 | if (!defined('DEBUG_MODE')) { die(); } |
4 | 4 |
|
| 5 | + |
| 6 | +/** |
| 7 | + * Initialize SieveService with all configured IMAP accounts if not already initialized |
| 8 | + * @param object $user_config User configuration object |
| 9 | + * @param object $cache Cache object |
| 10 | + * @return bool True if initialized successfully |
| 11 | + */ |
| 12 | +if (!hm_exists('ensure_sieve_service_initialized')) { |
| 13 | + function ensure_sieve_service_initialized($user_config, $cache) { |
| 14 | + if (SieveService::hasAccounts()) { |
| 15 | + return true; |
| 16 | + } |
| 17 | + |
| 18 | + $accounts = $user_config->get('imap_servers'); |
| 19 | + $sieve_accounts = array_filter($accounts, function ($account) { |
| 20 | + return !empty($account['sieve_config_host']); |
| 21 | + }); |
| 22 | + |
| 23 | + if (empty($sieve_accounts)) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + $sieve_accounts_configs = []; |
| 28 | + foreach ($sieve_accounts as $key => $item) { |
| 29 | + $parts = explode(':', $item['sieve_config_host']); |
| 30 | + $sieveHost = isset($parts[0]) ? $parts[0] : ''; |
| 31 | + $sievePort = isset($parts[1]) ? (int) $parts[1] : 4190; |
| 32 | + $sieve_accounts_configs[$key] = [ |
| 33 | + 'host' => $sieveHost, |
| 34 | + 'port' => $sievePort, |
| 35 | + 'username' => $item['user'], |
| 36 | + 'password' => $item['pass'], |
| 37 | + 'secure' => $item['sieve_tls'], |
| 38 | + 'authType' => 'PLAIN', |
| 39 | + 'id' => $key |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + if (!empty($sieve_accounts_configs)) { |
| 44 | + SieveService::init($cache, $sieve_accounts_configs); |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | + } |
| 50 | +} |
| 51 | + |
5 | 52 | if (!hm_exists('get_script_modal_content')) { |
6 | 53 | function get_script_modal_content() |
7 | 54 | { |
@@ -104,49 +151,22 @@ function get_classic_filter_modal_content() |
104 | 151 | if (!hm_exists('get_mailbox_filters')) { |
105 | 152 | function get_mailbox_filters($mailbox, $site_config, $user_config) |
106 | 153 | { |
| 154 | + $factory = get_sieve_client_factory($site_config); |
107 | 155 | try { |
108 | | - // Initialize SieveService with proper cache and server config |
109 | | - $servers = []; |
110 | | - foreach ($user_config->get('imap_servers') as $id => $server) { |
111 | | - if (!empty($server['sieve_config_host'])) { |
112 | | - $servers[$id] = $server; |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - // Get cache instance safely |
117 | | - $cacheInstance = null; |
118 | | - if ($site_config && method_exists($site_config, 'get')) { |
119 | | - $cacheInstance = $site_config->get('hm_cache_instance'); |
120 | | - } |
121 | | - |
122 | | - // Only initialize if we have a valid cache instance |
123 | | - if ($cacheInstance && is_object($cacheInstance)) { |
124 | | - SieveService::init($cacheInstance, $servers); |
125 | | - |
126 | | - // Get scripts using SieveService |
127 | | - $scripts = SieveService::listScripts($mailbox['id']); |
128 | | - } else { |
129 | | - // Fallback to direct client connection if no cache available |
130 | | - $factory = get_sieve_client_factory($site_config); |
131 | | - $client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true)); |
132 | | - $scripts = $client->listScripts(); |
133 | | - } |
134 | | - |
135 | | - // Filter only cypht scripts |
136 | | - $cypht_scripts = []; |
137 | | - foreach ($scripts as $script) { |
| 156 | + $client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true)); |
| 157 | + $scripts = []; |
| 158 | + foreach ($client->listScripts() as $script) { |
138 | 159 | if (mb_strstr($script, 'cypht')) { |
139 | | - $cypht_scripts[] = $script; |
| 160 | + $scripts[] = $script; |
140 | 161 | } |
141 | 162 | } |
142 | | - |
143 | 163 | } catch (Exception $e) { |
144 | 164 | Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger"); |
145 | 165 | return ['count' => 0, 'list' => '']; |
146 | 166 | } |
147 | 167 |
|
148 | 168 | $scripts_sorted = []; |
149 | | - foreach ($cypht_scripts as $script_name) { |
| 169 | + foreach ($scripts as $script_name) { |
150 | 170 | $exp_name = explode('-', $script_name); |
151 | 171 | if (end($exp_name) == 'cypht') { |
152 | 172 | $base_class = 'script'; |
@@ -198,7 +218,7 @@ function get_mailbox_filters($mailbox, $site_config, $user_config) |
198 | 218 | </tr> |
199 | 219 | '; |
200 | 220 | } |
201 | | - return ['count' => count($cypht_scripts), 'list' => $script_list]; |
| 221 | + return ['count' => count($scripts), 'list' => $script_list]; |
202 | 222 | } |
203 | 223 | } |
204 | 224 |
|
@@ -517,7 +537,6 @@ function get_blocked_senders_array($current_script, $scripts) |
517 | 537 | } |
518 | 538 | $blocked_senders = []; |
519 | 539 | if ($current_script != '') { |
520 | | - // $blocked_list = prepare_sieve_script ($current_script); |
521 | 540 | $base64_obj = str_replace("# ", "", preg_split('#\r?\n#', $current_script, 0)[1]); |
522 | 541 | $blocked_list = json_decode(base64_decode($base64_obj)); |
523 | 542 | if (!$blocked_list) { |
@@ -642,24 +661,22 @@ function initialize_sieve_client_factory($site_config, $user_config, $imapServer |
642 | 661 | if (!hm_exists('get_all_scripts')) { |
643 | 662 | function get_all_scripts($imapServer, $load_current = true, $return_only = null) { |
644 | 663 | try { |
645 | | - $client = SieveService::getConnection($imapServer); |
646 | | - if(!is_null($client)){ |
647 | | - $scripts = $client->listScripts(); |
648 | | - if (!is_array($scripts) || array_search('blocked_senders', $scripts, true) === false) { |
649 | | - return ''; |
650 | | - } |
651 | | - $current_script = ''; |
652 | | - if($load_current) { |
653 | | - $current_script = SieveService::getScript($imapServer, 'blocked_senders'); |
654 | | - } |
655 | | - if ($return_only === 'scripts') return $scripts; |
656 | | - if ($return_only === 'current_script') return $current_script; |
657 | | - |
658 | | - return [$scripts, $current_script]; |
| 664 | + $scripts = SieveService::listScripts($imapServer); |
| 665 | + if (!is_array($scripts) || array_search('blocked_senders', $scripts, true) === false) { |
| 666 | + return ''; |
659 | 667 | } |
660 | | - return null; |
| 668 | + $current_script = ''; |
| 669 | + if($load_current) { |
| 670 | + $current_script = SieveService::getScript($imapServer, 'blocked_senders'); |
| 671 | + } |
| 672 | + if ($return_only === 'scripts') return $scripts; |
| 673 | + if ($return_only === 'current_script') return $current_script; |
| 674 | + |
| 675 | + return [$scripts, $current_script]; |
661 | 676 | } catch (Exception $e) { |
662 | | - Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger"); |
| 677 | + // More specific error message to help debugging |
| 678 | + $error_msg = "Failed to get Sieve scripts for server '{$imapServer}': " . $e->getMessage(); |
| 679 | + Hm_Msgs::add("Sieve: {$error_msg}", "danger"); |
663 | 680 | return null; |
664 | 681 | } |
665 | 682 | } |
@@ -694,7 +711,9 @@ function get_sieve_linked_mailbox ($scripts, $client, $accountKey = null) { |
694 | 711 | $script = $client->getScript($s); |
695 | 712 | } |
696 | 713 | $base64_obj = str_replace("# ", "", preg_split('#\r?\n#', $script, 0)[2]); |
697 | | - $obj = json_decode(base64_decode($base64_obj))[0]; |
| 714 | + $decoded = json_decode(base64_decode($base64_obj)); |
| 715 | + |
| 716 | + $obj = $decoded === null ? null : (is_array($decoded) ? ($decoded[0] ?? null) : (is_object($decoded) ? reset(get_object_vars($decoded)) : null)); |
698 | 717 | if ($obj && in_array($obj->action, ['copy', 'move'])) { |
699 | 718 | $folders[$s] = $obj->value; |
700 | 719 | } |
|
0 commit comments