<?

/**
  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.
*/


    /* Set a variable if it does'nt have a value already */
    function set(&$variable, $value) {
       if (!isset($variable)) $variable = $value;
    }
    

    /* Initialize hooks for action and markup */
    $HandleActions['mailform'] = 'HandleMailForm';
    $LinkPatterns[125]["\\bmailform:(\w+)"]
    	= 'CreateMailForm';


    /* Initialize default values */
    set($MailFormMsg['success'],"Message has been sent successfully!");
    set($MailFormMsg['failure'],"Message could not be sent!");
    set($MailFormMsg['error'],"An error occured!");

    set($MailFormString['Your Address'],'Your address:');
    set($MailFormString['Subject'],'Subject:');
    set($MailFormString['Message'],'Message:');
    set($MailFormString['Send'],'Send');

    
    set($MailFormHeader,"");
    set($MailFormFooter,"\n-------------------------------------------\n"
                   ."This message was sent by the PmWiki MailForm at "
                   ."$ScriptUrl");

    set($MailFormDefaultSender,"");






    set($MailFormFmt,
	"<form action='$ScriptUrl'>
	    <input type='hidden' name='pagename' value='$pagename'>
    	    <input type='hidden' name='address' value='\$1'/>
    	    <input type='hidden' name='action' value='mailform'/>
	    <table>
   	      <tr><td colspan='3'>$[ackString]</td></tr>
   	      <tr>
   	        <td>$[Your Address]</td>
    	 	<td><input type='text' size='20' name='sender' value=''/></td>
    	 	<td>
    	 	  <input type='submit' name='send' value='$[Send]'>
    	 	</td>
    	      </tr>
    	      <tr>
    	        <td>$[Subject]</td>
    	        <td colspan=2>
    	          <input type='text' size='20' value='' name='subject'/>
    	        </td>
    	      </tr>
    	      <tr>
    	        <td>$[Message]</td>
    	        <td colspan='2'>
		  <textarea name='text' cols='41' rows='10'></textarea>
    	        </td>
    	      </tr>
    	    </table>
    	    </form>");

 




   function CreateMailForm($pattern, $ref, $txt) {
   	global $MailFormMsg,
   	   $MailFormFmt,$MailFormText, $MailFormString;

   	$MailFormString['ackString'] = $MailFormMsg[$_REQUEST['mailform']];

	$MailForm = $MailFormFmt;
   	foreach ($MailFormString as $key => $value) {
   	    $MailForm = str_replace('$['.$key.']', $value, $MailForm);
   	}

   	$MailForm = preg_replace('/'.$pattern.'/',$MailForm, $ref);
   	return $MailForm;

	

	return $MailFormFmt;

    }
    
               

    function HandleMailForm() {
    	global $pagename, $MailFormAddresses, $MailFormHeader, 
    	 	$MailFormFooter, $MailFormDefaultSender;

    	$to = $MailFormAddresses[$_REQUEST['address']];
    	$from = $_REQUEST['sender'];
    	$subject = $_REQUEST['subject'];
    	$text = $MailFormHeader.$_REQUEST['text'].$MailFormFooter;

	if ($from=="") $from=$MailFormDefaultSender;
	
	if ($to=="") {
	    $msg = 'error';
 	} else if (mail($to, $subject, $text, "From: $from")) {
	    $msg = 'success';
 	} else {
 	    $msg = 'failure';
 	}
 	
 	Header("Location: $ScriptUrl?pagename=$pagename&mailform=$msg");

    }


?>