| 1: | <?php |
| 2: | |
| 3: | if (!defined('DEBUG_MODE')) { die(); } |
| 4: | |
| 5: | if (!hm_exists('get_script_modal_content')) { |
| 6: | function get_script_modal_content() |
| 7: | { |
| 8: | return '<div id="edit_script_modal" class="d-none"> |
| 9: | <div class="mb-2"> |
| 10: | <h3 class="mb-1">General</h3> |
| 11: | <small>Input a name and order for your filter. In filters, the order of execution is important. You can define an order value (or priority value) for your filter. Filters will run from lowest to highest priority value.</small> |
| 12: | </div> |
| 13: | <div class="mb-2 mt-4"> |
| 14: | <label for="sieve-script-name" class="form-label fw-bold">Filter Name:</label> |
| 15: | <input class="modal_sieve_script_name form-control" type="text" placeholder="Your filter name" id="sieve-script-name" /> |
| 16: | </div> |
| 17: | <div class="mb-2"> |
| 18: | <label for="sieve-script-priority" class="form-label fw-bold">Priority:</label> |
| 19: | <input class="modal_sieve_script_priority form-control" type="number" placeholder="0" id="sieve-script-priority"" /> |
| 20: | </div> |
| 21: | <div class="mb-2"> |
| 22: | <h3 class="mb-1">Sieve Script</h3> |
| 23: | <small>Paste the Sieve script in the field below. Manually added scripts cannot be edited with the filters interface.</small> |
| 24: | </div> |
| 25: | <div class="mb-2 mt-4"> |
| 26: | <textarea rows="20" class="modal_sieve_script_textarea form-control"></textarea> |
| 27: | </div> |
| 28: | </div>'; |
| 29: | } |
| 30: | } |
| 31: | |
| 32: | |
| 33: | if (!hm_exists('get_classic_filter_modal_content')) { |
| 34: | function get_classic_filter_modal_content() |
| 35: | { |
| 36: | return '<div id="edit_filter_modal" class="d-none"> |
| 37: | <div class="mb-2"> |
| 38: | <h3 class="mb-1">General</h3> |
| 39: | <small>Input a name and order for your filter. In filters, the order of execution is important. You can define an order value (or priority value) for your filter. Filters will run from lowest to highest priority value.</small> |
| 40: | </div> |
| 41: | <div class="mb-2 mt-4"> |
| 42: | <label for="sieve-filter-name" class="form-label fw-bold">Filter Name:</label> |
| 43: | <input type="text" class="modal_sieve_filter_name form-control" placeholder="Your filter name" id="sieve-filter-name" /> |
| 44: | </div> |
| 45: | <div class="mb-2"> |
| 46: | <label for="sieve-filter-priority" class="form-label fw-bold">Priority:</label> |
| 47: | <input class="modal_sieve_filter_priority form-control" type="number" placeholder="0" id="sieve-filter-priority" /> |
| 48: | </div> |
| 49: | <div class="mb-2"> |
| 50: | <label for="sieve-filter-test" class="form-label fw-bold">Test:</label> |
| 51: | <select class="modal_sieve_filter_test form-control" name="test_type" placeholder="0" id="sieve-filter-test"> |
| 52: | <option value="ANYOF">ANYOF (OR)</option> |
| 53: | <option value="ALLOF" selected>ALLOF (AND)</option> |
| 54: | </select> |
| 55: | </div> |
| 56: | <div class="d-block mb-2 mt-4"> |
| 57: | <h3 class="mb-1">Conditions & Actions</h3> |
| 58: | <small>Filters must have at least one action and one condition</small> |
| 59: | </div> |
| 60: | <div class="mt-2 rounded card"> |
| 61: | <div class="p-3"> |
| 62: | <div class="d-flex"> |
| 63: | <div class="col-sm-10"> |
| 64: | <h5 class="mt-0">Conditions</h5> |
| 65: | </div> |
| 66: | <div class="flex-grow-1 text-end"> |
| 67: | <button class="sieve_add_condition_modal_button btn btn-sm border btn-primary">Add Condition</button> |
| 68: | </div> |
| 69: | </div> |
| 70: | <div class="d-block mt-3 table-responsive"> |
| 71: | <table class="sieve_list_conditions_modal table"> |
| 72: | </table> |
| 73: | </div> |
| 74: | </div> |
| 75: | <hr/> |
| 76: | <div class="p-3"> |
| 77: | <div class="d-flex"> |
| 78: | <div class="col-sm-10"> |
| 79: | <h5 class="mt-0">Actions</h5> |
| 80: | </div> |
| 81: | <div class="flex-grow-1 text-end"> |
| 82: | <button class="filter_modal_add_action_btn btn btn-sm border btn-primary">Add Action</button> |
| 83: | </div> |
| 84: | </div> |
| 85: | <div class="d-block mt-3 table-responsive"> |
| 86: | <table class="filter_actions_modal_table table"> |
| 87: | </table> |
| 88: | </div> |
| 89: | </div> |
| 90: | <hr/> |
| 91: | <div class="p-3"> |
| 92: | <div class="d-flex"> |
| 93: | <div class="col-sm-10"> |
| 94: | <input type="checkbox" id="stop_filtering"/> |
| 95: | <label for="stop_filtering" class="form-label">Stop filtering</label> |
| 96: | </div> |
| 97: | </div> |
| 98: | </div> |
| 99: | </div> |
| 100: | </div>'; |
| 101: | } |
| 102: | } |
| 103: | |
| 104: | if (!hm_exists('get_mailbox_filters')) { |
| 105: | function get_mailbox_filters($mailbox, $site_config, $user_config) |
| 106: | { |
| 107: | $factory = get_sieve_client_factory($site_config); |
| 108: | try { |
| 109: | $client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true)); |
| 110: | $scripts = []; |
| 111: | foreach ($client->listScripts() as $script) { |
| 112: | if (mb_strstr($script, 'cypht')) { |
| 113: | $scripts[] = $script; |
| 114: | } |
| 115: | } |
| 116: | } catch (Exception $e) { |
| 117: | Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger"); |
| 118: | return ['count' => 0, 'list' => '']; |
| 119: | } |
| 120: | |
| 121: | $scripts_sorted = []; |
| 122: | foreach ($scripts as $script_name) { |
| 123: | $exp_name = explode('-', $script_name); |
| 124: | if (end($exp_name) == 'cypht') { |
| 125: | $base_class = 'script'; |
| 126: | } |
| 127: | else if (end($exp_name) == 'cyphtfilter') { |
| 128: | $base_class = 'filter'; |
| 129: | } |
| 130: | else { |
| 131: | continue; |
| 132: | } |
| 133: | $parsed_name = str_replace('_', ' ', $exp_name[0]); |
| 134: | $scripts_sorted[$script_name] = $exp_name[sizeof($exp_name) - 2]; |
| 135: | } |
| 136: | asort($scripts_sorted); |
| 137: | |
| 138: | $script_list = ''; |
| 139: | foreach ($scripts_sorted as $script_name => $sc) { |
| 140: | $exp_name = explode('-', $script_name); |
| 141: | $parsed_name = str_replace('_', ' ', $exp_name[0]); |
| 142: | |
| 143: | if (count($exp_name) > 3) { |
| 144: | $name_parts = array_slice($exp_name, 0, count($exp_name) - 2); |
| 145: | $parsed_name = str_replace('_', ' ', implode('-', $name_parts)); |
| 146: | } |
| 147: | |
| 148: | $display_name = str_replace('_', ' ', implode('-', array_slice($exp_name, 0, count($exp_name) - 2))); |
| 149: | $checked = ' checked'; |
| 150: | if (preg_match('/^s(en|dis)abled/', $display_name)) { |
| 151: | $display_name = str_replace(['senabled ', 'sdisabled '], '', $display_name); |
| 152: | if (strpos($display_name, 'sdisabled') === 0) { |
| 153: | $checked = ''; |
| 154: | } |
| 155: | } |
| 156: | $script_list .= ' |
| 157: | <tr> |
| 158: | <td>'. $exp_name[sizeof($exp_name) - 2] .'</td> |
| 159: | <td>' . str_replace('_', ' ', implode('-', array_slice($exp_name, 0, count($exp_name) - 2))) . '</td> |
| 160: | <td> |
| 161: | <span class="form-switch"> |
| 162: | <input script_name_parsed="'.$parsed_name.'" priority="'.$exp_name[sizeof($exp_name) - 2].'" imap_account="'.$mailbox['name'].'" script_name="'.$script_name.'" class="toggle_filter form-check-input" type="checkbox" role="switch" id="Check" name="script_state"'.$checked.'> |
| 163: | </span> |
| 164: | <a href="#" script_name_parsed="'.$parsed_name.'" priority="'.$exp_name[sizeof($exp_name) - 2].'" imap_account="'.$mailbox['name'].'" script_name="'.$script_name.'" class="edit_'.$base_class.'"> |
| 165: | <i class="bi bi-pencil-fill"></i> |
| 166: | </a> |
| 167: | <a href="#" script_name_parsed="'.$parsed_name.'" priority="'.$exp_name[sizeof($exp_name) - 2].'" imap_account="'.$mailbox['name'].'" script_name="'.$script_name.'" class="delete_'.$base_class.' ps-2"> |
| 168: | <i class="bi bi-trash3 text-danger"></i> |
| 169: | </a> |
| 170: | </td> |
| 171: | </tr> |
| 172: | '; |
| 173: | } |
| 174: | return ['count' => count($scripts), 'list' => $script_list]; |
| 175: | } |
| 176: | } |
| 177: | |
| 178: | if (!hm_exists('generate_main_script')) { |
| 179: | function generate_main_script($script_list) |
| 180: | { |
| 181: | $sorted_list = []; |
| 182: | foreach ($script_list as $script_name) { |
| 183: | if ($script_name == 'main_script') { |
| 184: | continue; |
| 185: | } |
| 186: | |
| 187: | if (mb_strstr($script_name, 'cypht')) { |
| 188: | $ex_name = explode('-', $script_name); |
| 189: | $sorted_list[$script_name] = $ex_name[1]; |
| 190: | } |
| 191: | } |
| 192: | asort($sorted_list); |
| 193: | $include_header = 'require ["include"];'."\n\n"; |
| 194: | $include_body = ''; |
| 195: | |
| 196: | |
| 197: | $include_body .= 'include :personal "blocked_senders";'."\n"; |
| 198: | |
| 199: | foreach ($sorted_list as $script_name => $include_script) { |
| 200: | $include_body .= 'include :personal "'.$script_name.'";'."\n"; |
| 201: | } |
| 202: | return $include_header.$include_body; |
| 203: | } |
| 204: | } |
| 205: | |
| 206: | if (!hm_exists('save_main_script')) { |
| 207: | function save_main_script($client, $main_script, $scripts) |
| 208: | { |
| 209: | $success = $client->putScript( |
| 210: | 'main_script', |
| 211: | $main_script |
| 212: | ); |
| 213: | if (! $success && mb_strpos($client->getErrorMessage(), 'failed to include') !== false) { |
| 214: | $main_script = ''; |
| 215: | foreach ($scripts as $scriptName) { |
| 216: | if ($scriptName == 'main_script') { |
| 217: | $client->removeScripts('main_script'); |
| 218: | continue; |
| 219: | } |
| 220: | $script = $client->getScript($scriptName); |
| 221: | if (mb_strpos($script, 'failed to include') !== false) { |
| 222: | $script = mb_substr($script, mb_strpos($script, '#')); |
| 223: | $client->putScript( |
| 224: | $scriptName, |
| 225: | $script |
| 226: | ); |
| 227: | } |
| 228: | $main_script .= $script . "\n"; |
| 229: | } |
| 230: | $main_script = format_main_script($main_script); |
| 231: | $ret = $client->putScript( |
| 232: | 'main_script', |
| 233: | $main_script |
| 234: | ); |
| 235: | if (! $ret) { |
| 236: | throw new Exception($client->getErrorMessage()); |
| 237: | } |
| 238: | } |
| 239: | } |
| 240: | } |
| 241: | |
| 242: | if (!hm_exists('format_main_script')) { |
| 243: | function format_main_script($script) |
| 244: | { |
| 245: | |
| 246: | $lines = explode("\n", $script); |
| 247: | $reqs = []; |
| 248: | foreach ($lines as $key => $line) { |
| 249: | if (preg_match('/^require (\[.+\]);$/', $line, $matches)) { |
| 250: | unset($lines[$key]); |
| 251: | $reqs = array_merge($reqs, json_decode($matches[1])); |
| 252: | } else if (preg_match('/^#/', $line)) { |
| 253: | unset($lines[$key]); |
| 254: | } |
| 255: | } |
| 256: | $reqs = array_unique($reqs); |
| 257: | $reqs = array_map(function($req) { |
| 258: | return '"' . $req . '"'; |
| 259: | }, $reqs); |
| 260: | |
| 261: | $script = 'require [' . implode(',', $reqs) . '];' . "\n"; |
| 262: | $script .= implode("\n", $lines); |
| 263: | |
| 264: | return $script; |
| 265: | } |
| 266: | } |
| 267: | |
| 268: | if (!hm_exists('generate_script_name')) { |
| 269: | function generate_script_name($name, $priority) |
| 270: | { |
| 271: | return str_replace(' ', '_', mb_strtolower($name)).'-'.$priority.'-cypht'; |
| 272: | } |
| 273: | } |
| 274: | |
| 275: | if (!hm_exists('generate_filter_name')) { |
| 276: | function generate_filter_name($name, $priority) |
| 277: | { |
| 278: | return str_replace(' ', '_', mb_strtolower($name)).'-'.$priority.'-cyphtfilter'; |
| 279: | } |
| 280: | } |
| 281: | |
| 282: | if (!hm_exists('get_sieve_client_factory')) { |
| 283: | function get_sieve_client_factory($site_config) |
| 284: | { |
| 285: | if (!is_null($site_config) && isset($site_config) && $factory_class = $site_config->get('sieve_client_factory')) { |
| 286: | return new $factory_class; |
| 287: | } else { |
| 288: | return new Hm_Sieve_Client_Factory; |
| 289: | } |
| 290: | } |
| 291: | } |
| 292: | |
| 293: | if (!hm_exists('parse_sieve_config_host')) { |
| 294: | function parse_sieve_config_host($imap_account) { |
| 295: | $host = $imap_account['sieve_config_host']; |
| 296: | $url = parse_url($host); |
| 297: | if ($url === false) { |
| 298: | return $host; |
| 299: | } |
| 300: | $host = $url['host'] ?? $url['path']; |
| 301: | $port = $url['port'] ?? '4190'; |
| 302: | if (isset($url['scheme'])) { |
| 303: | $tls = $url['scheme'] == 'tls'; |
| 304: | } else { |
| 305: | $tls = $imap_account['tls'] ?? true; |
| 306: | } |
| 307: | return [$host, $port, $tls]; |
| 308: | }} |
| 309: | |
| 310: | if (!hm_exists('prepare_sieve_script')) { |
| 311: | function prepare_sieve_script ($script, $index = 1, $action = "decode") |
| 312: | { |
| 313: | $blocked_list = []; |
| 314: | if ($script != '') { |
| 315: | $base64_obj = str_replace("# ", "", preg_split('#\r?\n#', $script, 0)[$index]); |
| 316: | if ($action == "decode") { |
| 317: | $blocked_list = json_decode(str_replace("*", "", base64_decode($base64_obj)), true); |
| 318: | } else { |
| 319: | $blocked_list = json_encode(base64_decode($base64_obj)); |
| 320: | } |
| 321: | } |
| 322: | return $blocked_list; |
| 323: | } |
| 324: | } |
| 325: | |
| 326: | if (!hm_exists('get_domain')) { |
| 327: | function get_domain($email) |
| 328: | { |
| 329: | $domain = explode('@', $email)[1]; |
| 330: | if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) { |
| 331: | return $regs['domain']; |
| 332: | } |
| 333: | return false; |
| 334: | } |
| 335: | } |
| 336: | |
| 337: | if (!hm_exists('default_reject_message')) { |
| 338: | function default_reject_message($user_config, $imap_server_id) |
| 339: | { |
| 340: | $reject_message = ''; |
| 341: | if ($user_config->get('sieve_block_default_reject_message')) { |
| 342: | if (array_key_exists($imap_server_id, $user_config->get('sieve_block_default_reject_message'))) { |
| 343: | $reject_message = $user_config->get('sieve_block_default_reject_message')[$imap_server_id]; |
| 344: | } |
| 345: | } |
| 346: | return $reject_message; |
| 347: | } |
| 348: | } |
| 349: | |
| 350: | if (!hm_exists('block_filter')) { |
| 351: | function block_filter($filter, $user_config, $action, $imap_server_id, $sender, $custom_reject_message = '') |
| 352: | { |
| 353: | $ret = ['action' => $action]; |
| 354: | |
| 355: | if (explode('@', $sender)[0] == '*') { |
| 356: | $filter->addRequirement('regex'); |
| 357: | } |
| 358: | $custom_condition = new \PhpSieveManager\Filters\Condition( |
| 359: | "", 'anyof' |
| 360: | ); |
| 361: | $cond = \PhpSieveManager\Filters\FilterCriteria::if('header'); |
| 362: | $cond->contains('"From" ["'.$sender.'"]'); |
| 363: | |
| 364: | if ($action == 'default') { |
| 365: | $default_behaviour = 'Discard'; |
| 366: | if ($user_config->get('sieve_block_default_behaviour')) { |
| 367: | if (array_key_exists($imap_server_id, $user_config->get('sieve_block_default_behaviour'))) { |
| 368: | $default_behaviour = $user_config->get('sieve_block_default_behaviour')[$imap_server_id]; |
| 369: | if ($default_behaviour == 'Reject') { |
| 370: | $reject_message = default_reject_message($user_config, $imap_server_id); |
| 371: | } |
| 372: | } |
| 373: | } |
| 374: | } elseif ($action == 'discard') { |
| 375: | $default_behaviour = 'Discard'; |
| 376: | } elseif ($action == 'reject_default') { |
| 377: | $default_behaviour = 'Reject'; |
| 378: | $reject_message = default_reject_message($user_config, $imap_server_id); |
| 379: | $ret['reject_message'] = $reject_message; |
| 380: | } elseif ($action == 'reject_with_message') { |
| 381: | $default_behaviour = 'Reject'; |
| 382: | $reject_message = $custom_reject_message; |
| 383: | $ret['reject_message'] = $custom_reject_message; |
| 384: | } elseif ($action == 'blocked') { |
| 385: | $default_behaviour = 'Move'; |
| 386: | } |
| 387: | |
| 388: | $custom_condition->addCriteria($cond); |
| 389: | |
| 390: | if ($default_behaviour == 'Discard') { |
| 391: | $custom_condition->addAction( |
| 392: | new \PhpSieveManager\Filters\Actions\DiscardFilterAction() |
| 393: | ); |
| 394: | } |
| 395: | elseif ($default_behaviour == 'Reject') { |
| 396: | $filter->addRequirement('reject'); |
| 397: | $custom_condition->addAction( |
| 398: | new \PhpSieveManager\Filters\Actions\RejectFilterAction([$reject_message]) |
| 399: | ); |
| 400: | } |
| 401: | elseif ($default_behaviour == 'Move') { |
| 402: | $filter->addRequirement('fileinto'); |
| 403: | $custom_condition->addAction( |
| 404: | new \PhpSieveManager\Filters\Actions\FileIntoFilterAction(['Blocked']) |
| 405: | ); |
| 406: | } |
| 407: | |
| 408: | $custom_condition->addAction( |
| 409: | new \PhpSieveManager\Filters\Actions\StopFilterAction() |
| 410: | ); |
| 411: | |
| 412: | $filter->setCondition($custom_condition); |
| 413: | |
| 414: | return $ret; |
| 415: | } |
| 416: | } |
| 417: | |
| 418: | if (!hm_exists('block_filter_dropdown')) { |
| 419: | function block_filter_dropdown ($mod, $mailbox_id = null, $with_scope = true, $submit_id = 'block_sender', $submit_title = 'Block', $increment = "") { |
| 420: | $ret = '<div class="dropdown-menu p-3" id="dropdownMenuBlockSender' .$increment. '">' |
| 421: | .'<form id="block_sender_form' .$increment. '" >'; |
| 422: | if ($with_scope) { |
| 423: | $ret .= '<div class="mb-2">' |
| 424: | . '<label for="blockSenderScope" class="form-label">'.$mod->trans('Who Is Blocked').'</label>' |
| 425: | . '<select name="scope" class="form-select form-select-sm" id="blockSenderScope">' |
| 426: | . '<option value="sender">'.$mod->trans('This Sender').'</option>' |
| 427: | . '<option value="domain">'.$mod->trans('Whole domain').'</option></select>' |
| 428: | .'</div>'; |
| 429: | } |
| 430: | $ret .= '<div class="mb-2">' |
| 431: | . '<label for="block_action" class="form-label">'.$mod->trans('Action').'</label>' |
| 432: | . '<select class="form-select form-select-sm block_action" name="block_action" id="block_action' .$increment. '">' |
| 433: | . '<option value="default">'.$mod->trans('Default action').'</option>' |
| 434: | . '<option value="discard">'.$mod->trans('Discard').'</option>' |
| 435: | . '<option value="blocked">'.$mod->trans('Move To Blocked Folder').'</option>' |
| 436: | . '<option value="reject_default">'.$mod->trans('Reject With Default Message').'</option>' |
| 437: | . '<option value="reject_with_message">'.$mod->trans('Reject With Specific Message').'</option>' |
| 438: | . '</select>' |
| 439: | .'</div>' |
| 440: | .'<div class="d-grid gap-1">' |
| 441: | . '<button class="btn btn-danger btn-sm mt-2 '.$submit_id.'" type="submit" id="'.$submit_id.$increment. '" mailbox_id="'.$mailbox_id.'">' |
| 442: | . $mod->trans($submit_title) |
| 443: | . '</button>' |
| 444: | .'</div>' |
| 445: | .'</form>' |
| 446: | .'</div>'; |
| 447: | return $ret; |
| 448: | } |
| 449: | } |
| 450: | |
| 451: | if (!hm_exists('get_blocked_senders_array')) { |
| 452: | function get_blocked_senders_array($mailbox, $site_config, $user_config) |
| 453: | { |
| 454: | $factory = get_sieve_client_factory($site_config); |
| 455: | try { |
| 456: | $client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true)); |
| 457: | $scripts = $client->listScripts(); |
| 458: | |
| 459: | if (array_search('blocked_senders', $scripts, true) === false) { |
| 460: | return []; |
| 461: | } |
| 462: | |
| 463: | $blocked_senders = []; |
| 464: | $current_script = $client->getScript('blocked_senders'); |
| 465: | if ($current_script != '') { |
| 466: | $blocked_list = prepare_sieve_script ($current_script); |
| 467: | if (!$blocked_list) { |
| 468: | return []; |
| 469: | } |
| 470: | foreach ($blocked_list as $blocked_sender) { |
| 471: | if ($blocked_sender) { |
| 472: | if (explode('@', $blocked_sender)[0] == '') { |
| 473: | $blocked_sender = '*' . $blocked_sender; |
| 474: | } |
| 475: | } |
| 476: | $blocked_senders[] = $blocked_sender; |
| 477: | } |
| 478: | } |
| 479: | return $blocked_senders; |
| 480: | } catch (Exception $e) { |
| 481: | Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger"); |
| 482: | return []; |
| 483: | } |
| 484: | } |
| 485: | } |
| 486: | |
| 487: | if (!hm_exists('get_blocked_senders')){ |
| 488: | function get_blocked_senders($mailbox, $mailbox_id, $icon_svg, $icon_block_domain_svg, $site_config, $user_config, $module) { |
| 489: | $factory = get_sieve_client_factory($site_config); |
| 490: | try { |
| 491: | $client = $factory->init($user_config, $mailbox, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true)); |
| 492: | $scripts = $client->listScripts(); |
| 493: | if (array_search('blocked_senders', $scripts, true) === false) { |
| 494: | return ''; |
| 495: | } |
| 496: | $current_script = $client->getScript('blocked_senders'); |
| 497: | $blocked_list_actions = []; |
| 498: | $blocked_senders = []; |
| 499: | if ($current_script != '') { |
| 500: | $script_split = preg_split('#\r?\n#', $current_script, 0); |
| 501: | if (!isset($script_split[1])) { |
| 502: | return ''; |
| 503: | } |
| 504: | $base64_obj = str_replace("# ", "", $script_split[1]); |
| 505: | $blocked_list = json_decode(base64_decode($base64_obj)); |
| 506: | if (!$blocked_list) { |
| 507: | return ''; |
| 508: | } |
| 509: | if (isset($script_split[2])) { |
| 510: | $base64_obj_actions = str_replace("# ", "", $script_split[2]); |
| 511: | $blocked_list_actions = json_decode(base64_decode($base64_obj_actions), true); |
| 512: | } |
| 513: | foreach ($blocked_list as $blocked_sender) { |
| 514: | if (explode('@', $blocked_sender)[0] == '') { |
| 515: | $blocked_sender = '*'.$blocked_sender; |
| 516: | } |
| 517: | $blocked_senders[] = $blocked_sender; |
| 518: | } |
| 519: | } |
| 520: | |
| 521: | $actions_map = [ |
| 522: | 'blocked' => $module->trans('Move To Blocked'), |
| 523: | 'reject_with_message' => $module->trans('Reject With Message'), |
| 524: | 'reject_default' => $module->trans('Reject'), |
| 525: | 'discard' => $module->trans('Discard'), |
| 526: | 'default' => $module->trans('Default'), |
| 527: | ]; |
| 528: | $ret = ''; |
| 529: | foreach ($blocked_senders as $k => $sender) { |
| 530: | $reject_message = $blocked_list_actions[$sender]['reject_message']; |
| 531: | $ret .= '<tr><td>'.$sender.'</td><td>'; |
| 532: | if (is_array($blocked_list_actions) && array_key_exists($sender, $blocked_list_actions)) { |
| 533: | $action = $blocked_list_actions[$sender]['action'] ?: 'default'; |
| 534: | $ret .= $actions_map[$action]; |
| 535: | if ($action == 'reject_with_message') { |
| 536: | $ret .= ' - '.$reject_message; |
| 537: | } |
| 538: | } else { |
| 539: | $action = 'default'; |
| 540: | $ret .= 'Default'; |
| 541: | } |
| 542: | $ret .= '<a href="#" mailbox_id="'.$mailbox_id.'" data-action="'.$action.'" data-reject-message="'.$reject_message.'" title="'.$module->trans('Change Behavior').'" class="block_sender_link toggle-behavior-dropdown" aria-labelledby="dropdownMenuBlockSender'.$k.'" data-bs-toggle="dropdown" aria-expanded="false"> <i class="bi bi-pencil-fill ms-3"></i></a>'; |
| 543: | $ret .= block_filter_dropdown($module, $mailbox_id, false, 'edit_blocked_behavior', 'Edit', $k); |
| 544: | |
| 545: | $ret .= '</td><td><i class="bi bi-'.$icon_svg.' unblock_button" mailbox_id="'.$mailbox_id.'" data-title="unblock Sender '.$sender.'"></i>'; |
| 546: | if (!mb_strstr($sender, '*')) { |
| 547: | $sender_party = explode('@', $sender); |
| 548: | $domain_name = ""; |
| 549: | if(isset($sender_party[1])) { |
| 550: | $domain_name = $sender_party[1]; |
| 551: | } |
| 552: | $ret .= ' <i class="bi bi-'.$icon_block_domain_svg.' block_domain_button" mailbox_id="'.$mailbox_id.'" data-title="block domain '.$domain_name.'"></i>'; |
| 553: | } |
| 554: | $ret .= '</td></tr>'; |
| 555: | } |
| 556: | return $ret; |
| 557: | } catch (Exception $e) { |
| 558: | Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger"); |
| 559: | return ''; |
| 560: | } |
| 561: | } |
| 562: | } |
| 563: | |
| 564: | if (!hm_exists('initialize_sieve_client_factory')) { |
| 565: | function initialize_sieve_client_factory($site_config, $user_config, $imapServer) { |
| 566: | try { |
| 567: | $factory = get_sieve_client_factory($site_config); |
| 568: | return $factory->init($user_config, $imapServer, in_array(mb_strtolower('nux'), $site_config->get_modules(true), true)); |
| 569: | } catch (Exception $e) { |
| 570: | Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger"); |
| 571: | return null; |
| 572: | } |
| 573: | } |
| 574: | } |
| 575: | |
| 576: | if (!hm_exists('get_sieve_host_from_services')) { |
| 577: | require_once APP_PATH.'modules/nux/modules.php'; |
| 578: | function get_sieve_host_from_services($imap_host) { |
| 579: | $services = Nux_Quick_Services::get(); |
| 580: | foreach ($services as $service) { |
| 581: | if (isset($service['server']) && $service['server'] === $imap_host && isset($service['sieve'])) { |
| 582: | return [ |
| 583: | 'host' => $service['sieve']['host'], |
| 584: | 'port' => $service['sieve']['port'] ?? 4190, |
| 585: | 'tls' => $service['sieve']['tls'] ?? true, |
| 586: | ]; |
| 587: | } |
| 588: | } |
| 589: | return null; |
| 590: | } |
| 591: | } |
| 592: | |
| 593: | if (!hm_exists('get_sieve_linked_mailbox')) { |
| 594: | function get_sieve_linked_mailbox ($imap_account, $module) { |
| 595: | $factory = get_sieve_client_factory($site_config); |
| 596: | try { |
| 597: | $client = $factory->init($module->user_config, $imap_account, $module->module_is_supported('nux')); |
| 598: | $scripts = $client->listScripts(); |
| 599: | $folders = []; |
| 600: | foreach ($scripts as $s) { |
| 601: | $script = $client->getScript($s); |
| 602: | $base64_obj = str_replace("# ", "", preg_split('#\r?\n#', $script, 0)[2]); |
| 603: | $obj = json_decode(base64_decode($base64_obj))[0]; |
| 604: | if ($obj && in_array($obj->action, ['copy', 'move'])) { |
| 605: | $folders[$s] = $obj->value; |
| 606: | } |
| 607: | } |
| 608: | $client->close(); |
| 609: | return $folders; |
| 610: | } catch (Exception $e) { |
| 611: | Hm_Msgs::add("Sieve: {$e->getMessage()}", "danger"); |
| 612: | return; |
| 613: | } |
| 614: | } |
| 615: | } |
| 616: | |
| 617: | if (!hm_exists('is_mailbox_linked_with_filters')) { |
| 618: | function is_mailbox_linked_with_filters ($mailbox, $imap_server_id, $module) { |
| 619: | $imap_servers = $module->user_config->get('imap_servers'); |
| 620: | $imap_account = $imap_servers[$imap_server_id]; |
| 621: | if (isset($imap_account['sieve_config_host'])) { |
| 622: | $linked_mailboxes = get_sieve_linked_mailbox($imap_account, $module); |
| 623: | return in_array($mailbox, $linked_mailboxes); |
| 624: | } |
| 625: | return false; |
| 626: | } |
| 627: | } |
| 628: | |