0958126b207bb18e3b123113f9af6ecfcf4a719f
[lhc/web/wiklou.git] / includes / specials / SpecialEmailuser.php
1 <?php
2 /**
3 * Implements Special:Emailuser
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that allows users to send e-mails to other users
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialEmailUser extends UnlistedSpecialPage {
30 protected $mTarget;
31
32 /**
33 * @var User|string $mTargetObj
34 */
35 protected $mTargetObj;
36
37 public function __construct() {
38 parent::__construct( 'Emailuser' );
39 }
40
41 public function getDescription() {
42 $target = self::getTarget( $this->mTarget );
43 if ( !$target instanceof User ) {
44 return $this->msg( 'emailuser-title-notarget' )->text();
45 }
46
47 return $this->msg( 'emailuser-title-target', $target->getName() )->text();
48 }
49
50 protected function getFormFields() {
51 return array(
52 'From' => array(
53 'type' => 'info',
54 'raw' => 1,
55 'default' => Linker::link(
56 $this->getUser()->getUserPage(),
57 htmlspecialchars( $this->getUser()->getName() )
58 ),
59 'label-message' => 'emailfrom',
60 'id' => 'mw-emailuser-sender',
61 ),
62 'To' => array(
63 'type' => 'info',
64 'raw' => 1,
65 'default' => Linker::link(
66 $this->mTargetObj->getUserPage(),
67 htmlspecialchars( $this->mTargetObj->getName() )
68 ),
69 'label-message' => 'emailto',
70 'id' => 'mw-emailuser-recipient',
71 ),
72 'Target' => array(
73 'type' => 'hidden',
74 'default' => $this->mTargetObj->getName(),
75 ),
76 'Subject' => array(
77 'type' => 'text',
78 'default' => $this->msg( 'defemailsubject',
79 $this->getUser()->getName() )->inContentLanguage()->text(),
80 'label-message' => 'emailsubject',
81 'maxlength' => 200,
82 'size' => 60,
83 'required' => true,
84 ),
85 'Text' => array(
86 'type' => 'textarea',
87 'rows' => 20,
88 'cols' => 80,
89 'label-message' => 'emailmessage',
90 'required' => true,
91 ),
92 'CCMe' => array(
93 'type' => 'check',
94 'label-message' => 'emailccme',
95 'default' => $this->getUser()->getBoolOption( 'ccmeonemails' ),
96 ),
97 );
98 }
99
100 public function execute( $par ) {
101 $out = $this->getOutput();
102 $out->addModuleStyles( 'mediawiki.special' );
103
104 $this->mTarget = is_null( $par )
105 ? $this->getRequest()->getVal( 'wpTarget', $this->getRequest()->getVal( 'target', '' ) )
106 : $par;
107
108 // This needs to be below assignment of $this->mTarget because
109 // getDescription() needs it to determine the correct page title.
110 $this->setHeaders();
111 $this->outputHeader();
112
113 // error out if sending user cannot do this
114 $error = self::getPermissionsError(
115 $this->getUser(),
116 $this->getRequest()->getVal( 'wpEditToken' )
117 );
118
119 switch ( $error ) {
120 case null:
121 # Wahey!
122 break;
123 case 'badaccess':
124 throw new PermissionsError( 'sendemail' );
125 case 'blockedemailuser':
126 throw new UserBlockedError( $this->getUser()->mBlock );
127 case 'actionthrottledtext':
128 throw new ThrottledError;
129 case 'mailnologin':
130 case 'usermaildisabled':
131 throw new ErrorPageError( $error, "{$error}text" );
132 default:
133 # It's a hook error
134 list( $title, $msg, $params ) = $error;
135 throw new ErrorPageError( $title, $msg, $params );
136 }
137 // Got a valid target user name? Else ask for one.
138 $ret = self::getTarget( $this->mTarget );
139 if ( !$ret instanceof User ) {
140 if ( $this->mTarget != '' ) {
141 // Messages used here: notargettext, noemailtext, nowikiemailtext
142 $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
143 $out->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
144 }
145 $out->addHTML( $this->userForm( $this->mTarget ) );
146
147 return;
148 }
149
150 $this->mTargetObj = $ret;
151
152 $context = new DerivativeContext( $this->getContext() );
153 $context->setTitle( $this->getPageTitle() ); // Remove subpage
154 $form = new HTMLForm( $this->getFormFields(), $context );
155 // By now we are supposed to be sure that $this->mTarget is a user name
156 $form->addPreText( $this->msg( 'emailpagetext', $this->mTarget )->parse() );
157 $form->setSubmitTextMsg( 'emailsend' );
158 $form->setSubmitCallback( array( __CLASS__, 'uiSubmit' ) );
159 $form->setWrapperLegendMsg( 'email-legend' );
160 $form->loadData();
161
162 if ( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
163 return;
164 }
165
166 $result = $form->show();
167
168 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
169 $out->setPageTitle( $this->msg( 'emailsent' ) );
170 $out->addWikiMsg( 'emailsenttext', $this->mTarget );
171 $out->returnToMain( false, $this->mTargetObj->getUserPage() );
172 }
173 }
174
175 /**
176 * Validate target User
177 *
178 * @param string $target Target user name
179 * @return User User object on success or a string on error
180 */
181 public static function getTarget( $target ) {
182 if ( $target == '' ) {
183 wfDebug( "Target is empty.\n" );
184
185 return 'notarget';
186 }
187
188 $nu = User::newFromName( $target );
189 if ( !$nu instanceof User || !$nu->getId() ) {
190 wfDebug( "Target is invalid user.\n" );
191
192 return 'notarget';
193 } elseif ( !$nu->isEmailConfirmed() ) {
194 wfDebug( "User has no valid email.\n" );
195
196 return 'noemail';
197 } elseif ( !$nu->canReceiveEmail() ) {
198 wfDebug( "User does not allow user emails.\n" );
199
200 return 'nowikiemail';
201 }
202
203 return $nu;
204 }
205
206 /**
207 * Check whether a user is allowed to send email
208 *
209 * @param User $user
210 * @param string $editToken Edit token
211 * @return string|null Null on success or string on error
212 */
213 public static function getPermissionsError( $user, $editToken ) {
214 global $wgEnableEmail, $wgEnableUserEmail;
215
216 if ( !$wgEnableEmail || !$wgEnableUserEmail ) {
217 return 'usermaildisabled';
218 }
219
220 if ( !$user->isAllowed( 'sendemail' ) ) {
221 return 'badaccess';
222 }
223
224 if ( !$user->isEmailConfirmed() ) {
225 return 'mailnologin';
226 }
227
228 if ( $user->isBlockedFromEmailuser() ) {
229 wfDebug( "User is blocked from sending e-mail.\n" );
230
231 return "blockedemailuser";
232 }
233
234 if ( $user->pingLimiter( 'emailuser' ) ) {
235 wfDebug( "Ping limiter triggered.\n" );
236
237 return 'actionthrottledtext';
238 }
239
240 $hookErr = false;
241
242 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
243 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
244
245 if ( $hookErr ) {
246 return $hookErr;
247 }
248
249 return null;
250 }
251
252 /**
253 * Form to ask for target user name.
254 *
255 * @param string $name User name submitted.
256 * @return string Form asking for user name.
257 */
258 protected function userForm( $name ) {
259 global $wgScript;
260 $string = Xml::openElement(
261 'form',
262 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' )
263 ) .
264 Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
265 Xml::openElement( 'fieldset' ) .
266 Html::rawElement( 'legend', null, $this->msg( 'emailtarget' )->parse() ) .
267 Xml::inputLabel(
268 $this->msg( 'emailusername' )->text(),
269 'target',
270 'emailusertarget',
271 30,
272 $name
273 ) .
274 ' ' .
275 Xml::submitButton( $this->msg( 'emailusernamesubmit' )->text() ) .
276 Xml::closeElement( 'fieldset' ) .
277 Xml::closeElement( 'form' ) . "\n";
278
279 return $string;
280 }
281
282 /**
283 * Submit callback for an HTMLForm object, will simply call submit().
284 *
285 * @since 1.20
286 * @param array $data
287 * @param HTMLForm $form
288 * @return Status|string|bool
289 */
290 public static function uiSubmit( array $data, HTMLForm $form ) {
291 return self::submit( $data, $form->getContext() );
292 }
293
294 /**
295 * Really send a mail. Permissions should have been checked using
296 * getPermissionsError(). It is probably also a good
297 * idea to check the edit token and ping limiter in advance.
298 *
299 * @param array $data
300 * @param IContextSource $context
301 * @return Status|string|bool Status object, or potentially a String on error
302 * or maybe even true on success if anything uses the EmailUser hook.
303 */
304 public static function submit( array $data, IContextSource $context ) {
305 global $wgUserEmailUseReplyTo;
306
307 $target = self::getTarget( $data['Target'] );
308 if ( !$target instanceof User ) {
309 // Messages used here: notargettext, noemailtext, nowikiemailtext
310 return $context->msg( $target . 'text' )->parseAsBlock();
311 }
312
313 $to = new MailAddress( $target );
314 $from = new MailAddress( $context->getUser() );
315 $subject = $data['Subject'];
316 $text = $data['Text'];
317
318 // Add a standard footer and trim up trailing newlines
319 $text = rtrim( $text ) . "\n\n-- \n";
320 $text .= $context->msg( 'emailuserfooter',
321 $from->name, $to->name )->inContentLanguage()->text();
322
323 $error = '';
324 if ( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
325 return $error;
326 }
327
328 if ( $wgUserEmailUseReplyTo ) {
329 // Put the generic wiki autogenerated address in the From:
330 // header and reserve the user for Reply-To.
331 //
332 // This is a bit ugly, but will serve to differentiate
333 // wiki-borne mails from direct mails and protects against
334 // SPF and bounce problems with some mailers (see below).
335 global $wgPasswordSender;
336
337 $mailFrom = new MailAddress( $wgPasswordSender,
338 wfMessage( 'emailsender' )->inContentLanguage()->text() );
339 $replyTo = $from;
340 } else {
341 // Put the sending user's e-mail address in the From: header.
342 //
343 // This is clean-looking and convenient, but has issues.
344 // One is that it doesn't as clearly differentiate the wiki mail
345 // from "directly" sent mails.
346 //
347 // Another is that some mailers (like sSMTP) will use the From
348 // address as the envelope sender as well. For open sites this
349 // can cause mails to be flunked for SPF violations (since the
350 // wiki server isn't an authorized sender for various users'
351 // domains) as well as creating a privacy issue as bounces
352 // containing the recipient's e-mail address may get sent to
353 // the sending user.
354 $mailFrom = $from;
355 $replyTo = null;
356 }
357
358 $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
359
360 if ( !$status->isGood() ) {
361 return $status;
362 } else {
363 // if the user requested a copy of this mail, do this now,
364 // unless they are emailing themselves, in which case one
365 // copy of the message is sufficient.
366 if ( $data['CCMe'] && $to != $from ) {
367 $cc_subject = $context->msg( 'emailccsubject' )->rawParams(
368 $target->getName(), $subject )->text();
369 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
370 $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
371 $status->merge( $ccStatus );
372 }
373
374 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
375
376 return $status;
377 }
378 }
379
380 protected function getGroupName() {
381 return 'users';
382 }
383 }