ProtectedContactPage/SpecialContact.php

344 lines
10 KiB
PHP
Raw Normal View History

2007-02-12 14:50:29 -07:00
<?php
/**
* Speclial:Contact, a contact form for visitors.
* Based on SpecialEmailUser.php
*
2007-02-12 14:50:29 -07:00
* @addtogroup SpecialPage
* @author Daniel Kinzler, brightbyte.de
* @copyright © 2007 Daniel Kinzler
* @licence GNU General Public Licence 2.0 or later
*/
2007-02-13 15:33:42 -07:00
if( !defined( 'MEDIAWIKI' ) ) {
echo( "not a valid entry point.\n" );
die( 1 );
}
global $IP; #needed when called from the autoloader
require_once("$IP/includes/UserMailer.php");
2007-02-12 14:50:29 -07:00
/**
*
*/
class SpecialContact extends SpecialPage {
2007-02-12 14:50:29 -07:00
/**
* Constructor
*/
function __construct() {
global $wgOut;
SpecialPage::SpecialPage( 'Contact', '', true );
}
2007-02-12 14:50:29 -07:00
/**
* Main execution function
* @param $par Parameters passed to the page
*/
function execute( $par ) {
global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgContactUser;
wfLoadExtensionMessages( 'ContactPage' );
2007-04-18 06:41:35 -06:00
$fname = "SpecialContact::execute";
if( !$wgEnableEmail || !$wgContactUser ) {
2007-02-12 14:50:29 -07:00
$wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
return;
}
2007-02-12 14:50:29 -07:00
$action = $wgRequest->getVal( 'action' );
2007-02-12 14:50:29 -07:00
$nu = User::newFromName( $wgContactUser );
if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
wfDebug( "Target is invalid user or can't receive.\n" );
$wgOut->showErrorPage( "noemailtitle", "noemailtext" );
return;
}
2007-02-12 14:50:29 -07:00
$f = new EmailContactForm( $nu );
2007-02-12 14:50:29 -07:00
if ( "success" == $action ) {
2007-04-18 06:41:35 -06:00
wfDebug( "$fname: success.\n" );
2007-02-12 14:50:29 -07:00
$f->showSuccess( );
} else if ( "submit" == $action && $wgRequest->wasPosted() && $f->hasAllInfo() ) {
2007-04-18 06:41:35 -06:00
$token = $wgRequest->getVal( 'wpEditToken' );
if( $wgUser->isAnon() ) {
# Anonymous users may not have a session
# open. Check for suffix anyway.
$tokenOk = ( EDIT_TOKEN_SUFFIX == $token );
} else {
$tokenOk = $wgUser->matchEditToken( $token );
}
2007-10-13 05:41:44 -06:00
if ( !$tokenOk ) {
2007-04-18 06:41:35 -06:00
wfDebug( "$fname: bad token (".($wgUser->isAnon()?'anon':'user')."): $token\n" );
$wgOut->addWikiText( wfMsg( 'sessionfailure' ) );
$f->showForm();
2007-10-13 05:41:44 -06:00
} else if ( !$f->passCaptcha() ) {
wfDebug( "$fname: captcha failed" );
$wgOut->addWikiText( wfMsg( 'contactpage-captcha-failed' ) ); //TODO: provide a message for this!
$f->showForm();
} else {
wfDebug( "$fname: submit\n" );
$f->doSubmit();
2007-04-18 06:41:35 -06:00
}
2007-02-12 14:50:29 -07:00
} else {
2007-04-18 06:41:35 -06:00
wfDebug( "$fname: form\n" );
2007-02-12 14:50:29 -07:00
$f->showForm();
}
}
}
/**
* @todo document
* @addtogroup SpecialPage
*/
class EmailContactForm {
var $target;
var $text, $subject;
var $cc_me; // Whether user requested to be sent a separate copy of their email.
/**
* @param User $target
*/
function EmailContactForm( $target ) {
global $wgRequest, $wgUser;
2008-07-06 08:23:16 -06:00
global $wgCaptchaClass;
2007-10-13 05:41:44 -06:00
2007-02-12 14:50:29 -07:00
$this->target = $target;
$this->text = $wgRequest->getText( 'wpText' );
$this->subject = $wgRequest->getText( 'wpSubject' );
$this->cc_me = $wgRequest->getBool( 'wpCCMe' );
$this->fromname = $wgRequest->getText( 'wpFromName' );
$this->fromaddress = $wgRequest->getText( 'wpFromAddress' );
if ($wgUser->isLoggedIn()) {
if (!$this->fromname) $this->fromname = $wgUser->getName();
if (!$this->fromaddress) $this->fromaddress = $wgUser->getEmail();
}
2007-10-13 05:41:44 -06:00
//prepare captcha if applicable
2008-07-06 08:23:16 -06:00
if ( $this->useCaptcha() ) {
$captcha = ConfirmEditHooks::getInstance();
$captcha->trigger = 'contactpage';
$captcha->action = 'contact';
2007-10-13 05:41:44 -06:00
}
2007-02-12 14:50:29 -07:00
}
function hasAllInfo() {
global $wgContactRequireAll;
if ( $this->text === NULL ) return false;
else $this->text = trim( $this->text );
if ( $this->text === '' ) return false;
if ( $wgContactRequireAll ) {
if ( $this->fromname === NULL ) return false;
else $this->fromname = trim( $this->fromname );
if ( $this->fromname === '' ) return false;
if ( $this->fromaddress === NULL ) return false;
else $this->fromaddress = trim( $this->fromaddress );
if ( $this->fromaddress === '' ) return false;
}
return true;
}
2007-02-12 14:50:29 -07:00
function showForm() {
global $wgOut, $wgUser, $wgContactRequireAll;
2007-02-12 14:50:29 -07:00
#TODO: show captcha
$wgOut->setPagetitle( wfMsg( "contactpage-title" ) );
$wgOut->addWikiText( wfMsg( "contactpage-pagetext" ) );
2007-02-12 14:50:29 -07:00
if ( $this->subject === "" ) {
2007-02-16 15:20:14 -07:00
$this->subject = wfMsgForContent( "contactpage-defsubject" );
2007-02-12 14:50:29 -07:00
}
$msgSuffix = $wgContactRequireAll ? '-required' : '';
2007-02-12 14:50:29 -07:00
$emt = wfMsg( "emailto" );
$rcpt = $this->target->getName();
$emr = wfMsg( "emailsubject" );
$emm = wfMsg( "emailmessage" );
$ems = wfMsg( "emailsend" );
$emc = wfMsg( "emailccme" );
$emfn = wfMsg( "contactpage-fromname$msgSuffix" );
$emfa = wfMsg( "contactpage-fromaddress$msgSuffix" );
2007-02-12 14:50:29 -07:00
$encSubject = htmlspecialchars( $this->subject );
$encFromName = htmlspecialchars( $this->fromname );
$encFromAddress = htmlspecialchars( $this->fromaddress );
$titleObj = SpecialPage::getTitleFor( "Contact" );
$action = $titleObj->escapeLocalURL( "action=submit" );
2007-04-18 06:41:35 -06:00
$token = $wgUser->isAnon() ? EDIT_TOKEN_SUFFIX : $wgUser->editToken(); //this kind of sucks, really...
$token = htmlspecialchars( $token );
2007-02-12 14:50:29 -07:00
$wgOut->addHTML( "
<form id=\"emailuser\" method=\"post\" action=\"{$action}\">
<table border='0' id='mailheader'>
<tr>
<td align='right'>{$emr}:</td>
<td align='left'>
<input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
</td>
</tr><tr>
<td align='right'>{$emfn}:</td>
<td align='left'>
<input type='text' size='60' maxlength='200' name=\"wpFromName\" value=\"{$encFromName}\" />
</td>
<tr>
<td align='right'>{$emfa}:</td>
<td align='left'>
<input type='text' size='60' maxlength='200' name=\"wpFromAddress\" value=\"{$encFromAddress}\" />
</td>
</tr>
<tr>
<td></td>
<td align='left'>
<small>".wfMsg( "contactpage-formfootnotes$msgSuffix" )."</small>
2007-02-12 14:50:29 -07:00
</td>
</tr>
</table>
<span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
<textarea name=\"wpText\" rows='20' cols='80' wrap='virtual' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
"</textarea>
" . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
2007-10-13 05:41:44 -06:00
" . $this->getCaptcha() . "
2007-02-12 14:50:29 -07:00
<input type='submit' name=\"wpSend\" value=\"{$ems}\" />
<input type='hidden' name='wpEditToken' value=\"$token\" />
</form>\n" );
}
2008-07-06 08:23:16 -06:00
function useCaptcha() {
global $wgCaptchaClass, $wgCaptchaTriggers, $wgUser;
if ( !$wgCaptchaClass ) return false; //no captcha installed
if ( !@$wgCaptchaTriggers['contactpage'] ) return false; //don't trigger on contact form
2007-10-13 05:41:44 -06:00
if( $wgUser->isAllowed( 'skipcaptcha' ) ) {
2008-07-06 08:23:16 -06:00
wfDebug( "EmailContactForm::useCaptcha: user group allows skipping captcha\n" );
return false;
}
2008-07-06 08:23:16 -06:00
return true;
}
function getCaptcha() {
global $wgCaptcha;
if ( !$this->useCaptcha() ) return "";
wfSetupSession(); #NOTE: make sure we have a session. May be required for captchas to work.
2007-10-13 05:41:44 -06:00
return "<div class='captcha'>" .
$wgCaptcha->getForm() .
wfMsgWikiHtml( 'contactpage-captcha' ) .
"</div>\n";
}
function passCaptcha() {
2008-07-06 08:23:16 -06:00
global $wgCaptcha;
if ( !$this->useCaptcha() ) return true;
2007-10-13 05:41:44 -06:00
return $wgCaptcha->passCaptcha();
}
2007-02-12 14:50:29 -07:00
function doSubmit( ) {
global $wgOut;
global $wgEnableEmail, $wgUserEmailUseReplyTo, $wgEmergencyContact;
global $wgContactUser, $wgContactSender, $wgContactSenderName;
2007-02-12 14:50:29 -07:00
$csender = $wgContactSender ? $wgContactSender : $wgEmergencyContact;
$cname = $wgContactSenderName;
2007-02-12 14:50:29 -07:00
$fname = 'EmailContactForm::doSubmit';
wfDebug( "$fname: start\n" );
$to = new MailAddress( $this->target );
$replyto = NULL;
if ( !$this->fromaddress ) {
$from = new MailAddress( $csender, $cname );
}
else if ( $wgUserEmailUseReplyTo ) {
$from = new MailAddress( $csender, $cname );
$replyto = new MailAddress( $this->fromaddress, $this->fromname );
}
else {
$from = new MailAddress( $this->fromaddress, $this->fromname );
}
2007-02-16 15:20:14 -07:00
$subject = trim( $this->subject );
if ( $subject === "" ) {
$subject = wfMsgForContent( "contactpage-defsubject" );
}
if ( $this->fromname !== "" ) {
$subject = wfMsgForContent( "contactpage-subject-and-sender", $subject, $this->fromname );
}
else if ( $this->fromaddress !== "" ) {
$subject = wfMsgForContent( "contactpage-subject-and-sender", $subject, $this->fromaddress );
}
2007-02-12 14:50:29 -07:00
if( wfRunHooks( 'ContactForm', array( &$to, &$replyto, &$subject, &$this->text ) ) ) {
wfDebug( "$fname: sending mail from ".$from->toString()." to ".$to->toString()." replyto ".($replyto==null?'-/-':$replyto->toString())."\n" );
#HACK: in MW 1.9, replyto must be a string, in MW 1.10 it must be an object!
$ver = preg_replace( '![^\d._+]!', '', $GLOBALS['wgVersion'] );
$replyaddr = $replyto == null
? NULL : version_compare( $ver, '1.10', '<' )
? $replyto->toString() : $replyto;
$mailResult = userMailer( $to, $from, $subject, $this->text, $replyaddr );
2007-02-12 14:50:29 -07:00
if( WikiError::isError( $mailResult ) ) {
$wgOut->addWikiText( wfMsg( "usermailererror" ) . $mailResult->getMessage());
2007-02-12 14:50:29 -07:00
} else {
2007-02-12 14:50:29 -07:00
// if the user requested a copy of this mail, do this now,
// unless they are emailing themselves, in which case one copy of the message is sufficient.
if ($this->cc_me && $replyto) {
$cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
if( wfRunHooks( 'ContactForm', array( &$from, &$replyto, &$cc_subject, &$this->text ) ) ) {
wfDebug( "$fname: sending cc mail from ".$from->toString()." to ".$replyto->toString()."\n" );
$ccResult = userMailer( $replyto, $from, $cc_subject, $this->text );
2007-02-12 14:50:29 -07:00
if( WikiError::isError( $ccResult ) ) {
// At this stage, the user's CC mail has failed, but their
2007-02-12 14:50:29 -07:00
// original mail has succeeded. It's unlikely, but still, what to do?
// We can either show them an error, or we can say everything was fine,
// or we can say we sort of failed AND sort of succeeded. Of these options,
2007-02-12 14:50:29 -07:00
// simply saying there was an error is probably best.
$wgOut->addWikiText( wfMsg( "usermailererror" ) . $ccResult);
2007-02-12 14:50:29 -07:00
return;
}
}
}
2007-02-12 14:50:29 -07:00
wfDebug( "$fname: success\n" );
$titleObj = SpecialPage::getTitleFor( "Contact" );
$wgOut->redirect( $titleObj->getFullURL( "action=success" ) );
wfRunHooks( 'ContactFromComplete', array( $to, $replyto, $subject, $this->text ) );
}
}
wfDebug( "$fname: end\n" );
}
function showSuccess( ) {
global $wgOut;
$wgOut->setPagetitle( wfMsg( "emailsent" ) );
$wgOut->addWikiText( wfMsg( "emailsenttext" ) );
2007-02-12 14:50:29 -07:00
$wgOut->returnToMain( false );
}
}