* (bug 12655) Added $wgUserEmailUseReplyTo config option to put sender
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 require_once('UserMailer.php');
8
9 /**
10 * @todo document
11 */
12 function wfSpecialEmailuser( $par ) {
13 global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
14
15 if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
16 $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
17 return;
18 }
19
20 if( !$wgUser->canSendEmail() ) {
21 wfDebug( "User can't send.\n" );
22 $wgOut->showErrorPage( "mailnologin", "mailnologintext" );
23 return;
24 }
25
26 $action = $wgRequest->getVal( 'action' );
27 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
28 if ( "" == $target ) {
29 wfDebug( "Target is empty.\n" );
30 $wgOut->showErrorPage( "notargettitle", "notargettext" );
31 return;
32 }
33
34 $nt = Title::newFromURL( $target );
35 if ( is_null( $nt ) ) {
36 wfDebug( "Target is invalid title.\n" );
37 $wgOut->showErrorPage( "notargettitle", "notargettext" );
38 return;
39 }
40
41 $nu = User::newFromName( $nt->getText() );
42 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
43 wfDebug( "Target is invalid user or can't receive.\n" );
44 $wgOut->showErrorPage( "noemailtitle", "noemailtext" );
45 return;
46 }
47
48 if ( $wgUser->isBlockedFromEmailUser() ) {
49 // User has been blocked from sending e-mail. Show the std blocked form.
50 wfDebug( "User is blocked from sending e-mail.\n" );
51 $wgOut->blockedPage();
52 return;
53 }
54
55 $f = new EmailUserForm( $nu );
56
57 if ( "success" == $action ) {
58 $f->showSuccess( $nu );
59 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
60 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
61 {
62 # Check against the rate limiter
63 if( $wgUser->pingLimiter( 'emailuser' ) ) {
64 $wgOut->rateLimited();
65 return;
66 }
67
68 $f->doSubmit();
69 } else {
70 $f->showForm();
71 }
72 }
73
74 /**
75 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
76 * @addtogroup SpecialPage
77 */
78 class EmailUserForm {
79
80 var $target;
81 var $text, $subject;
82 var $cc_me; // Whether user requested to be sent a separate copy of their email.
83
84 /**
85 * @param User $target
86 */
87 function EmailUserForm( $target ) {
88 global $wgRequest;
89 $this->target = $target;
90 $this->text = $wgRequest->getText( 'wpText' );
91 $this->subject = $wgRequest->getText( 'wpSubject' );
92 $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
93 }
94
95 function showForm() {
96 global $wgOut, $wgUser;
97
98 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
99 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
100
101 if ( $this->subject === "" ) {
102 $this->subject = wfMsg( "defemailsubject" );
103 }
104
105 $emf = wfMsg( "emailfrom" );
106 $sender = $wgUser->getName();
107 $emt = wfMsg( "emailto" );
108 $rcpt = $this->target->getName();
109 $emr = wfMsg( "emailsubject" );
110 $emm = wfMsg( "emailmessage" );
111 $ems = wfMsg( "emailsend" );
112 $emc = wfMsg( "emailccme" );
113 $encSubject = htmlspecialchars( $this->subject );
114
115 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
116 $action = $titleObj->escapeLocalURL( "target=" .
117 urlencode( $this->target->getName() ) . "&action=submit" );
118 $token = htmlspecialchars( $wgUser->editToken() );
119
120 $wgOut->addHTML( "
121 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
122 <table border='0' id='mailheader'><tr>
123 <td align='right'>{$emf}:</td>
124 <td align='left'><strong>" . htmlspecialchars( $sender ) . "</strong></td>
125 </tr><tr>
126 <td align='right'>{$emt}:</td>
127 <td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</strong></td>
128 </tr><tr>
129 <td align='right'>{$emr}:</td>
130 <td align='left'>
131 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
132 </td>
133 </tr>
134 </table>
135 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
136 <textarea id=\"wpText\" name=\"wpText\" rows='20' cols='80' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
137 "</textarea>
138 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
139 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
140 <input type='hidden' name='wpEditToken' value=\"$token\" />
141 </form>\n" );
142
143 }
144
145 function doSubmit() {
146 global $wgOut, $wgUser, $wgUserEmailUseReplyTo;
147
148 $to = new MailAddress( $this->target );
149 $from = new MailAddress( $wgUser );
150 $subject = $this->subject;
151
152 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
153
154 if( $wgUserEmailUseReplyTo ) {
155 // Put the generic wiki autogenerated address in the From:
156 // header and reserve the user for Reply-To.
157 //
158 // This is a bit ugly, but will serve to differentiate
159 // wiki-borne mails from direct mails and protects against
160 // SPF and bounce problems with some mailers (see below).
161 global $wgEmergencyContact;
162 $mailFrom = new MailAddress( $wgEmergencyContact );
163 $replyTo = $from;
164 } else {
165 // Put the sending user's e-mail address in the From: header.
166 //
167 // This is clean-looking and convenient, but has issues.
168 // One is that it doesn't as clearly differentiate the wiki mail
169 // from "directly" sent mails.
170 //
171 // Another is that some mailers (like sSMTP) will use the From
172 // address as the envelope sender as well. For open sites this
173 // can cause mails to be flunked for SPF violations (since the
174 // wiki server isn't an authorized sender for various users'
175 // domains) as well as creating a privacy issue as bounces
176 // containing the recipient's e-mail address may get sent to
177 // the sending user.
178 $mailFrom = $from;
179 $replyTo = null;
180 }
181
182 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
183
184 if( WikiError::isError( $mailResult ) ) {
185 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
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( $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 $wgOut->addHTML( wfMsg( "usermailererror" ) . $ccResult);
201 return;
202 }
203 }
204 }
205
206 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
207 $encTarget = wfUrlencode( $this->target->getName() );
208 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
209 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
210 }
211 }
212 }
213
214 function showSuccess( &$user ) {
215 global $wgOut;
216
217 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
218 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
219
220 $wgOut->returnToMain( false, $user->getUserPage() );
221 }
222 }