(bug 14558) New system message (emailuserfooter) is now added to the footer of e...
[lhc/web/wiklou.git] / includes / specials / Emailuser.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, $wgSiteName;
146
147 $to = new MailAddress( $this->target );
148 $from = new MailAddress( $wgUser );
149 $subject = $this->subject;
150
151 $prefsTitle = Title::newFromText( 'Preferences', NS_SPECIAL );
152
153 // Add a standard footer
154 $this->text = $this->text . "\n" . wfMsg( 'emailuserfooter', array( $wgSitename, $prefsTitle->getFullURL() ) );
155
156 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
157
158 if( $wgUserEmailUseReplyTo ) {
159 // Put the generic wiki autogenerated address in the From:
160 // header and reserve the user for Reply-To.
161 //
162 // This is a bit ugly, but will serve to differentiate
163 // wiki-borne mails from direct mails and protects against
164 // SPF and bounce problems with some mailers (see below).
165 global $wgPasswordSender;
166 $mailFrom = new MailAddress( $wgPasswordSender );
167 $replyTo = $from;
168 } else {
169 // Put the sending user's e-mail address in the From: header.
170 //
171 // This is clean-looking and convenient, but has issues.
172 // One is that it doesn't as clearly differentiate the wiki mail
173 // from "directly" sent mails.
174 //
175 // Another is that some mailers (like sSMTP) will use the From
176 // address as the envelope sender as well. For open sites this
177 // can cause mails to be flunked for SPF violations (since the
178 // wiki server isn't an authorized sender for various users'
179 // domains) as well as creating a privacy issue as bounces
180 // containing the recipient's e-mail address may get sent to
181 // the sending user.
182 $mailFrom = $from;
183 $replyTo = null;
184 }
185
186 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
187
188 if( WikiError::isError( $mailResult ) ) {
189 return $mailResult;
190
191 } else {
192
193 // if the user requested a copy of this mail, do this now,
194 // unless they are emailing themselves, in which case one copy of the message is sufficient.
195 if ($this->cc_me && $to != $from) {
196 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
197 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
198 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
199 if( WikiError::isError( $ccResult ) ) {
200 // At this stage, the user's CC mail has failed, but their
201 // original mail has succeeded. It's unlikely, but still, what to do?
202 // We can either show them an error, or we can say everything was fine,
203 // or we can say we sort of failed AND sort of succeeded. Of these options,
204 // simply saying there was an error is probably best.
205 return $ccResult;
206 }
207 }
208 }
209
210 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
211 return;
212 }
213 }
214 }
215
216 function showSuccess( &$user = null ) {
217 global $wgOut;
218
219 if ( is_null($user) )
220 $user = $this->target;
221
222 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
223 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
224
225 $wgOut->returnToMain( false, $user->getUserPage() );
226 }
227
228 function getTarget() {
229 return $this->target;
230 }
231
232 static function validateEmailTarget ( $target ) {
233 global $wgEnableEmail, $wgEnableUserEmail;
234
235 if( !( $wgEnableEmail && $wgEnableUserEmail ) )
236 return array( "nosuchspecialpage", "nospecialpagetext" );
237
238 if ( "" == $target ) {
239 wfDebug( "Target is empty.\n" );
240 return array( "notargettitle", "notargettext" );
241 }
242
243 $nt = Title::newFromURL( $target );
244 if ( is_null( $nt ) ) {
245 wfDebug( "Target is invalid title.\n" );
246 return array( "notargettitle", "notargettext" );
247 }
248
249 $nu = User::newFromName( $nt->getText() );
250 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
251 wfDebug( "Target is invalid user or can't receive.\n" );
252 return array( "noemailtitle", "noemailtext" );
253 }
254
255 return $nu;
256 }
257 static function getPermissionsError ( $user, $editToken ) {
258 if( !$user->canSendEmail() ) {
259 wfDebug( "User can't send.\n" );
260 return array( "mailnologin", "mailnologintext" );
261 }
262
263 if( $user->isBlockedFromEmailuser() ) {
264 wfDebug( "User is blocked from sending e-mail.\n" );
265 return array( "blockedemailuser", "" );
266 }
267
268 if( $user->pingLimiter( 'emailuser' ) ) {
269 wfDebug( "Ping limiter triggered.\n" );
270 return array( 'actionthrottledtext', '' );
271 }
272
273 if( !$user->matchEditToken( $editToken ) ) {
274 wfDebug( "Matching edit token failed.\n" );
275 return array( 'sessionfailure', '' );
276 }
277
278 return;
279 }
280
281 static function newFromURL( $target, $text, $subject, $cc_me )
282 {
283 $nt = Title::newFromURL( $target );
284 $nu = User::newFromName( $nt->getText() );
285 return new EmailUserForm( $nu, $text, $subject, $cc_me );
286 }
287 }