<?php

    include_once '../account/session.php';
    include_once '../../scripts/bd.php';
    
    echo '<pre>';
    $cpt=0;
    foreach (new DirectoryIterator('/var/log/pmtaAccRep/') as $file) {
        if ($file->isFile()) {
            $logfile=$file->getFileInfo();
            if(filesize($logfile)>198){
                echo '<br>'.$logfile.'<br>';
                $file=$logfile;
                ini_set("memory_limit","10000M");
                $table = explode("\n", file_get_contents($file));
                echo '<pre>';
                foreach ($table as $row) {
                    $str=explode('"', $row);
                    $row=$str[0].$str[2];
                    $fields=explode(',', $row);
                    if($fields[0]=='b' && $fields[20]!='' && $fields[20]!='0' ){
                        $email_address=$fields[5];
                        $bounce_error=$fields[9];
                        $id_campaign=$fields[20];
                        $query=bd::query("select campaign.id_isp,data_isp.hard_bounce_code,campaign.id_data_list,data_list.data_table from campaign,data_list,data_isp where campaign.id='$id_campaign' && campaign.id_data_list=data_list.id && campaign.id_isp=data_isp.id");
                        $row=mysql_fetch_array($query);
                        $id_data_list=$row['id_data_list'];
                        $data_table=$row['data_table'];
                        $hard_bounce_code=$row['hard_bounce_code'];
                        $date_bounce=date('Y-m-d h:i:s');
                        ob_flush();
                        flush();
                        if(preg_match('#'.$hard_bounce_code.'#i', $bounce_error)){
                            $bounce_error=addslashes($bounce_error);
                            bd::query("INSERT INTO track_hard_bounce VALUES(NULL,'$id_campaign','$id_data_list','$email_address','$bounce_error','$date_bounce')");
                            bd::query("DELETE FROM $data_table WHERE email_address='$email_address'");
                            echo 'HARD BOUNCE: - '.$id_campaign.' - '.$email_address.' - '.$data_table.' - '.$bounce_error.'<br>';
                        }else{
                            $bounce_error=addslashes($bounce_error);
                            bd::query("INSERT INTO track_soft_bounce VALUES(NULL,'$id_campaign','$id_data_list','$email_address','$bounce_error','$date_bounce')");
                            echo 'SOFT BOUNCE: - '.$id_campaign.' - '.$email_address.' - '.$data_table.' - '.$bounce_error.'<br>';
                        }
                    }
                }
                $cpt++;
                bd::query("UPDATE table_test set process_count='$cpt' where id_server='{$_SESSION["id-server"]}'");
            }
            
        }
    }
    echo $cpt;
    echo '</pre>';

