<?php session_start();
require_once("./../../config/config.php");
require_once("./validated_admin.php");

$myformId=SESS_MASTER;
$myIp= ipCheck() ;
$dateceated=date("Y-m-d G:i:s");
$createdby='0';
$api_type=$_REQUEST['api_type'];

function limit_text($text, $limit) {
    if (str_word_count($text, 0) > $limit) {
        $words = str_word_count($text, 2);
        $pos   = array_keys($words);
        $text  = substr($text, 0, $pos[$limit]);
    }
    return $text;
}
 
  function file_get_contents_curl($url)
  {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');  // This is mandatory. //mandatory field
    $data = curl_exec($curl);
    curl_close($curl);  
    return $data;
  } 

  /*brand store category start*/

  function brand_store($data){
    $myIp= ipCheck() ;
    $dateceated=date("Y-m-d G:i:s");
    global $conn;
    $sqlno = "SELECT coupon_code FROM  coupon WHERE coupon_code!='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryno=$conn->query($sqlno);
               $mydarano= $myqueryno->fetchAll()  ;
               
               $rows_foundno = $myqueryno->rowCount();
               
                
               $sqloff = "SELECT coupon_code FROM  coupon WHERE coupon_code ='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryoff=$conn->query($sqloff);
               $mydaraoff= $myqueryoff->fetchAll()  ;
               
               $rows_foundoff = $myqueryoff->rowCount();
          $olgaquery="INSERT INTO brand_master SET createdIp='$myIp',datecreated='$dateceated',brand_name='$data',brand_key='$brand_key',total_coupon ='$rows_foundno',total_offers ='$rows_foundoff'"; 
     
        $stmt = $conn->prepare($olgaquery);
        $stmt->execute();
  }


   function store_store($data){
    $myIp= ipCheck() ;
    $dateceated=date("Y-m-d G:i:s");
    global $conn;
     $sqlno = "SELECT coupon_code FROM  coupon WHERE coupon_code!='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryno=$conn->query($sqlno);
               $mydarano= $myqueryno->fetchAll()  ;
               
               $rows_foundno = $myqueryno->rowCount();
               
                
               $sqloff = "SELECT coupon_code FROM  coupon WHERE coupon_code ='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryoff=$conn->query($sqloff);
               $mydaraoff= $myqueryoff->fetchAll()  ;
               
               $rows_foundoff = $myqueryoff->rowCount();

          $olgaquery="INSERT INTO store_master SET createdIp='$myIp',datecreated='$dateceated',store_name='$data',store_key='$store_key',total_coupon ='$rows_foundno',total_offers ='$rows_foundoff'"; 
     
        $stmt = $conn->prepare($olgaquery);
        $stmt->execute();
  }

   function category_store($data){
    $myIp= ipCheck() ;
    $dateceated=date("Y-m-d G:i:s");
    global $conn;
    $sqlno = "SELECT coupon_code FROM  coupon WHERE coupon_code!='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryno=$conn->query($sqlno);
               $mydarano= $myqueryno->fetchAll()  ;
               
               $rows_foundno = $myqueryno->rowCount();
               
                
               $sqloff = "SELECT coupon_code FROM  coupon WHERE coupon_code ='' AND company_name LIKE '$data%' OR short_title LIKE '$data%' OR full_title LIKE '$data%'";
               $myqueryoff=$conn->query($sqloff);
               $mydaraoff= $myqueryoff->fetchAll()  ;
               
               $rows_foundoff = $myqueryoff->rowCount();
          $olgaquery="INSERT INTO category_master SET createdIp='$myIp',datecreated='$dateceated',category='$data',category_key='$category_key',total_coupon ='$rows_foundno',total_offers ='$rows_foundoff'"; 
     
        $stmt = $conn->prepare($olgaquery);
        $stmt->execute();
  }




  /*brand store category end*/

  /*Icubeswire api*/
  if($api_type=='Icubeswire'){
      /*api credentials*/
       
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='Icubeswire'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
        $api_key  = $rowcon['api_key'];
        }
      
     /*api credentials*/
      
      
         $url ='http://icubeswire.co/api/GetOffers?key='.$api_key;
         //var_dump($data);
       $result = file_get_contents($url);
// Will dump a beauty json :3
$data=json_decode($result, true);
    $sqlde = "DELETE FROM coupon  WHERE from_site='icubeswire'";
   
    $myqde=$conn->query($sqlde);
       foreach($data as $dat){
          $offer_Id =$dat['offer_Id'];
           $short_title =addslashes($dat['Offer_Name']);
            $full_title =addslashes($dat['description']);
             $exp_date =addslashes($dat['End_Date']);
              $company_name=addslashes($dat['Offer_Name']);
                      $company_name=limit_text($company_name,3);
                        $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo =  $company_name;
      
  }
               $link =addslashes($dat['TrackingLink']);
               $category='Deals';
                    $type='';
                $coupon_code='Deals';
                            $from_site='icubeswire';
                            $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
            


        }
                           
                           
                              $trending=rand(0,1);
                                try{

$olgaquery="INSERT INTO coupon SET coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',image='$companylogo',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
         
    }
           
           
       }catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
            
       
    }
    
    $api = array("api"=>"without_popup");
  echo json_encode($api);

          }


 /*Icubeswire api end*/

  /*optimisemedia api*/
  if($api_type=='optimisemedia'){


$t = microtime(true);
$micro = sprintf("%03d",($t - floor($t)) * 1000);
$utc = gmdate('Y-m-d H:i:s.', $t).$micro;

$sig_data= $utc;
 /*api credentials*/
       
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='optimisemedia'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
        $api_key  = $rowcon['api_key'];
          // $private_key  = $rowcon['private_key'];
            $network_id = $rowcon['network_id'];
          
        
        }
      
  
$api_key=$api_key;



$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://public.api.optimisemedia.com/v1/vouchercodes/?contactId='.$network_id.'&agencyId=95',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    "apikey: $api_key"
  ),
));

$response = curl_exec($curl);

curl_close($curl);



 $data = json_decode($response,true);
 //var_dump($data);
 if(is_array($data) ){
 // echo"ffvvfvfv";
  $finaldata=$data['response'];
     $sqlde = "DELETE FROM coupon  WHERE from_site='optimisemedia'";
   
    $myqde=$conn->query($sqlde);
foreach( $finaldata as $dat){

       $short_title=addslashes($dat['title']);
         $coupon_code=addslashes($dat['code']);
          $company_name=addslashes($dat['companyName']);
            $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =   'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo =  $company_name;
      
  }
            $full_title=addslashes($dat['description']);
             $link=addslashes($dat['trackingURL']);
              $category=addslashes($dat['categoryName']);
                    $type=addslashes($dat['Type']);
                      
                        $exp_date=$dat['expiryDate'];
                    //       preg_match('/(\d{10})(\d{3})([\+\-]\d{4})/', $exp_date, $matches);
                    // $dt = DateTime::createFromFormat("U.u.O",vsprintf('%2$s.%3$s.%4$s', $matches));
                    //            $exp_date=$dt->format('r');
                            $from_site='optimisemedia';
                          $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
              

        }
                     
                           
                              $trending=rand(0,1);
                                                        
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo', coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store',trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->exec($olgaquery);
    if($stmt){
    
      
 
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
            
          
}
            
  } 

     
  // echo$short_title;
  $api = array("api"=>"without_popup");
  echo json_encode($api);
}
   /*optimisemedia end*/
     /*vcommission begin*/
  if($api_type=='vcommission'){
      
       /*api credentials*/
       
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='vcommission'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
        $api_key= $rowcon['api_key'];
        }
      
     /*api credentials*/
   $fetch = 'https://api.vcommission.com/v2/publishers/coupons?apiKey='.$api_key;
    $result = file_get_contents($fetch);
         //var_dump($data);
        $data = json_decode($result,true);
      
        if (is_array($data) || is_object($data)){
               $sqlde = "DELETE FROM coupon  WHERE from_site='vcommission'";
   
    $myqde=$conn->query($sqlde);
        foreach($data as $dat){
           
        $short_title=$dat['coupon_title'];
         $coupon_code=$dat['coupon_code'];
          $company_name=$dat['store_name'];
              $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo =  $company_name;
      
  }
            $full_title=$dat['coupon_description'];
             $link=$dat['link'];
              $category=$dat['category'];
                    $type=$dat['coupon_type'];
                        $exp_date=$dat['coupon_expiry'];
                            $from_site='vcommission';
                            $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     
        }else{
              $coupon_country_name=$country;
            

        }
                     
                           
                              $trending=rand(0,1);
                            
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo', coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
    
      
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
            
            
        }
      
      }
            

 
  
   
      $api = array("api"=>"without_popup");
  echo json_encode($api);
  } 
  /*vcommission end */
   /*cuelinks begin */
    if($api_type=='cuelinks'){
          /*api credentials*/
       
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='cuelinks'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
        $access_token = $rowcon['access_token'];
        }
      
     /*api credentials*/
   $ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://www.cuelinks.com/api/v2/offers.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Authorization: Token token=".$access_token."",
  "Content-Type: application/json"
));

$response = curl_exec($ch);
curl_close($ch);

 $data = json_decode($response,true);
   if (is_array($data) || is_object($data)){
   $sqlde = "DELETE FROM coupon  WHERE from_site='cuelinks'";
   
    $myqde=$conn->query($sqlde);
foreach($data['offers'] as $dat){
       $short_title=addslashes($dat['title']);
         $coupon_code=addslashes($dat['coupon_code']);
          $company_name=addslashes($dat['campaign']);
          $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo =  $company_name;
      
  }
            $full_title=addslashes($dat['description']);
             $link=addslashes($dat['affiliate_url']);
              $category=$dat['categories']['1'];
                    $type=$dat['type'];
                        $exp_date=$dat['end_date'];
                            $from_site='cuelinks';
                       $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
            


        }
                     
                        
                              $trending=rand(0,1);
                                                        
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo',coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
    
      
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
            
          
}
   
      
 }
    
   
      $api = array("api"=>"without_popup");
  echo json_encode($api);  
        
    }

     /*cuelinks end */
         /*skimlinks begin */

    if($api_type=='skimlinks'){
         /*api credentials*/
       
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='skimlinks'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
        $access_token = $rowcon['access_token'];
            $network_id = $rowcon['network_id'];
          
        }
      
     /*api credentials*/
    
$ch = curl_init();

$url="https://merchants.skimapis.com/v4/publisher/".$network_id."/offers?access_token=".$access_token;

$response = file_get_contents($url);

 $data = json_decode($response,true);
  $sqlcondele = "SELECT * FROM coupon  WHERE from_site='skimlinks'";
   
    $myquerycondele=$conn->query($sqlcondele);
  $mydaracon= $myquerycondele->fetchAll()  ;
  
  $rows_foundcodele = $myquerycondele->rowCount();

    foreach( $myquerycondele as $rowcondele ){
      
          $date_time  = $rowcondele['date_time'];
          $new_date = date('Y-m-d', $date_time);
          if($new_date!=date('Y-m-d')){
              die();
                $sqlde = "DELETE FROM coupon WHERE from_site='skimlinks'";
   
    $myqde=$conn->query($sqlde);
              
          }else{
              echo 'failed';
              die();
          }
            
        }
foreach($data['offers'] as $dat){
       $short_title=addslashes($dat['title']);
         $coupon_code=addslashes($dat['coupon_code']);
          $company_name=addslashes($dat['merchant_details']['name']);
            $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo =  $company_name;
      
  }
            $full_title=addslashes($dat['description']);
             $link=addslashes($dat['url']);
              $category=addslashes($dat['merchant_details']['verticals'][0]);
                    $type=addslashes($dat['offer_type']);
                        $exp_date=$dat['offer_ends'];
                            $from_site='skimlinks';
                            $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
             


        }
                     
                           
                              $trending=rand(0,1);
                                                        
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo',coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
    
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
            
          
}

     
$api = array("api"=>"without_popup");
  echo json_encode($api);
                
  }
/*skimlinks end*/
/*clickonik begin*/

  if($api_type=='clickonik'){
      $fetch = file_get_contents_curl('https://clickonik.api.hasoffers.com/Apiv3/json?Target=Affiliate_Offer&Method=findAll&api_key=98233e214d25a6009015e34e4c8a31561bd924d467914363a069809a40c564fa');
        //$fetch = file_get_contents_curl('http://www.clickonik.com/deeplink/coupons.php?af-id=2621&download=2');
         //var_dump($data);
        $data = json_decode($fetch,true);
        print_r($data);
  }
if($api_type=='brandreward'){
       /*api credentials*/
       
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='brandreward'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
          $api_key  = $rowcon['api_key'];
            $user_id= $rowcon['affiliate_id'];
          
        }
      
     /*api credentials*/
    
      $fetch = file_get_contents_curl('http://api.brandreward.com/?act=links.content_feed&user='.$user_id.'&language=en&key='.$api_key.'&outformat=json&pagesize=1000');
         //var_dump($data);
        $data = json_decode($fetch,true);
     
       
         $i='0';
         $sqlcondele = "SELECT * FROM coupon  WHERE from_site='brandreward'";
   
    $myquerycondele=$conn->query($sqlcondele);
  $mydaracon= $myquerycondele->fetchAll()  ;
  
  $rows_foundcon = $myquerycondele->rowCount();

    foreach( $myquerycondele as $rowcondele ){
      
          $date_time  = $rowcondele['date_time'];
          $new_date = date('Y-m-d', $date_time);
          if($new_date!=date('Y-m-d')){
                $sqlde = "DELETE FROM coupon  WHERE from_site='brandreward'";
   
    $myqde=$conn->query($sqlde);
              
          }
            
        }
      
         
foreach($data['data'] as $dat){
    
       $short_title=addslashes($dat['Title']);
         $coupon_code=addslashes($dat['CouponCode']);
          $company_name=addslashes($dat['Advertiser']);
              $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo =  $company_name;
      
  }
            $full_title=addslashes($dat['Description']);
             $link=addslashes($dat['LinkUrl']);
              $category=addslashes($dat['Category']);
                    $type=$dat['KeyTitle'];
                        $exp_date=$dat['EndDate'];
                        if($exp_date=='0000-00-00 00:00:00'){

                         $Date = date("Y-m-d H:i:s");
$exp_date = date('Y-m-d H:i:s', strtotime($Date. ' + 15 days'));
 
                        }
                            $from_site='brandreward';
                            $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
             


        }
                     
                         
                              $trending=rand(0,1);
                               $i++; 
                                                        
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo',coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
    
      

      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
          
          
}


$api = array("api"=>"without_popup");
  echo json_encode($api);
    
}
/*clickonik end*/
/*flipkart begin*/
if($api_type=='flipkart'){
        /*api credentials*/
       
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='flipkart'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
          $api_key  = $rowcon['api_key'];
            $user_id= $rowcon['affiliate_id'];
          
        }
      
     /*api credentials*/
    $url='https://affiliate-api.flipkart.net/affiliate/offers/v1/dotd/json';
    $offerJsonURL =  'https://affiliate-api.flipkart.net/affiliate/offers/v1/all/json';
    $headers = array( 
 "Content-Type: application/json",
 "Accept: application/json",
 "Access-Control-Request-Method: GET",
 "Fk-Affiliate-Token:".$api_key."",
 "Fk-Affiliate-Id:".$user_id.""
 );
   $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 $data = json_decode($result,true); 

 
  
   $sqlde = "DELETE FROM coupon  WHERE from_site='flipkart'";
   
    $myqde=$conn->query($sqlde);
         
foreach($data['dotdList'] as $dat){
    
       $short_title=addslashes($dat['title']);
         $coupon_code='Deals';
          $company_name='flipkart';
              $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo =  $company_name;
      
  }
            $full_title=addslashes($dat['description']);
             $link=addslashes($dat['url']);
              $category=addslashes($dat['name']);
                    $type='';
                        $exp_date=$dat['EndDate'];
                            $from_site='flipkart';
  $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
              


        }    
                          
                              $trending=rand(0,1);
                               
                                                        
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo',coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
    

  
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
          
          
}   
 
$api = array("api"=>"without_popup");
  echo json_encode($api);
} 
/*flipkart end */
if($api_type=='affiliatefuture'){
  /*api credentials*/
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='affiliatefuture'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
          $con_id = $rowcon['rowId'];
        $api_key  = $rowcon['api_key'];
        $private_key  = $rowcon['private_key'];
          
        }


 $xmlfile = file_get_contents('https://api.affiliatefuture.com/PublisherService.svc/getAllVouchers?key='.$api_key.'&passcode='.$private_key.'');
 $new = simplexml_load_string($xmlfile);
 $con = json_encode($new);
//  var_dump($con);
//  die;
// Convert into associative array
$newArr = json_decode($con, true);  
   $sqlde = "DELETE FROM coupon  WHERE from_site='affiliatefuture'";
   
    $myqde=$conn->query($sqlde);
   
foreach($newArr['Vouchers']['Voucher'] as $dat){


    
       $short_title=addslashes($dat['VoucherDescription']);
      
         $coupon_code=addslashes($dat['VoucherCode']);
        
          $company_name=addslashes($dat['MerchantSiteName']);
         
            $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                       
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo = $company_name;
      
  }
 
            $full_title=addslashes($dat['VoucherDescription']);
             $link=addslashes($dat['']);
            
              $category=addslashes($dat['CategoryName']);
             
                    $type='deal';
                        $exp_date=$dat['EndDate'];
                            $from_site='affiliatefuture';
                             //$link;
                           
                              $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll();
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
             
           

        }
                          
                              $trending=rand(0,1);
                               
                                                        
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo',coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
   
    if($stmt->execute()){
    

  
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
          
          
}
 
$api = array("api"=>"without_popup");
  echo json_encode($api);   
 
} 

if($api_type=='tradedoubler'){
 /*api credentials*/
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='tradedoubler'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
          $con_id = $rowcon['rowId'];
        $access_token = $rowcon['access_token'];
        $affiliate_id = $rowcon['affiliate_id'];
        $network_id = $rowcon['network_id'];
          
        }

 $xmlfile = file_get_contents('https://api.tradedoubler.com/1.0/vouchers?token='.$access_token.'');
 $new = simplexml_load_string($xmlfile);
 $con = json_encode($new);
  
// Convert into associative array
$newArr = json_decode($con, true);
   $sqlde = "DELETE FROM coupon  WHERE from_site='tradedoubler'";
   
    $myqde=$conn->query($sqlde);
foreach($newArr['voucher'] as $dat){
    
       $short_title=addslashes($dat['title']);
         $coupon_code=addslashes($dat['VoucherCode']);
          $company_name=addslashes($dat['programName']);
            $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo = 'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo = $company_name;
      
  }
            $full_title=addslashes($dat['description']);
             $link=addslashes($dat['defaultTrackUri']);
              $category='tradedoubler';
                    $type='deal';
                        $exp_date=$dat['endDate'];
                        $old_date_timestamp = strtotime($exp_date);
                          $exp_date = date('Y-m-d H:i:s', $old_date_timestamp);   
                        
                            $from_site='tradedoubler';
                            $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     
        }else{
              $coupon_country_name=$country;
           

        }
                     
                          
                              $trending=rand(0,1);
                               
                                                        
             try{

$olgaquery="INSERT INTO coupon SET image='$companylogo',coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
    

  
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
          
          
}   

 $api = array("api"=>"without_popup");
  echo json_encode($api);

 
} 

if($api_type=='2performant'){
/*api credentials*/
$sqlcon = "SELECT * FROM api_coupan  WHERE api_type='2performant'";
   
    $myquerycon=$conn->query($sqlcon);
  $mydaracon= $myquerycon->fetchAll()  ;
  
  $rows_foundcon = $myquerycon->rowCount();

    foreach( $mydaracon as $rowcon ) {
      
        $con_id = $rowcon['rowId'];
        $access_token = $rowcon['access_token'];
        $affiliate_id = $rowcon['affiliate_id'];
        $network_id = $rowcon['network_id'];
        $private_key  = $rowcon['private_key'];
          
        }

  $headers = array( 
 "Content-Type: application/json",
 "Accept: application/json",
 "Access-Control-Request-Method: GET",
 "access-token:".$access_token."",
 "client:".$network_id."",
 "uid:".$affiliate_id.""
 );
  $url="https://api.2performant.com/affiliate/programs";
   $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 $data = json_decode($result,true);
  $sqlcondele = "SELECT * FROM coupon  WHERE from_site='2performant'";
   
    $myquerycondele=$conn->query($sqlcondele);
  $mydaracon= $myquerycondele->fetchAll()  ;
  
  $rows_foundcodele = $myquerycondele->rowCount();

    foreach( $myquerycondele as $rowcondele ){
      
          $date_time  = $rowcondele['date_time'];
          $new_date = date('Y-m-d', $date_time);
          if($new_date!=date('Y-m-d')){
                $sqlde = "DELETE FROM coupon  WHERE from_site='brandreward'";
   
    $myqde=$conn->query($sqlde);
              
          }
            
        }
foreach($data['programs'] as $dat){
    
       $short_title=addslashes($dat['slug']);
         $coupon_code=addslashes($dat['unique_code']);
          $company_name=addslashes($dat['name']);
              $string = preg_replace('/\s+/', '', $company_name); 
                       $companylogo = strtolower($string);
                      if(@GetImageSize("https://design-collection-new.s3.us-east-2.amazonaws.com/".$companylogo.".webp" )){
      
      $companylogo =  'https://design-collection-new.s3.us-east-2.amazonaws.com/'.$companylogo.'.webp';
      
  }else{
       $companylogo = $company_name;
      
  }
            $full_title=addslashes($dat['description']);
             $link=addslashes($dat['main_url']);
              $category=$dat['category']['name'];
                    $type='deal';
                        $exp_date=$dat['endDate'];
                            $from_site='2performant';
                            $sqlc = "SELECT * FROM company_master  WHERE company='$company_name'";
   
    $myq=$conn->query($sqlc);
  $mydar= $myq->fetchAll()  ;
  
  $rows_fou = $myq->rowCount();

    foreach( $mydar as $rowcn) {
      
        $country  = $rowcn['country '];
        
        }
        if($rows_fou=='0'){
             $coupon_country_name='';
             $olgaquery="INSERT INTO company_master SET company='$company_name',image='$companylogo'"; 
    $stmt = $conn->prepare($olgaquery);
    $stmt->execute();
     $currency = 'INR' ;
        }else{
              $coupon_country_name=$country;
             


        }
                     
                          
                              $trending=rand(0,1);
                               
                                                        
             try{

$olgaquery="INSERT INTO coupon SET coupon_code='$coupon_code',company_name='$company_name', short_title ='$short_title', full_title='$full_title',link='$link',category='$category',type='$type',exp_date='$exp_date',from_site='$from_site',coupon_country_name='$coupon_country_name',brand_store='$brand_store'
    ,trending='$trending',created_by='0',created_type='Admin',date_time='$dateceated',currency='$currency'"; 
    $stmt = $conn->prepare($olgaquery);
    if($stmt->execute()){
    

  
      }
  }  catch(PDOException $exception){
            die('ERROR: ' . $exception->getMessage());
        } 
          
          
}   

 
} 
if($api_type=='awin'){
  $api = array("api"=>"awin");
  echo json_encode($api);
   
 } 

 if($api_type=='admitad'){ 
$api = array("api"=>"admitad");
  echo json_encode($api);
 } 
?>