<html>
<head></head>
<body onLoad="document.MyForm.Email.focus()">
<?php

echo <<< Text
<center>
<form name="MyForm" action={$_SERVER['PHP_SELF']} method="POST">
<b>To join the list, enter your email address: </b><BR>
<input name="Email" size=50>
<input type="hidden" name="submitted" value="TRUE">
<P>
<input type="submit" value="SEND"> 
<input type="reset" value="RESET">
</form></center>

Text;

if (isset($_POST['submitted']))
{
	$errors = array();
	$email = trim($_POST['Email']);
	if (empty($email))
		$errors[] = "<h1>You have to enter an email address to join the list</h1><br>";
	
	/* Now we'll check if the email address has "@" and "." in it and the part before "@" 
	is not empty and so are the parts before and after "."
	Note that this is not enough to prove that the email address is valid, because you
	need also to make sure the email domain exists. To see how to do that, see Page 908 
	in the book.
	*/
	
	/* Split the email address into two parts. The one before the first
	"@" and the one after the first "@". The first part will be stored in $sp[0] 
	and the second in $sp[1].
	Then split $sp[1] into two parts. The one before the first "." and the one
	after the first ".". The first part will be stored in $sp2[0] and the second 
	in $sp2[1].
	*/

	$sp = split("\@",$email,2);
	$sp2 =  split("\.",$sp[1],2);
	
	$z1 = trim($sp[0]); $z2 = trim($sp[1]);
	$z3 = trim($sp2[0]); $z4 = trim($sp2[1]);

	/* Check if the part before "@", the part before "." and the part 
	after "." are empty.
	*/

        if (empty($z1) || empty($z2) || empty($z3) || empty($z4))
		$errors[] =  "<h2 align=center>Invalid Email Address<BR></h2>\n";

	if (!empty($errors))
        {
               	echo "<h1 align=center>ERROR</h1><center>\n";
                foreach ($errors as $e)
                        echo $e, "<P>\n";
                echo "</center>\n";
        }
	else
	{
		$file_name = "/home/abu-jeib/temp/emails.txt";
		$a = get_array();

		/*
		Check if the email to be added is already in the list.
		If it's there, don't add it. If it is not there, add it.
		*/

		if (in_array($email,$a))
			exit("<b><center>Your email is already in the list.</center></b>.<br>\n");

		$a[] = $email;
		sort($a);

		write_array($a);
		echo "<h3 align=center>";
		echo "You've been added to the email list.<br></h3>\n";
	}
}

 function &get_array()
 {
	$file_name = "/home/abu-jeib/temp/emails.txt";
	$a = array();
        @ $f = fopen($file_name,'r');
        if (!$f)
        {
		if (!file_exists($file_name))
			echo "File \"$file_name\" does not exist.<br>\n";
		else
        		echo "<h2 align=center>Can't open file
				 \"$file_name\" for reading.</h2><BR>\n";
                exit;
        }
        $line = trim(fgets($f));
        while (!feof($f))
        {
        	if (!empty($line))
                	$a[] = $line;
                $line = trim(fgets($f));
        }
        fclose($f);
        return $a;
}
function write_array($a)
{
	$file_name = "/home/abu-jeib/temp/emails.txt";
	@ $f = fopen($file_name,'w');
        if (!$f)
        {
		echo "<h2 align=center>Can't open file \"$file_name\"
			for writing.</h2><BR>\n";
                exit;
        }
        foreach ($a as $val)
        	fwrite($f,$val."\n");
       	fclose($f);
}

?>
</body>
</html>
