3 * Implements Special:Emailuser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * A special page that allows users to send e-mails to other users
27 * @ingroup SpecialPage
29 class SpecialEmailUser
extends UnlistedSpecialPage
{
32 public function __construct() {
33 parent
::__construct( 'Emailuser' );
36 public function getDescription() {
37 $target = self
::getTarget( $this->mTarget
);
38 if( !$target instanceof User
) {
39 return $this->msg( 'emailuser-title-notarget' )->text();
42 return $this->msg( 'emailuser-title-target', $target->getName() )->text();
45 protected function getFormFields() {
50 'default' => Linker
::link(
51 $this->getUser()->getUserPage(),
52 htmlspecialchars( $this->getUser()->getName() )
54 'label-message' => 'emailfrom',
55 'id' => 'mw-emailuser-sender',
60 'default' => Linker
::link(
61 $this->mTargetObj
->getUserPage(),
62 htmlspecialchars( $this->mTargetObj
->getName() )
64 'label-message' => 'emailto',
65 'id' => 'mw-emailuser-recipient',
69 'default' => $this->mTargetObj
->getName(),
73 'default' => $this->msg( 'defemailsubject',
74 $this->getUser()->getName() )->inContentLanguage()->text(),
75 'label-message' => 'emailsubject',
84 'label-message' => 'emailmessage',
89 'label-message' => 'emailccme',
90 'default' => $this->getUser()->getBoolOption( 'ccmeonemails' ),
95 public function execute( $par ) {
96 $out = $this->getOutput();
97 $out->addModuleStyles( 'mediawiki.special' );
99 $this->mTarget
= is_null( $par )
100 ?
$this->getRequest()->getVal( 'wpTarget', $this->getRequest()->getVal( 'target', '' ) )
103 // This needs to be below assignment of $this->mTarget because
104 // getDescription() needs it to determine the correct page title.
106 $this->outputHeader();
108 // error out if sending user cannot do this
109 $error = self
::getPermissionsError( $this->getUser(), $this->getRequest()->getVal( 'wpEditToken' ) );
115 throw new PermissionsError( 'sendemail' );
116 case 'blockedemailuser':
117 throw new UserBlockedError( $this->getUser()->mBlock
);
118 case 'actionthrottledtext':
119 throw new ThrottledError
;
121 case 'usermaildisabled':
122 throw new ErrorPageError( $error, "{$error}text" );
125 list( $title, $msg, $params ) = $error;
126 throw new ErrorPageError( $title, $msg, $params );
128 // Got a valid target user name? Else ask for one.
129 $ret = self
::getTarget( $this->mTarget
);
130 if( !$ret instanceof User
) {
131 if( $this->mTarget
!= '' ) {
132 $ret = ( $ret == 'notarget' ) ?
'emailnotarget' : ( $ret . 'text' );
133 $out->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
135 $out->addHTML( $this->userForm( $this->mTarget
) );
139 $this->mTargetObj
= $ret;
141 $form = new HTMLForm( $this->getFormFields(), $this->getContext() );
142 $form->addPreText( $this->msg( 'emailpagetext' )->parse() );
143 $form->setSubmitTextMsg( 'emailsend' );
144 $form->setTitle( $this->getTitle() );
145 $form->setSubmitCallback( array( __CLASS__
, 'uiSubmit' ) );
146 $form->setWrapperLegendMsg( 'email-legend' );
149 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
153 $result = $form->show();
155 if( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
156 $out->setPageTitle( $this->msg( 'emailsent' ) );
157 $out->addWikiMsg( 'emailsenttext' );
158 $out->returnToMain( false, $this->mTargetObj
->getUserPage() );
163 * Validate target User
165 * @param $target String: target user name
166 * @return User object on success or a string on error
168 public static function getTarget( $target ) {
169 if ( $target == '' ) {
170 wfDebug( "Target is empty.\n" );
174 $nu = User
::newFromName( $target );
175 if( !$nu instanceof User ||
!$nu->getId() ) {
176 wfDebug( "Target is invalid user.\n" );
178 } elseif ( !$nu->isEmailConfirmed() ) {
179 wfDebug( "User has no valid email.\n" );
181 } elseif ( !$nu->canReceiveEmail() ) {
182 wfDebug( "User does not allow user emails.\n" );
183 return 'nowikiemail';
190 * Check whether a user is allowed to send email
192 * @param $user User object
193 * @param $editToken String: edit token
194 * @return null on success or string on error
196 public static function getPermissionsError( $user, $editToken ) {
197 global $wgEnableEmail, $wgEnableUserEmail;
198 if( !$wgEnableEmail ||
!$wgEnableUserEmail ) {
199 return 'usermaildisabled';
202 if( !$user->isAllowed( 'sendemail' ) ) {
206 if( !$user->isEmailConfirmed() ) {
207 return 'mailnologin';
210 if( $user->isBlockedFromEmailuser() ) {
211 wfDebug( "User is blocked from sending e-mail.\n" );
212 return "blockedemailuser";
215 if( $user->pingLimiter( 'emailuser' ) ) {
216 wfDebug( "Ping limiter triggered.\n" );
217 return 'actionthrottledtext';
221 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
222 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
231 * Form to ask for target user name.
233 * @param $name String: user name submitted.
234 * @return String: form asking for user name.
236 protected function userForm( $name ) {
238 $string = Xml
::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' ) ) .
239 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
240 Xml
::openElement( 'fieldset' ) .
241 Html
::rawElement( 'legend', null, $this->msg( 'emailtarget' )->parse() ) .
242 Xml
::inputLabel( $this->msg( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
243 Xml
::submitButton( $this->msg( 'emailusernamesubmit' )->text() ) .
244 Xml
::closeElement( 'fieldset' ) .
245 Xml
::closeElement( 'form' ) . "\n";
250 * Submit callback for an HTMLForm object, will simply call submit().
254 * @param $form HTMLForm object
255 * @return Status|string|bool
257 public static function uiSubmit( array $data, HTMLForm
$form ) {
258 return self
::submit( $data, $form->getContext() );
262 * Really send a mail. Permissions should have been checked using
263 * getPermissionsError(). It is probably also a good
264 * idea to check the edit token and ping limiter in advance.
266 * @return Mixed: Status object, or potentially a String on error
267 * or maybe even true on success if anything uses the EmailUser hook.
269 public static function submit( array $data, IContextSource
$context ) {
270 global $wgUserEmailUseReplyTo;
272 $target = self
::getTarget( $data['Target'] );
273 if( !$target instanceof User
) {
274 return $context->msg( $target . 'text' )->parseAsBlock();
276 $to = new MailAddress( $target );
277 $from = new MailAddress( $context->getUser() );
278 $subject = $data['Subject'];
279 $text = $data['Text'];
281 // Add a standard footer and trim up trailing newlines
282 $text = rtrim( $text ) . "\n\n-- \n";
283 $text .= $context->msg( 'emailuserfooter',
284 $from->name
, $to->name
)->inContentLanguage()->text();
287 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
291 if( $wgUserEmailUseReplyTo ) {
292 // Put the generic wiki autogenerated address in the From:
293 // header and reserve the user for Reply-To.
295 // This is a bit ugly, but will serve to differentiate
296 // wiki-borne mails from direct mails and protects against
297 // SPF and bounce problems with some mailers (see below).
298 global $wgPasswordSender, $wgPasswordSenderName;
299 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
302 // Put the sending user's e-mail address in the From: header.
304 // This is clean-looking and convenient, but has issues.
305 // One is that it doesn't as clearly differentiate the wiki mail
306 // from "directly" sent mails.
308 // Another is that some mailers (like sSMTP) will use the From
309 // address as the envelope sender as well. For open sites this
310 // can cause mails to be flunked for SPF violations (since the
311 // wiki server isn't an authorized sender for various users'
312 // domains) as well as creating a privacy issue as bounces
313 // containing the recipient's e-mail address may get sent to
319 $status = UserMailer
::send( $to, $mailFrom, $subject, $text, $replyTo );
321 if( !$status->isGood() ) {
324 // if the user requested a copy of this mail, do this now,
325 // unless they are emailing themselves, in which case one
326 // copy of the message is sufficient.
327 if ( $data['CCMe'] && $to != $from ) {
328 $cc_subject = $context->msg( 'emailccsubject' )->rawParams(
329 $target->getName(), $subject )->text();
330 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
331 $ccStatus = UserMailer
::send( $from, $from, $cc_subject, $text );
332 $status->merge( $ccStatus );
335 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );