<?php
	include_once '../../../scripts/bd.php';
    include_once '../../../scripts/bdd.php';
    
    if(isset($_GET['action']))
	{
        $action=$_GET['action'];
        if($action=='print_data')print_data();
        if($action=='md5_data')md5_data();
        if($action=='test')test();
		
		
		if($action=='get_offers')
		{
			
			if(    (isset($_GET['id_sponsor']))    and     (is_numeric($_GET['id_sponsor']))     and     ($_GET['id_sponsor'] >0) )
			{
				$id_sponsor	=	$_GET['id_sponsor'];
				$offers 	= 	get_offers_by_id_sponsor($id_sponsor);
				if($offers);
					echo $offers; 
			}
		}
	}
    
    function print_data(){
        global $bdd;
        if(isset($_GET['api_key']) && isset($_GET['id_campaign']) && isset($_GET['id_offer']) && isset($_GET['id_isp']) && isset($_GET['id_data_list']) && isset($_GET['data_from']) && isset($_GET['data_count'])){
            if(isset($_GET["global"])) {
            	 $query=bd::query("SELECT api_key from campaign_global where id='{$_GET["id_campaign"]}' && api_key='{$_GET["api_key"]}'");
            } else {
            	 $query=bd::query("SELECT api_key from campaign where id='{$_GET["id_campaign"]}' && api_key='{$_GET["api_key"]}'");
            }
           
            if(mysql_num_rows($query)>0){
            	$pass = null;
            	if($_GET['id_isp'] == 0 AND $_GET['id_data_list'] == 0) {
            		if($_GET['data_country'] == 'UK') {
				    	$regex = 'hotmail.co.uk|live.co.uk|outlook.co.uk|msn.co.uk';
				    } else {
				    	$regex = 'hotmail.com|live.com|outlook.com|msn.com';
				    }
            		$query_data = bd::query("SELECT * FROM hotmail_mesage_id WHERE email_address REGEXP '$regex' LIMIT {$_GET["data_from"]},{$_GET["data_count"]}");
            		$pass = true;
            	} else {
            		$query=bd::query("SELECT data_table from data_list where id='{$_GET["id_data_list"]}'");
	                $row=mysql_fetch_array($query);
	                $data_table=$row['data_table'];
	                $query = bd::query("SELECT regex FROM data_isp WHERE id='{$_GET["id_isp"]}'");
	                $row=  mysql_fetch_array($query);
	                $regexisp=$row['regex'];
	                ini_set('memory_limit', '1024M');
	                $query_data = bd::query("SELECT id,email_address,email_md5 FROM $data_table WHERE email_address REGEXP '$regexisp' LIMIT {$_GET["data_from"]},{$_GET["data_count"]}");
            	}
                
                $fh = fopen('/home/suppression/list_virtual/'.$_GET["id_campaign"], 'w');
                while ($row = mysql_fetch_array($query_data)){
                    if(!empty($row['email_address']) && !empty($row['email_md5'])) {
                    	if(is_null($pass)) {
                    		fwrite($fh,trim($row['id']).','.trim($row['email_address']).','.trim($row['email_md5']).",".trim($_GET["id_data_list"])."\r\n");
                    	} else {
                    		fwrite($fh,trim($row['id']).','.trim($row['email_address']).','.trim($row['email_md5']).",HOTMAIL_".uniqid().",".$row['message_id']."\r\n");
                    	}
                        
                    }
                }
                $arrayData = array();
                foreach(file('/home/suppression/list_virtual/'.$_GET["id_campaign"]) as $file) {
                    $arrayData[] = $file;
                }
                $arraySuppFile = array();
                foreach(file('/home/suppression/offer/'.$_GET["id_offer"].'/list') as $file) {
                    $arraySuppFile[] = $file;
                }
				
				$count = count($arrayData);
				for ($i = 0; $i < $count; $i++){
				    $valeurATester = trim($arraySuppFile[$i]);
				    if (strpos($arrayData[$i], $valeurATester) == true) {
				        unset($arrayData[$i]);
				    }
				}
				unlink('/home/suppression/list_virtual/'.$_GET["id_campaign"]);
				echo json_encode($arrayData);
            }else{
                echo "<div class='text-error'>ZOoOoOoT FiIiIiIiK</div>";
            }

        }else{
            echo "<div class='text-error'>Check suppression file failed</div>";
        }
        
        ///////////////////////////////////////////////////////////////
    }
    
    function md5_data(){
        $query=bd::query("SELECT id,email_address,email_md5 from list_mdm_1");
        while ($row = mysql_fetch_array($query)) {
            $id=$row['id'];
            if(empty($row['email_md5'])){
                $email_md5=md5($row['email_address']);
                bd::query("UPDATE list_mdm_1 SET email_md5='$email_md5' WHERE id='$id'");
            }
        }
        
        echo 'F8INIch';
    }
	
	
	function get_offers_by_id_sponsor($p_id_sponsor)
	{
		$result	=	null;
		if( is_numeric($p_id_sponsor) and ($p_id_sponsor > 0) )
		{
			include '../../../scripts/bdd.php';
			$sqlGetOffersByIdSponsor	=	'SELECT O.id,O.name FROM offer O WHERE O.active = ? and O.id_sponsor = ?';
			$cmdGetOffersByIdSponsor	=	$bdd->prepare($sqlGetOffersByIdSponsor);
			$cmdGetOffersByIdSponsor->execute(array(1,$p_id_sponsor));
			$strCombo		=	'';
			while($offers	=	$cmdGetOffersByIdSponsor->fetch())
			{
				$strCombo.='<option value="'.$offers["id"].'">'.$offers["name"].'</option>';
			}
			$cmdGetOffersByIdSponsor->closeCursor();
			
			
			$result	=	$strCombo;
		}
		else
		{
			$result	=	'get_offers_by_id_sponsor() : le paramètre $p_id_sponsor ==>  | is not numeric | < 0 | ';
		}
		
		return $result;
	}

