<?php
$email = $_REQUEST["email"];
$remove = (int)$_REQUEST["remove"];

if($remove == 0)
{

	$eList = fopen("mailUpdAddrs.txt", "a+");
	fputs($eList, trim($email) . "\n");
	fclose($eList);

	$subject = "ASF Sign-Up Confirmation";
	$headers = "From: A Space Forth";
	$message = "
You have been signed up to receive A Space Forth update e-mails. Each time material is added to the A Space Forth homepage or Voidqast, the A Space Forth podcast, you will receive an e-mail saying so, possibly with an additional explanation of the added content.
\n
If you received this email by accident, please visit http://aspaceforth.decidel.net/SignUp.php and enter your e-mail address to cancel this subscription and avoid receiving more e-mails.
";

	mail("decidel@gmail.com","New ASF Update User","Someone new has subscribed to receive ASF Updates:\n".trim($email),"From: A Space Forth");

}
else
{

	$eList = fopen("mailUpdAddrs.txt", "r+");
	$emails = array();
	$holder = "";
	$size = 0;
	$r = $remove - 1; //The index to exclude when re-writing the file without the given subscriber

	for ($i = 0; !feof($eList); $i++)
	{
		$holder = trim(fgets($eList));
		if ($holder != "\n" && $i != $r)
		{
			$emails[$i] = $holder;
			$size = $i;
		}
		else if($holder == "\n")
		{
			$i -= 2;
			$size = $i;
		}
	}
	
	fclose($eList);
	$eList = fopen("mailUpdAddrs.txt","w");
	
	for ($i = 0; $i < $size; $i++)
	{
		fputs($eList,$emails[$i] . '\n');
	}
	
	fclose($eList);
	
	$subject = "ASF Subscription Cancelled";
	$headers = "From: A Space Forth";
	$message = "
Your subscription to A Space Forth updates has been cancelled. You will no longer receive e-mails when new content is added on the A Space Forth homepage or the Voidqast podcast. I'll miss you! ='(
\n
If you did not unsubscribe from A Space Forth updates, please visit http://aspaceforth.decidel.net/SignUp.php and enter your e-mail address to be re-added to the list.
";

	mail("decidel@gmail.com","ASF Updates Subscriber Cancelled","One of the subscribers to ASF updates has unsubscribed:\n".trim($email),"From: A Space Forth");

}

echo "<head>
<title>A Space Forth: E-Mail Updates - Action Confirmation</title>
</head>
<body bgcolor=#FFFFFF>
<font face='Arial, Helvetica, sans-serif' color='#000000'>
<center>
<p>An e-mail has been sent to " . $email . " confirming the action you have just taken.</p>
</center>
</font>
</body>
";
mail($email,$subject,$message,$headers);

?>