<?php
include 'config.php';
include 'fungsi.php';
include 'fungsi2.php';

$mail = new VerifyEmail();
$mail->setStreamTimeoutWait(10);
$mail->Debug= FALSE; 
$mail->Debugoutput= 'html'; 
$mail->setEmailFrom($emailakun);

$query = mysqli_query($con,"SELECT * FROM grup order by rand()");	
	while ($target = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
		$grup = $target['id'];
		$link1 = $target['link1'];
		
		$htt = substr($link1,0,4);
                IF($htt=='http'){
                    $link1 = $link1;
                }else{    
                    $link1 = 'https://'.$link1;
                }
		//echo $link1;
		
		if (!empty($target['link1'])) {
		   
    		//header('Content-type: application/json');
    		 
    		// Set your CSV feed
    		$feed = $link1;
    		 
    		// Arrays we'll use later
    		$keys = array();
    		$newArray = array();
    		 
    		// Function to convert CSV into associative array
    		function csvToArray($file, $delimiter) { 
    		  if (($handle = fopen($file, 'r')) !== FALSE) { 
    			$i = 0; 
    			while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) { 
    			  for ($j = 0; $j < count($lineArray); $j++) { 
    				$arr[$i][$j] = $lineArray[$j]; 
    			  } 
    			  $i++; 
    			} 
    			fclose($handle); 
    		  } 
    		  return $arr; 
    		} 
    		 
    		// Do it
    		$data = csvToArray($feed, ',');
    		 
    		// Set number of elements (minus 1 because we shift off the first row)
    		$count = count($data) - 1;
    		 
    		//Use first row for names  
    		$labels = array_shift($data);  
    		 
    		foreach ($labels as $label) {
    		  $keys[] = $label;
    		}
    		 
    		// Add Ids, just in case we want them later
    		$keys[] = 'id';
    		 
    		for ($i = 0; $i < $count; $i++) {
    		  $data[$i][] = $i;
    		}
    		 
    		// Bring it all together
    		for ($j = 0; $j < $count; $j++) {
    		  $d = array_combine($keys, $data[$j]);
    		  $newArray[$j] = $d;
    		}
    		 
    		// Print it out as JSON
    		//echo json_encode($newArray);
    		$json = json_encode($newArray);
    		$array = json_decode($json);
    
    		//print_r($array);
    		 
    		 //echo '<hr />
    		 //<table border="1">
    		  //<tr>
    		  //<td>Email</td>
    		  //<td>Nama</td>
    		  //</tr>
    		 //';
    
    		 // meloop data Array Object
    		 foreach($array as $data){
    			  //echo '<tr> <td> '.$data->Email;
    			  //echo '</td> <td>'.$data->Nama;
    			  $nama = $data->Nama;
    			  $email = $data->Email;
    			  $HP = $data->HP;
    			  
    			  //echo $email;
    			  
    			  
    			  //$proteksi = 0;
                  IF($proteksi ==1){
                       if($mail->check($email)){
                           $email = $email;
                       }elseif(verifyEmail::validate($email)){    
                            $email ='';
                            exit;
                       }else{
                           $email ='';
                           exit;
                       } 
                  }else{   
                       if(domain_exists($email)) {    
                          $email = $email;
                       }else{
                          $email =''; 
                       }      
                  }    
    			      
    			  IF(!empty($nama) && !empty($email) && !empty($HP)){
    			  
        			  $ip = $_SERVER['REMOTE_ADDR'];
        			  $query = mysqli_query($con,"SELECT id FROM prospek WHERE email='$email'");
                    	$check = mysqli_fetch_array($query,MYSQLI_NUM);
                    	
                    	if (!isset($check[0])) { 
                    		mysqli_query($con,"INSERT INTO prospek (ID,nama,email,tgl_gabung,ip,hp,sts) VALUES (NULL,'$nama','$email','$nowtgl','$ip','$HP',0)");
                    		$user_id = mysqli_insert_id($con);
                    	} else {
                    		$user_id = $check[0];
                    	}
                    	
                    	$query1 = mysqli_query($con,"SELECT id FROM gtm WHERE id_member='$user_id' AND id_grup='$grup'");
                    	$check1 = mysqli_fetch_array($query1,MYSQLI_NUM);
                    	
                    	if (!isset($check1[0])) { 
                    		$query = mysqli_query($con,"SELECT * FROM grup,champ WHERE grup.id=$grup AND champ.grup=$grup ORDER BY champ.sort asc LIMIT 0,1");
                    		$grupdata = mysqli_fetch_array($query,MYSQLI_ASSOC);	
                    		$judul = $grupdata['judul'];
                    		$isi = $grupdata['isi'];
                    		$sort = $grupdata['sort'];
                    		$nextid = $grupdata['ID'];
                    		$delay2 = $grupdata['delay'];
                    		
                    		if ($grupdata['unsub'] != 0) {
                    				mysqli_query($con,"DELETE FROM gtm WHERE id_member='$user_id' AND id_grup='$grupdata[unsub]'");
                    		}
                    		
                    		$cek = mysqli_query($con,"select * FROM grup where id=$grup");
                    		$qcek = mysqli_fetch_array($cek,MYSQLI_ASSOC);
                    		$nextemail = $qcek['nextemail'];
                    		
                    		$dbcustom ='-';	
                    		$n = mysqli_query($con,"SELECT ID,delay FROM champ WHERE grup = $grup order by ID asc LIMIT 0,1");
                    		if (mysqli_num_rows($n) > 0) {
                    				$next = mysqli_fetch_array($n,MYSQLI_ASSOC);
                    				$delay = $next['delay'];
                    				$next_id = $next['ID'];
                    				mysqli_query($con,"INSERT INTO gtm VALUES(NULL,'$user_id','$grup','$next_id',NOW() + INTERVAL $delay DAY,0,0,NOW(),0,0,0,NOW(),'$dbcustom',1,0)");
                    				
                    		} else {
                    		        If ($nextemail==0){
                    				    mysqli_query($con,"INSERT INTO gtm VALUES(NULL,'$user_id','$grup',0,0,0,0,NOW(),0,0,0,NOW(),'$dbcustom',1,0)");
                    				    
                    		        }else{
                    		            mysqli_query($con,"INSERT INTO gtm VALUES(NULL,'$user_id','$grup','$nextid',NOW() + INTERVAL $delay2 DAY,0,0,'$nowtgl',0,0,0,NOW(),'$dbcustom',0,0)");
                    		            
                    		        }      
                    		}
                    	
                    	}
    			  }	
    			  
    			  
    		 }
    		 
    		 //echo '</td></tr></table>';
		 
		 
		}    
		
	}		 
    
?>