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
{
33 * @var User|string $mTargetObj
35 protected $mTargetObj;
37 public function __construct() {
38 parent
::__construct( 'Emailuser' );
41 public function doesWrites() {
45 public function getDescription() {
46 $target = self
::getTarget( $this->mTarget
);
47 if ( !$target instanceof User
) {
48 return $this->msg( 'emailuser-title-notarget' )->text();
51 return $this->msg( 'emailuser-title-target', $target->getName() )->text();
54 protected function getFormFields() {
59 'default' => Linker
::link(
60 $this->getUser()->getUserPage(),
61 htmlspecialchars( $this->getUser()->getName() )
63 'label-message' => 'emailfrom',
64 'id' => 'mw-emailuser-sender',
69 'default' => Linker
::link(
70 $this->mTargetObj
->getUserPage(),
71 htmlspecialchars( $this->mTargetObj
->getName() )
73 'label-message' => 'emailto',
74 'id' => 'mw-emailuser-recipient',
78 'default' => $this->mTargetObj
->getName(),
82 'default' => $this->msg( 'defemailsubject',
83 $this->getUser()->getName() )->inContentLanguage()->text(),
84 'label-message' => 'emailsubject',
93 'label-message' => 'emailmessage',
98 'label-message' => 'emailccme',
99 'default' => $this->getUser()->getBoolOption( 'ccmeonemails' ),
104 public function execute( $par ) {
105 $out = $this->getOutput();
106 $out->addModuleStyles( 'mediawiki.special' );
108 $this->mTarget
= is_null( $par )
109 ?
$this->getRequest()->getVal( 'wpTarget', $this->getRequest()->getVal( 'target', '' ) )
112 // This needs to be below assignment of $this->mTarget because
113 // getDescription() needs it to determine the correct page title.
115 $this->outputHeader();
117 // error out if sending user cannot do this
118 $error = self
::getPermissionsError(
120 $this->getRequest()->getVal( 'wpEditToken' ),
129 throw new PermissionsError( 'sendemail' );
130 case 'blockedemailuser':
131 throw new UserBlockedError( $this->getUser()->mBlock
);
132 case 'actionthrottledtext':
133 throw new ThrottledError
;
135 case 'usermaildisabled':
136 throw new ErrorPageError( $error, "{$error}text" );
139 list( $title, $msg, $params ) = $error;
140 throw new ErrorPageError( $title, $msg, $params );
142 // Got a valid target user name? Else ask for one.
143 $ret = self
::getTarget( $this->mTarget
);
144 if ( !$ret instanceof User
) {
145 if ( $this->mTarget
!= '' ) {
146 // Messages used here: notargettext, noemailtext, nowikiemailtext
147 $ret = ( $ret == 'notarget' ) ?
'emailnotarget' : ( $ret . 'text' );
148 $out->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
150 $out->addHTML( $this->userForm( $this->mTarget
) );
155 $this->mTargetObj
= $ret;
157 $context = new DerivativeContext( $this->getContext() );
158 $context->setTitle( $this->getPageTitle() ); // Remove subpage
159 $form = new HTMLForm( $this->getFormFields(), $context );
160 // By now we are supposed to be sure that $this->mTarget is a user name
161 $form->addPreText( $this->msg( 'emailpagetext', $this->mTarget
)->parse() );
162 $form->setSubmitTextMsg( 'emailsend' );
163 $form->setSubmitCallback( [ __CLASS__
, 'uiSubmit' ] );
164 $form->setWrapperLegendMsg( 'email-legend' );
167 if ( !Hooks
::run( 'EmailUserForm', [ &$form ] ) ) {
171 $result = $form->show();
173 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
174 $out->setPageTitle( $this->msg( 'emailsent' ) );
175 $out->addWikiMsg( 'emailsenttext', $this->mTarget
);
176 $out->returnToMain( false, $this->mTargetObj
->getUserPage() );
181 * Validate target User
183 * @param string $target Target user name
184 * @return User User object on success or a string on error
186 public static function getTarget( $target ) {
187 if ( $target == '' ) {
188 wfDebug( "Target is empty.\n" );
193 $nu = User
::newFromName( $target );
194 if ( !$nu instanceof User ||
!$nu->getId() ) {
195 wfDebug( "Target is invalid user.\n" );
198 } elseif ( !$nu->isEmailConfirmed() ) {
199 wfDebug( "User has no valid email.\n" );
202 } elseif ( !$nu->canReceiveEmail() ) {
203 wfDebug( "User does not allow user emails.\n" );
205 return 'nowikiemail';
212 * Check whether a user is allowed to send email
215 * @param string $editToken Edit token
216 * @param Config $config optional for backwards compatibility
217 * @return string|null Null on success or string on error
219 public static function getPermissionsError( $user, $editToken, Config
$config = null ) {
220 if ( $config === null ) {
221 wfDebug( __METHOD__
. ' called without a Config instance passed to it' );
222 $config = ConfigFactory
::getDefaultInstance()->makeConfig( 'main' );
224 if ( !$config->get( 'EnableEmail' ) ||
!$config->get( 'EnableUserEmail' ) ) {
225 return 'usermaildisabled';
228 if ( !$user->isAllowed( 'sendemail' ) ) {
232 if ( !$user->isEmailConfirmed() ) {
233 return 'mailnologin';
236 if ( $user->isBlockedFromEmailuser() ) {
237 wfDebug( "User is blocked from sending e-mail.\n" );
239 return "blockedemailuser";
242 if ( $user->pingLimiter( 'emailuser' ) ) {
243 wfDebug( "Ping limiter triggered.\n" );
245 return 'actionthrottledtext';
250 Hooks
::run( 'UserCanSendEmail', [ &$user, &$hookErr ] );
251 Hooks
::run( 'EmailUserPermissionsErrors', [ $user, $editToken, &$hookErr ] );
261 * Form to ask for target user name.
263 * @param string $name User name submitted.
264 * @return string Form asking for user name.
266 protected function userForm( $name ) {
267 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
268 $string = Html
::openElement(
270 [ 'method' => 'get', 'action' => wfScript(), 'id' => 'askusername' ]
272 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
273 Html
::openElement( 'fieldset' ) .
274 Html
::rawElement( 'legend', null, $this->msg( 'emailtarget' )->parse() ) .
276 $this->msg( 'emailusername' )->text(),
284 'id' => 'emailusertarget',
285 'class' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
291 Html
::submitButton( $this->msg( 'emailusernamesubmit' )->text(), [] ) .
292 Html
::closeElement( 'fieldset' ) .
293 Html
::closeElement( 'form' ) . "\n";
299 * Submit callback for an HTMLForm object, will simply call submit().
303 * @param HTMLForm $form
304 * @return Status|string|bool
306 public static function uiSubmit( array $data, HTMLForm
$form ) {
307 return self
::submit( $data, $form->getContext() );
311 * Really send a mail. Permissions should have been checked using
312 * getPermissionsError(). It is probably also a good
313 * idea to check the edit token and ping limiter in advance.
316 * @param IContextSource $context
317 * @return Status|string|bool Status object, or potentially a String on error
318 * or maybe even true on success if anything uses the EmailUser hook.
320 public static function submit( array $data, IContextSource
$context ) {
321 $config = $context->getConfig();
323 $target = self
::getTarget( $data['Target'] );
324 if ( !$target instanceof User
) {
325 // Messages used here: notargettext, noemailtext, nowikiemailtext
326 return $context->msg( $target . 'text' )->parseAsBlock();
329 $to = MailAddress
::newFromUser( $target );
330 $from = MailAddress
::newFromUser( $context->getUser() );
331 $subject = $data['Subject'];
332 $text = $data['Text'];
334 // Add a standard footer and trim up trailing newlines
335 $text = rtrim( $text ) . "\n\n-- \n";
336 $text .= $context->msg( 'emailuserfooter',
337 $from->name
, $to->name
)->inContentLanguage()->text();
340 if ( !Hooks
::run( 'EmailUser', [ &$to, &$from, &$subject, &$text, &$error ] ) ) {
344 if ( $config->get( 'UserEmailUseReplyTo' ) ) {
346 * Put the generic wiki autogenerated address in the From:
347 * header and reserve the user for Reply-To.
349 * This is a bit ugly, but will serve to differentiate
350 * wiki-borne mails from direct mails and protects against
351 * SPF and bounce problems with some mailers (see below).
353 $mailFrom = new MailAddress( $config->get( 'PasswordSender' ),
354 wfMessage( 'emailsender' )->inContentLanguage()->text() );
358 * Put the sending user's e-mail address in the From: header.
360 * This is clean-looking and convenient, but has issues.
361 * One is that it doesn't as clearly differentiate the wiki mail
362 * from "directly" sent mails.
364 * Another is that some mailers (like sSMTP) will use the From
365 * address as the envelope sender as well. For open sites this
366 * can cause mails to be flunked for SPF violations (since the
367 * wiki server isn't an authorized sender for various users'
368 * domains) as well as creating a privacy issue as bounces
369 * containing the recipient's e-mail address may get sent to
376 $status = UserMailer
::send( $to, $mailFrom, $subject, $text, [
377 'replyTo' => $replyTo,
380 if ( !$status->isGood() ) {
383 // if the user requested a copy of this mail, do this now,
384 // unless they are emailing themselves, in which case one
385 // copy of the message is sufficient.
386 if ( $data['CCMe'] && $to != $from ) {
387 $cc_subject = $context->msg( 'emailccsubject' )->rawParams(
388 $target->getName(), $subject )->text();
390 // target and sender are equal, because this is the CC for the sender
391 Hooks
::run( 'EmailUserCC', [ &$from, &$from, &$cc_subject, &$text ] );
393 $ccStatus = UserMailer
::send( $from, $from, $cc_subject, $text );
394 $status->merge( $ccStatus );
397 Hooks
::run( 'EmailUserComplete', [ $to, $from, $subject, $text ] );
404 * Return an array of subpages beginning with $search that this special page will accept.
406 * @param string $search Prefix to search for
407 * @param int $limit Maximum number of results to return (usually 10)
408 * @param int $offset Number of results to skip (usually 0)
409 * @return string[] Matching subpages
411 public function prefixSearchSubpages( $search, $limit, $offset ) {
412 $user = User
::newFromName( $search );
414 // No prefix suggestion for invalid user
417 // Autocomplete subpage as user list - public to allow caching
418 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
421 protected function getGroupName() {