Fix yet another instance of comparing the result of User::newFromName to null
[lhc/web/wiklou.git] / includes / specials / SpecialEmailuser.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * Constructor for Special:Emailuser.
9 */
10 function wfSpecialEmailuser( $par ) {
11 global $wgRequest, $wgUser, $wgOut;
12
13 if ( !EmailUserForm::userEmailEnabled() ) {
14 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
15 return;
16 }
17
18 $action = $wgRequest->getVal( 'action' );
19 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
20 $targetUser = EmailUserForm::validateEmailTarget( $target );
21
22 if ( !( $targetUser instanceof User ) ) {
23 $wgOut->showErrorPage( $targetUser.'title', $targetUser.'text' );
24 return;
25 }
26
27 $form = new EmailUserForm( $targetUser,
28 $wgRequest->getText( 'wpText' ),
29 $wgRequest->getText( 'wpSubject' ),
30 $wgRequest->getBool( 'wpCCMe' ) );
31 if ( $action == 'success' ) {
32 $form->showSuccess();
33 return;
34 }
35
36 $error = EmailUserForm::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
37 if ( $error ) {
38 switch ( $error ) {
39 case 'blockedemailuser':
40 $wgOut->blockedPage();
41 return;
42 case 'actionthrottledtext':
43 $wgOut->rateLimited();
44 return;
45 case 'sessionfailure':
46 $form->showForm();
47 return;
48 case 'mailnologin':
49 $wgOut->showErrorPage( 'mailnologin', 'mailnologintext' );
50 return;
51 default:
52 // It's a hook error
53 list( $title, $msg, $params ) = $error;
54 $wgOut->showErrorPage( $title, $msg, $params );
55 return;
56
57 }
58 }
59
60 if ( "submit" == $action && $wgRequest->wasPosted() ) {
61 $result = $form->doSubmit();
62
63 if ( !is_null( $result ) ) {
64 $wgOut->addHTML( wfMsg( "usermailererror" ) .
65 ' ' . htmlspecialchars( $result->getMessage() ) );
66 } else {
67 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
68 $encTarget = wfUrlencode( $form->getTarget()->getName() );
69 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
70 }
71 } else {
72 $form->showForm();
73 }
74 }
75
76 /**
77 * Implements the Special:Emailuser web interface, and invokes
78 * UserMailer::send() for sending the email message.
79 *
80 * @ingroup SpecialPage
81 */
82 class EmailUserForm {
83
84 var $target;
85 var $text, $subject;
86 var $cc_me; // Whether user requested to be sent a separate copy of their email.
87
88 /**
89 * Constructor
90 *
91 * @param $target User object
92 * @param $text String: message contents
93 * @param $subject String: message subject
94 * @param $cc_me Boolean: wheter to send a copy of the message to the sender user
95 */
96 public function EmailUserForm( $target, $text, $subject, $cc_me ) {
97 $this->target = $target;
98 $this->text = $text;
99 $this->subject = $subject;
100 $this->cc_me = $cc_me;
101 }
102
103 /**
104 * Display the form to send a email
105 */
106 public function showForm() {
107 global $wgOut, $wgUser;
108 $skin = $wgUser->getSkin();
109
110 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
111 $wgOut->addWikiMsg( "emailpagetext" );
112
113 if ( $this->subject === "" ) {
114 $this->subject = wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) );
115 }
116
117 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
118 $action = $titleObj->getLocalURL( "target=" .
119 urlencode( $this->target->getName() ) . "&action=submit" );
120
121 $wgOut->addHTML(
122 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'emailuser' ) ) .
123 Xml::openElement( 'fieldset' ) .
124 Xml::element( 'legend', null, wfMsgExt( 'email-legend', 'parsemag' ) ) .
125 Xml::openElement( 'table', array( 'class' => 'mw-emailuser-table' ) ) .
126 "<tr>
127 <td class='mw-label'>" .
128 Xml::label( wfMsg( 'emailfrom' ), 'emailfrom' ) .
129 "</td>
130 <td class='mw-input' id='mw-emailuser-sender'>" .
131 $skin->link( $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) ) .
132 "</td>
133 </tr>
134 <tr>
135 <td class='mw-label'>" .
136 Xml::label( wfMsg( 'emailto' ), 'emailto' ) .
137 "</td>
138 <td class='mw-input' id='mw-emailuser-recipient'>" .
139 $skin->link( $this->target->getUserPage(), htmlspecialchars( $this->target->getName() ) ) .
140 "</td>
141 </tr>
142 <tr>
143 <td class='mw-label'>" .
144 Xml::label( wfMsg( 'emailsubject' ), 'wpSubject' ) .
145 "</td>
146 <td class='mw-input'>" .
147 Xml::input( 'wpSubject', 60, $this->subject, array( 'type' => 'text', 'maxlength' => 200 ) ) .
148 "</td>
149 </tr>
150 <tr>
151 <td class='mw-label'>" .
152 Xml::label( wfMsg( 'emailmessage' ), 'wpText' ) .
153 "</td>
154 <td class='mw-input'>" .
155 Xml::textarea( 'wpText', $this->text, 80, 20, array( 'id' => 'wpText' ) ) .
156 "</td>
157 </tr>
158 <tr>
159 <td></td>
160 <td class='mw-input'>" .
161 Xml::checkLabel( wfMsg( 'emailccme' ), 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) .
162 "</td>
163 </tr>
164 <tr>
165 <td></td>
166 <td class='mw-submit'>" .
167 Xml::submitButton( wfMsg( 'emailsend' ), array( 'name' => 'wpSend', 'accesskey' => 's' ) ) .
168 "</td>
169 </tr>" .
170 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
171 Xml::closeElement( 'table' ) .
172 Xml::closeElement( 'fieldset' ) .
173 Xml::closeElement( 'form' )
174 );
175 }
176
177 /**
178 * Really send a mail. Permissions should have been checked using
179 * EmailUserForm::getPermissionsError. It is probably also a good idea to
180 * check the edit token and ping limiter in advance.
181 *
182 * @return Mixed: WikiError on error or null
183 */
184 public function doSubmit() {
185 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
186
187 $to = new MailAddress( $this->target );
188 $from = new MailAddress( $wgUser );
189 $subject = $this->subject;
190
191 // Add a standard footer and trim up trailing newlines
192 $this->text = rtrim($this->text) . "\n\n-- \n" . wfMsgExt( 'emailuserfooter',
193 array( 'content', 'parsemag' ), array( $from->name, $to->name ) );
194
195 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
196
197 if( $wgUserEmailUseReplyTo ) {
198 // Put the generic wiki autogenerated address in the From:
199 // header and reserve the user for Reply-To.
200 //
201 // This is a bit ugly, but will serve to differentiate
202 // wiki-borne mails from direct mails and protects against
203 // SPF and bounce problems with some mailers (see below).
204 global $wgPasswordSender;
205 $mailFrom = new MailAddress( $wgPasswordSender );
206 $replyTo = $from;
207 } else {
208 // Put the sending user's e-mail address in the From: header.
209 //
210 // This is clean-looking and convenient, but has issues.
211 // One is that it doesn't as clearly differentiate the wiki mail
212 // from "directly" sent mails.
213 //
214 // Another is that some mailers (like sSMTP) will use the From
215 // address as the envelope sender as well. For open sites this
216 // can cause mails to be flunked for SPF violations (since the
217 // wiki server isn't an authorized sender for various users'
218 // domains) as well as creating a privacy issue as bounces
219 // containing the recipient's e-mail address may get sent to
220 // the sending user.
221 $mailFrom = $from;
222 $replyTo = null;
223 }
224
225 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
226
227 if( WikiError::isError( $mailResult ) ) {
228 return $mailResult;
229 } else {
230 // if the user requested a copy of this mail, do this now,
231 // unless they are emailing themselves, in which case one copy of the message is sufficient.
232 if ($this->cc_me && $to != $from) {
233 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
234 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
235 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
236 if( WikiError::isError( $ccResult ) ) {
237 // At this stage, the user's CC mail has failed, but their
238 // original mail has succeeded. It's unlikely, but still, what to do?
239 // We can either show them an error, or we can say everything was fine,
240 // or we can say we sort of failed AND sort of succeeded. Of these options,
241 // simply saying there was an error is probably best.
242 return $ccResult;
243 }
244 }
245 }
246
247 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
248 return;
249 }
250 }
251 }
252
253 /**
254 * Show "Your e-mail message has been sent." message
255 */
256 public function showSuccess( &$user = null ) {
257 global $wgOut;
258
259 if ( is_null($user) )
260 $user = $this->target;
261
262 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
263 $wgOut->addWikiMsg( 'emailsenttext' );
264
265 $wgOut->returnToMain( false, $user->getUserPage() );
266 }
267
268 /**
269 * Get target user
270 *
271 * @return User object
272 */
273 public function getTarget() {
274 return $this->target;
275 }
276
277 /**
278 * Check whether user-to-user emails are enabled
279 *
280 * @return Boolean
281 */
282 public static function userEmailEnabled() {
283 global $wgEnableEmail, $wgEnableUserEmail;
284 return $wgEnableEmail && $wgEnableUserEmail;
285 }
286
287 /**
288 * Validate target User
289 *
290 * @param $target String: target user name
291 * @return User object on success or a string on error
292 */
293 public static function validateEmailTarget( $target ) {
294 if ( $target == "" ) {
295 wfDebug( "Target is empty.\n" );
296 return "notarget";
297 }
298
299 $nt = Title::newFromURL( $target );
300 if ( is_null( $nt ) ) {
301 wfDebug( "Target is invalid title.\n" );
302 return "notarget";
303 }
304
305 $nu = User::newFromName( $nt->getText() );
306 if( !$nu instanceof Title || !$nu->getId() ) {
307 wfDebug( "Target is invalid user.\n" );
308 return "notarget";
309 } else if ( !$nu->isEmailConfirmed() ) {
310 wfDebug( "User has no valid email.\n" );
311 return "noemail";
312 } else if ( !$nu->canReceiveEmail() ) {
313 wfDebug( "User does not allow user emails.\n" );
314 return "nowikiemail";
315 }
316
317 return $nu;
318 }
319
320 /**
321 * Check whether user is allowed to send email
322 *
323 * @param $user User object
324 * @param $editToken String: edit token
325 * @return null on success or string on error
326 */
327 public static function getPermissionsError( $user, $editToken ) {
328 if( !$user->canSendEmail() ) {
329 wfDebug( "User can't send.\n" );
330 // FIXME: this is also the error if user is in a group
331 // that is not allowed to send e-mail (no right
332 // 'sendemail'). Error messages should probably
333 // be more fine grained.
334 return "mailnologin";
335 }
336
337 if( $user->isBlockedFromEmailuser() ) {
338 wfDebug( "User is blocked from sending e-mail.\n" );
339 return "blockedemailuser";
340 }
341
342 if( $user->pingLimiter( 'emailuser' ) ) {
343 wfDebug( "Ping limiter triggered.\n" );
344 return 'actionthrottledtext';
345 }
346
347 $hookErr = null;
348 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
349
350 if ($hookErr) {
351 return $hookErr;
352 }
353
354 if( !$user->matchEditToken( $editToken ) ) {
355 wfDebug( "Matching edit token failed.\n" );
356 return 'sessionfailure';
357 }
358 }
359
360 /**
361 * Get a EmailUserForm object
362 *
363 * @param $target String: user name
364 * @param $text String: message contents
365 * @param $subject String: message subject
366 * @param $cc_me Boolean: wheter to send a copy of the message to the sender user
367 * @return EmailUserForm object
368 */
369 public static function newFromURL( $target, $text, $subject, $cc_me ) {
370 $nt = Title::newFromURL( $target );
371 $nu = User::newFromName( $nt->getText() );
372 return new EmailUserForm( $nu, $text, $subject, $cc_me );
373 }
374 }