<?php
/******************************************
Copyright 2002-2011 Adrian Ling Kong Heng
Software: ESP (Email Service Providers API) 
Version: 1.0 (PRO)
Date   : April 2011
Author : Adrian Ling
Email  : adrian@easybiztools.com
*******************************************/
/*
function ecm_aweber       ($awdata,$autores_api,$listname,$prodname) {
function ecm_getresponse  ($grdata,$autores_api,$listname,$prodname) { 
function ecm_mailchimp    ($mcdata,$autores_api,$listname,$prodname) {
*/
$software_name = "SubscriptionMate";
// ---------------------//  
//        Aweber        //
// ---------------------//
function subm_aweber($awdata, $autores_api, $listname, $prodname, $subtype) {
    list($email,$cname,$cbreceipt,$key,$vendormail,$level) = explode("|",$awdata);
      
    global $software_name;
    if(!preg_match("/@aweber/",$listname)) { 
      $listname_addr="$listname@aweber.com";
    } else { $listname_addr = $listname; }

    $subj="Purchase Receipt";
    $body="
    Email: $email
    Name: $cname
    Receipt: $cbreceipt
    Product Name: $prodname
    Product Number: $key
    Source: $software_name
    ----------------------------------
    EasyBiztools System Generated Mesg
    ----------------------------------
    ";
    wp_mail($listname_addr, $subj, $body);
    wp_mail($vendormail, $subj, $body);
} //___________________________END AWEBER____________________________//
 

// -------------------- //
// Getresponse Function //
// -------------------- //
function subm_getresponse( $grdata, $autores_api, $listname, $prodname, $subtype) {
  include_once('jsonRPCClient.php');
  list($email,$cname,$cbreceipt,$key,$vendormail,$level) = explode("|",$grdata);

  $api_key = $autores_api;      $campaign_name = $listname;
  $api_url = 'http://api2.getresponse.com';  // API 2.x URL
  $client = new jsonRPCClient($api_url);    $result = NULL;

  try {   //________MUST get CAMPAIGN_ID FIRST!!!___________//
    $result = $client->get_campaigns(
        $api_key,
        array (  // find by name literally
            'name' => array ( "EQUALS" => "$campaign_name" )
        )
    );
  }
  catch (Exception $e) {  //die($e->getMessage()); // display err mesg
     $err = $e->getMessage();
     wp_mail("$vendormail","Getresponse API Error","$err","From: $vendormail");
  }
  $CAMPAIGN_ID = array_pop(array_keys($result));

  //_____ now, create the Array data for subscriber ________//
  if($subtype == "Affiliates") {
     $nick = $cbreceipt; $passwd = $key;
     $contact_array = array (
            "campaign"  => "$CAMPAIGN_ID",
            "name"      => "$cname",
            "email"     => "$email",
            "cycle_day" => "0",
            "customs" => array(
                array(
                    "name" => "nick",
                    "content" => "$nick",
                ),
                array(
                    "name" => "passwd",
                    "content" => "$passwd",
                )
              ),
          );      
  }
  if($subtype == "Customers") {
     $contact_array = array (
            "campaign"  => "$CAMPAIGN_ID",
            "name"      => "$cname",
            "email"     => "$email",
            "cycle_day" => "0",
            "customs" => array(
                array(
                    "name" => "cbreceipt",
                    "content" => "$cbreceipt",
                ),
                array(
                    "name" => "productID",
                    "content" => "$key",
                )
              ),
          );  
  }
  //________ ADD subscriber to Campaign________________//
  if($CAMPAIGN_ID !="") {
     try {
      $result = $client->add_contact(
        $api_key,
        $contact_array
        );   //var_dump($result);
     }
    catch (Exception $e) {    //die($e->getMessage());
      $err = $e->getMessage();
      wp_mail("$vendormail","Getresponse API Error","$err","From: $vendormail");
    }
  } else {
     wp_mail("$vendormail","Getresponse API Error","No Campaign Name/ID found","From: $vendormail");
  }

} //___________________________END GETRESPONSE____________________________//


// -------------------- //
// MailChimp Function   //
// -------------------- //
function subm_mailchimp($mcdata, $autores_api, $listname, $prodname,$subtype) {
  include_once('MCAPI.class.php');
  list($email,$cname,$cbreceipt,$key,$vendormail,$level) = explode("|",$mcdata);
  
  if(file_exists("mailchimp_conf.php")) { include_once("mailchimp_conf.php"); }
  if($autores_api=="") { $autores_api = $mc_apikey; }
  if($listname == "")  { $listname = $mc_list_id; }
  
  // setting some defaults //
  if($alertsuccess=="") { $alertsuccess=0;}   if($alertfailure=="") { $alertfailure=1;}
  if($mc_doubleoptin=="") { $mc_doubleoptin = "1"; }
  if(mc_emailtype=="") { $mc_emailtype = "text"; }
  
  list($fname, $lname) = explode(" ", $cname);
  if($subtype=="Affiliates") {
        $mc_mergevars = array(
                    'FNAME' =>  "$fname",
                    'LNAME' =>  "$lname",
                    'NICK'  =>  "$tempx",
                    'PASSWD'=>  "$tempy"
                  );       
  }
  if($subtype=="Customers") { //  customer's data
        $mc_mergevars = array(
                    'FNAME' =>  "$fname",
                    'LNAME' =>  "$lname",
                    'CBRECEIPT'  =>  "$cbreceipt",
                    'PRODUCTID'=>  "$key"
                  );
  }
  
  $api_chimp = new MCAPI($autores_api);    $list_id = "$listname";
  if($api_chimp->listSubscribe($list_id, $email, $mc_mergevars, $mc_emailtype, $mc_doubleoptin) === true) {		
		if($alertsuccess) {
		 if($subtype=='Affiliates') {
        $msg = "New Subscriber info:
        Name: $fname $lname
        Email: $email
        Nickname: $tempx
        Passwd: $tempy\n\nSubscribed via API";
      } 
      if($subtype=='Customers') {
        $msg = "New Member Signup:
        Name: $fname $lname
        Email: $email
        Receipt: $cbreceipt
        Product ID: $key\n\nSubscribed via API";
      }
      wp_mail("$vendormail","MailChimp New Subscriber","$msg","From: $vendormail");
    } 
  } else { // API error
		 //return 'Error: ' . $api_chimp->errorMessage;
		 if($alertfailure) {
       $err = 'Subscription Error: ' . $api_chimp->errorMessage;
       wp_mail("$vendormail","MailChimp Error","$err","From: $vendormail");
     }
  }
} //------------------ END MAILCHIMP ----------------------- //

// -------------------------------//
// Simple Autosubscribe via Email //
// -------------------------------//
function subm_emailsub($emdata, $autores_api, $listname, $prodname, $subtype) {
    list($email,$cname,$cbreceipt,$key,$vendormail,$level) = explode("|",$emdata);
    global $software_name;
    if($subj=='') { $subj="New Member Sign-up"; }
    $body="
    Email: $email
    Name: $cname
    Receipt: $cbreceipt
    Product Name: Membership Level $level
    Product Number: $key
    Source: $software_name 
    ----------------------------------
    EasyBiztools System Generated Mesg
    ----------------------------------\n";
    wp_mail($listname, $subj, $body);  
}
?>