<?php
/*///////////////////////////////////////////////////////////////////////
Part of the code from the book 
Building Findable Websites: Web Standards, SEO, and Beyond
by Aarron Walter (aarron@buildingfindablewebsites.com)
http://buildingfindablewebsites.com

Distrbuted under Creative Commons license
http://creativecommons.org/licenses/by-sa/3.0/us/
///////////////////////////////////////////////////////////////////////*/


function storeAddress(){

	// Specific to Joomla to call the API Key and List ID in a session var from the mod_s5_contact_popup.php page
	define( '_JEXEC', 1 );
	define( 'JPATH_BASE', dirname(dirname(dirname(dirname(__FILE__)))));

	require_once ( JPATH_BASE .'/includes/defines.php' );
	require_once ( JPATH_BASE .'/includes/framework.php' );
	$mainframe = JFactory::getApplication('site');
	$mainframe->initialise();

	$session = JFactory::getSession();
	$mailchimp_apikey = $session->get('mailchimp_apikey');
	$mailchimp_listid = $session->get('mailchimp_listid');
	
	// Validation
	if(!$_GET['email']){ return "No email address provided"; } 

	if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
		return "Email address is invalid"; 
	}

	require_once('MCAPI.class.php');
	// grab an API Key from http://admin.mailchimp.com/account/api/
	$api = new MCAPI(''.$mailchimp_apikey.'');
	

	// grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
	// Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
	$list_id = ''.$mailchimp_listid.'';

	if($api->listSubscribe($list_id, $_GET['email'], '') === true) {
		// It worked!	
		return 'Success! Check your email to confirm sign up.';
	}else{
		// An error ocurred, return error message	
		return 'Error: ' . $api->errorMessage;
	}
	
}

// If being called via ajax, autorun the function
if($_GET['ajax']){ echo storeAddress(); }
?>
