<?php

/*
account settings / my apps / create new
need userid, app key, app secret
Ours - uid: 2344
	App Key: 6934599572 | App Secret: 15426697771084872030

*/

class opt_api_sendreach implements opt_mailer_api {

	private $user_id;
	private $app_key;
	private $app_secret;
	private $url;
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }
		String on failure
		23959"	
	*/
	public function aGetLists(){
		$action = '&action=lists_view';	
		$response = file_get_contents($this->url.$action);
		$response = json_decode($response);
		$return = array();
		
		if(isset($response->error)){
			return $response->message;	
		}
		
		foreach($response as $val){
			$return[$val->id]['name'] = $val->list_name;
			$return[$val->id]['contacts'] = $this->getListSize($val->id);	
		}
		return $return;
	}
	/*"CAMPAIGN_ID" : {
            "name"              : "my_campaign_1",
            "description"       : "My campaign",
			"contacts"          : 56785
        }*/
	private function getListSize($id){
		$response = file_get_contents($this->url.'&action=list_size&list_id='.$id);
		$response = json_decode($response);
		return $response->list_size;	
	}
	public function aGetListById($id){
		
	}
	
	//campaign is the campaign id
	public function CreateContact($name,$email,$campaign,$phone,$extra){
		
		$ipAddress = $_SERVER['REMOTE_ADDR'];
		if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
			$ipAddress = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
		}
		
		$call = $this->url.'&action=subscriber_add&list_id='.$campaign.'&first_name='.rawurlencode($name).'&client_ip='.$ipAddress.'&last_name=%20&email='.rawurlencode($email);
		
		if($phone != ''){
			$call .= '&cell='.	rawurlencode($phone);
		}
		if(is_array($extra)){
			$i = 1;
			foreach($extra as $key=>$value){
				if($key == 'gender'){
					$call .= '&gender=' . rawurlencode($value);	
				}elseif($key == 'dob'){
					$call .= '&dob=' . rawurlencode($value);	
				}elseif($key == 'address1'){
					$call .= '&address_1='.rawurlencode($value);	
				}elseif($key == 'address2'){
					$call .= '&address_2='.rawurlencode($value);	
				}elseif($key == 'city'){
					$call .= '&city='.rawurlencode($value);	
				}elseif($key == 'state'){
					$call .= '&state='.rawurlencode($value);	
				}elseif($key == 'zip'){
					$call .= '&zip='.rawurlencode($value);	
				}elseif($key == 'country'){
					$call .= '&country='.rawurlencode($value);	
				}else{
					$call .= '&custom_'.$i.'='.rawurlencode($value);
					$i++;	
				}
			}
		}
		$response = file_get_contents($call);
		$response = json_decode($response,true);
		$response['api_type'] = 'SendReach';
		return $response;
	}
	
	//do we even actually need this one? I'll put off writing it for now until we decide. It would be a little strenuous on the server for aweber
	/*"CONTACT_ID" : {
            "campaign"      : "CAMPAIGN_ID",
            "name"          : "My Contact Name",
            "email"         : "my_contact_1@emailaddress.com",
        }*/
	public function aGetContacts($campaign=''){
		$call = $this->url.'&action=list_subscribers&list_id='.$campaign;
		$response = file_get_contents($call);
		return json_decode($response,true);	
	}
	
	//returns bool for subscription status 
	public function CheckOptinStatus($leads_id = 0){
		$lead = $wpdb->get_row('SELECT * FROM '.OPT_DB_PREFIX.'opt_leads WHERE id='.$leads_id);
		$lead['api_data'] = opt_unserialize($lead['api_data']);
		
		$sr_row = false;
		foreach($lead['api_data'] as $data){
			if($data['api_type'] == 'SendReach'){
				$sr_row = $data;
				break;	
			}
		}
		
		if($sr_row === false || $sr_row['status'] != 'success'){
			return false;	
		}
		
		$lead = $this->aGetLead($sr_row['hash']);
		if($lead['subscriber_optin_status'] == 'confirmed'){
			return true;
		}else{
			return false;
		}
		
	}
	public function VerifyFacebook($campaign){
		$contacts = $this->aGetContacts($campaign);
		$curTime = time();
		$maxTimeDiff = 20;	
		foreach($contacts as $contact){
			if($contact['social_site'] == 'facebook' && $curTime - $contact['added'] < $maxTimeDiff){
				return $contact;	
			}
		}
		return false;
	}
	public function aGetLead($hash){
		$call = $this->url.'&action=subscriber_view&subscriber_hash='.$hash;
		return json_decode(file_get_contents($call),true);	
	}
	function __construct($userid='',$appkey='',$appsecret=''){
		$this->user_id = $userid;
		$this->app_key = $appkey;
		$this->app_secret = $appsecret;
		$this->url = 'http://api.sendreach.com/index.php?key='.$this->app_key.'&secret='.$this->app_secret.'&user_id='.$this->user_id;
	}

}

?>