* Introduced OutputPage::addWikiMsg() and OutputPage::wrapWikiMsg(), to make it easie...
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 * @todo document
9 */
10 function wfSpecialEmailuser( $par ) {
11 global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
12
13 if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
14 $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
15 return;
16 }
17
18 if( !$wgUser->canSendEmail() ) {
19 wfDebug( "User can't send.\n" );
20 $wgOut->showErrorPage( "mailnologin", "mailnologintext" );
21 return;
22 }
23
24 $action = $wgRequest->getVal( 'action' );
25 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
26 if ( "" == $target ) {
27 wfDebug( "Target is empty.\n" );
28 $wgOut->showErrorPage( "notargettitle", "notargettext" );
29 return;
30 }
31
32 $nt = Title::newFromURL( $target );
33 if ( is_null( $nt ) ) {
34 wfDebug( "Target is invalid title.\n" );
35 $wgOut->showErrorPage( "notargettitle", "notargettext" );
36 return;
37 }
38
39 $nu = User::newFromName( $nt->getText() );
40 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
41 wfDebug( "Target is invalid user or can't receive.\n" );
42 $wgOut->showErrorPage( "noemailtitle", "noemailtext" );
43 return;
44 }
45
46 if ( $wgUser->isBlockedFromEmailUser() ) {
47 // User has been blocked from sending e-mail. Show the std blocked form.
48 wfDebug( "User is blocked from sending e-mail.\n" );
49 $wgOut->blockedPage();
50 return;
51 }
52
53 $f = new EmailUserForm( $nu );
54
55 if ( "success" == $action ) {
56 $f->showSuccess( $nu );
57 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
58 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
59 {
60 # Check against the rate limiter
61 if( $wgUser->pingLimiter( 'emailuser' ) ) {
62 $wgOut->rateLimited();
63 return;
64 }
65
66 $f->doSubmit();
67 } else {
68 $f->showForm();
69 }
70 }
71
72 /**
73 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
74 * @addtogroup SpecialPage
75 */
76 class EmailUserForm {
77
78 var $target;
79 var $text, $subject;
80 var $cc_me; // Whether user requested to be sent a separate copy of their email.
81
82 /**
83 * @param User $target
84 */
85 function EmailUserForm( $target ) {
86 global $wgRequest;
87 $this->target = $target;
88 $this->text = $wgRequest->getText( 'wpText' );
89 $this->subject = $wgRequest->getText( 'wpSubject' );
90 $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
91 }
92
93 function showForm() {
94 global $wgOut, $wgUser;
95
96 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
97 $wgOut->addWikiMsg( "emailpagetext" );
98
99 if ( $this->subject === "" ) {
100 $this->subject = wfMsg( "defemailsubject" );
101 }
102
103 $emf = wfMsg( "emailfrom" );
104 $sender = $wgUser->getName();
105 $emt = wfMsg( "emailto" );
106 $rcpt = $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>" . htmlspecialchars( $sender ) . "</strong></td>
123 </tr><tr>
124 <td align='right'>{$emt}:</td>
125 <td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</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}\" />
138 <input type='hidden' name='wpEditToken' value=\"$token\" />
139 </form>\n" );
140
141 }
142
143 function doSubmit() {
144 global $wgOut, $wgUser, $wgUserEmailUseReplyTo;
145
146 $to = new MailAddress( $this->target );
147 $from = new MailAddress( $wgUser );
148 $subject = $this->subject;
149
150 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
151
152 if( $wgUserEmailUseReplyTo ) {
153 // Put the generic wiki autogenerated address in the From:
154 // header and reserve the user for Reply-To.
155 //
156 // This is a bit ugly, but will serve to differentiate
157 // wiki-borne mails from direct mails and protects against
158 // SPF and bounce problems with some mailers (see below).
159 global $wgPasswordSender;
160 $mailFrom = new MailAddress( $wgPasswordSender );
161 $replyTo = $from;
162 } else {
163 // Put the sending user's e-mail address in the From: header.
164 //
165 // This is clean-looking and convenient, but has issues.
166 // One is that it doesn't as clearly differentiate the wiki mail
167 // from "directly" sent mails.
168 //
169 // Another is that some mailers (like sSMTP) will use the From
170 // address as the envelope sender as well. For open sites this
171 // can cause mails to be flunked for SPF violations (since the
172 // wiki server isn't an authorized sender for various users'
173 // domains) as well as creating a privacy issue as bounces
174 // containing the recipient's e-mail address may get sent to
175 // the sending user.
176 $mailFrom = $from;
177 $replyTo = null;
178 }
179
180 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
181
182 if( WikiError::isError( $mailResult ) ) {
183 $wgOut->addHTML( wfMsg( "usermailererror" ) .
184 ' ' . htmlspecialchars( $mailResult->getMessage() ) );
185 } else {
186
187 // if the user requested a copy of this mail, do this now,
188 // unless they are emailing themselves, in which case one copy of the message is sufficient.
189 if ($this->cc_me && $to != $from) {
190 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
191 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
192 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
193 if( WikiError::isError( $ccResult ) ) {
194 // At this stage, the user's CC mail has failed, but their
195 // original mail has succeeded. It's unlikely, but still, what to do?
196 // We can either show them an error, or we can say everything was fine,
197 // or we can say we sort of failed AND sort of succeeded. Of these options,
198 // simply saying there was an error is probably best.
199 $wgOut->addHTML( wfMsg( "usermailererror" ) .
200 ' ' . htmlspecialchars( $ccResult->getMessage() ) );
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 }