<?php
///////////////////////////////////////////////////////////////////////////////////////
// PHPizabi 0.848b C1 [ALICIA]                               http://www.phpizabi.net //
///////////////////////////////////////////////////////////////////////////////////////
// Please read the license.txt file before using / modifying this software           //
// Original author:         Claude Desjardins, R - feedback@realitymedias.com        //
// Last modification date:  August 13th 2006                                         //
// Version:                 PHPizabi 0.848b C1                                       //
//                                                                                   //
// (C) 2005, 2006 Real!ty Medias / PHPizabi - All rights reserved                    //
///////////////////////////////////////////////////////////////////////////////////////

	/* Check Structure Availability */
	if (!defined("CORE_STRAP")) die("Out of structure call");


	$tpl = new template;
	$tpl -> Load("tellafriend");
	$tpl -> GetObjects();

	/*
		Let's see if we got an origin value, set one
		if we got nothing
	*/
	if (isset($_GET["origin"])) $origin = base64_decode($_GET["origin"]);
	else $origin = "http://".$_SERVER['HTTP_HOST'].str_replace("/index.php", NULL, $_SERVER['PHP_SELF']);
	
	/*
		Assign the origin address to the template
	*/
	$tpl -> AssignArray(array("origin.address" => substr($origin, 0, 40)));
	
	
	// HANDLE POST /////////////////////////////////////////////////////////////////////
	if (isset($_POST["Submit"]) && isset($_POST["email"])) {
			
		$emailAddressesArray = explode(",", $_POST["email"]);
			
		foreach ($emailAddressesArray as $email) {
		
			$email = trim($email);
		
			if (preg_match($CONF["REGEXP_EMAIL"], $email)) {
		
				if (isset($_POST["type"]) && $_POST["type"] == "invite") {
				
					// INVITE MODE //
				
					$mail = new template;
					$mail -> LoadThis(file_get_contents("theme/templates/GLOBALS/mails/invite.tpl"));
					$mail -> AssignArray(array(
						"invite.url" => "http://".$_SERVER['HTTP_HOST'].str_replace("/index.php", NULL, $_SERVER['PHP_SELF']),
						"invite.email" => $email,
						"invite.body" => $_POST["body"],
						"site.name" => $CONF["SITE_NAME"],
						"site.url" => "http://".$_SERVER['HTTP_HOST'].str_replace("/index.php", NULL, $_SERVER['PHP_SELF']),
						"me.username" => (me("username")?me("username"):$GLOBALS["OBJ"]["guest"])
					));
				
					$mailContent = explode("\n", $mail -> Flush(1), 2);

					include_once("system/functions/classes/mail.class.php");
							
					$mail = new SendMail;
					$mail -> From = 			"myqun.com <{$CONF["SITE_SYSTEM_EMAIL"]}>";
					$mail -> To =				$email;
					$mail -> Subject = 			$mailContent[0];
					$mail -> Body = 			$mailContent[1];
					$mail -> SMTPHost = 		$CONF["MAIL_SMTP_HOST"];
					$mail -> SMTPPort = 		$CONF["MAIL_SMTP_PORT"];
					$mail -> SMTPUser = 		$CONF["MAIL_SMTP_USER"];
					$mail -> SMTPPassword = 	$CONF["MAIL_SMTP_PASSWORD"];
					$mail -> SMTPTimeout = 		$CONF["MAIL_SMTP_TIMEOUT"];
					$mail -> MailMethod = 		$CONF["MAIL_METHOD"];
					$mail -> Charset = 			$CONF["MAIL_CHARSET"];
					$mail -> Encoding = 		$CONF["MAIL_ENCODING"];
					$mail -> SendmailPath = 	$CONF["MAIL_SENDMAIL_PATH"];
					$mail -> Send();
			
				} else {

					// TELL A FRIEND MODE //
						
					$mail = new template;
					$mail -> LoadThis(file_get_contents("theme/templates/GLOBALS/mails/tellafriend.tpl"));
					$mail -> AssignArray(array(
						"tell.url" => $origin,
						"tell.email" => $email,
						"tell.body" => $_POST["body"],
						"site.name" => $CONF["SITE_NAME"],
						"site.url" => "http://".$_SERVER['HTTP_HOST'].str_replace("/index.php", NULL, $_SERVER['PHP_SELF']),
						"me.username" => (me("username")?me("username"):$GLOBALS["OBJ"]["guest"])
					));
						
					$mailContent = explode("\n", $mail -> Flush(1), 2);

					include_once("system/functions/classes/mail.class.php");
							
					$mail = new SendMail;
					$mail -> From = 			"myqun.com <{$CONF["SITE_SYSTEM_EMAIL"]}>";
					$mail -> To =				$email;
					$mail -> Subject = 			$mailContent[0];
					$mail -> Body = 			$mailContent[1];
					$mail -> SMTPHost = 		$CONF["MAIL_SMTP_HOST"];
					$mail -> SMTPPort = 		$CONF["MAIL_SMTP_PORT"];
					$mail -> SMTPUser = 		$CONF["MAIL_SMTP_USER"];
					$mail -> SMTPPassword = 	$CONF["MAIL_SMTP_PASSWORD"];
					$mail -> SMTPTimeout = 		$CONF["MAIL_SMTP_TIMEOUT"];
					$mail -> MailMethod = 		$CONF["MAIL_METHOD"];
					$mail -> Charset = 			$CONF["MAIL_CHARSET"];
					$mail -> Encoding = 		$CONF["MAIL_ENCODING"];
					$mail -> SendmailPath = 	$CONF["MAIL_SENDMAIL_PATH"];
					$mail -> Send();
				}
			}			
		}
	}
	
	$tpl -> Flush();
?>