function sender_dependent_relayhost_maps_build()
{
    $ldap = new clladp();
    $main = new maincf_multi();
    $sender_dependent_relayhost_maps = array();
    $filter = "(&(objectClass=SenderDependentRelayhostMaps)(cn=*))";
    $attrs = array("cn", "SenderRelayHost");
    $dn = "cn=Sender_Dependent_Relay_host_Maps,cn=artica,{$ldap->suffix}";
    $hash = $ldap->Ldap_search($dn, $filter, $attrs);
    for ($i = 0; $i < $hash["count"]; $i++) {
        $mail = $hash[$i]["cn"][0];
        $value = trim($hash[$i][strtolower("SenderRelayHost")][0]);
        if ($value == null) {
            continue;
        }
        if ($value == ":") {
            continue;
        }
        $sender_dependent_relayhost_maps[$mail] = $value;
        //$GLOBALS["sender_dependent_relayhost_maps"][]="$mail\t$value";
    }
    $filter = "(&(objectClass=userAccount)(mail=*))";
    $attrs = array("mail", "AlternateSmtpRelay");
    $dn = "dc=organizations,{$ldap->suffix}";
    $hash = $ldap->Ldap_search($dn, $filter, $attrs);
    for ($i = 0; $i < $hash["count"]; $i++) {
        $mail = $hash[$i]["mail"][0];
        if (!isset($hash[$i][strtolower("AlternateSmtpRelay")])) {
            continue;
        }
        $value = trim($hash[$i][strtolower("AlternateSmtpRelay")][0]);
        if ($value == null) {
            continue;
        }
        if ($value == ":") {
            continue;
        }
        $sender_dependent_relayhost_maps[$mail] = $value;
        //$GLOBALS["sender_dependent_relayhost_maps"][]="$mail\t$value";
    }
    $filter = "(&(objectClass=SenderDependentSaslInfos)(cn=*))";
    $attrs = array("cn", "SenderCanonicalRelayHost");
    $dn = "dc=organizations,{$ldap->suffix}";
    $hash = $ldap->Ldap_search($dn, $filter, $attrs);
    for ($i = 0; $i < $hash["count"]; $i++) {
        $mail = $hash[$i]["cn"][0];
        $value = trim($hash[$i][strtolower("SenderCanonicalRelayHost")][0]);
        if ($value == null) {
            continue;
        }
        if ($value == ":") {
            continue;
        }
        $sender_dependent_relayhost_maps[$mail] = $value;
    }
    $arr = array("SmtpSaslPasswordString");
    $filter = "(&(objectclass=PostfixSmtpSaslPaswordMaps)(cn=*))";
    $dn = "cn=smtp_sasl_password_maps,cn=artica,{$ldap->suffix}";
    $hash = $ldap->Ldap_search($dn, $filter, $attrs);
    for ($i = 0; $i < $hash["count"]; $i++) {
        $mail = "{$hash[$i]["cn"][0]}";
        $value = trim($hash[$i][strtolower("SmtpSaslPasswordString")][0]);
        if ($value == null) {
            continue;
        }
        if ($value == ":") {
            continue;
        }
        $sender_dependent_relayhost_maps[$mail] = $value;
    }
    $q = new mysql();
    $sql = "SELECT * FROM sender_dependent_relay_host WHERE enabled=1\n\t\t\tAND `override_transport`=0\n\t\t\tAND `override_relay`=0\n\t\t\tAND `hostname`='master' ORDER by zOrders";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    while ($ligne = mysql_fetch_assoc($results)) {
        $relay = $ligne["relay"];
        $relay_port_text = null;
        $relay_port = $ligne["relay_port"];
        $lookups = $ligne["lookups"];
        $relay_text = $main->RelayToPattern($relay, $relay_port, $lookups);
        if ($ligne["directmode"] == 1) {
            $relay_text = "smtp:";
        }
        $domain = $ligne["domain"];
        $sender_dependent_relayhost_maps[$domain] = $relay_text;
    }
    if (is_array($sender_dependent_relayhost_maps)) {
        while (list($mail, $value) = each($sender_dependent_relayhost_maps)) {
            $mail = str_replace(".", "\\.", $mail);
            $mail = str_replace("*", ".*", $mail);
            if (strpos($mail, "@") == 0) {
                $mail = ".*@{$mail}";
            }
            $GLOBALS["sender_dependent_relayhost_maps"][] = "/{$mail}/\t{$value}";
        }
    }
}
function main_search()
{
    $MyPage = CurrentPageName();
    $main = new maincf_multi();
    $page = CurrentPageName();
    $tpl = new templates();
    $sock = new sockets();
    $q = new mysql();
    $t = $_GET["t"];
    $table = "sender_dependent_relay_host";
    $searchstring = string_to_flexquery();
    $page = 1;
    $table = "(SELECT * FROM sender_dependent_relay_host WHERE `hostname`='{$_GET["hostname"]}' ORDER by zOrders) as t";
    if (isset($_POST["sortname"])) {
        if ($_POST["sortname"] != null) {
            $ORDER = "ORDER BY `{$_POST["sortname"]}` {$_POST["sortorder"]}";
        }
    }
    if (isset($_POST['page'])) {
        $page = $_POST['page'];
    }
    if ($searchstring != null) {
        $sql = "SELECT COUNT( * ) AS tcount FROM {$table} WHERE 1 {$searchstring}";
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup"));
        if (!$q->ok) {
            json_error_show("Mysql Error [" . __LINE__ . "]: <br>{$q->mysql_error}.<br>{$sql}", 1);
        }
        $total = $ligne["tcount"];
    } else {
        $sql = "SELECT COUNT( * ) AS tcount FROM {$table} WHERE 1 {$searchstring}";
        $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup"));
        if (!$q->ok) {
            json_error_show("Mysql Error [" . __LINE__ . "]: <br>{$q->mysql_error}.<br>{$sql}", 1);
        }
        $total = $ligne["tcount"];
    }
    if (isset($_POST['rp'])) {
        $rp = $_POST['rp'];
    }
    if (!is_numeric($rp)) {
        $rp = 50;
    }
    $pageStart = ($page - 1) * $rp;
    $limitSql = "LIMIT {$pageStart}, {$rp}";
    $sql = "SELECT * FROM {$table} WHERE 1 {$searchstring} {$ORDER} {$limitSql} ";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    if (!$q->ok) {
        if ($q->mysql_error != null) {
            json_error_show(date("H:i:s") . "<br>SORT:{$_POST["sortname"]}:<br>Mysql Error [L." . __LINE__ . "]: {$q->mysql_error}<br>{$sql}", 1);
        }
    }
    if (mysql_num_rows($results) == 0) {
        json_error_show("no data", 1);
    }
    $data = array();
    $data['page'] = $page;
    $data['total'] = $total;
    $data['rows'] = array();
    $fontsize = "22";
    $free_text = $tpl->javascript_parse_text("{free}");
    $computers = $tpl->javascript_parse_text("{computers}");
    $overloaded_text = $tpl->javascript_parse_text("{overloaded}");
    $orders_text = $tpl->javascript_parse_text("{orders}");
    $directories_monitor = $tpl->javascript_parse_text("{directories_monitor}");
    $dns_destination = $tpl->javascript_parse_text("{direct_mode}");
    $all_others_domains = $tpl->javascript_parse_text("{all_others_domains}");
    while ($ligne = mysql_fetch_assoc($results)) {
        $LOGSWHY = array();
        $overloaded = null;
        $loadcolor = "black";
        $StatHourColor = "black";
        $ColorTime = "black";
        $hostname = $ligne["hostname"];
        $domain = $ligne["domain"];
        $zmd5 = $ligne["zmd5"];
        $relay = $ligne["relay"];
        $relay_port = $ligne["relay_port"];
        $lookups = $ligne["lookups"];
        $relay_text = $main->RelayToPattern($relay, $relay_port, $lookups);
        $icon_grey = "ok32-grey.png";
        $icon_warning_32 = "warning32.png";
        $icon_red_32 = "32-red.png";
        $icon = "ok-32.png";
        $icon_f = $icon_grey;
        if ($ligne["enabled"] == 0) {
            $ColorTime = "#8a8a8a";
        }
        $styleHref = " style='font-size:{$fontsize}px;text-decoration:underline;color:{$ColorTime}'";
        $style = " style='font-size:{$fontsize}px;color:{$ColorTime}'";
        $urijs = "Loadjs('{$MyPage}?item-js=yes&zmd5={$zmd5}&hostname={$hostname}');";
        $link = "<a href=\"javascript:blur();\" OnClick=\"javascript:{$urijs}\" {$styleHref}>";
        $orders = imgtootltip("48-settings.png", null, "Loadjs('artica-meta.menus.php?gpid={$ligne["ID"]}');");
        $delete = imgtootltip("delete-32.png", null, "Loadjs('{$MyPage}?item-delete-js={$zmd5}')");
        $up = imgsimple("arrow-up-32.png", null, "MoveSubRuleLinks{$t}('{$zmd5}','up')");
        $down = imgsimple("arrow-down-32.png", null, "MoveSubRuleLinks{$t}('{$zmd5}','down')");
        if ($ligne["directmode"] == 1) {
            $relay_text = "{$dns_destination}";
        }
        if ($ligne["enabledauth"] == 1) {
            $icon_f = $icon;
        }
        if ($domain == "*") {
            $domain = $all_others_domains;
        }
        $cell = array();
        $cell[] = "<span {$style}>{$link}{$domain}</a></span>";
        $cell[] = "<span {$style}>{$link}{$relay_text}</a></span>";
        $cell[] = "<span {$style}><img src='img/{$icon_f}'></a></span>";
        $cell[] = "<span {$style}>{$up}</a></span>";
        $cell[] = "<span {$style}>{$down}</a></span>";
        $cell[] = "<span {$style}>{$delete}</a></span>";
        $data['rows'][] = array('id' => $ligne['uuid'], 'cell' => $cell);
    }
    echo json_encode($data);
}
function MasterCFBuilder($restart_service = false)
{
    $smtp_ssl = null;
    if (!isset($GLOBALS["CLASS_SOCKET"])) {
        $GLOBALS["CLASS_SOCKET"] = new sockets();
        $sock = $GLOBALS["CLASS_SOCKET"];
    } else {
        $sock = $GLOBALS["CLASS_SOCKET"];
    }
    if (!is_object($GLOBALS["CLASS_SOCKET"])) {
        $GLOBALS["CLASS_SOCKET"] = new sockets();
        $sock = $GLOBALS["CLASS_SOCKET"];
    } else {
        $sock = $GLOBALS["CLASS_SOCKET"];
    }
    $EnableArticaSMTPFilter = $sock->GET_INFO("EnableArticaSMTPFilter");
    $EnableArticaSMTPFilter = 0;
    $EnableAmavisInMasterCF = intval($sock->GET_INFO('EnableAmavisInMasterCF'));
    $EnableAmavisDaemon = intval($sock->GET_INFO('EnableAmavisDaemon'));
    $PostfixEnableMasterCfSSL = $sock->GET_INFO("PostfixEnableMasterCfSSL");
    $ArticaFilterMaxProc = $sock->GET_INFO("ArticaFilterMaxProc");
    $PostfixEnableSubmission = $sock->GET_INFO("PostfixEnableSubmission");
    $EnableASSP = $sock->GET_INFO('EnableASSP');
    $PostfixBindInterfacePort = $sock->GET_INFO("PostfixBindInterfacePort");
    $TrustMyNetwork = $sock->GET_INFO("TrustMyNetwork");
    if (!is_numeric($TrustMyNetwork)) {
        $TrustMyNetwork = 1;
    }
    $user = new usersMenus();
    $main = new maincf_multi("master", "master");
    $EnablePostScreen = $main->GET("EnablePostScreen");
    $postscreen_line = null;
    $tlsproxy = null;
    $dnsblog = null;
    $re_cleanup_infos = null;
    $smtp_submission = null;
    $pre_cleanup_addons = null;
    $master = new master_cf(1, "master");
    $ver210 = false;
    $users = new usersMenus();
    echo "Starting......: " . date("H:i:s") . " Postfix master version: {$users->POSTFIX_VERSION}\n";
    if (preg_match("#^([0-9]+)\\.([0-9]+)#", $users->POSTFIX_VERSION, $re)) {
        $major = intval($re[1]);
        $minor = intval($re[2]);
        $binver = intval("{$major}{$minor}");
        if ($binver >= 210) {
            echo "Starting......: " . date("H:i:s") . " Postfix master version: 2.10 [{$binver}] OK\n";
            $ver210 = true;
        }
    }
    $MASTER_CF_DEFINED = $master->GetArray();
    if ($EnablePostScreen == null) {
        $EnablePostScreen = 0;
    }
    if (!$user->POSTSCREEN_INSTALLED) {
        $EnablePostScreen = 0;
    }
    if ($EnablePostScreen == 1) {
        $PostfixEnableSubmission = 1;
    }
    $ADD_PRECLEANUP = false;
    $TLSSET = false;
    if ($GLOBALS["EnablePostfixMultiInstance"] == 1) {
        $EnableAmavisDaemon = 0;
        $PostfixEnableMasterCfSSL = 0;
    }
    if (!is_numeric($PostfixBindInterfacePort)) {
        $PostfixBindInterfacePort = 25;
    }
    if ($EnableAmavisDaemon == 0) {
        $EnableAmavisInMasterCF = 0;
    }
    if (!is_numeric($PostfixEnableSubmission)) {
        $PostfixEnableSubmission = 0;
    }
    if (!is_numeric($EnableASSP)) {
        $EnableASSP = 0;
    }
    shell_exec("{$GLOBALS["postconf"]} -X \"content_filter\" >/dev/null 2>&1");
    build_progress_sender_routing("{building} Master.cf", 35);
    if ($EnableAmavisInMasterCF == 1) {
        build_progress_sender_routing("{building} Amavis hooks", 40);
        $MasterCFAmavisInstancesCount = intval($sock->GET_INFO("MasterCFAmavisInstancesCount"));
        if ($MasterCFAmavisInstancesCount == 0) {
            $MasterCFAmavisInstancesCount = "-";
        }
        if ($MasterCFAmavisInstancesCount < 0) {
            $MasterCFAmavisInstancesCount = "-";
        }
        $ADD_PRECLEANUP = true;
        echo "Starting......: " . date("H:i:s") . " Amavis is enabled using post-queue mode\n";
        shell_exec("{$GLOBALS["postconf"]} -e \"content_filter = amavis:[127.0.0.1]:10024\" >/dev/null 2>&1");
        echo "Starting......: " . date("H:i:s") . " Amavis max process: {$MasterCFAmavisInstancesCount}\n";
        if (isset($MASTER_CF_DEFINED["amavis"])) {
            unset($MASTER_CF_DEFINED["amavis"]);
        }
        $amavis[] = "amavis\tunix\t-\t-\t-\t-\t{$MasterCFAmavisInstancesCount}\tsmtp";
        $amavis[] = " -o smtp_data_done_timeout=1200";
        $amavis[] = " -o smtp_send_xforward_command=yes";
        $amavis[] = " -o disable_dns_lookups=yes";
        $amavis[] = " -o smtp_generic_maps=";
        $amavis[] = " -o smtpd_sasl_auth_enable=no";
        $amavis[] = " -o smtpd_use_tls=no";
        $amavis[] = " -o max_use=20";
        $amavis[] = "";
        $amavis[] = "";
        if (isset($MASTER_CF_DEFINED["127.0.0.1:10025"])) {
            unset($MASTER_CF_DEFINED["127.0.0.1:10025"]);
        }
        $amavis[] = "127.0.0.1:10025\tinet\tn\t-\tn\t-\t-\tsmtpd";
        $amavis[] = " -o local_recipient_maps=";
        $amavis[] = " -o relay_recipient_maps=";
        $amavis[] = " -o smtpd_restriction_classes=";
        $amavis[] = " -o smtpd_client_restrictions=";
        $amavis[] = " -o smtpd_helo_restrictions=";
        $amavis[] = " -o smtpd_sender_restrictions=";
        $artica[] = " -o smtpd_end_of_data_restrictions=";
        $amavis[] = " -o smtp_generic_maps=";
        $amavis[] = " -o smtpd_recipient_restrictions=permit_mynetworks,reject";
        $amavis[] = " -o mynetworks=127.0.0.0/8";
        $amavis[] = " -o mynetworks_style=host";
        $amavis[] = " -o strict_rfc821_envelopes=yes";
        $amavis[] = " -o smtpd_error_sleep_time=0";
        $amavis[] = " -o smtpd_soft_error_limit=1001";
        $amavis[] = " -o smtpd_hard_error_limit=1000";
        $amavis[] = " -o receive_override_options=no_header_body_checks";
        $amavis[] = "\t-o smtpd_sasl_auth_enable=no";
        $amavis[] = " -o smtpd_milters=";
        if ($ver210) {
            $amavis[] = "\t-o smtpd_upstream_proxy_protocol=";
        }
        $amavis[] = "\t-o smtpd_use_tls=no";
        $master_amavis = @implode("\n", $amavis);
    }
    if ($ADD_PRECLEANUP) {
        echo "Starting......: " . date("H:i:s") . " Enable pre-cleanup service...\n";
        $pre_cleanup_addons = " -o smtp_generic_maps= -o canonical_maps= -o sender_canonical_maps= -o recipient_canonical_maps= -o masquerade_domains= -o recipient_bcc_maps= -o sender_bcc_maps=";
        $re_cleanup_infos = " -o cleanup_service_name=pre-cleanup";
    }
    $permit_mynetworks = null;
    if ($PostfixEnableMasterCfSSL == 1) {
        if ($TrustMyNetwork == 1) {
            $permit_mynetworks = "permit_mynetworks,";
        }
        echo "Starting......: " . date("H:i:s") . " Enabling SSL (465 port)\n";
        SetTLS();
        $TLSSET = true;
        if (isset($MASTER_CF_DEFINED["smtps"])) {
            unset($MASTER_CF_DEFINED["smtps"]);
        }
        $SSL_INSTANCE[] = "smtps\tinet\tn\t-\tn\t-\t-\tsmtpd";
        if ($re_cleanup_infos != null) {
            $SSL_INSTANCE[] = $re_cleanup_infos;
        }
        $SSL_INSTANCE[] = " -o smtpd_tls_wrappermode=yes";
        $SSL_INSTANCE[] = " -o smtpd_delay_reject=yes";
        //$SSL_INSTANCE[]=" -o smtpd_client_restrictions={$permit_mynetworks}permit_sasl_authenticated,reject\n";
        //$SSL_INSTANCE[]=" -o smtpd_sender_restrictions=permit_sasl_authenticated,reject";
        //$SSL_INSTANCE[]=" -o smtpd_helo_restrictions=permit_sasl_authenticated,reject";
        //$SSL_INSTANCE[]=" -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject";
        $smtp_ssl = @implode("\n", $SSL_INSTANCE);
    } else {
        echo "Starting......: " . date("H:i:s") . " SSL (465 port) Disabled\n";
    }
    if ($PostfixEnableSubmission == 1) {
        echo "Starting......: " . date("H:i:s") . " Enabling submission (587 port)\n";
        if (isset($MASTER_CF_DEFINED["submission"])) {
            unset($MASTER_CF_DEFINED["submission"]);
        }
        if (!$TLSSET) {
            SetTLS();
        }
        $TLSSET = true;
        $SUBMISSION_INSTANCE[] = "submission\tinet\tn\t-\tn\t-\t-\tsmtpd";
        if ($re_cleanup_infos != null) {
            $SUBMISSION_INSTANCE[] = $re_cleanup_infos;
        }
        $SUBMISSION_INSTANCE[] = " -o smtpd_etrn_restrictions=reject";
        $SUBMISSION_INSTANCE[] = " -o smtpd_enforce_tls=yes";
        $SUBMISSION_INSTANCE[] = " -o smtpd_sasl_auth_enable=yes";
        $SUBMISSION_INSTANCE[] = " -o smtpd_delay_reject=yes";
        $SUBMISSION_INSTANCE[] = " -o smtpd_client_restrictions=permit_sasl_authenticated,reject";
        $SUBMISSION_INSTANCE[] = " -o smtpd_sender_restrictions=permit_sasl_authenticated,reject";
        $SUBMISSION_INSTANCE[] = " -o smtpd_helo_restrictions=permit_sasl_authenticated,reject";
        $SUBMISSION_INSTANCE[] = " -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject";
        $SUBMISSION_INSTANCE[] = " -o smtp_generic_maps=";
        $SUBMISSION_INSTANCE[] = " -o sender_canonical_maps=";
        $smtp_submission = @implode("\n", $SUBMISSION_INSTANCE);
    } else {
        echo "Starting......: " . date("H:i:s") . " submission (587 port) Disabled\n";
    }
    if ($PostfixBindInterfacePort == 25) {
        $postfix_listen_port = "smtp";
        $postscreen_listen_port = "smtp";
    } else {
        $postfix_listen_port = $PostfixBindInterfacePort;
        $postscreen_listen_port = $PostfixBindInterfacePort;
    }
    echo "Starting......: " . date("H:i:s") . " Postfix intended to listen SMTP Port {$postfix_listen_port}\n";
    $smtp_in_proto = "inet";
    $smtp_private = "n";
    if ($EnableASSP == 1) {
        echo "Starting......: " . date("H:i:s") . " ASSP is enabled change postfix listen port to 127.0.0.1:26\n";
        $postfix_listen_port = "127.0.0.1:6000";
        $postscreen_listen_port = "127.0.0.1:6000";
    }
    if ($EnablePostScreen == 1) {
        if (isset($MASTER_CF_DEFINED["tlsproxy"])) {
            unset($MASTER_CF_DEFINED["tlsproxy"]);
        }
        if (isset($MASTER_CF_DEFINED["dnsblog"])) {
            unset($MASTER_CF_DEFINED["dnsblog"]);
        }
        echo "Starting......: " . date("H:i:s") . " PostScreen is enabled, users should use 587 port to send mails internally\n";
        $smtp_in_proto = "pass";
        $smtp_private = "-";
        if ($postfix_listen_port == "smtp") {
            $postfix_listen_port = "smtpd";
        }
        $postscreen_line = "{$postscreen_listen_port}\tinet\tn\t-\tn\t-\t1\tpostscreen -o soft_bounce=yes";
        $tlsproxy = "tlsproxy\tunix\t-\t-\tn\t-\t0\ttlsproxy";
        $dnsblog = "dnsblog\tunix\t-\t-\tn\t-\t0\tdnsblog";
    } else {
        echo "Starting......: " . date("H:i:s") . " PostScreen is disabled\n";
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Starting......: " . date("H:i:s") . " run MasterCF_DOMAINS_THROTTLE()\n";
    }
    build_progress_sender_routing("{building} DOMAINS_THROTTLE", 45);
    $smtp_throttle = MasterCF_DOMAINS_THROTTLE();
    // http://www.ijs.si/software/amavisd/README.postfix.html
    $conf[] = "#";
    $conf[] = "# Postfix master process configuration file.  For details on the format";
    $conf[] = "# of the file, see the master(5) manual page (command: \"man 5 master\").";
    $conf[] = "#";
    $conf[] = "# ==========================================================================";
    $conf[] = "# service type  private unpriv  chroot  wakeup  maxproc command + args";
    $conf[] = "#               (yes)   (yes)   (yes)   (never) (100)";
    $conf[] = "# ==========================================================================";
    if (isset($MASTER_CF_DEFINED[$postfix_listen_port])) {
        unset($MASTER_CF_DEFINED[$postfix_listen_port]);
    }
    if ($postscreen_line != null) {
        $conf[] = $postscreen_line;
    }
    if ($tlsproxy != null) {
        $conf[] = $tlsproxy;
    }
    if ($dnsblog != null) {
        $conf[] = $dnsblog;
    }
    $conf[] = "{$postfix_listen_port}\t{$smtp_in_proto}\t{$smtp_private}\t-\tn\t-\t-\tsmtpd{$re_cleanup_infos}";
    if ($smtp_ssl != null) {
        $conf[] = $smtp_ssl;
    }
    if ($smtp_submission != null) {
        $conf[] = $smtp_submission;
    }
    if ($smtp_throttle != null) {
        $conf[] = $smtp_throttle;
    }
    if (isset($MASTER_CF_DEFINED["pickup"])) {
        unset($MASTER_CF_DEFINED["pickup"]);
    }
    if (isset($MASTER_CF_DEFINED["cleanup"])) {
        unset($MASTER_CF_DEFINED["cleanup"]);
    }
    if (isset($MASTER_CF_DEFINED["mailman"])) {
        unset($MASTER_CF_DEFINED["mailman"]);
    }
    if (count($MASTER_CF_DEFINED) == 0) {
        $conf[] = "pickup\tfifo\tn\t-\tn\t60\t1\tpickup{$re_cleanup_infos}";
        $conf[] = "cleanup\tunix\tn\t-\tn\t-\t0\tcleanup";
        $conf[] = "pre-cleanup\tunix\tn\t-\tn\t-\t0\tcleanup{$pre_cleanup_addons}";
        $conf[] = "qmgr\tfifo\tn\t-\tn\t300\t1\tqmgr";
        $conf[] = "tlsmgr\tunix\t-\t-\tn\t1000?\t1\ttlsmgr";
        $conf[] = "rewrite\tunix\t-\t-\tn\t-\t-\ttrivial-rewrite";
        $conf[] = "bounce\tunix\t-\t-\tn\t-\t0\tbounce";
        $conf[] = "defer\tunix\t-\t-\tn\t-\t0\tbounce";
        $conf[] = "trace\tunix\t-\t-\tn\t-\t0\tbounce";
        $conf[] = "verify\tunix\t-\t-\tn\t-\t1\tverify";
        $conf[] = "flush\tunix\tn\t-\tn\t1000?\t0\tflush";
        $conf[] = "proxymap\tunix\t-\t-\tn\t-\t-\tproxymap";
        $conf[] = "proxywrite\tunix\t-\t-\tn\t-\t1\tproxymap";
        $conf[] = "smtp\tunix\t-\t-\tn\t-\t-\tsmtp";
        $conf[] = "relay\tunix\t-\t-\tn\t-\t-\tsmtp -o fallback_relay=";
        $conf[] = "showq\tunix\tn\t-\tn\t-\t-\tshowq";
        $conf[] = "error\tunix\t-\t-\tn\t-\t-\terror";
        $conf[] = "discard\tunix\t-\t-\tn\t-\t-\tdiscard";
        $conf[] = "local\tunix\t-\tn\tn\t-\t-\tlocal";
        $conf[] = "virtual\tunix\t-\tn\tn\t-\t-\tvirtual";
        $conf[] = "lmtp\tunix\t-\t-\tn\t-\t-\tlmtp";
        $conf[] = "anvil\tunix\t-\t-\tn\t-\t1\tanvil";
        $conf[] = "scache\tunix\t-\t-\tn\t-\t1\tscache";
        $conf[] = "scan\tunix\t-\t-\tn\t\t-\t10\tsm -v";
        $conf[] = "maildrop\tunix\t-\tn\tn\t-\t-\tpipe ";
        $conf[] = "retry\tunix\t-\t-\tn\t-\t-\terror ";
        $conf[] = "uucp\tunix\t-\tn\tn\t-\t-\tpipe flags=Fqhu user=uucp argv=uux -r -n -z -a\$sender - \$nexthop!rmail (\$recipient)";
        $conf[] = "ifmail\tunix\t-\tn\tn\t-\t-\tpipe flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r \$nexthop (\$recipient)";
        $conf[] = "bsmtp\tunix\t-\tn\tn\t-\t-\tpipe flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t\$nexthop -f\$sender \$recipient";
    } else {
        if (!isset($MASTER_CF_DEFINED["pickup"])) {
            $conf[] = "pickup\tfifo\tn\t-\tn\t60\t1\tpickup{$re_cleanup_infos}";
        }
        if (!isset($MASTER_CF_DEFINED["cleanup"])) {
            $conf[] = "cleanup\tunix\tn\t-\tn\t-\t0\tcleanup";
        }
        if (!isset($MASTER_CF_DEFINED["pre-cleanup"])) {
            $conf[] = "pre-cleanup\tunix\tn\t-\tn\t-\t0\tcleanup{$pre_cleanup_addons}";
        }
        if (!isset($MASTER_CF_DEFINED["qmgr"])) {
            $conf[] = "qmgr\tfifo\tn\t-\tn\t300\t1\tqmgr";
        }
        if (!isset($MASTER_CF_DEFINED["rewrite"])) {
            $conf[] = "rewrite\tunix\t-\t-\tn\t-\t-\ttrivial-rewrite";
        }
        if (!isset($MASTER_CF_DEFINED["bounce"])) {
            $conf[] = "bounce\tunix\t-\t-\tn\t-\t0\tbounce";
        }
        if (!isset($MASTER_CF_DEFINED["defer"])) {
            $conf[] = "defer\tunix\t-\t-\tn\t-\t0\tbounce";
        }
        if (!isset($MASTER_CF_DEFINED["trace"])) {
            $conf[] = "trace\tunix\t-\t-\tn\t-\t0\tbounce";
        }
        if (!isset($MASTER_CF_DEFINED["verify"])) {
            $conf[] = "verify\tunix\t-\t-\tn\t-\t1\tverify";
        }
        if (!isset($MASTER_CF_DEFINED["flush"])) {
            $conf[] = "flush\tunix\tn\t-\tn\t1000?\t0\tflush";
        }
        if (!isset($MASTER_CF_DEFINED["proxymap"])) {
            $conf[] = "proxymap\tunix\t-\t-\tn\t-\t-\tproxymap";
        }
        if (!isset($MASTER_CF_DEFINED["proxywrite"])) {
            $conf[] = "proxywrite\tunix\t-\t-\tn\t-\t1\tproxymap";
        }
        if (!isset($MASTER_CF_DEFINED["smtp"])) {
            $conf[] = "smtp\tunix\t-\t-\tn\t-\t-\tsmtp";
        }
        if (!isset($MASTER_CF_DEFINED["relay"])) {
            $conf[] = "relay\tunix\t-\t-\tn\t-\t-\tsmtp -o fallback_relay=";
        }
        if (!isset($MASTER_CF_DEFINED["showq"])) {
            $conf[] = "showq\tunix\tn\t-\tn\t-\t-\tshowq";
        }
        if (!isset($MASTER_CF_DEFINED["error"])) {
            $conf[] = "error\tunix\t-\t-\tn\t-\t-\terror";
        }
        if (!isset($MASTER_CF_DEFINED["discard"])) {
            $conf[] = "discard\tunix\t-\t-\tn\t-\t-\tdiscard";
        }
        if (!isset($MASTER_CF_DEFINED["local"])) {
            $conf[] = "local\tunix\t-\tn\tn\t-\t-\tlocal";
        }
        if (!isset($MASTER_CF_DEFINED["virtual"])) {
            $conf[] = "virtual\tunix\t-\tn\tn\t-\t-\tvirtual";
        }
        if (!isset($MASTER_CF_DEFINED["lmtp"])) {
            $conf[] = "lmtp\tunix\t-\t-\tn\t-\t-\tlmtp";
        }
        if (!isset($MASTER_CF_DEFINED["anvil"])) {
            $conf[] = "anvil\tunix\t-\t-\tn\t-\t1\tanvil";
        }
        if (!isset($MASTER_CF_DEFINED["scache"])) {
            $conf[] = "scache\tunix\t-\t-\tn\t-\t1\tscache";
        }
        if (!isset($MASTER_CF_DEFINED["scan"])) {
            $conf[] = "scan\tunix\t-\t-\tn\t\t-\t10\tsm -v";
        }
        if (!isset($MASTER_CF_DEFINED["maildrop"])) {
            $conf[] = "maildrop\tunix\t-\tn\tn\t-\t-\tpipe ";
        }
        if (!isset($MASTER_CF_DEFINED["retry"])) {
            $conf[] = "retry\tunix\t-\t-\tn\t-\t-\terror ";
        }
        if (!isset($MASTER_CF_DEFINED["uucp"])) {
            $conf[] = "uucp\tunix\t-\tn\tn\t-\t-\tpipe flags=Fqhu user=uucp argv=uux -r -n -z -a\$sender - \$nexthop!rmail (\$recipient)";
        }
        if (!isset($MASTER_CF_DEFINED["ifmail"])) {
            $conf[] = "ifmail\tunix\t-\tn\tn\t-\t-\tpipe flags=F user=ftn argv=/usr/lib/ifmail/ifmail -r \$nexthop (\$recipient)";
        }
        if (!isset($MASTER_CF_DEFINED["bsmtp"])) {
            $conf[] = "bsmtp\tunix\t-\tn\tn\t-\t-\tpipe flags=Fq. user=bsmtp argv=/usr/lib/bsmtp/bsmtp -t\$nexthop -f\$sender \$recipient";
        }
    }
    while (list($service, $MFARRY) = each($MASTER_CF_DEFINED)) {
        $MFARRY["MAXPROC"] = intval($MFARRY["MAXPROC"]);
        $conf[] = "{$service}\t{$MFARRY["TYPE"]}\t{$MFARRY["PRIVATE"]}\t{$MFARRY["UNIPRIV"]}\t{$MFARRY["CHROOT"]}\t{$MFARRY["WAKEUP"]}\t{$MFARRY["MAXPROC"]}\t{$MFARRY["COMMAND"]}";
        echo "Starting......: " . date("H:i:s") . " master.cf adding {$service} ({$MFARRY["TYPE"]})\n";
    }
    $conf[] = "mailman\tunix\t-\tn\tn\t-\t-\tpipe flags=FR user=mail:mail argv=/etc/mailman/postfix-to-mailman.py \${nexthop} \${mailbox}";
    $conf[] = "artica-whitelist\tunix\t-\tn\tn\t-\t-\tpipe flags=F  user=mail argv=/usr/share/artica-postfix/bin/artica-whitelist -a \${nexthop} -s \${sender} --white";
    $conf[] = "artica-blacklist\tunix\t-\tn\tn\t-\t-\tpipe flags=F  user=mail argv=/usr/share/artica-postfix/bin/artica-whitelist -a \${nexthop} -s \${sender} --black";
    $conf[] = "artica-reportwbl\tunix\t-\tn\tn\t-\t-\tpipe flags=F  user=mail argv=/usr/share/artica-postfix/bin/artica-whitelist -a \${nexthop} -s \${sender} --report";
    $conf[] = "artica-reportquar\tunix\t-\tn\tn\t-\t-\tpipe flags=F  user=mail argv=/usr/share/artica-postfix/bin/artica-whitelist -a \${nexthop} -s \${sender} --quarantines";
    $conf[] = "artica-spam\tunix\t-\tn\tn\t-\t-\tpipe flags=F  user=mail argv=/usr/share/artica-postfix/bin/artica-whitelist -a \${nexthop} -s \${sender} --spam";
    $conf[] = "zarafa\tunix\t-\tn\tn\t-\t-\tpipe\tuser=mail argv=/usr/bin/zarafa-dagent \${user}";
    $unix = new unix();
    $cyrdeliver = $unix->find_program("cyrdeliver");
    if (is_file($cyrdeliver)) {
        echo "Starting......: " . date("H:i:s") . " master.cf adding cyrus\n";
        $conf[] = "cyrus\tunix\t-\tn\tn\t-\t-\tpipe\tflags=R user=cyrus argv=/usr/sbin/cyrdeliver -e -m \${extension} \${user}";
    } else {
        $conf[] = "# cyrdeliver no such binary.";
    }
    $conf[] = "";
    $conf[] = "";
    $conf[] = $master_amavis;
    $conf[] = "";
    $conf[] = "127.0.0.1:33559\tinet\tn\t-\tn\t-\t-\tsmtpd";
    $conf[] = "    -o notify_classes=protocol,resource,software";
    $conf[] = "    -o header_checks=";
    $conf[] = "    -o content_filter=";
    $conf[] = "    -o smtpd_restriction_classes=";
    $conf[] = "    -o smtpd_delay_reject=no";
    $conf[] = "    -o smtpd_client_restrictions=permit_mynetworks,reject";
    $conf[] = "    -o smtpd_helo_restrictions=";
    $conf[] = "    -o smtpd_sender_restrictions=";
    $conf[] = "    -o smtpd_recipient_restrictions=permit_mynetworks,reject";
    $conf[] = "    -o smtpd_data_restrictions=reject_unauth_pipelining";
    $conf[] = "    -o smtpd_end_of_data_restrictions=";
    $conf[] = "    -o mynetworks=127.0.0.0/8";
    $conf[] = "    -o strict_rfc821_envelopes=yes";
    $conf[] = "    -o smtpd_error_sleep_time=0";
    $conf[] = "    -o smtpd_soft_error_limit=1001";
    $conf[] = "    -o smtpd_hard_error_limit=1000";
    $conf[] = "    -o smtpd_client_connection_count_limit=0";
    $conf[] = "    -o smtpd_client_connection_rate_limit=0";
    $conf[] = "    -o receive_override_options=no_header_body_checks,no_unknown_recipient_checks";
    $conf[] = "    -o smtp_send_xforward_command=yes";
    $conf[] = "    -o disable_dns_lookups=yes";
    $conf[] = "    -o local_header_rewrite_clients=";
    $conf[] = "    -o smtp_generic_maps=";
    $conf[] = "    -o sender_canonical_maps=";
    $conf[] = "    -o smtpd_milters=";
    $conf[] = "    -o smtpd_sasl_auth_enable=no";
    $conf[] = "    -o smtpd_use_tls=no";
    if ($ver210) {
        $conf[] = "\t -o smtpd_upstream_proxy_protocol=";
    }
    $q = new mysql();
    $sql = "SELECT * FROM sender_dependent_relay_host WHERE enabled=1 \n\t\t\t\tAND `override_transport`=1 \n\t\t\t\tAND `hostname`='master' ORDER by zOrders";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    echo "Starting......: " . date("H:i:s") . " master.cf sender_dependent_relay_host " . mysql_num_rows($results) . " item(s)\n";
    build_progress_sender_routing("{building} master.cf sender_dependent_relay_host", 50);
    $main = new maincf_multi();
    while ($ligne = mysql_fetch_assoc($results)) {
        $domain = $ligne["domain"];
        $md5 = $ligne["zmd5"];
        $relay = $ligne["relay"];
        $relay_port_text = null;
        $relay_port = $ligne["relay_port"];
        $lookups = $ligne["lookups"];
        $relay_text = $main->RelayToPattern($relay, $relay_port, $lookups);
        $conf[] = "";
        $conf[] = "{$md5}\tunix\t-\t-\tn\t-\t-\tsmtp";
        if ($ligne["smtp_bind_address"] != null) {
            $conf[] = "    -o smtp_bind_address={$ligne["smtp_bind_address"]}";
        }
        if ($ligne["smtp_helo_name"] != null) {
            $conf[] = "    -o smtp_helo_name={$ligne["smtp_helo_name"]}";
        }
        if ($ligne["syslog_name"] != null) {
            $ligne["syslog_name"] = str_replace(" ", "-", $ligne["syslog_name"]);
            $conf[] = "    -o syslog_name={$ligne["syslog_name"]}";
        }
        if ($ligne["directmode"] == 0) {
            if ($ligne["relay"] != null) {
                $conf[] = "    -o relayhost={$relay_text}";
                if ($ligne["enabledauth"] == 0) {
                    $conf[] = "    -o smtp_sasl_password_maps=hash:/etc/postfix/smtp_sasl_password";
                    $conf[] = "    -o smtp_sasl_auth_enable=yes";
                }
            }
        } else {
            $conf[] = "    -o relayhost=";
            $conf[] = "    -o smtp_host_lookup=dns";
        }
        //	04 	-o syslog_name=postfix-customer1
    }
    $conf[] = "";
    $conf[] = "";
    build_progress_sender_routing("{building} master.cf {done}", 55);
    @file_put_contents("/etc/postfix/master.cf", @implode("\n", $conf));
    echo "Starting......: " . date("H:i:s") . " master.cf done\n";
    if ($GLOBALS["RELOAD"]) {
        shell_exec("/usr/sbin/postfix reload >/dev/null 2>&1");
    }
    if ($restart_service) {
        build_progress_sender_routing("{restarting_service}", 60);
        shell_exec("{$GLOBALS["postfix"]} stop");
        shell_exec("{$GLOBALS["postfix"]} start");
    }
}