<?php

global $local_remote;
$local_remote = true;
define ('DWPC_DISABLE_AUTOLOG',true);

require_once('../../../../../wp-config.php');
require_once('../../opt.php');
require_once('../../includes/dwpc_start.php');
require_once('../../includes/opt_apptop.php');


global $wpdb;
global $dwpc;

$mailers = $wpdb->get_results('SELECT * FROM '.OPT_DB_PREFIX.'opt_mailers WHERE mailer_type = "GetResponse"',ARRAY_A);


if(empty($mailers)){echo 'No GetResponse Account authorized on this site. I smell Hax.';exit;}

$found = false;
foreach($mailers as $mailer)
{
	$api = opt_api_from_id($mailer['id']);

	if($api->aAccountInfo->login == $_REQUEST['account_login']){
		$found = true;
		break;	
	}
}


if(!$found){echo 'The Account Doesn\'t exist. Hax for sure.';exit;}

$action = $_REQUEST['action'];
$contact_email_id =  urldecode($_REQUEST['contact_email']);

$lead = $wpdb->get_row('SELECT * FROM '.OPT_DB_PREFIX.'opt_leads WHERE email_text_id="'.$contact_email_id.'"',ARRAY_A);

if(empty($lead)){
	$lead = $wpdb->get_results('SELECT * FROM '.OPT_DB_PREFIX.'opt_leads WHERE ip_address = "'.$_REQUEST['contact_ip'].'" AND subscription_status="Pending"',ARRAY_A);
}

if($action==''){$action='subscribe';}

if($action == 'subscribe')
{
	$set = array('subscription_status' => 'Subscribed');
	$where = array('id'=>$lead['id']);
	$wpdb->update(OPT_DB_PREFIX.'opt_leads',$set,$where);
}elseif($action == 'unsubscribe'){
	$set = array('subscription_status' => 'Unsubscribed');
	$where = array('id'=>$lead['id']);
	$wpdb->update(OPT_DB_PREFIX.'opt_leads',$set,$where);
}

exit();
?>