<?php if (!defined('PmWiki')) exit();

/*
  PmWiki module to generate a that allows you to send email to
  a person specified in the Wiki.
  Copyright (C) 2004  Nils Knappmeier <nk@knappi.org>
  This is GPL, only it seems odd to include a license in a programm
  that has twice the size of the license.

  Modifications to work with PmWiki 2.0, 
    Copyright (c) 2005 by Patrick R. Michaud <pmichaud@pobox.com>
	
	Modifications to work with the MyWikiFriends.php cookbook module,
	anti-copyright 2005 by Jeffrey W Barke <jbarke@milwaukeedept.org>
	
	Revision 1.1	20050802	Jeffrey W Barke
	Changed all variables and functions to "e"mailform.
	
	Revision 1.2    20061215 Sandy Schoen
	Added Confirmation Code. Bit of a kludge, but it works.
	Relies on the server to check the access code.
	You'll probably want to get into the code to format it for your site.
	
*/

# Version date
SDV($RecipeInfo['MailForm']['Version'], '2007-02-15');

XLSDV('en', array(
  'MF' => '',
  'MFsuccess' => 'Message has been sent successfully.',
  'MFfailure' => 'Message could not be sent.',
  'MFerror' => 'An error has occurred.',
  'MFwrongcode' => 'Wrong confirmation code. Are you sure you are human?'));

$r = @$_REQUEST['emailform'];
$EMailFormResult = FmtPageName("$[MF$r]", $pagename);
$ACodeCalc = rand(100,999);

# This is the default form, as a table.  The '$1' is replaced with
# the word that comes after the mailform: markup -- the rest are
# straightforward $...Fmt substitutions.
SDV($EMailFormFmt,"<form action='\$PageUrl'>
  <input type='hidden' name='pagename' value='\$FullName' />
  <input type='hidden' name='address' value='\$1' />
  <input type='hidden' name='action' value='emailform' />
  <input type='hidden' name='ACodeReturn' value='\$ACodeCalc' />
  	<table width='95%'>
    	<tr>
			<td colspan='2'><span class='message'>\$EMailFormResult</span></td>
		</tr>
    	<tr>
			<td width='25%' class='inputbox commentauthorbox'>$[Your Address:]</td>
      		<td><input type='text' size='20' name='sender' value=''/></td>
      	</tr>
    	<tr>
			<td>$[Subject:]</td>
      		<td><input class='inputbox commentauthorbox' type='text' size='20' value='' name='subject'/></td>
		</tr>
    	<tr>
			<td>$[Message:]</td>
      		<td><textarea class='inputtext commenttext' name='text' rows='10' cols='40'></textarea></td>
		</tr>
		<tr>
			<td>Enter code: <span style='color:red;font-weight:bold;'>$ACodeCalc</span></td>
			<td><input type='text' size='4' maxlength='3' name='ACodeEntered' value='' />
				&nbsp;
				<input type='submit' name='send' value='$[Send]'></td>
		</tr>
	</table></form>");

# This defines the mailform: markup -- it's just a straight text
# substitution.  
Markup('emailform', '>links', 
  '/\\bmailform:(\\w+)/',
  FmtPageName($EMailFormFmt, $pagename));

# These define what happens after someone has submitted a message.
# The variables are the header and footer for the email message,
# while HandleMailForm sends the message according to the
# value of 'address' in the request.
SDV($EMailFormHeader,"");
SDV($EMailFormFooter,
   "\n-------------------------------------------\n"
  ."This message was sent by the PmWiki MailForm at $ScriptUrl\n");
SDV($EMailFormDefaultSender,"");

$HandleActions['emailform'] = 'HandleEMailForm';

function HandleEMailForm($pagename) {

	global $EMailFormAddresses, $EMailFormHeader, $EMailFormFooter,
    	$EMailFormDefaultSender;

  	$to = $EMailFormAddresses[$_REQUEST['address']];
  	$from = $_REQUEST['sender'];
  	$subject = $_REQUEST['subject'];
  	$text = $EMailFormHeader.stripmagic($_REQUEST['text']).$EMailFormFooter;



  	if (!$from) $from = $EMailFormDefaultSender;
  	if (!$to || !$_REQUEST['text']
  		|| $_REQUEST['ACodeReturn'] != $_REQUEST['ACodeEntered']
   		) $msg = 'error';
  	else if (mail($to, $subject, $text, "From: $from")) $msg = 'success';
  	else $msg = 'failure';

 	header("Location: $ScriptUrl?pagename=$pagename&emailform=$msg");}