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

/*
	TellAFriend PmWiki Module
	Version: 2.1.3
	This file permits sending a wiki page to multiple recipients via
	PHP's mail function.

	Created by Jeffrey W Barke (jbarke@milwaukeedept.org)
	Based on the TellAFriend PmWiki Module
		$Id: TellAFriend.php,v 2.0 2004/12/08 15:12:10 pts00065 Exp $
		Created by bram brambring - http://www.brambring.nl

	Copyright 2005
	This file may be redistributed and/or modified under the
	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.

	Tested with PmWiki 2.1.12

	Copy this file into the 'cookbook/' subdirectory.
	Make sure the variables $MailPostsFrom and $MailPostsTo are set in your local.php
       (these are the same as used by mailposts).
	Change the parameters below to something you like.
	Then type the following line in the local/config.php file:
		include_once("$FarmD/cookbook/tellafriend.php");

	Log: TellAFriend.php

	Revision 2.1.3 20060816
	Show "from" as the email address of the sending user, rather than as
	the originating wiki.  Change text of $MailFormNoHTML.

	Revision 2.0 20050521
	Cleaned up HTML, improved responses to user, improved error handling,
	improved emailing to multiple recipients, made script more efficient.

	...

	Revision 1.0 20041208
	Updated TellAFriend.php to work with PmWiki 2.0

	$Log: TellAFriend.php, v $

	Revision 1.4  2004/02/12 08:11:04  pts00065
	MailFormThankYou global

	Revision 1.3  2004/02/12 07:41:44  pts00065
	beta9

	Revision 1.2  2004/02/12 07:15:06  pts00065
	-

	Revision 1.1  2004/02/11 16:49:07  pts00065
	email form

*/

$MailFormTitle = 'Email This Page';
$MailFormThankYou = '<br /><p>Thank you!&nbsp; The page has successfully been sent.';
$MailFormSendTo = 'Send this page by email to:';
$MailFormSendFrom = 'Your email address:';
$MailFormText = 'Enter a message:';
$MailFormSubject = $WikiTitle . ' - ' . $pagename;
$MailFormDefaultText = '';
$MailFromErrorEmail = 'It appears as if your email address is invalid.&nbsp; Please
	check the spelling of your email address and try again.';
$MailFormNoHTML = 'If your email doesn\'t support HTML,&nbsp; please visit the page: ';
$MailFromDisclaimer = 'This page has been sent from a mailform on <a href="' .
	$ScriptUrl . '">' . $WikiTitle . '</a>.';

SDV($HandleActions['mailform'], 'HandleMailForm');

SDV($HandleMailFmt, array(&$PageStartFmt,
  &$PageMailFormFmt, '$PageText',
  &$PageEndFmt));

SDV($PageMailFormFmt, '
  <a id="top" name="top"></a><h1 class="wikiaction">' . $MailFormTitle . '</h1></a>
  <form action="$PageUrl" method="post">
  	<input type="hidden" name="pagename" value="$PageName" />
  	<input type="hidden" name="action" value="mailform" />' .
  	$MailFormSendTo . '<br /><input type="text" name="sendto" size="50" value="' .
		$_POST['sendto'] . '" /><br />' .
  	$MailFormSendFrom . '<br /><input type="text" name="sendfrom" size="50" value="' .
		$_POST['sendfrom'] . '" /><br />Subject:<br /><input type="text" name="sendsubject" size="50" value="'.$WikiTitle.' - '.$pagename.'"/><br />Enter your message here.<br/><textarea cols="50" rows="5" name="mailtext" size="50">' .
		$_POST['mailtext'] . '</textarea><br />
  	Send a copy to yourself? <input type="checkbox" name="chkSendSelf" /><br /><br />
  	<input type="submit" name="post" value="Send" />
  	<input type="reset" value="Reset" />
  </form>');

function HandleMailForm($pagename) {

	global $HandleMailFmt, $HandleActions, $FmtV, $MailFormTitle;

  	$page = RetrieveAuthPage($pagename,"read");
  	if (!$page) { Abort("?cannot read $pagename"); }
  	PCache($pagename, $page);
  	if (isset($page['text'])) $text=$page['text'];
  	else $text = FmtPageName($DefaultPageTextFmt,$pagename);
  	$text = '(:groupheader:)'.@$text.'(:groupfooter:)';
  	$FmtV['$PageText'] = MarkupToHTML($pagename,$text);
  	if (@$_POST['post'])
    	{ HandlePostMailForm($pagename); }
  	PrintFmt($pagename,$HandleMailFmt);

}

function HandlePostMailForm($pagename) {

	global $PageMailFormFmt, $MailFormSubject, $MailFromErrorEmail, $WikiTitle,
		$MailPostsFrom, $MailPostsTo, $MailFormNoHTML, $MailFormThankYou, $FmtV;

	$sendfrom = @$_POST['sendfrom'];
	$mailtext = @$_POST['mailtext'];

	// Validate the send from address
	if (!ereg("^([a-zA-Z0-9_-]|\\-|\\.)+@(([a-zA-Z0-9]|\\-)+\\.)+[a-z]{2,4}\$", $sendfrom)) {
  		$error_html .= $MailFromErrorEmail . '<br /><br/ >';
		$PageMailFormFmt = $error_html . $PageMailFormFmt;
		return;
	}

	// Replace all semi-colons with commas, explode the sendto parameter on commas,
	// validate each piece, and mail.
	$sendto = $_POST['sendto'];
	$sendto = str_replace(';', ',', $sendto);
	$sendto = explode(',', $sendto);

	$pagelink = FmtPageName('$PageUrl', $pagename);
	$PageMailFormFmt = $MailFormThankYou;

	// Message
	$mailtext = htmlspecialchars($mailtext, ENT_NOQUOTES);
	$message = nl2br(stripslashes($mailtext)) . "<br /><br />" ;
	$message .= $MailFormNoHTML . '<a href="' . $pagelink . '">' . $pagelink .
		'</a><br /><br /><hr /><br /><br />';
	$message .= $FmtV['$PageText'];
	$message .="<hr />$MailFromDisclaimer";

	// Subject
	$subject = $_POST['sendsubject'];
	$headers  = "MIME-Version: 1.0\n";
	$headers .= "Content-type: text/html; charset=iso-8859-1\n";
	// Additional headers
//	$headers .= "From: $WikiTitle <$sendfrom>\n";
	$headers .= "From: <$sendfrom>\n";
	$headers .= "Bcc: $MailPostsTo\n";

	while (list($key, $val) = each($sendto)) {

		$val = trim($val);
		if (!ereg("^([a-zA-Z0-9_-]|\\-|\\.)+@(([a-zA-Z0-9]|\\-)+\\.)+[a-z]{2,4}\$", $val)) {
  			$error = 1;
  			$error_html .= $val . ' appears to be invalid.&nbsp; This page was
				not sent to this email address.<br />';
		} else {
			// Recipients
			$to = $val;
			// Mail it
			mail($to, $subject, $message, $headers);
		}
	}

	if ($error == 1) {
		$PageMailFormFmt .= '<br />' . $error_html;
	}

	if ($_POST['chkSendSelf'] == true) {
		mail($sendfrom, $subject, $message, $headers);
	}

	$PageMailFormFmt .= '</p><br /><hr /><br />';

}

?>