Check on $wgEnableEmail && $wgEnableUserEmail
[lhc/web/wiklou.git] / includes / specials / 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 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 $emf = wfMsg( "emailfrom" );
102 $senderLink = $skin->makeLinkObj(
103 $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) );
104 $emt = wfMsg( "emailto" );
105 $recipientLink = $skin->makeLinkObj(
106 $this->target->getUserPage(), htmlspecialchars( $this->target->getName() ) );
107 $emr = wfMsg( "emailsubject" );
108 $emm = wfMsg( "emailmessage" );
109 $ems = wfMsg( "emailsend" );
110 $emc = wfMsg( "emailccme" );
111 $encSubject = htmlspecialchars( $this->subject );
112
113 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
114 $action = $titleObj->escapeLocalURL( "target=" .
115 urlencode( $this->target->getName() ) . "&action=submit" );
116 $token = htmlspecialchars( $wgUser->editToken() );
117
118 $wgOut->addHTML( "
119 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
120 <table border='0' id='mailheader'><tr>
121 <td align='right'>{$emf}:</td>
122 <td align='left'><strong>{$senderLink}</strong></td>
123 </tr><tr>
124 <td align='right'>{$emt}:</td>
125 <td align='left'><strong>{$recipientLink}</strong></td>
126 </tr><tr>
127 <td align='right'>{$emr}:</td>
128 <td align='left'>
129 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
130 </td>
131 </tr>
132 </table>
133 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
134 <textarea id=\"wpText\" name=\"wpText\" rows='20' cols='80' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
135 "</textarea>
136 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
137 <input type='submit' name=\"wpSend\" value=\"{$ems}\" accesskey=\"s\" />
138 <input type='hidden' name='wpEditToken' value=\"$token\" />
139 </form>\n" );
140
141 }
142
143 /*
144 * Really send a mail. Permissions should have been checked using
145 * EmailUserForm::getPermissionsError. It is probably also a good idea to
146 * check the edit token and ping limiter in advance.
147 */
148 function doSubmit() {
149 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
150
151 $to = new MailAddress( $this->target );
152 $from = new MailAddress( $wgUser );
153 $subject = $this->subject;
154
155 // Add a standard footer and trim up trailing newlines
156 $this->text = rtrim($this->text) . "\n\n---\n" . wfMsgExt( 'emailuserfooter',
157 array( 'content', 'parsemag' ), array( $from->name, $to->name ) );
158
159 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
160
161 if( $wgUserEmailUseReplyTo ) {
162 // Put the generic wiki autogenerated address in the From:
163 // header and reserve the user for Reply-To.
164 //
165 // This is a bit ugly, but will serve to differentiate
166 // wiki-borne mails from direct mails and protects against
167 // SPF and bounce problems with some mailers (see below).
168 global $wgPasswordSender;
169 $mailFrom = new MailAddress( $wgPasswordSender );
170 $replyTo = $from;
171 } else {
172 // Put the sending user's e-mail address in the From: header.
173 //
174 // This is clean-looking and convenient, but has issues.
175 // One is that it doesn't as clearly differentiate the wiki mail
176 // from "directly" sent mails.
177 //
178 // Another is that some mailers (like sSMTP) will use the From
179 // address as the envelope sender as well. For open sites this
180 // can cause mails to be flunked for SPF violations (since the
181 // wiki server isn't an authorized sender for various users'
182 // domains) as well as creating a privacy issue as bounces
183 // containing the recipient's e-mail address may get sent to
184 // the sending user.
185 $mailFrom = $from;
186 $replyTo = null;
187 }
188
189 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
190
191 if( WikiError::isError( $mailResult ) ) {
192 return $mailResult;
193
194 } else {
195
196 // if the user requested a copy of this mail, do this now,
197 // unless they are emailing themselves, in which case one copy of the message is sufficient.
198 if ($this->cc_me && $to != $from) {
199 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
200 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
201 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
202 if( WikiError::isError( $ccResult ) ) {
203 // At this stage, the user's CC mail has failed, but their
204 // original mail has succeeded. It's unlikely, but still, what to do?
205 // We can either show them an error, or we can say everything was fine,
206 // or we can say we sort of failed AND sort of succeeded. Of these options,
207 // simply saying there was an error is probably best.
208 return $ccResult;
209 }
210 }
211 }
212
213 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
214 return;
215 }
216 }
217 }
218
219 function showSuccess( &$user = null ) {
220 global $wgOut;
221
222 if ( is_null($user) )
223 $user = $this->target;
224
225 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
226 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
227
228 $wgOut->returnToMain( false, $user->getUserPage() );
229 }
230
231 function getTarget() {
232 return $this->target;
233 }
234
235 static function userEmailEnabled() {
236 global $wgEnableEmail, $wgEnableUserEmail;
237 return $wgEnableEmail && $wgEnableUserEmail;
238
239 }
240 static function validateEmailTarget ( $target ) {
241 if ( "" == $target ) {
242 wfDebug( "Target is empty.\n" );
243 return "notarget";
244 }
245
246 $nt = Title::newFromURL( $target );
247 if ( is_null( $nt ) ) {
248 wfDebug( "Target is invalid title.\n" );
249 return "notarget";
250 }
251
252 $nu = User::newFromName( $nt->getText() );
253 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
254 wfDebug( "Target is invalid user or can't receive.\n" );
255 return "noemail";
256 }
257
258 return $nu;
259 }
260 static function getPermissionsError ( $user, $editToken ) {
261 if( !$user->canSendEmail() ) {
262 wfDebug( "User can't send.\n" );
263 return "mailnologin";
264 }
265
266 if( $user->isBlockedFromEmailuser() ) {
267 wfDebug( "User is blocked from sending e-mail.\n" );
268 return "blockedemailuser";
269 }
270
271 if( $user->pingLimiter( 'emailuser' ) ) {
272 wfDebug( "Ping limiter triggered.\n" );
273 return 'actionthrottledtext';
274 }
275
276 if( !$user->matchEditToken( $editToken ) ) {
277 wfDebug( "Matching edit token failed.\n" );
278 return 'sessionfailure';
279 }
280
281 return;
282 }
283
284 static function newFromURL( $target, $text, $subject, $cc_me )
285 {
286 $nt = Title::newFromURL( $target );
287 $nu = User::newFromName( $nt->getText() );
288 return new EmailUserForm( $nu, $text, $subject, $cc_me );
289 }
290 }