<?php
/* =============================================================== *\
|		Module name:      Tell friend								|
|																	|
\* =============================================================== */

if (!defined('IN_SITE')){
     die('Hacking attempt!');
}
//Module language
$Func->import_module_language("home/lang_tellfriend". PHP_EX);

$TellFriend = new Tell_Friend;

class Tell_Friend
{
	var $data	= array();

	function Tell_Friend(){
		global $Info;

		$Info->tpl_header	= "";
		$Info->tpl_footer	= "";
		$Info->tpl_main		= "tellfriend";

		$act	= isset($_POST['act']) ? htmlspecialchars($_POST['act']) : '';
		switch ($act){
			case "send":
				$this->do_send_email();
				break;
			default:
				$this->pre_send_email();
		}
	}

	function pre_send_email($msg = ""){
		global $Func, $Info, $Template, $Lang;

		if ( !isset($this->data['u_referer']) ){
			$this->data['u_referer']	= urldecode($Func->get_vars('u_referer', 1));
			if ( !empty($this->data['u_referer']) ){
				$this->data['u_referer']	= str_replace('@@', '/', $this->data['u_referer']);
			}
			else{
				$this->data['u_referer']	= $Info->option['site_url'];
			}
		}
		else{
			$this->data['u_referer']	= stripslashes($this->data['u_referer']);
		}

		$Template->set_vars(array(
			'MSG'				=> $msg,
			'S_ACTION'			=> $Info->option['site_url'] . $Func->compile_url(array('mod' => MOD_TELLFRIEND)),
			'L_TELLFRIEND'		=> $Lang->data['general_tellfriend'],
			'L_YOUR_NAME'		=> $Lang->data['tellfriend_your_name'],
			'L_YOUR_EMAIL'		=> $Lang->data['tellfriend_your_email'],
			'L_FRIEND_NAME'		=> $Lang->data['tellfriend_friend_name'],
			'L_FRIEND_EMAIL'	=> $Lang->data['tellfriend_friend_email'],
			'L_MESSAGE'			=> $Lang->data['tellfriend_message'],
			'L_ERROR_NOT_FULL'	=> $Lang->data['general_error_not_full'],
			'L_ERROR_EMAIL'		=> $Lang->data['general_error_email'],
			'L_BUTTON'			=> $Lang->data['button_send'],
			'U_REFERER'			=> $this->data['u_referer'],
			'YOUR_NAME'			=> isset($this->data['your_name']) ? stripslashes($this->data['your_name']) : '',
			'YOUR_EMAIL'		=> isset($this->data['your_email']) ? stripslashes($this->data['your_email']) : '',
			'FRIEND_NAME'		=> isset($this->data['friend_name']) ? stripslashes($this->data['friend_name']) : '',
			'FRIEND_EMAIL'		=> isset($this->data['friend_email']) ? stripslashes($this->data['friend_email']) : '',
			'MESSAGE'			=> isset($this->data['message']) ? stripslashes($this->data['message']) : '',
		));
	}

	function do_send_email(){
		global $Func, $Info, $Template, $DB, $Lang;

		$this->data['u_referer']	= isset($_POST['u_referer']) ? htmlspecialchars($_POST['u_referer']) : '';
		$this->data['your_name']	= isset($_POST['your_name']) ? htmlspecialchars($_POST['your_name']) : '';
		$this->data['your_email']	= isset($_POST['your_email']) ? htmlspecialchars($_POST['your_email']) : '';
		$this->data['friend_name']	= isset($_POST['friend_name']) ? htmlspecialchars($_POST['friend_name']) : '';
		$this->data['friend_email']	= isset($_POST['friend_email']) ? htmlspecialchars($_POST['friend_email']) : '';
		$this->data['message']		= isset($_POST['message']) ? htmlspecialchars($_POST['message']) : '';

		if ( empty($this->data['your_name']) || empty($this->data['your_email']) || empty($this->data['friend_name']) || empty($this->data['friend_email']) || empty($this->data['message']) ){
			$this->pre_send_email($Lang->data['general_error_not_full']);
			return false;
		}
		if ( !$Func->check_email($this->data['your_email']) || !$Func->check_email($this->data['friend_email']) ){
			$this->pre_send_email($Lang->data['general_error_email']);
			return false;
		}

		$mail_subject	= $Lang->data['tellfriend_message_subject'];
		$mail_subject	= str_replace('{YOUR_NAME}', $this->data['your_name'], $mail_subject);
		$mail_subject	= str_replace('{FRIEND_NAME}', $this->data['friend_name'], $mail_subject);

		$mail_content	= $Lang->data['tellfriend_message_content'];
		$mail_content	= str_replace('{YOUR_NAME}', $this->data['your_name'], $mail_content);
		$mail_content	= str_replace('{YOUR_EMAIL}', $this->data['your_email'], $mail_content);
		$mail_content	= str_replace('{FRIEND_NAME}', $this->data['friend_name'], $mail_content);
		$mail_content	= str_replace('{FRIEND_EMAIL}', $this->data['friend_email'], $mail_content);
		$mail_content	= str_replace('{MESSAGE}', stripslashes($this->data['message']), $mail_content);
		$mail_content	= str_replace('{U_ARTICLE}', stripslashes($this->data['u_referer']), $mail_content);
		$mail_content	= str_replace('{U_REFERER}', stripslashes($this->data['u_referer']), $mail_content);//Compatible with old versions

//		$Func->send_email($this->data['friend_email'], $mail_subject, $mail_content, $this->data['your_name'] ."<" . $this->data['your_email'] .">");
		//SMTP -------------------------------
		include('includes/smtp'. PHP_EX);
		$SMTP	= new SMTP($Info->option['smtp_host'], $Info->option['smtp_username'], $Info->option['smtp_password']);
		$SMTP->email_from($this->data['your_email']);
		$SMTP->email_to($this->data['friend_email']);
		$SMTP->message_charset($Lang->charset);
		$SMTP->message_subject($mail_subject);
		$SMTP->message_content($mail_content);
		$SMTP->send();
		//------------------------------------

		$this->data['friend_name']	= "";
		$this->pre_send_email($Lang->data["tellfriend_success_send"]);
		return true;
	}
}

?>