PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/securemail-recaptcha/sendattach.php

https://github.com/learnsia/GnuPG-Secure-Form
PHP | 208 lines | 173 code | 16 blank | 19 comment | 10 complexity | 4a4334d9330278d0e63b544113762ca6 MD5 | raw file
  1. <?php
  2. include('recaptcha/recaptchalib.php');
  3. include('config.php');
  4. /* Script put together with code from:
  5. July 22, 2008 by Emanuele Feronato http://www.emanueleferonato.com/2008/07/22/sending-email-with-multiple-attachments-with-php/
  6. and Julie Meloni "Getting Started with GnuPG"
  7. */
  8. /************* CAPTCHA *********************/
  9. # the response from reCAPTCHA
  10. $resp = null;
  11. # the error code from reCAPTCHA, if any
  12. $error = null;
  13. # are we submitting the page?
  14. $resp = recaptcha_check_answer ($privatekey,
  15. $_SERVER["REMOTE_ADDR"],
  16. $_POST["recaptcha_challenge_field"],
  17. $_POST["recaptcha_response_field"]);
  18. if ($resp->is_valid) {
  19. # in a real application, you should send an email, create an account, etc
  20. } else {
  21. # set the error code so that we can display it. You could also use
  22. # die ("reCAPTCHA failed"), but using the error message is
  23. # more user friendly
  24. die("Incorrect Captcha Code entered. Please <a href=\"index.php\">go back</a> and try again." . $t_error);
  25. }
  26. /************ END CAPTCHA **********************/
  27. // Declare Variables
  28. $sender_name = trim(addslashes(htmlentities($_POST['sender_name'])));
  29. $sender_email = trim(addslashes($_POST['sender_email']));
  30. $secret_msg = trim(addslashes(htmlspecialchars($_POST['secret_msg'])));
  31. // Some data validation
  32. if ($sender_name == "" || $sender_email == "" || $secret_msg == "" )
  33. die("All fields must be filled out.");
  34. // Check to be sure we have a valid email address
  35. if (eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+))*$",$sender_email, $regs)) {
  36. } else {
  37. echo "Error: '$sender_email' isn't a valid mail address!\n";
  38. exit();
  39. }
  40. // array with filenames to be sent as attachment
  41. // Temp file name to read in to be encrypted
  42. $file1_tmp = $_FILES['file']['tmp_name'][0];
  43. $file2_tmp = $_FILES['file']['tmp_name'][1];
  44. // Real file names
  45. $file1 = $_FILES['file']['name'][0];
  46. $file2 = $_FILES['file']['name'][1];
  47. // Size
  48. $file1_size = $_FILES['file']['size'][0];
  49. $file2_size = $_FILES['file']['size'][1];
  50. // If the files are over 5MB then error
  51. if ($file1_size > $FILE_SIZE_LIMIT)
  52. die("<b>$file1</b> is too large. Files must be less than 5MB");
  53. if ($file2_size > $FILE_SIZE_LIMIT)
  54. die("<b>$file2</b> is too large. Files must be less than 5MB");
  55. // Clean up file names
  56. $file1 = preg_replace("/[^[[:alnum:]]]/", "_", $file1);
  57. $file2 = preg_replace("/[^[[:alnum:]]]/", "_", $file2);
  58. // Put the files into an array to be processed for encryption
  59. $the_files = array($file1, $file2);
  60. $the_files_tmp = array($file1_tmp, $file2_tmp);
  61. //set the environment variable for PGPPATH
  62. putenv("GNUPGHOME=".$GNUPG_HOME);
  63. // email fields: to, from, subject, and so on
  64. $to = $SEND_TO;
  65. $from = "$sender_email";
  66. $subject ="Secure email from $sender_email";
  67. $message = "$secret_msg";
  68. $headers = "From: $from";
  69. // boundary
  70. $semi_rand = md5(time());
  71. $mime_boundary = "------------{$semi_rand}x";
  72. // headers for attachment
  73. $headers .= "\nMIME-Version: 1.0\n" . "X-Enigmail-Version: 1.1.1\n" . "Content-Type: multipart/mixed;" . " boundary={$mime_boundary}";
  74. /*********************************************
  75. BEGIN MESSAGE ENCRYPTION
  76. *********************************************/
  77. // escape command arguments
  78. $GNUPG = escapeshellcmd($GNUPG);
  79. //create vars to hold paths and filenames
  80. $plainTxt = $TEMP_DIR. "$random_hash" . "data";
  81. $crypted = $TEMP_DIR. "$random_hash" . "pgpdata";
  82. //open file and dump in plaintext contents
  83. $fp = fopen($plainTxt, "w+");
  84. fputs($fp, $message);
  85. fclose($fp);
  86. //invoke PGP to encrypt file contents
  87. system("$GNUPG --encrypt -ao $crypted -r $YOUR_KEY $plainTxt");
  88. //open file and read encrypted contents into var
  89. $fd = fopen($crypted, "r");
  90. $message = fread($fd, filesize($crypted));
  91. fclose($fd);
  92. //delete files!
  93. unlink($plainTxt);
  94. unlink($crypted);
  95. /*********************************************
  96. END MESSAGE ENCRYPTION
  97. *********************************************/
  98. // multipart boundary
  99. $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
  100. $message .= "--{$mime_boundary}\n";
  101. // preparing attachments
  102. for($x=0;$x<count($the_files);$x++){
  103. $the_file = $the_files[$x];
  104. if ($the_file == "") {
  105. } else {
  106. // Read file contents into variable
  107. $file = file_get_contents($the_files_tmp[$x]);
  108. /*********************************************
  109. BEGIN FILE ENCRYPTION
  110. *********************************************/
  111. //create vars to hold paths and filenames
  112. $plainTxt = $TEMP_DIR. "$random_hash" . "data";
  113. $crypted = $TEMP_DIR. "$random_hash" . "pgpdata";
  114. //open file and dump in plaintext contents
  115. $fp = fopen($plainTxt, "w+");
  116. fputs($fp, $file);
  117. fclose($fp);
  118. //invoke PGP to encrypt file contents
  119. system("$GNUPG --encrypt -ao $crypted -r $YOUR_KEY $plainTxt");
  120. //open file and read encrypted contents into var
  121. $fd = fopen($crypted, "r");
  122. $data = fread($fd, filesize($crypted));
  123. fclose($fd);
  124. //delete files!
  125. unlink($plainTxt);
  126. unlink($crypted);
  127. $the_files[$x] = preg_replace("/[^a-zA-Z0-9\._-]/", "_", $the_files[$x]);
  128. $data = chunk_split(base64_encode($data));
  129. $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$the_files[$x].gpg\"\n" .
  130. "Content-Disposition: attachment;\n" . " filename=\"$the_files[$x].gpg\"\n" .
  131. "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
  132. $message .= "--{$mime_boundary}\n";
  133. /*********************************************
  134. END FILE ENCRYPTION
  135. *********************************************/
  136. }
  137. } //end for loop
  138. // send
  139. $ok = @mail($to, $subject, $message, $headers);
  140. if ($ok) {
  141. echo "<p>mail sent to $to!</p>";
  142. } else {
  143. echo "<p>mail could not be sent!</p>";
  144. }
  145. ?>