<?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 

  
  // Variables
$cardfile="/home/cart/quetzalcoatl/ecards/cardfile.txt";			// Text file for saving cards

  // 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);    		
$semail = str_replace(":",chr(0),$semail);    		
$sender = str_replace(":",chr(0),$sender);    		

// 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($semail)) . ":" . nl2br(htmlentities($sender)) . ":" . eregi_replace("\n", "<br>", $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 "<table BORDER=0 WIDTH=\"100%\">\n<tr>\n<td ALIGN=CENTER VALIGN=TOP>\n<IMG src=\"graphics/$picture\">\n</td>\n ";
echo "<td VALIGN=TOP>\n<table BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=2 WIDTH=\"100%\">\n<tr>\n<td>\n<b>To:</b>\n</td>\n<td>\n<a href=\"mailto:$remail\">";
echo stripslashes(htmlentities($recipient))  . "</a>\n</td>\n</tr>\n";
echo "<tr>\n<td>\n<b>From:</b></td>\n<td>\n<a href=\"mailto:$semail\">";
echo stripslashes(htmlentities($sender))  . "</a>\n</td>\n</tr>\n</table>\n";
echo "<B>Message:</B><BR>" . stripslashes(nl2br(htmlentities($message)))  . "</td>\n</td>\n</tr>\n</table>\n";

  // If the sender's email is correct
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$", $semail)) {

if ($epost == "yes") {   // Write to file
$cartFile = fopen("$cardfile","w"); 
fwrite($cartFile,$allcards); 
fclose($cartFile); 


  // Message to be included in email
  
$email_message = "To pick up your card, visit 
	http://www.celtic-art.com/ecards/receive.php3 
and enter Card Number  $idnumber and Random Number  $eci

	or 

click on
http://www.celtic-art.com/ecards/receive.php3?viewcard=$idnumber&eci=$eci




The Message Text Follows: 
" . eregi_replace("<br>", "\n",stripslashes($message));


  // Send to recipient  - Taken Out: Reply-To: $semail \r\n X-Mailer: PHP3 \r\n 
mail(stripslashes($recipient) . "<$remail>", stripslashes($sender) . " has sent you a card", $email_message,"From: $semail \nReply-To:$semail\nX-Mailer: PHP3\r\n Errors-To: mike@flora.org"); 

// To send another card
echo "<P><hr width=\"50%\"><P>Your card number $idnumber has been sent to " . stripslashes($recipient); 
echo "<p>\n<strong>Would you like to send another card?</strong><p>\n";
echo "<form method=\"GET\" action=\"send.php3\">";
echo "<input type=\"radio\" value=\"yes\" NAME=\"epost\">Yes, please!";
echo "<input type=\"radio\" value=\"no\" NAME=\"epost\">No, thank you.";
echo "<input type=hidden name=\"remail\" value=\"$remail\">";
echo "<input type=hidden name=\"recipient\" value=\"" . stripslashes(htmlentities($recipient)) . "\">";
echo "<input type=hidden name=\"semail\" value=\"$semail\">";
echo "<input type=hidden name=\"sender\" value=\"" . stripslashes(htmlentities($sender)) . "\">";
echo "<input type=\"submit\" value=\"Submit\"></form><br>";

}
elseif ($epost == "no") { 

echo "<P><hr width=\"50%\"><P><strong>Would you like to try again?</strong><p>\n";
echo "<form method=\"GET\" action=\"send.php3\">";
echo "<input type=\"radio\" value=\"yes\" NAME=\"epost\">Yes, please!";
echo "<input type=\"radio\" value=\"no\" NAME=\"epost\">No, thank you.";
echo "<input type=hidden name=\"picture\" value=\"$picture\">";
echo "<input type=hidden name=\"remail\" value=\"$remail\">";
echo "<input type=hidden name=\"recipient\" value=\"" . stripslashes(htmlentities($recipient)) . "\">";
echo "<input type=hidden name=\"semail\" value=\"$semail\">";
echo "<input type=hidden name=\"sender\" value=\"" . stripslashes(htmlentities($sender)) . "\">";
echo "<input type=hidden name=\"message\" value=\"" . stripslashes(htmlentities($message)) . "\">";
echo "<input type=\"submit\" value=\"Submit\"></form><br>";

 }

else  { 

echo "<P><hr width=\"50%\"><P><strong>You have not YET sent the card.</strong><p>\n";
echo "<form method=\"GET\" action=\"send.php3\">";
echo "<input type=\"radio\" value=\"yes\" NAME=\"epost\">Yes, please send my card!";
echo "<input type=\"radio\" value=\"no\" NAME=\"epost\">No thank you.";
echo "<input type=hidden name=\"picture\" value=\"$picture\">";
echo "<input type=hidden name=\"remail\" value=\"$remail\">";
echo "<input type=hidden name=\"recipient\" value=\"" . stripslashes(htmlentities($recipient)) . "\">";
echo "<input type=hidden name=\"semail\" value=\"$semail\">";
echo "<input type=hidden name=\"sender\" value=\"" . stripslashes(htmlentities($sender)) . "\">";
echo "<input type=hidden name=\"message\" value=\"" . stripslashes(htmlentities($message)) . "\">";
echo "<input type=hidden name=\"idnumber\" value=\"$idnumber\">";
echo "<input type=hidden name=\"\" value=\"$\">";
echo "<input type=\"submit\" value=\"Submit\"></form><br>";

}

}  		// End of verification for $semail
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"; }

?>