@@ -864,7 +864,7 @@ public function get_postfix_service($service, $type) {
864864 exec ("postconf -M $ service. $ type 2> /dev/null " , $ out , $ ret );
865865 }
866866 $ postfix_service = @($ out [0 ]=='' )?false :true ;
867- } else { //* fallback - Postfix < 2.9
867+ } else { //* fallback - Postfix < 2.9
868868 $ content = rf ($ conf ['postfix ' ]['config_dir ' ].'/master.cf ' );
869869 $ regex = "/^((?!#) " .$ service .".* " .$ type .".*)$/m " ;
870870 $ postfix_service = @(preg_match ($ regex , $ content ))?true :false ;
@@ -873,6 +873,68 @@ public function get_postfix_service($service, $type) {
873873 return $ postfix_service ;
874874 }
875875
876+ public function remove_postfix_service ( $ service , $ type ) {
877+ global $ conf ;
878+
879+ // nothing to do if the service isn't even defined.
880+ if (! $ this ->get_postfix_service ( $ service , $ type ) ) {
881+ return true ;
882+ }
883+
884+ $ postfix_version = `postconf -d mail_version 2>/dev/null `;
885+ $ postfix_version = preg_replace ( '/mail_version\s*=\s*(.*)\s*/ ' , '$1 ' , $ postfix_version );
886+
887+ if ( version_compare ( $ postfix_version , '2.11 ' , '>= ' ) ) {
888+
889+ exec ("postconf -X -M $ service/ $ type 2> /dev/null " , $ out , $ ret );
890+
891+ # reduce 3 or more newlines to 2
892+ $ content = rf ($ conf ['postfix ' ]['config_dir ' ].'/master.cf ' );
893+ $ content = preg_replace ( '/(\r?\n){3,}/ ' , '$1$1 ' , $ content );
894+ wf ( $ conf ['postfix ' ]['config_dir ' ].'/master.cf ' , $ content );
895+
896+ } else { //* fallback - Postfix < 2.11
897+
898+ if ( ! $ cf = fopen ( $ conf ['postfix ' ]['config_dir ' ].'/master.cf ' , 'r ' ) ) {
899+ return false ;
900+ }
901+
902+ $ out = "" ;
903+ $ reading_service = false ;
904+
905+ while ( !feof ( $ cf ) ) {
906+ $ line = fgets ( $ cf );
907+
908+ if ( $ reading_service ) {
909+ # regex matches a new service or "empty" (whitespace) line
910+ if ( preg_match ( '/^([^\s#]+.*|\s*)$/ ' , $ line ) &&
911+ ! preg_match ( '/^ ' .$ service .'\s+ ' .$ type .'/ ' , $ line ) ) {
912+ $ out .= $ line ;
913+ $ reading_service = false ;
914+ }
915+
916+ # $skipped_lines .= $line;
917+
918+ # regex matches definition matching service to be removed
919+ } else if ( preg_match ( '/^ ' .$ service .'\s+ ' .$ type .'/ ' , $ line ) ) {
920+
921+ $ reading_service = true ;
922+ # $skipped_lines .= $line;
923+
924+ } else {
925+ $ out .= $ line ;
926+ }
927+ }
928+ fclose ( $ cf );
929+
930+ $ out = preg_replace ( '/(\r?\n){3,}/ ' , '$1$1 ' , $ out ); # reduce 3 or more newlines to 2
931+
932+ return wf ( $ conf ['postfix ' ]['config_dir ' ].'/master.cf ' , $ out );
933+ }
934+
935+ return true ;
936+ }
937+
876938 public function configure_postfix ($ options = '' ) {
877939 global $ conf ,$ autoinstall ;
878940 $ cf = $ conf ['postfix ' ];
@@ -1376,12 +1438,12 @@ public function configure_dovecot() {
13761438
13771439 //* dovecot-lmtpd
13781440 if ($ configure_lmtp ) {
1379- $ dovecot_protocols .= ' lmtp '
1441+ $ dovecot_protocols .= ' lmtp ' ;
13801442 }
13811443
13821444 //* dovecot-managesieved
13831445 if (is_file ('/usr/lib/dovecot/managesieve ' )) {
1384- $ dovecot_protocols .= ' sieve '
1446+ $ dovecot_protocols .= ' sieve ' ;
13851447 }
13861448
13871449 replaceLine ($ config_dir .'/ ' .$ configfile , 'protocols = imap pop3 ' , "protocols = $ dovecot_protocols " , 1 , 0 );
@@ -1458,11 +1520,16 @@ public function configure_amavis() {
14581520 $ config_dir = $ conf ['postfix ' ]['config_dir ' ];
14591521
14601522 // Adding amavis-services to the master.cf file if the service does not already exists
1461- $ add_amavis = !$ this ->get_postfix_service ('amavis ' ,'unix ' );
1462- $ add_amavis_10025 = !$ this ->get_postfix_service ('127.0.0.1:10025 ' ,'inet ' );
1463- $ add_amavis_10027 = !$ this ->get_postfix_service ('127.0.0.1:10027 ' ,'inet ' );
1523+ // $add_amavis = !$this->get_postfix_service('amavis','unix');
1524+ // $add_amavis_10025 = !$this->get_postfix_service('127.0.0.1:10025','inet');
1525+ // $add_amavis_10027 = !$this->get_postfix_service('127.0.0.1:10027','inet');
14641526 //*TODO: check templates against existing postfix-services to make sure we use the template
14651527
1528+ // Or just remove the old service definitions and add them again?
1529+ $ add_amavis = $ this ->remove_postfix_service ('amavis ' ,'unix ' );
1530+ $ add_amavis_10025 = $ this ->remove_postfix_service ('127.0.0.1:10025 ' ,'inet ' );
1531+ $ add_amavis_10027 = $ this ->remove_postfix_service ('127.0.0.1:10027 ' ,'inet ' );
1532+
14661533 if ($ add_amavis || $ add_amavis_10025 || $ add_amavis_10027 ) {
14671534 //* backup master.cf
14681535 if (is_file ($ config_dir .'/master.cf ' )) copy ($ config_dir .'/master.cf ' , $ config_dir .'/master.cf~ ' );
0 commit comments