<?php
    date_default_timezone_set('America/New_York');
	require 'vendor/autoload.php';
	use GuzzleHttp\Client;
	use GuzzleHttp\Psr7\Response;
	use STS\Backoff;

	$adate = date("F j, Y, g:i a");
	
    function sa_request($sa_params, $uri, $list) {
        global $email, $adate;
        $sa_client = new GuzzleHttp\Client(['base_uri' => 'https://api.beaconstreetservices.com/leads/v1/external/api/']);
        $response = $sa_client->request('POST', $uri, ['headers' => ['Content-Type' => 'application/json', 'x-api-key' => 'KV5DRehMVa7kKAdSduU1D9WG4eGxCkBXyEvD9sL5']], [ 'json' => $sa_params]);
        $body = $response->getBody();
        $f = fopen('./logs/'.$list.'_cobrand.csv', 'a');
	    fwrite($f,$adate.", ".$email. ", ".$body.PHP_EOL); 
	    fclose($f);	        
    }
    
	function lm_request($uri) {
	    global $email, $first_name, $campaign, $adate;
	    $lm_client = new GuzzleHttp\Client(['base_uri' => 'https://api.smartdataprocessor.com']);
		$response = $lm_client->request('GET', $uri, ['query' => ['id' => 'LM1909', 'key' => '4t3oibt0dzmn3ivg', 'email' => $email, 'fName' => urlencode($first_name), 'siteId' => urlencode('wswd-'.$campaign)]]);
		$body = $response->getBody();
	    $f = fopen('./logs/lm_cobrand.csv', 'a');
	    fwrite($f,$adate.", ".$email. ", ".$body.PHP_EOL); 
	    fclose($f);		
	}
	
	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]);		
	}	

    
    $email = $_GET['email'];
    $first_name = $_GET['first_name'] || 'Reader';	
    $campaign = $_GET['utm_campaign'] || '0';


    $rand1 = rand(1,5);
    if($rand1 == 5) {
        $rand = rand(1, 4);
        if($rand == 1) {
            $list = 'lmleg';
        } elseif($rand == 2) {
            $list = 'cpf';
        } elseif($rand == 3) {
            $list = 'sdw';
        } elseif($rand == 4) {
            $list = 'ipm';
        }
    } else {
        $list = $_GET['list'];
    }

    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();	

    switch ($list) {
        case "lmleg":
            lm_request('leadintake');
            break;
        
        case "sdw":
     		$ipdata = array( 0 => 
    			array(
    				"brandId" => "1001",
    				"pubCode"=> "SDW",
    				"campaignId"=> "MKT657484",
    				"effortId"=> "MKT660692",
    				"source"=> "MKT660692",
    				'authOptIn'=> true,            	
    				"email"=> $email,
    				"firstname" => $first_name,
    				"emailTemplateUrl"=> "https://marketingassets.cloudsna.com/prod/common/SDW/ConfirmationEmail/1659448436451.html",
    				"emailSubject"=> 'Your requested copy of: "Nightmare Winter: Our Secret Script for 2022" report',
    				"externalChannel"=> "ace_trwb"
    			),
    		);
    		$ippost = "";
    		foreach( $ipdata as $key => $value ) $ippost .= urlencode(settype($key, 'string')) . '=' . urlencode(settype($value, 'string')) . '&';
    		$ippost = rtrim($ippost, '& ');
    		$ipheaders = array("Content-Type: application/json","x-api-key: KV5DRehMVa7kKAdSduU1D9WG4eGxCkBXyEvD9sL5");
    
    
    		$ipc = curl_init();			
    		curl_setopt($ipc, CURLOPT_URL,"https://api.beaconstreetservices.com/leads/v1/external/api/createEletter");
    		curl_setopt($ipc, CURLOPT_POST, 1);
    		curl_setopt($ipc, CURLOPT_HTTPHEADER, $ipheaders);			
    		curl_setopt($ipc, CURLOPT_POSTFIELDS, json_encode($ipdata));			
    		curl_setopt($ipc, CURLOPT_RETURNTRANSFER, true);
    		$ipresponse = curl_exec($ipc);
    		curl_close ($ipc);
    		$f = fopen('./logs/bonner.csv', 'a');
    			fwrite($f,$adate.", ".$email. ", ".$ipresponse.PHP_EOL); 
    			fclose($f);   
    	    break;
    	    
    	case "cpf":
			$ipdata = array( 0 => 
				array(
					"brandId" => "11001",
					"pubCode"=> "CPF",
					"campaignId"=> "MKT588180",
					"effortId"=> "MKT653132",
					"source"=> "MKT653132",
					"email"=> $email,
					"firstname" => $first_name,
					"emailTemplateUrl"=> "marketingassets.cloudsna.com/prod/common/CPF/ConfirmationEmail/1636557551565.html",
					"emailSubject"=> "Your requested copy of: \"Your Secret to Getting an 'Unfair Advantage' Over Wall Street\" report",
					"externalChannel"=> "TrafficGrid"
				),
			);
			$ippost = "";
			foreach( $ipdata as $key => $value ) $ippost .= urlencode(settype($key, 'string')) . '=' . urlencode(settype($value, 'string')) . '&';
			$ippost = rtrim($ippost, '& ');
			$ipheaders = array("Content-Type: application/json","x-api-key: KV5DRehMVa7kKAdSduU1D9WG4eGxCkBXyEvD9sL5");
			$ipc = curl_init();			
			curl_setopt($ipc, CURLOPT_URL,"https://api.beaconstreetservices.com/leads/v1/external/api/createEletter");
			curl_setopt($ipc, CURLOPT_POST, 1);
			curl_setopt($ipc, CURLOPT_HTTPHEADER, $ipheaders);			
			curl_setopt($ipc, CURLOPT_POSTFIELDS, json_encode($ipdata));			
			curl_setopt($ipc, CURLOPT_RETURNTRANSFER, true);
			$ipresponse = curl_exec($ipc);
			curl_close ($ipc);
			$f = fopen('./logs/chaikin.csv', 'a');
				fwrite($f,$adate.", ".$email. ", ".$ipresponse.PHP_EOL); 
				fclose($f); 	
            break;
    	case "ipm":
			$ipdata = array( 0 => 
				array(
                	"brandId" => "6001",
                	"pubCode" => "IHOT",
                	"campaignId" => "MKT664303",
                	"effortId" => "MKT667489",
                	"source" => "MKT667489",
                	"email" => $email,
                	"firstname" => $first_name,
                	"emailTemplateUrl" => "https://marketingassets.cloudsna.com/prod/common/IHOT/ConfirmationEmail/1660673642393.html",
                	"emailSubject" => "CONFIRMED: Get ready for the Rapid Cash Flow Summit",
                	"externalChannel" => "Charles Flagle: Traffic Grid"
				),
			);
			$ippost = "";
			foreach( $ipdata as $key => $value ) $ippost .= urlencode(settype($key, 'string')) . '=' . urlencode(settype($value, 'string')) . '&';
			$ippost = rtrim($ippost, '& ');
			$ipheaders = array("Content-Type: application/json","x-api-key: 0KnGvAu4zO76HD8gie3lL5Q4WTVWnuvD93UmFYLg");
			$ipc = curl_init();			
			curl_setopt($ipc, CURLOPT_URL,"https://api.beaconstreetservices.com/leads/v1/external/api/createHotList");
			curl_setopt($ipc, CURLOPT_POST, 1);
			curl_setopt($ipc, CURLOPT_HTTPHEADER, $ipheaders);			
			curl_setopt($ipc, CURLOPT_POSTFIELDS, json_encode($ipdata));			
			curl_setopt($ipc, CURLOPT_RETURNTRANSFER, true);
			$ipresponse = curl_exec($ipc);
			curl_close ($ipc);
			$f = fopen('./logs/ipm_launch.csv', 'a');
				fwrite($f,$adate.", ".$email. ", ".$ipresponse.PHP_EOL); 
				fclose($f); 	
            break;        
        default:   
            lm_request('leadintake');
    }
    
    
	$wid = array(
		"project" => 'wallstreetwatchdogs.com',
		"cv_email" => $email
	); 
	
	woo_request($wid, 'track/identify');    
	
	
	
	$wdata = array(
		"domain" => 'wallstreetwatchdogs.com',
		'event' => 'sent_to_cobranded_list',
		'cv_email' => $email,
		'cv_cobranded_with_'.$list => true,
		'ce_cobranded_with_'.$list => true,
		'ce_revenue' => 3
    );
    woo_request($wdata, 'track/ce');	
sleep(2);
exit;    
?>