<?php
    date_default_timezone_set('America/New_York');
	require 'vendor/autoload.php';

    use STS\Backoff;
    use GuzzleHttp\Client;
    
    
    $adate = date("F j, Y, g:i a");
    $email = $_GET['email'];
    $supp_list = $_GET['list'];
    $md_email = md5(strtolower(trim($email)));
    
    
	$dupe = false;
	$send_lead = '';
	function check_hashed($list) {
		global $email, $adate, $md_email, $dupe, $send_lead;
		$context = stream_context_create( array(
		  'http'=> array(
			'timeout' => 60.0
		  )
		));
    		$handle = fopen(getcwd().'/suppression_lists/'.$list.'/suppress.txt', 'r', false, $context);
    		while (($buffer = fgets($handle)) !== false) {
    			$dupe = false;
    			$send_lead = 'not_found';
    			if (strpos($buffer, $md_email) !== false) {
    				$dupe = TRUE;
    				$send_lead = 'yes_found';
    				break; 
    			}
    		}
		fclose($handle);
		ob_end_clean(); 
			$f = fopen(getcwd().'/suppression_lists/'.$list.'/suppress.csv', 'a');
			fwrite($f, $adate.','.$email.','.$md_email.','.$send_lead.PHP_EOL);
			fclose($f);
		return $dupe;	
	}
	
	function woo_request($query_params, $uri) {
	    $woo_client = new GuzzleHttp\Client(['base_uri' => 'http://www.woopra.com']);
		$response = $woo_client->request('GET', $uri, ['query' => $query_params]);		
	}
	
	
    ignore_user_abort(true);
    set_time_limit(0);
    
    ob_start();
    header('Connection: close');
    header('Content-Length: '.ob_get_length());
    ob_end_flush();
    @ob_flush();
    flush();
    fastcgi_finish_request();	
	
	$result = backoff(function() {
	    global $supp_list;
		return check_hashed($supp_list);
	});
	
	sleep(3);
	
	$wid = array(
		"project" => 'wallstreetwatchdogs.com',
		"cv_email" => $email
	); 
	woo_request($wid, 'track/identify');

				$wdata = array(
					"domain" => 'wallstreetwatchdogs.com',
					'event' => 'suppression_check',
					'cv_email' => $email,
					'cv_supress_from_'.$supp_list => $result,
					'ce_supress_from_'.$supp_list => $result,
					'cv_md5' => $md_email,

                );
    woo_request($wdata, 'track/ce');	


sleep(2);
exit;
?>