<?php
    
    include_once '../account/session.php';
    include_once '../../scripts/bd.php';
    
    
    
    /*$bounce_msg_aol="Recipient address rejected: aol.com";
    $bounce_msg_hotmail="550 Requested action not taken: mailbox unavailable";
    $bounce_msg_gmail="550-5.1.1 The email account that you tried to reach does not exist. Please try";
    $bounce_msg_yahoo="554 delivery error: dd This user doesn't have";
    $bounce_msg_global=">>> 550";*/
    
    //clean_bounce_campaign(72);
    
    
    function clean_bounce_campaign($id_campaign){
        $query=bd::query("select id_isp from campaign where id='$id_campaign'");
        $row= mysql_fetch_array($query);
        $isp=$row['id_isp'];
        if($isp=="1") clean_bounce_hotmail($id_campaign);
        if($isp=="2") clean_bounce_gmail($id_campaign);
        if($isp=="3") clean_bounce_yahoo($id_campaign);
        if($isp=="4") clean_bounce_aol($id_campaign);
    }
    
    function clean_bounce_hotmail($id_campaign){
        echo '<pre>';
        $bounce_msg="Requested action not taken: mailbox unavailable";
        $query=bd::query("select campaign.id_isp,campaign.id_data_list,campaign.data_from,campaign.data_count,data_isp.regex,data_list.data_table from campaign,data_isp,data_list where campaign.id='$id_campaign' && campaign.id_data_list=data_list.id && campaign.id_isp=data_isp.id");
        $row=mysql_fetch_array($query);
        $regexisp=$row['regex'];
        $id_data_list=$row['id_data_list'];
        $data_table=$row['data_table'];
        $data_from=$row['data_from'];
        $data_count=$row['data_count'];
        
        ini_set("memory_limit","10000M");
        $table = explode("\n", file_get_contents('/var/log/pmta/log'));
        
        $ok=true;
        for($i=0;$i<count($table);$i++) {
            if(stristr($table[$i],$bounce_msg)){
                $strspace=explode(" ",$table[$i]);
                $strwriter=explode(">>> ",$table[$i]);
                $date_bounce=trim($strspace[0]." ".$strspace[1]);
                $error=addslashes(trim($strwriter[1]));
                $j=$i-1;
                $ok=true;
                while($ok){
                    if(stristr($table[$j],'<<< RCPT TO:')){
                        $ok=false;
                        $str=explode("RCPT TO:",$table[$j]);
                        $str[1]=str_replace(array("<",">"),"", $str[1]);
                        $user=trim($str[1]);
                        $querymail=bd::query("SELECT id FROM $data_table WHERE email_address REGEXP '$regexisp' && email_address like '$user' LIMIT $data_from,$data_count");
                        if(mysql_num_rows($querymail)>0){
                            $querybounce=bd::query("SELECT id FROM track_bounce WHERE email_address like '$user' && id_campaign='$id_campaign'");
                            if(mysql_num_rows($querybounce)<=0){
                                echo $user.'<br>';
                                $row = mysql_fetch_array($querymail);
                                bd::query("INSERT INTO track_bounce VALUES(NULL,'$id_campaign','$id_data_list','{$row['id']}','$user','$error','$date_bounce')");
                            }
                        }
                    }
                    $j--;
                }
             }
        }
        echo '</pre>';
    }
    
    function clean_bounce_gmail($id_campaign){
        echo '<pre>';
        $bounce_msg="The email account that you tried to reach does not exist. Please try";
         $query=bd::query("select campaign.id_isp,campaign.id_data_list,campaign.data_from,campaign.data_count,data_isp.regex,data_list.data_table from campaign,data_isp,data_list where campaign.id='$id_campaign' && campaign.id_data_list=data_list.id && campaign.id_isp=data_isp.id");
        $row=mysql_fetch_array($query);
        $regexisp=$row['regex'];
        $id_data_list=$row['id_data_list'];
        $data_table=$row['data_table'];
        $data_from=$row['data_from'];
        $data_count=$row['data_count'];
        
        ini_set("memory_limit","10000M");
        $table = explode("\n", file_get_contents('/var/log/pmta/log'));
        
        $ok=true;
        for($i=0;$i<count($table);$i++) {
            if(stristr($table[$i],$bounce_msg)){
                $strspace=explode(" ",$table[$i]);
                $strwriter=explode(">>> ",$table[$i]);
                $date_bounce=trim($strspace[0]." ".$strspace[1]);
                $error=addslashes(trim($strwriter[1]));
                $j=$i-1;
                $ok=true;
                while($ok){
                    if(stristr($table[$j],'<<< RCPT TO:')){
                        $ok=false;
                        $str=explode("RCPT TO:",$table[$j]);
                        $str[1]=str_replace(array("<",">"),"", $str[1]);
                        $user=trim($str[1]);
                        $querymail=bd::query("SELECT id FROM $data_table WHERE email_address REGEXP '$regexisp' && email_address like '$user' LIMIT $data_from,$data_count");
                        if(mysql_num_rows($querymail)>0){
                            $querybounce=bd::query("SELECT id FROM track_bounce WHERE email_address like '$user' && id_campaign='$id_campaign'");
                            if(mysql_num_rows($querybounce)<=0){
                                echo $user.'<br>';
                                $row = mysql_fetch_array($querymail);
                                bd::query("INSERT INTO track_bounce VALUES(NULL,'$id_campaign','$id_data_list','{$row['id']}','$user','$error','$date_bounce')");
                            }
                        }
                    }
                    $j--;
                }
             }
        }
        echo '</pre>';
    }
    
    function clean_bounce_yahoo($id_campaign){
        echo '<pre>';
        $bounce_msg="delivery error: dd This user doesn't have";
        $query=bd::query("select campaign.id_isp,campaign.id_data_list,campaign.data_from,campaign.data_count,data_isp.regex,data_list.data_table from campaign,data_isp,data_list where campaign.id='$id_campaign' && campaign.id_data_list=data_list.id && campaign.id_isp=data_isp.id");
        $row=mysql_fetch_array($query);
        $regexisp=$row['regex'];
        $id_data_list=$row['id_data_list'];
        $data_table=$row['data_table'];
        $data_from=$row['data_from'];
        $data_count=$row['data_count'];
        
        ini_set("memory_limit","10000M");
        $table = explode("\n", file_get_contents('/var/log/pmta/log'));
        
        for($i=0;$i<count($table);$i++) {
            if(stristr($table[$i],$bounce_msg)){
                $strspace=explode(" ",$table[$i]);
                $strwriter=explode(">>> ",$table[$i]);
                $date_bounce=trim($strspace[0]." ".$strspace[1]);
                $error=addslashes(trim($strwriter[1]));
                $strbracket=explode("(",$error);
                $strbracket=explode(")",$strbracket[1]);
                $user=trim($strbracket[0]);
                $querymail=bd::query("SELECT id FROM $data_table WHERE email_address REGEXP '$regexisp' && email_address like '$user' LIMIT $data_from,$data_count");
                if(mysql_num_rows($querymail)>0){
                    echo $user.'<br>';
                    $row = mysql_fetch_array($querymail);
                    bd::query("INSERT INTO track_bounce VALUES(NULL,'$id_campaign','$id_data_list','{$row['id']}','$user','$error','$date_bounce')");
                }
             }
        }
        echo '</pre>';
    }
    
    function clean_bounce_aol($id_campaign){
        echo '<pre>';
        $bounce_msg="Recipient address rejected: aol.com";
        $query=bd::query("select campaign.id_isp,campaign.id_data_list,campaign.data_from,campaign.data_count,data_isp.regex,data_list.data_table from campaign,data_isp,data_list where campaign.id='$id_campaign' && campaign.id_data_list=data_list.id && campaign.id_isp=data_isp.id");
        $row=mysql_fetch_array($query);
        $regexisp=$row['regex'];
        $id_data_list=$row['id_data_list'];
        $data_table=$row['data_table'];
        $data_from=$row['data_from'];
        $data_count=$row['data_count'];
        
        ini_set("memory_limit","10000M");
        $table = explode("\n", file_get_contents('/var/log/pmta/log'));
        
        for($i=0;$i<count($table);$i++) {
            if(stristr($table[$i],$bounce_msg)){
                $strspace=explode(" ",$table[$i]);
                $strwriter=explode(">>> ",$table[$i]);
                $date_bounce=trim($strspace[0]." ".$strspace[1]);
                $error=addslashes(trim($strwriter[1]));
                $strbracket=explode("(",$error);
                $strbracket=explode(")",$strbracket[1]);
                $user=trim($strbracket[0]);
                $querymail=bd::query("SELECT id FROM $data_table WHERE email_address REGEXP '$regexisp' && email_address like '$user' LIMIT $data_from,$data_count");
                if(mysql_num_rows($querymail)>0){
                    echo $user.'<br>';
                    $row = mysql_fetch_array($querymail);
                    bd::query("INSERT INTO track_bounce VALUES(NULL,'$id_campaign','$id_data_list','{$row['id']}','$user','$error','$date_bounce')");
                }
             }
        }
        echo '</pre>';
    }










