<?php
		  // PHPCards v0.2, Allows people to set up simple online greeting cards. 
                  // Copyright (C) 2000, Mike Gifford www.openconcept.on.ca 
                  // 
                  // This script is free software; you can redistribute it and/or 
                  // modify it under the terms of the GNU General Public License 
                  // as published by the Free Software Foundation; either version 2 
                  // of the License, or (at your option) any later version. 
                  // 
                  // This program is distributed in the hope that it will be useful, 
                  // but WITHOUT ANY WARRANTY; without even the implied warranty of 
                  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
                  // GNU General Public License http://www.gnu.org/copyleft/ for more details. 
                  // 
                  // If you distribute this code, please maintain this header. 
                  // 
                  // This script allows users to select an image and write a personalized message. 
                  // It then sends a notice to the user specified email address and saves that 
                  // information to a text file. The receipient then can go and access both 
                  // the saved message and image from the website at a specified URL. 
                  // 
                  // Includes: send.php3, send.php3, receive.phtml, receive.php3 

include("$DOCUMENT_ROOT/ireru/ecardsconfig.php3");

// Counter Script
$free=0;
$empty="empty\n";
$oldcards = file($cardfile);
$total = count($oldcards);



for($i = 0 ; $i < count($oldcards) ; $i++) 
{
	if($array[$i] == $empty) 
	{
	$free=$i;
	}
}

if($free=="0")
{
if($oldcards==$empty)
	{
	$free=0;
	}
	else
	{
	$free=$total;
	}
}

// Place existing cards into an array.
$fd = fopen("$cardfile", "r"); 
$oldcards = "";       // Leave this blank 
	while ($buffer = fgets($fd, 4096)) { 
	$oldcards .= $buffer; 
} 
fclose($fd); 

// Change Text so that it doesn't interfere with messages
$message = str_replace(":",chr(0),$message);  		    // Colon to null character
$message = str_replace("  ","&nbsp; ",$message);  	  // Double Space to HTML Space (&nbsp;) 
$remail = str_replace(":",chr(0),$remail);    		
$recipient = str_replace(":",chr(0),$recipient);    		
$email = str_replace(":",chr(0),$email);    		
$name = str_replace(":",chr(0),$name);    		

// Generate a Random Number - Security
srand((double)microtime()*1000000);			// Random Seed	
$eci = rand(1000,9000);				       // Range (between 1000-9000)

  // What is written to the Text File
$newcard = $free+1 . ":" . $picture . ":" . nl2br(htmlentities($remail)) . ":" . nl2br(htmlentities($recipient)) . ":" . nl2br(htmlentities($email)) . ":" . nl2br(htmlentities($name)) . ":" . nl2br(htmlentities($message )) . ":" . $eci . "\n";     

$allcards = $oldcards . $newcard;
$idnumber=$free+1;

  // If the recipient's email is correct
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$", $remail)) {  
                  
// Preview Message

// echo "<img src=\"graphics/$picture\"><a href=\"mailto:$remail\">";
// echo stripslashes(htmlentities($recipient))."</a>From: <a href=\"mailto:$email\">";
// echo stripslashes(htmlentities($name))  . "</a>";
// echo "Message:<br />" . stripslashes(nl2br(htmlentities($message))) ;

  // If the name's email is correct
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$", $email)) {

	
		// ***************************************************************************************************************************************************
		// Post the message
		// ***************************************************************************************************************************************************

		
		$cartFile = fopen("$cardfile","w"); 
		fwrite($cartFile,$allcards); 
		fclose($cartFile); 
		
		
		// Message to be included in email
		  
		$email_message = "Greetings!
		This is to let you know that $name has created a $sitename e-card for you.  
		To see your card, click the link below:
		
		".$site_url."ecards/receive.php3?viewcard=$idnumber&eci=$eci
		 	
		or copy and paste the following website address into your browser:
		
		".$site_url."ecards/receive.php3 
		and enter Pick-Up number: $eci";
		
		
		  // Send to recipient  - Taken Out: Reply-To: $email \r\n X-Mailer: PHP3 \r\n 
		mail(stripslashes($recipient) . "<$remail>", "A ".$sitename." e-card from ".stripslashes($name) , $email_message,"From: $email \nReply-To:$email\nX-Mailer: PHP3\r\n Errors-To: $errors_to_email"); 
		
		// ***************************************************************************************************************************************************
		// Print a confirmation
		// ***************************************************************************************************************************************************
		print '<h1>Celtic e-Cards</h1>';		
		print '<p>Your e-card has been sent to '.stripslashes($recipient); 
		print '<p>Click <a href="'.$ecards_home_url.'">here</a> to send another card<p>';
		

}  

// ***************************************************************************************************************************************************

		// End of verification for $email
else { echo "<strong>This card was NOT sent because a valid email address was not provided!</strong><p>\n"; }

}  		// End of verification for $remail
else { echo "<strong>A preview was NOT provided because a valid email address was not provided!</strong><p>\n"; }

?>