22
33if (!defined ('DEBUG_MODE ' )) { die (); }
44
5+ if (!function_exists ('vendor_detection_load_registry ' )) {
6+ require_once APP_PATH .'modules/vendor_detection/functions.php ' ;
7+ }
8+
59if (!hm_exists ('get_script_modal_content ' )) {
610 function get_script_modal_content ()
711 {
@@ -350,6 +354,11 @@ function default_reject_message($user_config, $imap_server_id)
350354if (!hm_exists ('block_filter ' )) {
351355 function block_filter ($ filter , $ user_config , $ action , $ imap_server_id , $ sender , $ custom_reject_message = '' )
352356 {
357+ if (strpos ($ sender , 'platform: ' ) === 0 ) {
358+ $ vendor_id = substr ($ sender , strlen ('platform: ' ));
359+ return block_filter_platform ($ filter , $ user_config , $ action , $ imap_server_id , $ vendor_id , $ custom_reject_message );
360+ }
361+
353362 $ ret = ['action ' => $ action ];
354363
355364 if (explode ('@ ' , $ sender )[0 ] == '* ' ) {
@@ -394,14 +403,17 @@ function block_filter($filter, $user_config, $action, $imap_server_id, $sender,
394403 }
395404 elseif ($ default_behaviour == 'Reject ' ) {
396405 $ filter ->addRequirement ('reject ' );
406+ if (!isset ($ reject_message ) || $ reject_message === '' ) {
407+ $ reject_message = 'Blocked by Cypht ' ;
408+ }
397409 $ custom_condition ->addAction (
398- new \PhpSieveManager \Filters \Actions \RejectFilterAction ([$ reject_message ])
410+ new \PhpSieveManager \Filters \Actions \RejectFilterAction ([' reason ' => $ reject_message ])
399411 );
400412 }
401413 elseif ($ default_behaviour == 'Move ' ) {
402414 $ filter ->addRequirement ('fileinto ' );
403415 $ custom_condition ->addAction (
404- new \PhpSieveManager \Filters \Actions \FileIntoFilterAction (['Blocked ' ])
416+ new \PhpSieveManager \Filters \Actions \FileIntoFilterAction (['mailbox ' => ' Blocked ' ])
405417 );
406418 }
407419
@@ -415,16 +427,168 @@ function block_filter($filter, $user_config, $action, $imap_server_id, $sender,
415427 }
416428}
417429
430+ if (!hm_exists ('block_filter_platform ' )) {
431+ function block_filter_platform ($ filter , $ user_config , $ action , $ imap_server_id , $ vendor_id , $ custom_reject_message = '' ) {
432+ $ ret = ['action ' => $ action ];
433+ if (!$ vendor_id ) {
434+ return $ ret ;
435+ }
436+ $ vendor = vendor_detection_get_vendor_by_id ($ vendor_id );
437+ if (empty ($ vendor )) {
438+ if (defined ('DEBUG_MODE ' ) && DEBUG_MODE ) {
439+ error_log ('[sieve_block_debug] platform vendor not found: ' .$ vendor_id );
440+ }
441+ return $ ret ;
442+ }
443+
444+ $ criteria = build_platform_block_criteria ($ vendor );
445+ if (empty ($ criteria )) {
446+ if (defined ('DEBUG_MODE ' ) && DEBUG_MODE ) {
447+ error_log ('[sieve_block_debug] platform criteria empty for: ' .$ vendor_id );
448+ }
449+ return $ ret ;
450+ }
451+ if (defined ('DEBUG_MODE ' ) && DEBUG_MODE ) {
452+ error_log ('[sieve_block_debug] platform criteria for ' .$ vendor_id .': ' .json_encode ($ criteria ));
453+ }
454+
455+ $ custom_condition = new \PhpSieveManager \Filters \Condition (
456+ "" , 'anyof '
457+ );
458+ foreach ($ criteria as $ criterion ) {
459+ $ cond = \PhpSieveManager \Filters \FilterCriteria::if ('header ' );
460+ $ cond ->contains ($ criterion );
461+ $ custom_condition ->addCriteria ($ cond );
462+ }
463+
464+ if ($ action == 'default ' ) {
465+ $ default_behaviour = 'Discard ' ;
466+ if ($ user_config ->get ('sieve_block_default_behaviour ' )) {
467+ if (array_key_exists ($ imap_server_id , $ user_config ->get ('sieve_block_default_behaviour ' ))) {
468+ $ default_behaviour = $ user_config ->get ('sieve_block_default_behaviour ' )[$ imap_server_id ];
469+ if ($ default_behaviour == 'Reject ' ) {
470+ $ reject_message = default_reject_message ($ user_config , $ imap_server_id );
471+ }
472+ }
473+ }
474+ } elseif ($ action == 'discard ' ) {
475+ $ default_behaviour = 'Discard ' ;
476+ } elseif ($ action == 'reject_default ' ) {
477+ $ default_behaviour = 'Reject ' ;
478+ $ reject_message = default_reject_message ($ user_config , $ imap_server_id );
479+ $ ret ['reject_message ' ] = $ reject_message ;
480+ } elseif ($ action == 'reject_with_message ' ) {
481+ $ default_behaviour = 'Reject ' ;
482+ $ reject_message = $ custom_reject_message ;
483+ $ ret ['reject_message ' ] = $ custom_reject_message ;
484+ } elseif ($ action == 'blocked ' ) {
485+ $ default_behaviour = 'Move ' ;
486+ }
487+
488+ if ($ default_behaviour == 'Discard ' ) {
489+ $ custom_condition ->addAction (
490+ new \PhpSieveManager \Filters \Actions \DiscardFilterAction ()
491+ );
492+ }
493+ elseif ($ default_behaviour == 'Reject ' ) {
494+ $ filter ->addRequirement ('reject ' );
495+ if (!isset ($ reject_message ) || $ reject_message === '' ) {
496+ $ reject_message = 'Blocked by Cypht ' ;
497+ }
498+ $ custom_condition ->addAction (
499+ new \PhpSieveManager \Filters \Actions \RejectFilterAction (['reason ' => $ reject_message ])
500+ );
501+ }
502+ elseif ($ default_behaviour == 'Move ' ) {
503+ $ filter ->addRequirement ('fileinto ' );
504+ $ custom_condition ->addAction (
505+ new \PhpSieveManager \Filters \Actions \FileIntoFilterAction (['mailbox ' => 'Blocked ' ])
506+ );
507+ }
508+
509+ $ custom_condition ->addAction (
510+ new \PhpSieveManager \Filters \Actions \StopFilterAction ()
511+ );
512+
513+ $ filter ->setCondition ($ custom_condition );
514+
515+ return $ ret ;
516+ }
517+ }
518+
519+ if (!hm_exists ('build_platform_block_criteria ' )) {
520+ function build_platform_block_criteria ($ vendor ) {
521+ $ criteria = array ();
522+ $ header_values = array ();
523+ $ dkim_domains = vendor_detection_lowercase_list ($ vendor ['dkim_domains ' ] ?? array ());
524+ $ return_domains = vendor_detection_lowercase_list ($ vendor ['return_path_domains ' ] ?? array ());
525+ $ received_domains = vendor_detection_lowercase_list ($ vendor ['received_domains ' ] ?? array ());
526+ $ platform_domains = vendor_detection_lowercase_list ($ vendor ['platform_domains ' ] ?? array ());
527+ $ header_names = vendor_detection_lowercase_list ($ vendor ['header_names ' ] ?? array ());
528+
529+ $ return_domains = array_unique (array_merge ($ return_domains , $ platform_domains ));
530+ $ received_domains = array_unique (array_merge ($ received_domains , $ platform_domains ));
531+
532+ foreach ($ dkim_domains as $ domain ) {
533+ $ header_values [] = array ('DKIM-Signature ' , $ domain );
534+ }
535+ foreach ($ return_domains as $ domain ) {
536+ $ header_values [] = array ('Return-Path ' , $ domain );
537+ }
538+ foreach ($ received_domains as $ domain ) {
539+ $ header_values [] = array ('Received ' , $ domain );
540+ }
541+ foreach ($ header_names as $ header_name ) {
542+ $ header_values [] = array ($ header_name , '' );
543+ }
544+
545+ foreach ($ header_values as $ pair ) {
546+ $ header = $ pair [0 ];
547+ $ value = $ pair [1 ];
548+ if (!$ header ) {
549+ continue ;
550+ }
551+ $ key = $ header .'| ' .$ value ;
552+ if (isset ($ criteria [$ key ])) {
553+ continue ;
554+ }
555+ $ criteria [$ key ] = '" ' .$ header .'" [" ' .$ value .'"] ' ;
556+ }
557+
558+ return array_values ($ criteria );
559+ }
560+ }
561+
418562if (!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 = "" ) {
563+ function block_filter_dropdown ($ mod , $ mailbox_id = null , $ with_scope = true , $ submit_id = 'block_sender ' , $ submit_title = 'Block ' , $ increment = "" , $ block_data = array () ) {
420564 $ ret = '<div class="dropdown-menu p-3" id="dropdownMenuBlockSender ' .$ increment . '"> '
421565 .'<form id="block_sender_form ' .$ increment . '" > ' ;
422566 if ($ with_scope ) {
567+ $ data_sender = $ block_data ['sender ' ] ?? '' ;
568+ $ data_domain = $ block_data ['domain ' ] ?? '' ;
569+ $ data_vendor_id = $ block_data ['vendor_id ' ] ?? '' ;
570+ $ data_vendor_name = $ block_data ['vendor_name ' ] ?? '' ;
571+ $ platform_enabled = false ;
572+ $ site_config = $ mod ->get ('site_config ' );
573+ if ($ site_config && method_exists ($ site_config , 'get ' )) {
574+ $ platform_enabled = (bool ) $ site_config ->get ('enable_platform_blocking ' , false );
575+ }
576+
577+ $ select_attrs = ' data-sender=" ' .$ mod ->html_safe ($ data_sender ).'" ' .
578+ ' data-domain=" ' .$ mod ->html_safe ($ data_domain ).'" ' .
579+ ' data-vendor-id=" ' .$ mod ->html_safe ($ data_vendor_id ).'" ' .
580+ ' data-vendor-name=" ' .$ mod ->html_safe ($ data_vendor_name ).'" ' ;
581+
423582 $ ret .= '<div class="mb-2"> '
424583 . '<label for="blockSenderScope" class="form-label"> ' .$ mod ->trans ('Who Is Blocked ' ).'</label> '
425- . '<select name="scope" class="form-select form-select-sm" id="blockSenderScope"> '
584+ . '<select name="scope" class="form-select form-select-sm" id="blockSenderScope" ' . $ select_attrs . ' > '
426585 . '<option value="sender"> ' .$ mod ->trans ('This Sender ' ).'</option> '
427- . '<option value="domain"> ' .$ mod ->trans ('Whole domain ' ).'</option></select> '
586+ . '<option value="domain"> ' .$ mod ->trans ('Whole domain ' ).'</option> ' ;
587+ if ($ platform_enabled && $ data_vendor_id ) {
588+ $ label = $ data_vendor_name ? $ data_vendor_name : $ data_vendor_id ;
589+ $ ret .= '<option value="platform"> ' .$ mod ->trans ('Platform ' ).': ' .$ mod ->html_safe ($ label ).'</option> ' ;
590+ }
591+ $ ret .= '</select> '
428592 .'</div> ' ;
429593 }
430594 $ ret .= '<div class="mb-2"> '
0 commit comments