Example #1
0
function build_line($ligne)
{
    $sock = new sockets();
    $unix = new unix();
    $EnablePostfixMultiInstance = $sock->GET_INFO("EnablePostfixMultiInstance");
    $fetchmail_version = fetchmail_version();
    if (!isset($GLOBALS["FetchMailToZarafa"])) {
        $GLOBALS["FetchMailToZarafa"] = $sock->GET_INFO("FetchMailToZarafa");
        if (!is_numeric($GLOBALS["FetchMailToZarafa"])) {
            $GLOBALS["FetchMailToZarafa"] = 1;
        }
    }
    if (!isset($GLOBALS["ZARAFA_D_AGENT_BIN"])) {
        $GLOBALS["ZARAFA_D_AGENT_BIN"] = $unix->find_program("zarafa-dagent");
        if (!is_file($GLOBALS["ZARAFA_D_AGENT_BIN"])) {
            $GLOBALS["FetchMailToZarafa"] = 0;
        }
    }
    if (preg_match("#^([0-9]+)\\.([0-9]+)\\.([0-9]+)#", $fetchmail_version, $re)) {
        $MAJOR = $re[1];
        $MINOR = $re[2];
        $REV = $re[3];
    }
    $ID = $ligne["ID"];
    writelogs("Building fetchmail rule for ID: {$ligne["ID"]},Zarafa Dagent:`{$GLOBALS["ZARAFA_D_AGENT_BIN"]}`  user:{$ligne["uid"]}, FetchMailToZarafa:{$GLOBALS["FetchMailToZarafa"]}", __FUNCTION__, __FILE__, __LINE__);
    $ligne["poll"] = trim($ligne["poll"]);
    if ($ligne["poll"] == null) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} rule {$ligne["ID"]} as no poll, skip it..\n";
        return;
    }
    if ($ligne["proto"] == null) {
        $ligne["proto"] = "auto";
    }
    if ($ligne["uid"] == null) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} rule {$ligne["ID"]} as no uid, skip it..\n";
        return;
    }
    writelogs("Building \$user->user({$ligne["uid"]})", __FUNCTION__, __FILE__, __LINE__);
    $user = new user($ligne["uid"]);
    writelogs("Building {$user->mail}", __FUNCTION__, __FILE__, __LINE__);
    if (trim($user->mail) == null) {
        writelogs("Building fetchmail uid has no mail !!!, skip it.. user:{$ligne["uid"]}", __FUNCTION__, __FILE__, __LINE__);
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} uid has no mail !!!, skip it..\n";
        $unix->send_email_events("Fetchmail rule for {$ligne["uid"]}/{$ligne["poll"]} has been skipped", "cannot read email address from LDAP", "mailbox");
        return;
    }
    $ligne["is"] = $user->mail;
    $smtphost = null;
    $sslfingerprint = null;
    $fetchall = null;
    $timeout = null;
    $port = null;
    $aka = null;
    $folder = null;
    $tracepolls = null;
    $interval = null;
    $keep = null;
    $fetchall = null;
    $sslcertck = null;
    $limit = null;
    $dropdelivered = null;
    $smtpport = null;
    $multidrop = null;
    if ($ligne["proto"] == "httpp") {
        $ligne["proto"] = "pop3";
    }
    if (!isset($ligne["folder"])) {
        $ligne["folder"] = null;
    }
    if (!is_numeric($ligne["UseDefaultSMTP"])) {
        $ligne["UseDefaultSMTP"] = 1;
    }
    if (trim($ligne["port"]) > 0) {
        $port = "port {$ligne["port"]}";
    }
    if (trim($ligne["aka"]) != null) {
        $aka = "\n\taka {$ligne["aka"]}";
    }
    if ($ligne["ssl"] == 1) {
        $ssl = "\n\tssl\n\tsslproto ''";
    }
    if ($ligne["timeout"] > 0) {
        $timeout = "\n\ttimeout {$ligne["timeout"]}";
    }
    if ($ligne["folder"] != null) {
        $folder = "\n\tfolder {$ligne["folder"]}";
    }
    if ($ligne["tracepolls"] == 1) {
        $tracepolls = "\n\ttracepolls";
    }
    if ($ligne["interval"] > 0) {
        $interval = "\n\tinterval {$ligne["interval"]}";
    }
    if ($ligne["keep"] == 1) {
        $keep = "\n\tkeep ";
    }
    if ($ligne["keep"] == 0) {
        $keep = "\n\tnokeep";
    }
    if ($ligne["multidrop"] == 1) {
        $ligne["is"] = "*";
    }
    if ($ligne["fetchall"] == 1) {
        $fetchall = "\n\tfetchall";
    }
    if (strlen(trim($ligne["sslfingerprint"])) > 10) {
        $sslfingerprint = "\n\tsslfingerprint '{$ligne["sslfingerprint"]}'";
    }
    if ($ligne["sslcertck"] == 1) {
        $sslcertck = "\n\tsslcertck";
    }
    if ($GLOBALS["FetchMailGLobalDropDelivered"] == 1) {
        $ligne["dropdelivered"] = 1;
    }
    if (!is_numeric($ligne["limit"])) {
        $ligne["limit"] = 2097152;
    }
    if ($ligne["limit"] == 0) {
        $ligne["limit"] = 2097152;
    }
    if (!isset($ligne["smtp_port"])) {
        $ligne["smtp_port"] = 25;
    }
    if (!isset($ligne["smtp_host"])) {
        $ligne["smtp_host"] = "127.0.0.1";
    }
    if (!is_numeric($ligne["smtp_port"])) {
        $ligne["smtp_port"] = 25;
    }
    if (trim($ligne["smtp_host"]) == null) {
        $ligne["smtp_host"] = "127.0.0.1";
    }
    if ($ligne["smtp_port"] != 25) {
        $smtpport = "/{$ligne["smtp_port"]}";
    }
    $smtp = "\n\tsmtphost {$ligne["smtp_host"]}{$smtpport}";
    $limit = "\n\tlimit {$ligne["limit"]}";
    if ($ligne["dropdelivered"] == 1) {
        $dropdelivered = "\n\tdropdelivered is {$ligne["is"]} here";
    }
    if ($GLOBALS["FetchMailToZarafa"] == 1) {
        if ($ligne["UseDefaultSMTP"] == 1) {
            $smtp = "\n\tmda \"{$GLOBALS["ZARAFA_D_AGENT_BIN"]} {$ligne["uid"]}\"";
        }
    }
    $tf = array();
    $folders = unserialize(base64_decode($ligne["folders"]));
    if ($GLOBALS["VERBOSE"]) {
        echo "Folder: " . count($folders) . " items\n";
    }
    if (is_array($folders)) {
        if (count($folders) > 0) {
            while (list($md, $fenc) = each($folders)) {
                $fff = base64_decode($fenc);
                if ($GLOBALS["VERBOSE"]) {
                    echo "Folder: `{$fff}`\n";
                }
                $tf[] = "\"{$fff}\"";
            }
        }
    }
    if ($GLOBALS["VERBOSE"]) {
        echo "Folder: final -> " . count($folders) . " items\n";
    }
    if (count($tf) > 0) {
        $folder = "\n\tfolder INBOX," . @implode(",", $tf);
    }
    if ($EnablePostfixMultiInstance == 1) {
        if ($GLOBALS["DEBUG"]) {
            echo "multiple instances::poll={$ligne["poll"]} smtp_host={$ligne["smtp_host"]}\n";
        }
        if (strlen(trim($ligne["smtp_host"])) == 0) {
            continue;
        }
        $smtphost = "\n\tsmtphost " . multi_get_smtp_ip($ligne["smtp_host"]);
    }
    echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} poll {$ligne["poll"]} - version {$MAJOR}.{$MINOR}.{$REV}\n";
    if ($MAJOR < 7) {
        if ($MINOR < 4) {
            if ($REV < 21) {
                if (trim($ssl) == null) {
                    $ssl = "\n\tsslproto ssl23\n\tno ssl";
                }
            }
        }
    }
    $pattern = "poll {$ligne["poll"]}{$tracepolls}\n\tproto {$ligne["proto"]} {$port}{$interval}{$timeout}{$aka}\n\tuser \"{$ligne["user"]}\"\n\tpass {$ligne["pass"]}\n\tis {$ligne["is"]}{$dropdelivered}{$folder}{$ssl}{$fetchall}{$keep}{$multidrop}{$sslfingerprint}{$sslcertck}{$smtphost}{$limit}{$smtp}\n\n";
    if ($GLOBALS["DEBUG"]) {
        echo "{$pattern}\n";
    }
    $GLOBALS["multi_smtp"][$ligne["smtp_host"]][] = $pattern;
    echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["SERVICE_NAME"]} poll {$ligne["poll"]} -> {$ligne["user"]} limit " . round($ligne["limit"] / 1024) / 1024 . " Mo\n";
    return $pattern;
}
Example #2
0
function BuildRules()
{
    $sock = new sockets();
    $EnablePostfixMultiInstance = $sock->GET_INFO("EnablePostfixMultiInstance");
    $fetch = new fetchmail();
    $l[] = "set logfile /var/log/fetchmail.log";
    $l[] = "set daemon {$fetch->FetchmailPoolingTime}";
    $l[] = "set postmaster \"{$fetch->FetchmailDaemonPostmaster}\"";
    $l[] = "set idfile \"/var/log/fetchmail.id\"";
    $l[] = "";
    $sql = "SELECT * FROM fetchmail_rules WHERE enabled=1";
    $q = new mysql();
    $results = $q->QUERY_SQL($sql, "artica_backup");
    if (!$q->ok) {
        echo "Starting......: fetchmail saving configuration file FAILED\n";
        return false;
    }
    $array = array();
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $ligne["poll"] = trim($ligne["poll"]);
        if ($ligne["poll"] == null) {
            continue;
        }
        if ($ligne["proto"] == null) {
            continue;
        }
        if ($ligne["uid"] == null) {
            continue;
        }
        $user = new user($ligne["uid"]);
        $ligne["is"] = $user->mail;
        $smtphost = null;
        $sslfingerprint = null;
        $fetchall = null;
        $timeout = null;
        $port = null;
        $aka = null;
        $folder = null;
        $tracepolls = null;
        $interval = null;
        $keep = null;
        $fetchall = null;
        $sslcertck = null;
        if ($ligne["proto"] == "httpp") {
            $ligne["proto"] = "pop3";
        }
        if (trim($ligne["port"]) > 0) {
            $port = "port {$ligne["port"]}";
        }
        if (trim($ligne["aka"]) != null) {
            $aka = "\n\taka {$ligne["aka"]}";
        }
        if ($ligne["ssl"] == 1) {
            $ssl = "\n\tssl\n\tsslproto ''";
        }
        if ($ligne["timeout"] > 0) {
            $timeout = "\n\ttimeout {$ligne["timeout"]}";
        }
        if ($ligne["folder"] != null) {
            $folder = "\n\tfolder {$ligne["folder"]}";
        }
        if ($ligne["tracepolls"] == 1) {
            $tracepolls = "\n\ttracepolls";
        }
        if ($ligne["interval"] > 0) {
            $interval = "\n\\tinterval {$ligne["interval"]}";
        }
        if ($ligne["keep"] == 1) {
            $keep = "\n\tkeep ";
        }
        if ($ligne["nokeep"] == 1) {
            $keep = "\n\tnokeep";
        }
        if ($ligne["multidrop"] == 1) {
            $ligne["is"] = "*";
        }
        if ($ligne["fetchall"] == 1) {
            $fetchall = "\n\tfetchall";
        }
        if (strlen(trim($ligne["sslfingerprint"])) > 10) {
            $sslfingerprint = "\n\tsslfingerprint '{$ligne["sslfingerprint"]}'";
        }
        if ($ligne["sslcertck"] == 1) {
            $sslcertck = "\n\tsslcertck";
        }
        if ($EnablePostfixMultiInstance == 1) {
            if ($GLOBALS["DEBUG"]) {
                echo "multiple instances::poll={$ligne["poll"]} smtp_host={$ligne["smtp_host"]}\n";
            }
            if (strlen(trim($ligne["smtp_host"])) == 0) {
                continue;
            }
            $smtphost = "\n\tsmtphost " . multi_get_smtp_ip($ligne["smtp_host"]);
        }
        if (trim($ssl) == null) {
            $ssl = "\n\tsslproto ssl23\n\tno ssl";
        }
        $pattern = "poll {$ligne["poll"]}{$tracepolls}\n\tproto {$ligne["proto"]} {$port}\n\tuser \"{$ligne["user"]}\"\n\tpass {$ligne["pass"]}\n\tis {$ligne["is"]}{$aka}{$folder}{$ssl}{$fetchall}{$interval}{$timeout}{$keep}{$multidrop}{$sslfingerprint}{$sslcertck}{$smtphost}\n\n";
        if ($GLOBALS["DEBUG"]) {
            echo "{$pattern}\n";
        }
        $multi_smtp[$ligne["smtp_host"]][] = $pattern;
        $l[] = $pattern;
    }
    if ($EnablePostfixMultiInstance == 1) {
        echo "Starting......: fetchmail postfix multiple instances enabled (" . count($multi_smtp) . ") hostnames\n";
        @unlink("/etc/artica-postfix/fetchmail.schedules");
        if (is_array($multi_smtp)) {
            if ($GLOBALS["DEBUG"]) {
                print_r($multi_smtp);
            }
            while (list($hostname, $rules) = each($multi_smtp)) {
                echo "Starting......: fetchmail {$hostname} save rules...\n";
                @file_put_contents("/etc/postfix-{$hostname}/fetchmail.rc", @implode("\n", $rules));
                @chmod("/etc/postfix-{$hostname}/fetchmail.rc", 0600);
                $schedule[] = multi_build_schedule($hostname);
                if (!is_fetchmailset($hostname)) {
                    $restart = true;
                } else {
                    echo "Starting......: fetchmail {$hostname} already scheduled...\n";
                }
            }
            if ($restart) {
                @file_put_contents("/etc/artica-postfix/fetchmail.schedules", @implode("\n", $schedule));
                system("/etc/init.d/artica-postfix restart fcron");
            }
        }
        return;
    }
    if (is_array($l)) {
        $conf = implode("\n", $l);
    } else {
        $conf = null;
    }
    @file_put_contents("/etc/fetchmailrc", $conf);
    @chmod("/etc/fetchmailrc", 600);
    echo "Starting......: fetchmail saving configuration file done\n";
}