function parse_db($id)
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/" . basename(__FILE__) . ".{$id}.pid";
    $pid = trim(@file_get_contents($pidfile));
    if ($unix->process_exists($pid)) {
        $pid = getmypid();
        echo "[{$pid}]:: Process {$pid} already running...\n";
        die;
    }
    $pid = getmypid();
    file_put_contents($pidfile, $pid);
    $sql = "SELECT * FROM emailing_db_paths WHERE ID={$id}";
    $q = new mysql();
    $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup"));
    $unzipbin = $unix->find_program("unzip");
    if ($unzipbin == null) {
        echo "[{$pid}]:: unzip tool no such file or directory...\n";
        update_status($id, "110", 1, "unzip tool no such file or directory");
        die;
    }
    $zip_path = $ligne["filepath"];
    if (!is_file($zip_path)) {
        echo "[{$pid}]:: {$zip_path} no such file or directory\n";
        update_status($id, "110", 1, "zip db no such file or directory");
        die;
    }
    $tmp_path = "/tmp/emailing-import/{$id}";
    writeevent("using {$tmp_path}", $id);
    @mkdir($tmp_path, 666, true);
    writeevent("Uncompress {$zip_path}", $id);
    echo "{$unzipbin} -o {$zip_path} -d {$tmp_path}\n";
    shell_exec("{$unzipbin} -o {$zip_path} -d {$tmp_path}");
    $files = $unix->DirFiles($tmp_path);
    if (!is_array($files)) {
        update_status($id, "110", 1, "{$zip_path} corrupted or no files stored");
        die;
    }
    while (list($filename, $file_name) = each($files)) {
        writeevent("parsing {$file_name}", $pid);
        $max = $unix->COUNT_LINES_OF_FILE("{$tmp_path}/{$file_name}");
        writeevent("parsing {$file_name} {$max} entries", $pid);
        $handle = @fopen("{$tmp_path}/{$file_name}", "r");
        $ligne["databasename"] = format_mysql_table($ligne["databasename"]);
        $q = new mysql();
        $q->CheckTableEmailingContacts("emailing_{$ligne["databasename"]}");
        $sql = "INSERT INTO emailing_{$ligne["databasename"]} (`gender`,`firstname`, `lastname`,`email`,`phone`,`city`,`cp`,`postaladdress`,`domain`) VALUES";
        if ($handle) {
            while (!feof($handle)) {
                $tw = $tw + 1;
                $count = $count + 1;
                unset($re);
                $buffer = fgets($handle, 4096);
                if (trim($buffer) == null) {
                    continue;
                }
                $buffer = str_replace('"', "", $buffer);
                $buffer = str_replace(';', ",", $buffer);
                $lines = explode(",", addslashes($buffer));
                if (!is_array($lines)) {
                    if (preg_match("#.+?@.+#", $buffer)) {
                        for ($i = 0; $i < 8; $i++) {
                            $lines[$i] = "";
                        }
                        $lines[3] = trim($buffer);
                    }
                }
                if (count($lines) < 2) {
                    if (preg_match("#.+?@.+#", $buffer)) {
                        for ($i = 0; $i < 8; $i++) {
                            $lines[$i] = "";
                        }
                        $lines[3] = trim($buffer);
                    }
                }
                $lines[3] = str_replace(";", ".", $lines[3]);
                $lines[3] = str_replace("?", ".", $lines[3]);
                $lines[3] = str_replace("@.", "@", $lines[3]);
                $lines[3] = str_replace('^', '@', $lines[3]);
                $lines[3] = str_replace(',', '.', $lines[3]);
                if (trim($lines[3]) == null) {
                    writeevent("failed 3:[{$buffer}] [" . __LINE__ . "]", $pid);
                    $GLOBALS["FAILED_CONTACTS"] = $GLOBALS["FAILED_CONTACTS"] + 1;
                    unset($lines);
                    continue;
                }
                if (!preg_match("#(.+?)@(.+?)\\.(.+)#", $lines[3], $re)) {
                    writeevent("failed 3:{$lines[3]} bad email address [" . __LINE__ . "]", $pid);
                    $GLOBALS["FAILED_CONTACTS"] = $GLOBALS["FAILED_CONTACTS"] + 1;
                    unset($lines);
                    continue;
                }
                $domain = $re[2];
                if (substr($domain, strlen($domain) - 1, 1) == '.') {
                    $domain = substr($domain, 0, strlen($domain) - 1);
                }
                if (preg_match("#^\\..+#", $domain)) {
                    writeevent("failed 3:{$lines[3]} bad domain {$domain} [" . __LINE__ . "]", $pid);
                    $GLOBALS["FAILED_CONTACTS"] = $GLOBALS["FAILED_CONTACTS"] + 1;
                    unset($lines);
                    continue;
                }
                if ($GLOBALS["EMAILS"][$lines[3]]) {
                    writeevent("failed 3:{$lines[3]} already exists [" . __LINE__ . "]", $pid);
                    $GLOBALS["FAILED_CONTACTS"] = $GLOBALS["FAILED_CONTACTS"] + 1;
                    continue;
                }
                if (isBlockedMail($lines[3])) {
                    writeevent("failed 3:{$lines[3]} is blacklisted [" . __LINE__ . "]", $pid);
                    $GLOBALS["FAILED_CONTACTS"] = $GLOBALS["FAILED_CONTACTS"] + 1;
                    continue;
                }
                $sqla[] = "('{$lines[0]}',\n\t\t\t\t'{$lines[1]}',\n\t\t\t\t'{$lines[2]}',\n\t\t\t\t'{$lines[3]}',\n\t\t\t\t'{$lines[4]}',\n\t\t\t\t'{$lines[5]}',\n\t\t\t\t'{$lines[6]}',\n\t\t\t\t'{$lines[7]}','{$domain}')";
                $GLOBALS["SUCCESS_CONTACTS"] = $GLOBALS["SUCCESS_CONTACTS"] + 1;
                $GLOBALS["EMAILS"][$lines[3]] = true;
                if (count($GLOBALS["EMAILS"]) > 10000) {
                    $GLOBALS["EMAILS"] = array();
                }
                unset($lines);
                if ($tw > 100) {
                    $fullsql = $sql . "\n" . @implode(",", $sqla);
                    $q = new mysql();
                    $q->QUERY_SQL($fullsql, "artica_backup");
                    if (!$q->ok) {
                        writeevent($id, "{$q->mysql_error}\n{$fullsql}");
                    }
                    unset($sqla);
                    $purc = $count / $max;
                    $purc = $purc * 100;
                    $purc = round($purc, 0);
                    update_status($id, $purc, 0);
                    $tw = 0;
                }
            }
            fclose($handle);
            if (is_array($sqla)) {
                $fullsql = $sql . "\n" . @implode(",", $sqla);
                $q->QUERY_SQL($fullsql, "artica_backup");
                if (!$q->ok) {
                    writeevent($id, "{$q->mysql_error}\n{$fullsql}");
                }
            }
        }
    }
    if ($GLOBALS["SUCCESS_CONTACTS"] > 0) {
        writeevent("Failed.:{$GLOBALS["FAILED_CONTACTS"]}", $id);
        writeevent("Success:{$GLOBALS["SUCCESS_CONTACTS"]}", $id);
        update_status($id, 100, 1, null);
    } else {
        update_status($id, 110, 0, null);
    }
}
Exemple #2
0
function construct_queue($ID, $ou)
{
    $q = new mysql();
    $GLOBALS["campain_linker_id"] = $ID;
    $q->QUERY_SQL("UPDATE `emailing_campain_linker` SET `queue_builder_pourc`=0 `locked`=1 WHERE `ID`={$GLOBALS["campain_linker_id"]}", "artica_backup");
    $sql = "SELECT * FROM emailing_campain_linker WHERE ID='{$ID}'";
    $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "artica_backup"));
    $unix = new unix();
    $email_relay_submit = $unix->find_program("emailrelay-submit");
    $GLOBALS["SartON"] = time();
    if ($email_relay_submit == null) {
        event(__FUNCTION__, __LINE__, "Unable to stat {$email_relay_submit}");
        mass_mailing_finish(0);
        return;
    }
    $parameters = unserialize(base64_decode($ligne["parameters"]));
    $template_id = $ligne["template_id"];
    $database_id = $ligne["database_id"];
    $GLOBALS["template_id"] = $template_id;
    $GLOBALS["database_id"] = $database_id;
    $simulation = $parameters["simulation"];
    $GLOBALS["start_date_task"] = date('Y-m-d H:i:s');
    $simulation_recipipent = $parameters["recipient"];
    $database_name = emailing_get_database_name($database_id);
    event(__FUNCTION__, __LINE__, "database name={$database_name}");
    $database_name = format_mysql_table($database_name);
    $template_name = emailing_get_template_name($template_id);
    $template_parameters = emailing_get_template_parameters($template_id);
    event(__FUNCTION__, __LINE__, "Opening {$ID} {$ligne["name"]} campaign template {$template_name} N.{$template_id}, database {$database_name} N.{$database_id}");
    event(__FUNCTION__, __LINE__, "simulation={$simulation} =>{$simulation_recipipent}");
    @mkdir("/tmp/emailing/{$ID}", 0666, true);
    if (is_array($template_parameters["ATTACHS"])) {
        while (list($filename, $attch_ID) = each($template_parameters["ATTACHS"])) {
            event(__FUNCTION__, __LINE__, "saving /tmp/emailing/{$ID}/{$filename}");
            emailing_save_attachment($attch_ID, "/tmp/emailing/{$ID}/{$filename}");
        }
        reset($template_parameters["ATTACHS"]);
    }
    $advanced_options = unserialize(base64_decode($template_parameters["advopts"]));
    if ($advanced_options["From_name"] == null) {
        $advanced_options["From_name"] = $advanced_options["From"];
    }
    if ($advanced_options["From"] == null) {
        event(__FUNCTION__, __LINE__, "From field is null, aborting");
        mass_mailing_finish(0);
        return false;
    }
    $sql = "SELECT ID,smtpserver,parameters FROM emailing_mailers WHERE ou='{$ou}'";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $datas = unserialize(base64_decode($ligne["parameters"]));
        $smtp_servs[] = $ligne["ID"];
    }
    if (!is_array($smtp_servs)) {
        event(__FUNCTION__, __LINE__, "Unable to define SMTP servers");
        mass_mailing_finish(0);
        return false;
    }
    $sql = "SELECT * FROM emailing_{$database_name}";
    $results = $q->QUERY_SQL($sql, "artica_backup");
    $t = 0;
    $max = mysql_num_rows($results);
    event(__FUNCTION__, __LINE__, "{$max} contacts in emailing_{$database_name} table");
    $smtp_servs_count = 0;
    $smtp_servs_max = count($smtp_servs) - 1;
    $messages_number = 0;
    event(__FUNCTION__, __LINE__, $smtp_servs_max + 1 . " SMTP servers");
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $final_count = $final_count + 1;
        $contact_id = $ligne["ID"];
        $gender = $ligne["gender"];
        $firstname = $ligne["firstname"];
        $lastname = $ligne["lastname"];
        $email = $ligne["email"];
        if (isBlockedMail($email)) {
            continue;
        }
        $phone = $ligne["phone"];
        $city = $ligne["city"];
        $cp = $ligne["cp"];
        $postaladdress = $ligne["postaladdress"];
        if ($simulation == 1) {
            $email = $simulation_recipipent;
        }
        $unikey = md5("{$template_id}{$database_id}{$contact_id}");
        $htmldatas = $template_parameters["template_datas"];
        $htmldatas = str_replace("%gender%", $gender, $htmldatas);
        $htmldatas = str_replace("%lastname%", $lastname, $htmldatas);
        $htmldatas = str_replace("%firstname%", $firstname, $htmldatas);
        $htmldatas = str_replace("%email%", $email, $htmldatas);
        $htmldatas = str_replace("%phone%", $phone, $htmldatas);
        $htmldatas = str_replace("%city%", $city, $htmldatas);
        $htmldatas = str_replace("%cp%", $cp, $htmldatas);
        $htmldatas = str_replace("%postaladdress%", $postaladdress, $htmldatas);
        $mailer = new PHPMailer();
        $mailer->AddAddress($email, "{$firstname} {$lastname}");
        $mailer->From = $advanced_options["From"];
        $mailer->FromName = $advanced_options["From_name"];
        $mailer->Subject = $template_parameters["subject"];
        $mailer->Body = $htmldatas;
        $mailer->IsHTML(true);
        $mailer->Mailer = 'smtp';
        if ($advanced_options["Reply-to"] != null) {
            $mailer->AddReplyTo($advanced_options["Reply-to"]);
        }
        if ($advanced_options["Disposition-Notification-To"] != null) {
            $mailer->AddCustomHeader("Disposition-Notification-To: {$advanced_options["Disposition-Notification-To"]}");
        }
        if ($advanced_options["Return-Path"] != null) {
            $mailer->AddCustomHeader("Return-Path: {$advanced_options["Return-Path"]}");
        }
        if ($advanced_options["X-Mailer"] != null) {
            $mailer->AddCustomHeader("X-Mailer: {$advanced_options["X-Mailer"]}");
        }
        if (is_array($template_parameters["ATTACHS"])) {
            while (list($filename, $attch_ID) = each($template_parameters["ATTACHS"])) {
                $mailer->AddAttachment("/tmp/emailing/{$ID}/{$filename}", $filename);
            }
            reset($template_parameters["ATTACHS"]);
        }
        $queue_ID = "/var/spool/artica-emailing/queues/{$smtp_servs[$smtp_servs_count]}";
        $smtp_servs_count = $smtp_servs_count + 1;
        if ($smtp_servs_count > $smtp_servs_max) {
            $smtp_servs_count = 0;
        }
        $maildata = $mailer->Send(true);
        // send mail into queue
        $mail_from = "{$advanced_options["From"]}";
        $recipient = $email;
        $filetemp = $unix->FILE_TEMP() . "{$contact_id}.msg";
        @file_put_contents($filetemp, $maildata);
        $GLOBALS["massmailing_size"] = $GLOBALS["massmailing_size"] + filesize($filetemp);
        $cmd = "{$email_relay_submit} --spool-dir={$queue_ID} --from={$mail_from} {$recipient} < {$filetemp}";
        if ($GLOBALS["VERBOSE"]) {
            event(__FUNCTION__, __LINE__, "{$cmd}");
        }
        shell_exec("{$email_relay_submit} --spool-dir={$queue_ID} --from={$mail_from} {$recipient} < {$filetemp}");
        if ($GLOBALS["VERBOSE"]) {
            event(__FUNCTION__, __LINE__, @implode("\n", $results));
        }
        @unlink($filetemp);
        $maildata = null;
        $mailer = nil;
        $GLOBALS["messages_number"] = $GLOBALS["messages_number"] + 1;
        $t = $t + 1;
        if ($t > 200) {
            $pourcentage = round($final_count / $max * 100, 0);
            echo "{$pourcentage}% {$final_count}/{$max}\n";
            $q = new mysql();
            event(__FUNCTION__, __LINE__, "{$pourcentage}%");
            $q->QUERY_SQL("UPDATE emailing_campain_linker SET queue_builder_pourc={$pourcentage} WHERE ID={$ID}", "artica_backup");
            $t = 0;
        }
    }
    mass_mailing_finish(1);
}