Add a way for packagers to override some installation details
[mediawiki.git] / includes / specials / SpecialEmailuser.php
blob2ab02cb5a90aca060a06acf8fa8c74f0a66e89b2
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that allows users to send e-mails to other users
27 * @ingroup SpecialPage
29 class SpecialEmailUser extends UnlistedSpecialPage {
30 protected $mTarget;
32 public function __construct() {
33 parent::__construct( 'Emailuser' );
36 protected function getFormFields() {
37 return array(
38 'From' => array(
39 'type' => 'info',
40 'raw' => 1,
41 'default' => Linker::link(
42 $this->getUser()->getUserPage(),
43 htmlspecialchars( $this->getUser()->getName() )
45 'label-message' => 'emailfrom',
46 'id' => 'mw-emailuser-sender',
48 'To' => array(
49 'type' => 'info',
50 'raw' => 1,
51 'default' => Linker::link(
52 $this->mTargetObj->getUserPage(),
53 htmlspecialchars( $this->mTargetObj->getName() )
55 'label-message' => 'emailto',
56 'id' => 'mw-emailuser-recipient',
58 'Target' => array(
59 'type' => 'hidden',
60 'default' => $this->mTargetObj->getName(),
62 'Subject' => array(
63 'type' => 'text',
64 'default' => $this->msg( 'defemailsubject',
65 $this->getUser()->getName() )->inContentLanguage()->text(),
66 'label-message' => 'emailsubject',
67 'maxlength' => 200,
68 'size' => 60,
69 'required' => 1,
71 'Text' => array(
72 'type' => 'textarea',
73 'rows' => 20,
74 'cols' => 80,
75 'label-message' => 'emailmessage',
76 'required' => 1,
78 'CCMe' => array(
79 'type' => 'check',
80 'label-message' => 'emailccme',
81 'default' => $this->getUser()->getBoolOption( 'ccmeonemails' ),
86 public function execute( $par ) {
87 $this->setHeaders();
88 $this->outputHeader();
89 $out = $this->getOutput();
90 $out->addModuleStyles( 'mediawiki.special' );
91 $this->mTarget = is_null( $par )
92 ? $this->getRequest()->getVal( 'wpTarget', $this->getRequest()->getVal( 'target', '' ) )
93 : $par;
94 // error out if sending user cannot do this
95 $error = self::getPermissionsError( $this->getUser(), $this->getRequest()->getVal( 'wpEditToken' ) );
96 switch ( $error ) {
97 case null:
98 # Wahey!
99 break;
100 case 'badaccess':
101 throw new PermissionsError( 'sendemail' );
102 case 'blockedemailuser':
103 throw new UserBlockedError( $this->getUser()->mBlock );
104 case 'actionthrottledtext':
105 throw new ThrottledError;
106 case 'mailnologin':
107 case 'usermaildisabled':
108 throw new ErrorPageError( $error, "{$error}text" );
109 default:
110 # It's a hook error
111 list( $title, $msg, $params ) = $error;
112 throw new ErrorPageError( $title, $msg, $params );
114 // Got a valid target user name? Else ask for one.
115 $ret = self::getTarget( $this->mTarget );
116 if( !$ret instanceof User ) {
117 if( $this->mTarget != '' ) {
118 $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
119 $out->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
121 $out->addHTML( $this->userForm( $this->mTarget ) );
122 return false;
125 $this->mTargetObj = $ret;
127 $form = new HTMLForm( $this->getFormFields(), $this->getContext() );
128 $form->addPreText( $this->msg( 'emailpagetext' )->parse() );
129 $form->setSubmitTextMsg( 'emailsend' );
130 $form->setTitle( $this->getTitle() );
131 $form->setSubmitCallback( array( __CLASS__, 'uiSubmit' ) );
132 $form->setWrapperLegendMsg( 'email-legend' );
133 $form->loadData();
135 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
136 return false;
139 $out->setPageTitle( $this->msg( 'emailpage' ) );
140 $result = $form->show();
142 if( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
143 $out->setPageTitle( $this->msg( 'emailsent' ) );
144 $out->addWikiMsg( 'emailsenttext' );
145 $out->returnToMain( false, $this->mTargetObj->getUserPage() );
150 * Validate target User
152 * @param $target String: target user name
153 * @return User object on success or a string on error
155 public static function getTarget( $target ) {
156 if ( $target == '' ) {
157 wfDebug( "Target is empty.\n" );
158 return 'notarget';
161 $nu = User::newFromName( $target );
162 if( !$nu instanceof User || !$nu->getId() ) {
163 wfDebug( "Target is invalid user.\n" );
164 return 'notarget';
165 } elseif ( !$nu->isEmailConfirmed() ) {
166 wfDebug( "User has no valid email.\n" );
167 return 'noemail';
168 } elseif ( !$nu->canReceiveEmail() ) {
169 wfDebug( "User does not allow user emails.\n" );
170 return 'nowikiemail';
173 return $nu;
177 * Check whether a user is allowed to send email
179 * @param $user User object
180 * @param $editToken String: edit token
181 * @return null on success or string on error
183 public static function getPermissionsError( $user, $editToken ) {
184 global $wgEnableEmail, $wgEnableUserEmail;
185 if( !$wgEnableEmail || !$wgEnableUserEmail ) {
186 return 'usermaildisabled';
189 if( !$user->isAllowed( 'sendemail' ) ) {
190 return 'badaccess';
193 if( !$user->isEmailConfirmed() ) {
194 return 'mailnologin';
197 if( $user->isBlockedFromEmailuser() ) {
198 wfDebug( "User is blocked from sending e-mail.\n" );
199 return "blockedemailuser";
202 if( $user->pingLimiter( 'emailuser' ) ) {
203 wfDebug( "Ping limiter triggered.\n" );
204 return 'actionthrottledtext';
207 $hookErr = false;
208 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
209 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
210 if ( $hookErr ) {
211 return $hookErr;
214 return null;
218 * Form to ask for target user name.
220 * @param $name String: user name submitted.
221 * @return String: form asking for user name.
223 protected function userForm( $name ) {
224 global $wgScript;
225 $string = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' ) ) .
226 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
227 Xml::openElement( 'fieldset' ) .
228 Html::rawElement( 'legend', null, $this->msg( 'emailtarget' )->parse() ) .
229 Xml::inputLabel( $this->msg( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
230 Xml::submitButton( $this->msg( 'emailusernamesubmit' )->text() ) .
231 Xml::closeElement( 'fieldset' ) .
232 Xml::closeElement( 'form' ) . "\n";
233 return $string;
237 * Submit callback for an HTMLForm object, will simply call submit().
239 * @since 1.20
240 * @param $data array
241 * @param $form HTMLForm object
242 * @return Status|string|bool
244 public static function uiSubmit( array $data, HTMLForm $form ) {
245 return self::submit( $data, $form->getContext() );
249 * Really send a mail. Permissions should have been checked using
250 * getPermissionsError(). It is probably also a good
251 * idea to check the edit token and ping limiter in advance.
253 * @return Mixed: Status object, or potentially a String on error
254 * or maybe even true on success if anything uses the EmailUser hook.
256 public static function submit( array $data, IContextSource $context ) {
257 global $wgUserEmailUseReplyTo;
259 $target = self::getTarget( $data['Target'] );
260 if( !$target instanceof User ) {
261 return $context->msg( $target . 'text' )->parseAsBlock();
263 $to = new MailAddress( $target );
264 $from = new MailAddress( $context->getUser() );
265 $subject = $data['Subject'];
266 $text = $data['Text'];
268 // Add a standard footer and trim up trailing newlines
269 $text = rtrim( $text ) . "\n\n-- \n";
270 $text .= $context->msg( 'emailuserfooter',
271 $from->name, $to->name )->inContentLanguage()->text();
273 $error = '';
274 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
275 return $error;
278 if( $wgUserEmailUseReplyTo ) {
279 // Put the generic wiki autogenerated address in the From:
280 // header and reserve the user for Reply-To.
282 // This is a bit ugly, but will serve to differentiate
283 // wiki-borne mails from direct mails and protects against
284 // SPF and bounce problems with some mailers (see below).
285 global $wgPasswordSender, $wgPasswordSenderName;
286 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
287 $replyTo = $from;
288 } else {
289 // Put the sending user's e-mail address in the From: header.
291 // This is clean-looking and convenient, but has issues.
292 // One is that it doesn't as clearly differentiate the wiki mail
293 // from "directly" sent mails.
295 // Another is that some mailers (like sSMTP) will use the From
296 // address as the envelope sender as well. For open sites this
297 // can cause mails to be flunked for SPF violations (since the
298 // wiki server isn't an authorized sender for various users'
299 // domains) as well as creating a privacy issue as bounces
300 // containing the recipient's e-mail address may get sent to
301 // the sending user.
302 $mailFrom = $from;
303 $replyTo = null;
306 $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
308 if( !$status->isGood() ) {
309 return $status;
310 } else {
311 // if the user requested a copy of this mail, do this now,
312 // unless they are emailing themselves, in which case one
313 // copy of the message is sufficient.
314 if ( $data['CCMe'] && $to != $from ) {
315 $cc_subject = $context->msg( 'emailccsubject' )->rawParams(
316 $target->getName(), $subject )->text();
317 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
318 $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
319 $status->merge( $ccStatus );
322 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
323 return $status;