58e2514e0882b8c61e632491d2bb29f975965719
[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 }
52 }
53
54 if ( "submit" == $action && $wgRequest->wasPosted() ) {
55 $result = $form->doSubmit();
56
57 if ( !is_null( $result ) ) {
58 $wgOut->addHTML( wfMsg( "usermailererror" ) .
59 ' ' . htmlspecialchars( $result->getMessage() ) );
60 } else {
61 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
62 $encTarget = wfUrlencode( $form->getTarget()->getName() );
63 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
64 }
65 } else {
66 $form->showForm();
67 }
68 }
69
70 /**
71 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
72 * @ingroup SpecialPage
73 */
74 class EmailUserForm {
75
76 var $target;
77 var $text, $subject;
78 var $cc_me; // Whether user requested to be sent a separate copy of their email.
79
80 /**
81 * @param User $target
82 */
83 function EmailUserForm( $target, $text, $subject, $cc_me ) {
84 $this->target = $target;
85 $this->text = $text;
86 $this->subject = $subject;
87 $this->cc_me = $cc_me;
88 }
89
90 function showForm() {
91 global $wgOut, $wgUser;
92 $skin = $wgUser->getSkin();
93
94 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
95 $wgOut->addWikiMsg( "emailpagetext" );
96
97 if ( $this->subject === "" ) {
98 $this->subject = wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) );
99 }
100
101 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
102 $action = $titleObj->getLocalURL( "target=" .
103 urlencode( $this->target->getName() ) . "&action=submit" );
104
105 $wgOut->addHTML(
106 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'emailuser' ) ) .
107 Xml::openElement( 'fieldset' ) .
108 Xml::element( 'legend', null, wfMsgExt( 'email-legend', 'parsemag' ) ) .
109 Xml::openElement( 'table', array( 'class' => 'mw-emailuser-table' ) ) .
110 "<tr>
111 <td class='mw-label'>" .
112 Xml::label( wfMsg( 'emailfrom' ), 'emailfrom' ) .
113 "</td>
114 <td class='mw-input' id='mw-emailuser-sender'>" .
115 $skin->link( $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) ) .
116 "</td>
117 </tr>
118 <tr>
119 <td class='mw-label'>" .
120 Xml::label( wfMsg( 'emailto' ), 'emailto' ) .
121 "</td>
122 <td class='mw-input' id='mw-emailuser-recipient'>" .
123 $skin->link( $this->target->getUserPage(), htmlspecialchars( $this->target->getName() ) ) .
124 "</td>
125 </tr>
126 <tr>
127 <td class='mw-label'>" .
128 Xml::label( wfMsg( 'emailsubject' ), 'wpSubject' ) .
129 "</td>
130 <td class='mw-input'>" .
131 Xml::input( 'wpSubject', 60, $this->subject, array( 'type' => 'text', 'maxlength' => 200 ) ) .
132 "</td>
133 </tr>
134 <tr>
135 <td class='mw-label'>" .
136 Xml::label( wfMsg( 'emailmessage' ), 'wpText' ) .
137 "</td>
138 <td class='mw-input'>" .
139 Xml::textarea( 'wpText', $this->text, 80, 20, array( 'id' => 'wpText' ) ) .
140 "</td>
141 </tr>
142 <tr>
143 <td></td>
144 <td class='mw-input'>" .
145 Xml::checkLabel( wfMsg( 'emailccme' ), 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) .
146 "</td>
147 </tr>
148 <tr>
149 <td></td>
150 <td class='mw-submit'>" .
151 Xml::submitButton( wfMsg( 'emailsend' ), array( 'name' => 'wpSend', 'accesskey' => 's' ) ) .
152 "</td>
153 </tr>" .
154 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
155 Xml::closeElement( 'table' ) .
156 Xml::closeElement( 'fieldset' ) .
157 Xml::closeElement( 'form' )
158 );
159 }
160
161 /*
162 * Really send a mail. Permissions should have been checked using
163 * EmailUserForm::getPermissionsError. It is probably also a good idea to
164 * check the edit token and ping limiter in advance.
165 */
166 function doSubmit() {
167 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
168
169 $to = new MailAddress( $this->target );
170 $from = new MailAddress( $wgUser );
171 $subject = $this->subject;
172
173 // Add a standard footer and trim up trailing newlines
174 $this->text = rtrim($this->text) . "\n\n-- \n" . wfMsgExt( 'emailuserfooter',
175 array( 'content', 'parsemag' ), array( $from->name, $to->name ) );
176
177 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
178
179 if( $wgUserEmailUseReplyTo ) {
180 // Put the generic wiki autogenerated address in the From:
181 // header and reserve the user for Reply-To.
182 //
183 // This is a bit ugly, but will serve to differentiate
184 // wiki-borne mails from direct mails and protects against
185 // SPF and bounce problems with some mailers (see below).
186 global $wgPasswordSender;
187 $mailFrom = new MailAddress( $wgPasswordSender );
188 $replyTo = $from;
189 } else {
190 // Put the sending user's e-mail address in the From: header.
191 //
192 // This is clean-looking and convenient, but has issues.
193 // One is that it doesn't as clearly differentiate the wiki mail
194 // from "directly" sent mails.
195 //
196 // Another is that some mailers (like sSMTP) will use the From
197 // address as the envelope sender as well. For open sites this
198 // can cause mails to be flunked for SPF violations (since the
199 // wiki server isn't an authorized sender for various users'
200 // domains) as well as creating a privacy issue as bounces
201 // containing the recipient's e-mail address may get sent to
202 // the sending user.
203 $mailFrom = $from;
204 $replyTo = null;
205 }
206
207 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
208
209 if( WikiError::isError( $mailResult ) ) {
210 return $mailResult;
211
212 } else {
213
214 // if the user requested a copy of this mail, do this now,
215 // unless they are emailing themselves, in which case one copy of the message is sufficient.
216 if ($this->cc_me && $to != $from) {
217 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
218 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
219 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
220 if( WikiError::isError( $ccResult ) ) {
221 // At this stage, the user's CC mail has failed, but their
222 // original mail has succeeded. It's unlikely, but still, what to do?
223 // We can either show them an error, or we can say everything was fine,
224 // or we can say we sort of failed AND sort of succeeded. Of these options,
225 // simply saying there was an error is probably best.
226 return $ccResult;
227 }
228 }
229 }
230
231 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
232 return;
233 }
234 }
235 }
236
237 function showSuccess( &$user = null ) {
238 global $wgOut;
239
240 if ( is_null($user) )
241 $user = $this->target;
242
243 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
244 $wgOut->addWikiMsg( 'emailsenttext' );
245
246 $wgOut->returnToMain( false, $user->getUserPage() );
247 }
248
249 function getTarget() {
250 return $this->target;
251 }
252
253 static function userEmailEnabled() {
254 global $wgEnableEmail, $wgEnableUserEmail;
255 return $wgEnableEmail && $wgEnableUserEmail;
256
257 }
258 static function validateEmailTarget ( $target ) {
259 if ( "" == $target ) {
260 wfDebug( "Target is empty.\n" );
261 return "notarget";
262 }
263
264 $nt = Title::newFromURL( $target );
265 if ( is_null( $nt ) ) {
266 wfDebug( "Target is invalid title.\n" );
267 return "notarget";
268 }
269
270 $nu = User::newFromName( $nt->getText() );
271 if( is_null( $nu ) || !$nu->getId() ) {
272 wfDebug( "Target is invalid user.\n" );
273 return "notarget";
274 } else if ( !$nu->isEmailConfirmed() ) {
275 wfDebug( "User has no valid email.\n" );
276 return "noemail";
277 } else if ( !$nu->canReceiveEmail() ) {
278 wfDebug( "User does not allow user emails.\n" );
279 return "nowikiemail";
280 }
281
282 return $nu;
283 }
284 static function getPermissionsError ( $user, $editToken ) {
285 if( !$user->canSendEmail() ) {
286 wfDebug( "User can't send.\n" );
287 return "mailnologin";
288 }
289
290 if( $user->isBlockedFromEmailuser() ) {
291 wfDebug( "User is blocked from sending e-mail.\n" );
292 return "blockedemailuser";
293 }
294
295 if( $user->pingLimiter( 'emailuser' ) ) {
296 wfDebug( "Ping limiter triggered.\n" );
297 return 'actionthrottledtext';
298 }
299
300 if( !$user->matchEditToken( $editToken ) ) {
301 wfDebug( "Matching edit token failed.\n" );
302 return 'sessionfailure';
303 }
304
305 return;
306 }
307
308 static function newFromURL( $target, $text, $subject, $cc_me )
309 {
310 $nt = Title::newFromURL( $target );
311 $nu = User::newFromName( $nt->getText() );
312 return new EmailUserForm( $nu, $text, $subject, $cc_me );
313 }
314 }