<?php
/***************************************************************************
 *  functions.php                                   script-name: phpECard  *
 *  ---------------------------------------------------------------------  *
 *  version                  : 2.1.4                                       *
 *  script suport page       : http://quick-xs.net/phpecard                *
 *  author email             : phpecard@quick-xs.net                       *
 *                                                                         *
 ***************************************************************************
 *                                                                         *
 *  The complete functionality of the script is defined in this file.      *
 *  ANYTHING YOU EDIT HERE CAN STOP THE SCRIPT FROM WORKING CORRECT!       *
 *                                                                         *
 ***************************************************************************/


// First lets perform some required actions:

	// Include configuration file (phpECard/config.php)
	include($_GET['include_path'].'config.php');


	// Include default language file
	include($_GET['include_path'].'lang_'.$default_language.'.php');
	global $lang;


	// Function to make links in gallery
	function makelink($image_name) {
		return $_SERVER['PHP_SELF'].'?'.$_GET['preset_vars'].'card='.$image_name; 
	}


	// Make sure that $action always is "compose" if not "send"
	if ($_POST['action'] != 'send') 	
	{ 
		$action = 'compose'; 
	}


	// function to test email-syntax, used later on
	function IsEmail($mail) 
	{ 
		return ereg("[A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+", $mail); 
	}

	$_GET['index_url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$preset_vars;
	$_GET['preview_link'] = $_SERVER['PHP_SELF'].'?'.$preset_vars.'id=preview_'.$default_language;
	$_GET['include_path'] = $include_path;
	$_GET['preset_vars'] = $preset_vars;



// check to see weither an id is defined
if ((IsSet($_GET['id'])) && ($_GET['id'] != '')) 
{
	// Check to see if id-datafile is present
	if(file_exists($_GET['include_path'].'userdata/'.$_GET['id'].'.php')) 
	{ 
		// Include the datafile and send confirmation back to sender if wanted
		include($_GET['include_path'].'userdata/'.$_GET['id'].'.php');

		if ($id_info['notify'] == 'on') 
		{
			$notify_mail_sender = $lang['dear'].' '.$id_info['s_name'].','."\n\n".$id_info['r_name'].' ('.$id_info['r_mail'].')'.$lang['recipient_has_seen']."\n\n".$lang['please_come_back']."\n".$_GET['index_url']."\n\n".$lang['regards']."\n\n".$service_name.' ('.$admin_email.')'.$add;

			mail($id_info['s_mail'],$id_info['r_name'].$lang['notify_sender_subject'],$notify_mail_sender, "From: ".$service_name."<".$admin_email.">");

			$CRD_info = '<?php $id_info = array(\'notify\' => \'off\',\'sub\' => \''.$id_info['sub'].'\',\'s_name\' => \''.$id_info['s_name'].'\',\'s_mail\' => \''.$id_info['s_mail'].'\',\'r_name\' => \''.$id_info['r_name'].'\',\'r_mail\' => \''.$id_info['r_mail'].'\',\'date\' => \''.$id_info['date'].'\',\'time\' => \''.$id_info['time'].'\',\'card\' => \''.$id_info['card'].'\',\'msg\' => \''.$id_info['msg'].'\', ); ?>';

			fwrite(fopen($_GET['include_path'].'userdata/'.$_GET['id'].'.php',"w"),$CRD_info);
		}

		$card_info = $lang['card_to'].' <u>'.$id_info['r_name'].'</u> - '.$lang['sent_at'].' '.$id_info['date'].', '.$lang['at'].' '.$id_info['time'].$lang['hour'];

		$template = 'view';

		include($_GET['include_path'].'template.php');
	}

	// else id is not present
	else 
	{
		$template = 'id_error';

		include($_GET['include_path'].'template.php');
	}

}

// So if no id is defined... lets see weither to send or to compose?
else
{
	if ($_POST['action'] == 'send')
	{
		// Check the variables for IsSet & syntaxis:
		if ($_POST['subject'] == '') 
		{ 
			$error1 = $lang['enter_subject'].'<br><br>'; 
		}

		if (($_POST['msg'] == '') or ($_POST['msg'] == $lang['type_msg'])) 
		{ 
			$error2 = $lang['enter_msg'].'<br><br>'; 
		}

		if (($_POST['sender_name'] == '') or ($_POST['sender_name'] == $lang['name_here'])) 
		{ 
			$error3 = $lang['enter_sender_name'].'<br><br>'; 
		}

		if ((!IsEmail($_POST['sender_email'])) or ($_POST['sender_email'] == '')) 
		{ 
			$error4 = $lang['enter_sender_email'].'<br><br>'; 
		}

		if ($_POST['recipient_name'] == '') 
		{ 
			$error5 = $lang['enter_recipient_name'].'<br><br>'; 
		}

		if ((!IsEmail($_POST['recipient_email'])) or ($_POST['recipient_email'] == '')) 
		{ 
			$error6 = $lang['enter_recipient_email'].'<br><br>'; 
		}

		$error = $error1.$error2.$error3.$error4.$error5.$error6;

		if ($error == '')
		{
			// So the form is submitted and there are no errors
			// ... now we can save the data and email the recipient

			$count = fread(fopen($_GET['include_path'].'userdata/count.txt',"r"),4096) + 1;

			fwrite(fopen($_GET['include_path'].'userdata/count.txt',"w+"),$count);

			$id = 'CRD'.date("ymdHi").$count;

			$view_card_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_GET['preset_vars'].'id='.$id;

			$CRD_info = '<?php $id_info = array(\'notify\' => \''.$_POST['notify_sender'].'\',\'sub\' => \''.htmlspecialchars($_POST['subject']).'\',\'s_name\' => \''.htmlspecialchars($_POST['sender_name']).'\',\'s_mail\' => \''.$_POST['sender_email'].'\',\'r_name\' => \''.htmlspecialchars($_POST['recipient_name']).'\',\'r_mail\' => \''.$_POST['recipient_email'].'\',\'date\' => \''.date("d-m-Y").'\',\'time\' => \''.date("H:i").'\',\'card\' => \''.$_POST['card'].'\',\'msg\' => \''.htmlspecialchars(stripslashes($_POST['msg'])).'\', ); ?>';

			fwrite(fopen($_GET['include_path'].'userdata/'.$id.'.php',"w+"),$CRD_info);

			$notify_mail_recipient = $lang['dear_recipient']."\n\n".$lang['sender_has_sent']."\n\n". $lang['get_card_here'] ."\n".$view_card_url."\n\n". $lang['time_limit']."\n\n". $lang['regards'] ."\n\n".$service_name.' ('.$admin_email.')'.$add;

			mail($_POST['recipient_email'],$lang['notify_recipient_subject'],$notify_mail_recipient, "From: ".$service_name."<".$admin_email.">");

			if ($_POST['notify_sender'] != 'on') 
			{ 
				$lang['card_viewed_confirm'] = ''; 
			}

			$template = 'sent';

			include($_GET['include_path'].'template.php');
		}

		// Else if no sending is wanted or if there are errors ...
		elseif ($error != '')
		{
			$action = 'compose';
		}

	}

	if ($action == 'compose')
	{
		if (((!IsSet($_GET['card'])) or ($_GET['card'] == '')) and ((!IsSet($_POST['card'])) or ($_POST['card'] == '')))
		{
			// Show gallery
			include($_GET['include_path'].'_gallery.php');
		}

		else
		{
			// Check input fields for IsSet, if not assign defaults
			if ((!IsSet($_POST['msg'])) or ($_POST['msg'] == '')) 
			{
			$msg_echo = $lang['type_msg']; 
			}

			else 
			{
			$msg_echo = $_POST['msg']; 
			}

			if ((!IsSet($_POST['sender_email'])) or ($_POST['sender_email'] == '')) 
			{
			$sender_email_echo = $lang['email_here']; 
			}

			else 
			{
			$sender_email_echo = $_POST['sender_email']; 
			}

			if ((!IsSet($_POST['sender_name'])) or ($_POST['sender_name'] == '')) 
			{
			$sender_name_echo = $lang['name_here']; 
			}

			else 
			{
			$sender_name_echo = $_POST['sender_name']; 
			}

			// Include compose-template

			$template = 'compose';

			include($_GET['include_path'].'template.php');
		}

	}

}

// Next line is a footer message to promote this script, please leave intact!
echo('<center><font style="color:#000000; font: 12px Arial;"><i>Powered by: <a href="http://www.quick-xs.net/phpecard" target="_blank">phpECard</a></i></font></center>');
?>