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