Andy, have a read at this. I hope there are no typos as this was written in a major hurry.
Let me know how you get on.
Mike.
When I tried to get PGP + PHP working a couple of days ago I searched through lots of tutorials that were way!! above my head. I shall try to explain in detail what I did and how. These details may seem very simple but if I had access to them a few days ago I would have been a happy man.
1) Contacted my host and asked about PGP paths and the paths to my webspace.
PGP as entered: /usr/local/bin/pgp
Webspace as entered: /home/public_html
2) Downloaded a copy of freeware PGP from www.pgpi.org to suit my OS.
3) Installed & setup PGP (ver 7.03) on my home PC and found my keyring file which is called whateveryoucallit.pkr
My host id running PGP 5.0i.
4) set up a directory called .pgp in the root of my webspace:
/home/public_html/.pgp and used CUTEFTP to chmod it to 777
5) Uploaded whateveryoucallit.pkr to the .pgp directory and used CUTEFTP to chmod it to 644.
6) Changed the necessary details of the code below (from Julie Meloni) to make it work on my system, surrounded by either side !remove ** before uploading.
If the steps detailed above seem too simple then it was not meant for you, just others like me!!
With these settings the script seems to work and decrypt ok, but it is a work in progress and I have to research it a whole lot more before I put peoples card details through it. There is possibly a problem where if the server should fail mid encryption the original unencrypted message might be left on the server, eg. if the unlink commands were not executed..
I would like to refine this to the point where no temporary files are created at all. But this is beyond me at the moment.
All suggestions gracefully accepted.
code for sendsecret.php
<?
//build the message string
$msg = "Sender's Full Name:\t$sender_name\n";
$msg .= "Sender's E-Mail:\t$sender_email\n";
$msg .= "Secret Message?\t$secret_msg\n\n";
//set the environment variable for PGPPATH
putenv("PGPPATH=/home/public_html/.pgp");
//generate token for unique filenames
$tmpToken = md5(uniqid(rand()));
//create vars to hold paths and filenames
$plainTxt = "/home/public_html/.pgp" . "$tmpToken" . "data";
$crypted = "/home/public_html/.pgp" . "$tmpToken" . "pgpdata";
//open file and dump in plaintext contents
$fp = fopen($plainTxt, "w+");
fputs($fp, $msg);
fclose($fp);
//invoke PGP to encrypt file contents
// Your Name & Email Address must be exactly
// as your DH/DSS key pair in PGPKeys
system("/usr/local/bin/pgpe -r 'Your Name <your@emailaddress.com>' -o $crypted -a $plainTxt");
//open file and read encrypted contents into var
$fd = fopen($crypted, "r");
$mail_cont = fread($fd, filesize($crypted));
fclose($fd);
//delete files!
unlink($plainTxt);
unlink($crypted);
// Build mail message and send it to target recipient.
$recipient = "your email address";
$subject = "Secret Message";
$mailheaders = "From: My Web Site\n";
$mailheaders .= "Reply-To: $sender_email\n\n";
mail("$recipient", "$subject", $mail_cont, $mailheaders);
// Print confirmation to screen.
echo "
<H1 align=center>Thank You, $sender_name</h1>
<p align=center>Your secret message has been sent.</p>
";
?>
Code for mail.php
<HEAD>
<TITLE>Secret Form</TITLE>
</HEAD>
<BODY>
<h1>Form to Send Secret Stuff</h1>
<FORM method="POST" action="sendsecret.php">
<p>Your Name:<br>
<INPUT type="text" name="sender_name" size=25></p>
<p>Your E-Mail Address:<br>
<INPUT type="text" name="sender_email" size=25></p>
<p>The Secret Message:<br>
<TEXTAREA name="secret_msg" cols=35 rows=5></TEXTAREA></p>
<p><INPUT type="submit" value="Send Secret Message"></p>
</FORM>
</BODY>