<?php
// * Part of the e-mailing script
@extract($POST);
$remark = stripslashes($_POST['remarks']);
$ip = getenv(REMOTE_ADDR);
// * End of necessary part for the e-mail script

session_start();

include "../config.php";
include "../header.php";
include "../style.php";

if( isset($_SESSION["ulogin"]) )
   	{  // members only stuff!

		include("navigation.php");
        include("../banners2.php");

		//////////////////////////////////////////////////////////
		//////////// E-mail notification to the Admin ////////////
		/////////// Part of the Deleted members script ///////////
		//////////////// Coded by Jonathan, v0.1 /////////////////
		//////////////////////////////////////////////////////////
		//Get the options of Leaves
		$options = mysql_query ("select * from leaves_options where id='1'");
        $array = mysql_fetch_array($options);
		$scriptactive = $array["leave_active"];
		$email = $array["leave_email"];
		$logleaved = $array["log_leaved"];
		
		if($scriptactive == 1)
		{
			//If the remarks are empty, inject the the notification in its stead
			if($remark == '') {$remark = "No remark was given.";}
			
			if($logleaved == 1)
			{
				//Get the user's details and parse the parts needed so it can be injected
				$userinfo=mysql_query ("select * from members where userid='".$userid."'");
				$userrecord=mysql_fetch_array($userinfo);

				$id=$userrecord["id"];
				$name=$userrecord["name"];
				$contact_email=$userrecord["contact_email"];
				$userid=$userrecord["userid"];
				$ip=$userrecord["ip"];
				$joindate=$userrecord["joindate"];
				$memtype=$userrecord["memtype"];
				$today = date('Y-m-d',strtotime("now"));
				
				//Insert the user's details into the database
				$query1 = "insert into leaves (id, ip, name, username, memtype, contact_email, reason, joindate, deleted) values
					('$id', '$ip', '$name', '$userid', '$memtype', '$contact_email', '$remark', '$joindate', '$today')";
					$result1 = mysql_query ($query1);
			}
			
			if($email == 1)
			{	
				//Where the e-mail has to go to
				$to = $array["adminemail"];
				
				//Subject of the e-mail
				$subject = "Member ".$_SESSION[uname]." deletion";
				
				//Body of the e-mail
				$body = "Hi Admin,\n\nThe member ".$_SESSION[uname]." deleted his/her account.\n\nThe IP used by the user is: ".$ip."\n\nThe reason of deletion was:\n".$remark;
				
				
				//Send the e-mail
				if (mail($to, $subject, $body))
				{
					//Delete the account
					delete_account($_SESSION[uname]);
					echo "<center>Your account has been successfully deleted from Database.</center>";
					
					//echo("<p>Admin was notified of the account deletion</p>");
				}
				else 
				{
					echo("<p><center>Could not delete the account at this moment, please try again in an hour or so.</center>");
					echo("\n\n<center>If this problem persists please contact the Administrator.</center>");
					
					//echo("<p>Mail() function failed!</p>");
				}
			}
			else
			{
				delete_account($_SESSION[uname]);
				echo "<center>Your account has been successfully deleted from Database.</center>";
			}
		}
		else
		{
			delete_account($_SESSION[uname]);
			echo "<center>Your account has been successfully deleted from Database.</center>";
		}
		//////////////////////////////////////////////////////////
		//////////// E-mail notification to the Admin ////////////
		//////////////// Coded by Jonathan, v0.1 /////////////////
		//////////////////////////////////////////////////////////

        echo "</td></tr></table>";

	}

else
  { ?>

  <p>You must be logged in to access this site. Please <a href="../memberlogin.php">click here</a> to login.</p>

  <? }

include "../footer.php";
mysql_close($dblink);
?>