Add two new debug log groups
[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 $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
142 $out->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
143 }
144 $out->addHTML( $this->userForm( $this->mTarget ) );
145
146 return false;
147 }
148
149 $this->mTargetObj = $ret;
150
151 $context = new DerivativeContext( $this->getContext() );
152 $context->setTitle( $this->getPageTitle() ); // Remove subpage
153 $form = new HTMLForm( $this->getFormFields(), $context );
154 // By now we are supposed to be sure that $this->mTarget is a user name
155 $form->addPreText( $this->msg( 'emailpagetext', $this->mTarget )->parse() );
156 $form->setSubmitTextMsg( 'emailsend' );
157 $form->setSubmitCallback( array( __CLASS__, 'uiSubmit' ) );
158 $form->setWrapperLegendMsg( 'email-legend' );
159 $form->loadData();
160
161 if ( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
162 return false;
163 }
164
165 $result = $form->show();
166
167 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
168 $out->setPageTitle( $this->msg( 'emailsent' ) );
169 $out->addWikiMsg( 'emailsenttext', $this->mTarget );
170 $out->returnToMain( false, $this->mTargetObj->getUserPage() );
171 }
172 }
173
174 /**
175 * Validate target User
176 *
177 * @param string $target target user name
178 * @return User object on success or a string on error
179 */
180 public static function getTarget( $target ) {
181 if ( $target == '' ) {
182 wfDebug( "Target is empty.\n" );
183
184 return 'notarget';
185 }
186
187 $nu = User::newFromName( $target );
188 if ( !$nu instanceof User || !$nu->getId() ) {
189 wfDebug( "Target is invalid user.\n" );
190
191 return 'notarget';
192 } elseif ( !$nu->isEmailConfirmed() ) {
193 wfDebug( "User has no valid email.\n" );
194
195 return 'noemail';
196 } elseif ( !$nu->canReceiveEmail() ) {
197 wfDebug( "User does not allow user emails.\n" );
198
199 return 'nowikiemail';
200 }
201
202 return $nu;
203 }
204
205 /**
206 * Check whether a user is allowed to send email
207 *
208 * @param $user User object
209 * @param string $editToken edit token
210 * @return null on success or string on error
211 */
212 public static function getPermissionsError( $user, $editToken ) {
213 global $wgEnableEmail, $wgEnableUserEmail;
214
215 if ( !$wgEnableEmail || !$wgEnableUserEmail ) {
216 return 'usermaildisabled';
217 }
218
219 if ( !$user->isAllowed( 'sendemail' ) ) {
220 return 'badaccess';
221 }
222
223 if ( !$user->isEmailConfirmed() ) {
224 return 'mailnologin';
225 }
226
227 if ( $user->isBlockedFromEmailuser() ) {
228 wfDebug( "User is blocked from sending e-mail.\n" );
229
230 return "blockedemailuser";
231 }
232
233 if ( $user->pingLimiter( 'emailuser' ) ) {
234 wfDebug( "Ping limiter triggered.\n" );
235
236 return 'actionthrottledtext';
237 }
238
239 $hookErr = false;
240
241 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
242 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
243
244 if ( $hookErr ) {
245 return $hookErr;
246 }
247
248 return null;
249 }
250
251 /**
252 * Form to ask for target user name.
253 *
254 * @param string $name user name submitted.
255 * @return String: form asking for user name.
256 */
257 protected function userForm( $name ) {
258 global $wgScript;
259 $string = Xml::openElement(
260 'form',
261 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' )
262 ) .
263 Html::hidden( 'title', $this->getPageTitle()->getPrefixedText() ) .
264 Xml::openElement( 'fieldset' ) .
265 Html::rawElement( 'legend', null, $this->msg( 'emailtarget' )->parse() ) .
266 Xml::inputLabel(
267 $this->msg( 'emailusername' )->text(),
268 'target',
269 'emailusertarget',
270 30,
271 $name
272 ) .
273 ' ' .
274 Xml::submitButton( $this->msg( 'emailusernamesubmit' )->text() ) .
275 Xml::closeElement( 'fieldset' ) .
276 Xml::closeElement( 'form' ) . "\n";
277
278 return $string;
279 }
280
281 /**
282 * Submit callback for an HTMLForm object, will simply call submit().
283 *
284 * @since 1.20
285 * @param $data array
286 * @param $form HTMLForm object
287 * @return Status|string|bool
288 */
289 public static function uiSubmit( array $data, HTMLForm $form ) {
290 return self::submit( $data, $form->getContext() );
291 }
292
293 /**
294 * Really send a mail. Permissions should have been checked using
295 * getPermissionsError(). It is probably also a good
296 * idea to check the edit token and ping limiter in advance.
297 *
298 * @param array $data
299 * @param IContextSource $context
300 * @return Mixed: Status object, or potentially a String on error
301 * or maybe even true on success if anything uses the EmailUser hook.
302 */
303 public static function submit( array $data, IContextSource $context ) {
304 global $wgUserEmailUseReplyTo;
305
306 $target = self::getTarget( $data['Target'] );
307 if ( !$target instanceof User ) {
308 return $context->msg( $target . 'text' )->parseAsBlock();
309 }
310
311 $to = new MailAddress( $target );
312 $from = new MailAddress( $context->getUser() );
313 $subject = $data['Subject'];
314 $text = $data['Text'];
315
316 // Add a standard footer and trim up trailing newlines
317 $text = rtrim( $text ) . "\n\n-- \n";
318 $text .= $context->msg( 'emailuserfooter',
319 $from->name, $to->name )->inContentLanguage()->text();
320
321 $error = '';
322 if ( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
323 return $error;
324 }
325
326 if ( $wgUserEmailUseReplyTo ) {
327 // Put the generic wiki autogenerated address in the From:
328 // header and reserve the user for Reply-To.
329 //
330 // This is a bit ugly, but will serve to differentiate
331 // wiki-borne mails from direct mails and protects against
332 // SPF and bounce problems with some mailers (see below).
333 global $wgPasswordSender;
334
335 $mailFrom = new MailAddress( $wgPasswordSender,
336 wfMessage( 'emailsender' )->inContentLanguage()->text() );
337 $replyTo = $from;
338 } else {
339 // Put the sending user's e-mail address in the From: header.
340 //
341 // This is clean-looking and convenient, but has issues.
342 // One is that it doesn't as clearly differentiate the wiki mail
343 // from "directly" sent mails.
344 //
345 // Another is that some mailers (like sSMTP) will use the From
346 // address as the envelope sender as well. For open sites this
347 // can cause mails to be flunked for SPF violations (since the
348 // wiki server isn't an authorized sender for various users'
349 // domains) as well as creating a privacy issue as bounces
350 // containing the recipient's e-mail address may get sent to
351 // the sending user.
352 $mailFrom = $from;
353 $replyTo = null;
354 }
355
356 $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
357
358 if ( !$status->isGood() ) {
359 return $status;
360 } else {
361 // if the user requested a copy of this mail, do this now,
362 // unless they are emailing themselves, in which case one
363 // copy of the message is sufficient.
364 if ( $data['CCMe'] && $to != $from ) {
365 $cc_subject = $context->msg( 'emailccsubject' )->rawParams(
366 $target->getName(), $subject )->text();
367 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
368 $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
369 $status->merge( $ccStatus );
370 }
371
372 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
373
374 return $status;
375 }
376 }
377
378 protected function getGroupName() {
379 return 'users';
380 }
381 }