c06d7a5a18d533e03b0e820a1789185ea94c7eea
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * @todo document
9 */
10 function wfSpecialEmailuser( $par ) {
11 global $wgRequest, $wgUser, $wgOut;
12
13 $action = $wgRequest->getVal( 'action' );
14 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
15 $targetUser = EmailUserForm::validateEmailTarget( $target );
16
17 if ( !( $targetUser instanceof User ) ) {
18 $wgOut->showErrorPage( $targetUser[0], $targetUser[1] );
19 return;
20 }
21
22 $form = new EmailUserForm( $targetUser,
23 $wgRequest->getText( 'wpText' ),
24 $wgRequest->getText( 'wpSubject' ),
25 $wgRequest->getBool( 'wpCCMe' ) );
26 if ( $action == 'success' ) {
27 $form->showSuccess();
28 return;
29 }
30
31 $error = EmailUserForm::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
32 if ( $error ) {
33 switch ( $error[0] ) {
34 case 'blockedemailuser':
35 $wgOut->blockedPage();
36 return;
37 case 'actionthrottledtext':
38 $wgOut->rateLimited();
39 return;
40 case 'sessionfailure':
41 $form->showForm();
42 return;
43 default:
44 $wgOut->showErrorPage( $error[0], $error[1] );
45 return;
46 }
47 }
48
49
50 if ( "submit" == $action && $wgRequest->wasPosted() ) {
51 $result = $form->doSubmit();
52
53 if ( !is_null( $result ) ) {
54 $wgOut->addHTML( wfMsg( "usermailererror" ) .
55 ' ' . htmlspecialchars( $result->getMessage() ) );
56 } else {
57 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
58 $encTarget = wfUrlencode( $form->getTarget()->getName() );
59 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
60 }
61 } else {
62 $form->showForm();
63 }
64 }
65
66 /**
67 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
68 * @ingroup SpecialPage
69 */
70 class EmailUserForm {
71
72 var $target;
73 var $text, $subject;
74 var $cc_me; // Whether user requested to be sent a separate copy of their email.
75
76 /**
77 * @param User $target
78 */
79 function EmailUserForm( $target, $text, $subject, $cc_me ) {
80 $this->target = $target;
81 $this->text = $text;
82 $this->subject = $subject;
83 $this->cc_me = $cc_me;
84 }
85
86 function showForm() {
87 global $wgOut, $wgUser;
88 $skin = $wgUser->getSkin();
89
90 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
91 $wgOut->addWikiMsg( "emailpagetext" );
92
93 if ( $this->subject === "" ) {
94 $this->subject = wfMsgForContent( "defemailsubject" );
95 }
96
97 $emf = wfMsg( "emailfrom" );
98 $senderLink = $skin->makeLinkObj(
99 $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) );
100 $emt = wfMsg( "emailto" );
101 $recipientLink = $skin->makeLinkObj(
102 $this->target->getUserPage(), htmlspecialchars( $this->target->getName() ) );
103 $emr = wfMsg( "emailsubject" );
104 $emm = wfMsg( "emailmessage" );
105 $ems = wfMsg( "emailsend" );
106 $emc = wfMsg( "emailccme" );
107 $encSubject = htmlspecialchars( $this->subject );
108
109 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
110 $action = $titleObj->escapeLocalURL( "target=" .
111 urlencode( $this->target->getName() ) . "&action=submit" );
112 $token = htmlspecialchars( $wgUser->editToken() );
113
114 $wgOut->addHTML( "
115 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
116 <table border='0' id='mailheader'><tr>
117 <td align='right'>{$emf}:</td>
118 <td align='left'><strong>{$senderLink}</strong></td>
119 </tr><tr>
120 <td align='right'>{$emt}:</td>
121 <td align='left'><strong>{$recipientLink}</strong></td>
122 </tr><tr>
123 <td align='right'>{$emr}:</td>
124 <td align='left'>
125 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
126 </td>
127 </tr>
128 </table>
129 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
130 <textarea id=\"wpText\" name=\"wpText\" rows='20' cols='80' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
131 "</textarea>
132 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
133 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
134 <input type='hidden' name='wpEditToken' value=\"$token\" />
135 </form>\n" );
136
137 }
138
139 /*
140 * Really send a mail. Permissions should have been checked using
141 * EmailUserForm::getPermissionsError. It is probably also a good idea to
142 * check the edit token and ping limiter in advance.
143 */
144 function doSubmit() {
145 global $wgUser, $wgUserEmailUseReplyTo;
146
147 $to = new MailAddress( $this->target );
148 $from = new MailAddress( $wgUser );
149 $subject = $this->subject;
150
151 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
152
153 if( $wgUserEmailUseReplyTo ) {
154 // Put the generic wiki autogenerated address in the From:
155 // header and reserve the user for Reply-To.
156 //
157 // This is a bit ugly, but will serve to differentiate
158 // wiki-borne mails from direct mails and protects against
159 // SPF and bounce problems with some mailers (see below).
160 global $wgPasswordSender;
161 $mailFrom = new MailAddress( $wgPasswordSender );
162 $replyTo = $from;
163 } else {
164 // Put the sending user's e-mail address in the From: header.
165 //
166 // This is clean-looking and convenient, but has issues.
167 // One is that it doesn't as clearly differentiate the wiki mail
168 // from "directly" sent mails.
169 //
170 // Another is that some mailers (like sSMTP) will use the From
171 // address as the envelope sender as well. For open sites this
172 // can cause mails to be flunked for SPF violations (since the
173 // wiki server isn't an authorized sender for various users'
174 // domains) as well as creating a privacy issue as bounces
175 // containing the recipient's e-mail address may get sent to
176 // the sending user.
177 $mailFrom = $from;
178 $replyTo = null;
179 }
180
181 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
182
183 if( WikiError::isError( $mailResult ) ) {
184 return $mailResult;
185
186 } else {
187
188 // if the user requested a copy of this mail, do this now,
189 // unless they are emailing themselves, in which case one copy of the message is sufficient.
190 if ($this->cc_me && $to != $from) {
191 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
192 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
193 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
194 if( WikiError::isError( $ccResult ) ) {
195 // At this stage, the user's CC mail has failed, but their
196 // original mail has succeeded. It's unlikely, but still, what to do?
197 // We can either show them an error, or we can say everything was fine,
198 // or we can say we sort of failed AND sort of succeeded. Of these options,
199 // simply saying there was an error is probably best.
200 return $ccResult;
201 }
202 }
203 }
204
205 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
206 return;
207 }
208 }
209 }
210
211 function showSuccess( &$user = null ) {
212 global $wgOut;
213
214 if ( is_null($user) )
215 $user = $this->target;
216
217 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
218 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
219
220 $wgOut->returnToMain( false, $user->getUserPage() );
221 }
222
223 function getTarget() {
224 return $this->target;
225 }
226
227 static function validateEmailTarget ( $target ) {
228 global $wgEnableEmail, $wgEnableUserEmail;
229
230 if( !( $wgEnableEmail && $wgEnableUserEmail ) )
231 return array( "nosuchspecialpage", "nospecialpagetext" );
232
233 if ( "" == $target ) {
234 wfDebug( "Target is empty.\n" );
235 return array( "notargettitle", "notargettext" );
236 }
237
238 $nt = Title::newFromURL( $target );
239 if ( is_null( $nt ) ) {
240 wfDebug( "Target is invalid title.\n" );
241 return array( "notargettitle", "notargettext" );
242 }
243
244 $nu = User::newFromName( $nt->getText() );
245 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
246 wfDebug( "Target is invalid user or can't receive.\n" );
247 return array( "noemailtitle", "noemailtext" );
248 }
249
250 return $nu;
251 }
252 static function getPermissionsError ( $user, $editToken ) {
253 if( !$user->canSendEmail() ) {
254 wfDebug( "User can't send.\n" );
255 return array( "mailnologin", "mailnologintext" );
256 }
257
258 if( $user->isBlockedFromEmailuser() ) {
259 wfDebug( "User is blocked from sending e-mail.\n" );
260 return array( "blockedemailuser", "" );
261 }
262
263 if( $user->pingLimiter( 'emailuser' ) ) {
264 wfDebug( "Ping limiter triggered.\n" );
265 return array( 'actionthrottledtext', '' );
266 }
267
268 if( !$user->matchEditToken( $editToken ) ) {
269 wfDebug( "Matching edit token failed.\n" );
270 return array( 'sessionfailure', '' );
271 }
272
273 return;
274 }
275
276 static function newFromURL( $target, $text, $subject, $cc_me )
277 {
278 $nt = Title::newFromURL( $target );
279 $nu = User::newFromName( $nt->getText() );
280 return new EmailUserForm( $nu, $text, $subject, $cc_me );
281 }
282 }