ProtectedContactPage/SpecialContact.php

404 lines
12 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
*
* @file
* @ingroup SpecialPage
2007-02-12 14:50:29 -07:00
* @author Daniel Kinzler, brightbyte.de
* @copyright © 2007 Daniel Kinzler
* @license GNU General Public Licence 2.0 or later
2007-02-12 14:50:29 -07:00
*/
2007-02-13 15:33:42 -07:00
if( !defined( 'MEDIAWIKI' ) ) {
echo( "not a valid entry point.\n" );
die( 1 );
}
2007-02-12 14:50:29 -07:00
/**
* Provides the contact form
* @ingroup SpecialPage
2007-02-12 14:50:29 -07:00
*/
class SpecialContact extends SpecialPage {
2007-02-12 14:50:29 -07:00
/**
* Constructor
*/
public function __construct() {
parent::__construct( 'Contact' );
2007-02-12 14:50:29 -07:00
}
2007-02-12 14:50:29 -07:00
/**
* Main execution function
*
* @param $par Mixed: Parameters passed to the page
2007-02-12 14:50:29 -07:00
*/
public function execute( $par ) {
global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgContactUser;
wfLoadExtensionMessages( 'ContactPage' );
if( !$wgEnableEmail || !$wgContactUser ) {
$wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
2007-02-12 14:50:29 -07:00
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' );
2007-02-12 14:50:29 -07:00
return;
}
2007-02-12 14:50:29 -07:00
$f = new EmailContactForm( $nu );
if ( 'success' == $action ) {
wfDebug( __METHOD__ . ": success.\n" );
$f->showSuccess();
} elseif ( '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 ) {
wfDebug( __METHOD__ . ": bad token (" . ( $wgUser->isAnon() ? 'anon' : 'user' ) . "): $token\n" );
$wgOut->addWikiMsg( 'sessionfailure' );
2007-04-18 06:41:35 -06:00
$f->showForm();
} elseif ( !$f->passCaptcha() ) {
wfDebug( __METHOD__ . ": captcha failed" );
$wgOut->addWikiMsg( 'contactpage-captcha-failed' );
2007-10-13 05:41:44 -06:00
$f->showForm();
} else {
wfDebug( __METHOD__ . ": submit\n" );
2007-10-13 05:41:44 -06:00
$f->doSubmit();
2007-04-18 06:41:35 -06:00
}
2007-02-12 14:50:29 -07:00
} else {
wfDebug( __METHOD__ . ": form\n" );
2007-02-12 14:50:29 -07:00
$f->showForm();
}
}
}
/**
* @todo document
* @ingroup SpecialPage
2007-02-12 14:50:29 -07:00
*/
class EmailContactForm {
var $target;
var $text, $subject;
var $cc_me; // Whether user requested to be sent a separate copy of their email.
2007-02-12 14:50:29 -07:00
/**
* @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->includeIP = $wgRequest->getBool( 'wpIncludeIP' );
2007-02-12 14:50:29 -07:00
$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-02-12 14:50:29 -07:00
}
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, $wgContactIncludeIP;
2007-02-12 14:50:29 -07:00
#TODO: show captcha
$wgOut->setPageTitle( wfMsg( 'contactpage-title' ) );
$wgOut->addWikiMsg( 'contactpage-pagetext' );
2007-02-12 14:50:29 -07:00
if ( $this->subject === '' ) {
$this->subject = wfMsgForContent( 'contactpage-defsubject' );
2007-02-12 14:50:29 -07:00
}
$msgSuffix = $wgContactRequireAll ? '-required' : '';
$titleObj = SpecialPage::getTitleFor( 'Contact' );
$action = $titleObj->getLocalURL( 'action=submit' );
2007-04-18 06:41:35 -06:00
$token = $wgUser->isAnon() ? EDIT_TOKEN_SUFFIX : $wgUser->editToken(); //this kind of sucks, really...
2007-02-12 14:50:29 -07:00
$form =
Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'emailuser' ) ) .
Xml::openElement( 'fieldset' ) .
Xml::element( 'legend', null, wfMsg( 'contactpage-legend' ) ) .
Xml::openElement( 'table', array( 'id' => 'mailheader' ) ) .
'<tr>
<td class="mw-label">' .
Xml::label( wfMsg( 'emailsubject' ), 'wpSubject' ) .
'</td>
<td class="mw-input" id="mw-contactpage-subject">' .
Xml::input( 'wpSubject', 60, $this->subject, array( 'type' => 'text', 'maxlength' => 200 ) ) .
'</td>
</tr>
<tr>
<td class="mw-label">' .
Xml::label( wfMsg( "contactpage-fromname$msgSuffix" ), 'wpFromName' ) .
'</td>
<td class="mw-input" id="mw-contactpage-from">' .
Xml::input( 'wpFromName', 60, $this->fromname, array( 'type' => 'text', 'maxlength' => 200 ) ) .
'</td>
</tr>
<tr>
<td class="mw-label">' .
Xml::label( wfMsg( "contactpage-fromaddress$msgSuffix" ), 'wpFromAddress' ) .
'</td>
<td class="mw-input" id="mw-contactpage-address">' .
Xml::input( 'wpFromAddress', 60, $this->fromaddress, array( 'type' => 'text', 'maxlength' => 200 ) ) .
'</td>
</tr>';
// Allow other extensions to add more fields into Special:Contact
wfRunHooks( 'ContactFormBeforeMessage', array( $this, &$form ) );
$form .= '<tr>
<td></td>
<td class="mw-input" id="mw-contactpage-formfootnote">
<small>' . wfMsg( "contactpage-formfootnotes$msgSuffix" ) . '</small>
</td>
</tr>
<tr>
<td class="mw-label">' .
Xml::label( wfMsg( 'emailmessage' ), 'wpText' ) .
'</td>
<td class="mw-input">' .
Xml::textarea( 'wpText', $this->text, 80, 20, array( 'id' => 'wpText' ) ) .
'</td>
</tr>';
if ( $wgContactIncludeIP && $wgUser->isLoggedIn() ) {
$form .= '<tr>
<td></td>
<td class="mw-input">' .
Xml::checkLabel( wfMsg( 'contactpage-includeip' ), 'wpIncludeIP', 'wpIncludeIP', false ) .
'</td>
</tr>';
}
$form .= '<tr>
<td></td>
<td class="mw-input">' .
Xml::checkLabel( wfMsg( 'emailccme' ), 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) .
'<br />' . $this->getCaptcha() .
'</td>
</tr>
<tr>
<td></td>
<td class="mw-submit">' .
Xml::submitButton( wfMsg( 'emailsend' ), array( 'name' => 'wpSend', 'accesskey' => 's' ) ) .
'</td>
</tr>' .
Xml::hidden( 'wpEditToken', $token ) .
Xml::closeElement( 'table' ) .
Xml::closeElement( 'fieldset' ) .
Xml::closeElement( 'form' );
$wgOut->addHTML( $form );
2007-02-12 14:50:29 -07:00
}
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 '';
}
2008-07-06 08:23:16 -06:00
wfSetupSession(); #NOTE: make sure we have a session. May be required for captchas to work.
return '<div class="captcha">' .
$wgCaptcha->getForm() .
wfMsgWikiHtml( 'contactpage-captcha' ) .
2007-10-13 05:41:44 -06:00
"</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();
}
function doSubmit() {
global $wgOut, $wgUser;
global $wgEnableEmail, $wgUserEmailUseReplyTo, $wgEmergencyContact;
global $wgContactUser, $wgContactSender, $wgContactSenderName, $wgContactIncludeIP;
2007-02-12 14:50:29 -07:00
$csender = $wgContactSender ? $wgContactSender : $wgEmergencyContact;
$cname = $wgContactSenderName;
$senderIP = wfGetIP();
2007-02-12 14:50:29 -07:00
wfDebug( __METHOD__ . ": start\n" );
2007-02-12 14:50:29 -07:00
$targetAddress = new MailAddress( $this->target );
$replyto = null;
$contactSender = new MailAddress( $csender, $cname );
if ( !$this->fromaddress ) {
$submitterAddress = $contactSender;
} else {
$submitterAddress = new MailAddress( $this->fromaddress, $this->fromname );
if ( $wgUserEmailUseReplyTo ) {
$replyto = $submitterAddress;
}
}
2007-02-16 15:20:14 -07:00
$subject = trim( $this->subject );
if ( $subject === '' ) {
$subject = wfMsgForContent( 'contactpage-defsubject' );
2007-02-16 15:20:14 -07:00
}
$includeIP = $wgContactIncludeIP && ( $this->includeIP || $wgUser->isAnon() );
if ( $this->fromname !== '' ) {
if ( $includeIP ) {
$subject = wfMsgForContent( 'contactpage-subject-and-sender-withip', $subject, $this->fromname, $senderIP );
} else {
$subject = wfMsgForContent( 'contactpage-subject-and-sender', $subject, $this->fromname );
}
} elseif ( $this->fromaddress !== '' ) {
if ( $includeIP ) {
$subject = wfMsgForContent( 'contactpage-subject-and-sender-withip', $subject, $this->fromaddress, $senderIP );
} else {
$subject = wfMsgForContent( 'contactpage-subject-and-sender', $subject, $this->fromaddress );
}
} else if ( $includeIP ) {
$subject = wfMsgForContent( 'contactpage-subject-and-sender', $subject, $senderIP );
}
2007-02-12 14:50:29 -07:00
if( !wfRunHooks( 'ContactForm', array( &$targetAddress, &$replyto, &$subject, &$this->text ) ) ) {
wfDebug( __METHOD__ . ": aborted by hook\n" );
return;
}
2007-02-12 14:50:29 -07:00
wfDebug( __METHOD__ . ": sending mail from " . $submitterAddress->toString() .
" to " . $targetAddress->toString().
" replyto " . ( $replyto == null ? '-/-' : $replyto->toString() ) . "\n" );
$mailResult = UserMailer::send( $targetAddress, $submitterAddress, $subject, $this->text, $replyto );
if( WikiError::isError( $mailResult ) ) {
$wgOut->addWikiMsg( 'usermailererror' ) . $mailResult->getMessage();
wfDebug( __METHOD__ . ": got error from UserMailer: " . $mailResult->getMessage() . "\n" );
return;
}
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 && $this->fromaddress ) {
$cc_subject = wfMsg( 'emailccsubject', $this->target->getName(), $subject );
if( wfRunHooks( 'ContactForm', array( &$submitterAddress, &$contactSender, &$cc_subject, &$this->text ) ) ) {
wfDebug( __METHOD__ . ": sending cc mail from " . $contactSender->toString() .
" to " . $submitterAddress->toString() . "\n" );
$ccResult = UserMailer::send( $submitterAddress, $contactSender, $cc_subject, $this->text );
if( WikiError::isError( $ccResult ) ) {
// At this stage, the user's CC mail has failed, but their
// 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,
// simply saying there was an error is probably best.
$wgOut->addWikiText( wfMsg( 'usermailererror' ) . $ccResult );
return;
2007-02-12 14:50:29 -07:00
}
}
}
wfDebug( __METHOD__ . ": success\n" );
$titleObj = SpecialPage::getTitleFor( 'Contact' );
$wgOut->redirect( $titleObj->getFullURL( 'action=success' ) );
wfRunHooks( 'ContactFromComplete', array( $targetAddress, $replyto, $subject, $this->text ) );
wfDebug( __METHOD__ . ": end\n" );
2007-02-12 14:50:29 -07:00
}
function showSuccess() {
2007-02-12 14:50:29 -07:00
global $wgOut;
$wgOut->setPageTitle( wfMsg( 'emailsent' ) );
$wgOut->addWikiMsg( 'emailsenttext' );
2007-02-12 14:50:29 -07:00
$wgOut->returnToMain( false );
}
}