<?php
   include_once '../account/session.php';
   include_once '../../scripts/bd.php';
   session_start();
   $id_server=$_SESSION['id-server'];
   $id_mailer=$_SESSION['id-mailer'];
   $username_mailer=$_SESSION['username-mailer'];
   
   if(isset($_GET['action'])){
    $action=$_GET['action'];
    if($action=='get_servers')get_servers();
    if($action=='get_domains')get_domains();
    if($action=='get_vmtas')get_vmtas();
    if($action=='get_sponsors')get_sponsors();
     if($action=='get_offers')get_offers();
    if($action=='get_fname')get_fname();
    if($action=='get_subject')get_subject();
    if($action=='get_news')get_news();
    if($action=='get_isps')get_isps();
    if($action=='get_data_lists')get_data_lists();
    if($action=='show')show();
    if($action=='edit')edit();
    if($action=='stop_campaign')stop_campaign();
    if($action=='get_real_sent')get_real_sent();
    if($action=='get_real_status')get_real_status();
   }
   
   function get_servers(){
    if(isset($_GET["servers"]) AND !empty($_GET["servers"])) {
      $servers = explode(';',$_GET["servers"]);
          $data = array();
          $server_in = '';
          foreach($servers as $server) {
            $server_in .= $server.';';
          }
          $server_in = str_replace(';', ',', $server_in);
          $query = bd::query("SELECT id,name FROM server WHERE active='1' and id in (".rtrim($server_in,',').")");
           while ($row = mysql_fetch_object($query)) {
              $data[]=$row;
          }
          echo json_encode($data);
    } else {
      $data=array();
      $query = bd::query("SELECT id,name FROM server WHERE active='1'");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
    }
      
   }
   
   function get_domains(){
      $data=array();
      $query = bd::query("SELECT host_name FROM server_vmta WHERE id_server='{$_GET["server"]}' && active='1'");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   
   function get_vmtas(){
        if(isset($_GET["servers"]) AND !empty($_GET["servers"])) {
          $servers = explode(';',$_GET["servers"]);
          $data = array();
          $server_in = '';
          foreach($servers as $server) {
            $server_in .= $server.';';
          }
          $server_in = str_replace(';', ',', $server_in);
          $query = bd::query("SELECT V.id,V.name,V.address_ip,V.host_name,S.id as id_server,S.name as name_server,S.main_ip FROM server_vmta V,server S WHERE S.id=V.id_server and  id_server in (".rtrim($server_in,',').") && S.active=1 && V.active=1;");
           while ($row = mysql_fetch_object($query)) {
              $data[]=$row;
          }
          echo json_encode($data);

        } else {
          $data=array();

          $servers  =   arraykeysToInSqlClause($_GET['server']);
          $query = bd::query("SELECT V.id,V.name,V.address_ip,V.host_name,S.id as id_server,S.name as name_server,S.main_ip FROM server_vmta V,server S WHERE S.id=V.id_server and  id_server in $servers && S.active=1 && V.active=1;");

          while ($row = mysql_fetch_object($query)) {
              $data[]=$row;
          }
          echo json_encode($data);
        }       
   }
   
   function get_sponsors(){
        $data=array();
        $query = bd::query("SELECT id,name FROM sponsor WHERE active='1'");
        while ($row = mysql_fetch_object($query)) {
            $data[]=$row;
        }
        echo json_encode($data);
   }
   
  function get_offers(){
      $data=array();
      if(empty($_GET['country'])) {
        $query = bd::query("SELECT id,name FROM offer WHERE id_sponsor='{$_GET["sponsor"]}' && active='1'");
      } else {
        $query = bd::query("SELECT id,name FROM offer WHERE id_sponsor='{$_GET["sponsor"]}' && country = '{$_GET['country']}' && active='1'");
      }
      
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   function get_fname(){
      $data=array();
      $query = bd::query("SELECT fname FROM offer WHERE id='{$_GET["offer"]}' &&  active='1'");
      $row = mysql_fetch_array($query);
      $ex = explode(chr(10), $row['fname']);
      foreach ($ex as $e) {
        $data[] = $e;
      }
      echo json_encode($data);
   }
   function get_subject(){
      $data=array();
      $query = bd::query("SELECT subject FROM offer WHERE id='{$_GET["offer"]}' &&  active='1'");
      $row = mysql_fetch_array($query);
      $ex = explode(chr(10), $row['subject']);
      foreach ($ex as $e) {
        $data[] = $e;
      }
      echo json_encode($data);
   }
   
   function get_news(){
      $data=array();
      $query = bd::query("SELECT id,name FROM data_news WHERE active='1'");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   
   function get_isps(){
      $data=array();
      $query = bd::query("SELECT id,name FROM data_isp WHERE active='1'");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   
   function get_data_lists(){
      $data=array();
      if(!empty($_GET["news"]) && !empty($_GET["isp"])  ){
        calculate_data_isp();
        $query = bd::query("SELECT id,name,count_isp FROM data_list WHERE id_news='{$_GET["news"]}' && data_table!='' && active='1'");
        while ($row = mysql_fetch_object($query)) {
            $data[]=$row;
        } 
      }
      echo json_encode($data);
   }
   
   function calculate_data_isp(){
      $query = bd::query("SELECT regex FROM data_isp WHERE id={$_GET["isp"]}");
      $row=  mysql_fetch_array($query);
      $regexisp=$row['regex'];
      $query = bd::query("SELECT id,data_table FROM data_list WHERE id_news='{$_GET["news"]}' && data_table!='' && active='1' ");
      while($row=mysql_fetch_row($query)){
          bd::query("UPDATE data_list set count_isp=(select count(*) from $row[1] where email_address REGEXP '$regexisp') where id='$row[0]' ");
      } 
   }
    
   function show(){
      $data=array();
      $query = bd::query("SELECT campaign_global.id, campaign_global.date_send, campaign_global.id_offer, campaign_global.id_news, campaign_global.id_isp, campaign_global.id_data_list, campaign_global.data_from, campaign_global.data_count, campaign_global.data_sent, campaign_global.status, offer.name as offer_name, data_news.name as news_name, data_isp.name as isp_name, data_list.name as list_name FROM campaign_global, offer, data_news, data_isp, data_list WHERE campaign_global.id_mailer='{$_SESSION['id-mailer']}' && campaign_global.id_server='{$_SESSION['id-server']}' && campaign_global.id_offer=offer.id && campaign_global.id_news=data_news.id && campaign_global.id_isp=data_isp.id && campaign_global.id_data_list=data_list.id   ORDER BY campaign_global.date_send DESC");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   
   function edit(){
      $data=array();
      $query = bd::query("SELECT * FROM campaign_global WHERE id='{$_GET["id_campaign"]}' && id_mailer='{$_SESSION['id-mailer']}'");
      while ($row = mysql_fetch_object($query)) {
          $data[]=$row;
      }
      echo json_encode($data);
   }
   
   function stop_campaign(){
       $res=bd::query("select status from campaign_global where id='{$_GET["id_campaign"]}'");
        $rowstatus= mysql_fetch_array($res);
        if($rowstatus['status']=='Sending'){
            $res = bd::query("UPDATE campaign_global set status='Stopped' where id='{$_GET["id_campaign"]}'");
            if($res){
                echo "Campaign {$_GET["id_campaign"]} Stopped";
            }else{
                echo "There was an error Stopping your Campaign";
            }
        }else{
            echo "You cannot stop this Campaign! Try again";
        }
      
   }
   
   function get_real_sent(){
       $res=bd::query("select data_sent from campaign_global where id='{$_GET["id_campaign"]}'");
       $rowsent= mysql_fetch_array($res);
       echo $rowsent['data_sent'];
   }
   
   function get_real_status(){
       $res=bd::query("select status from campaign_global where id='{$_GET["id_campaign"]}'");
       $rowstatus= mysql_fetch_array($res);
       echo $rowstatus['status'];
   }

   function RandomPrefix($length){
        $random= "";
        srand((double)microtime()*1000000);
        $data = "bc";
        $data .= "adeijklmnopqrstuvwxyz";
        $data .= "fgh";
        for($i = 0; $i < $length; $i++){
            $random .= substr($data, (rand()%(strlen($data))), 1);
        }
        return $random;
    }
    
    

function arraykeysToInSqlClause($p_arra_keys)
{
        $strKeys		=	'';
        foreach($p_arra_keys as $key):
                $strKeys	=	$strKeys.','.$key;
        endforeach;
        $strKeys		=	substr($strKeys,1);
        $strKeys		=	'('.$strKeys.')';

        return $strKeys;
}
