* Add form to RCLinked and add to sp:specialpages
[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 $skin = $wgUser->getSkin();
96
97 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
98 $wgOut->addWikiMsg( "emailpagetext" );
99
100 if ( $this->subject === "" ) {
101 $this->subject = wfMsgForContent( "defemailsubject" );
102 }
103
104 $emf = wfMsg( "emailfrom" );
105 $senderLink = $skin->makeLinkObj(
106 $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) );
107 $emt = wfMsg( "emailto" );
108 $recipientLink = $skin->makeLinkObj(
109 $this->target->getUserPage(), htmlspecialchars( $this->target->getName() ) );
110 $emr = wfMsg( "emailsubject" );
111 $emm = wfMsg( "emailmessage" );
112 $ems = wfMsg( "emailsend" );
113 $emc = wfMsg( "emailccme" );
114 $encSubject = htmlspecialchars( $this->subject );
115
116 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
117 $action = $titleObj->escapeLocalURL( "target=" .
118 urlencode( $this->target->getName() ) . "&action=submit" );
119 $token = htmlspecialchars( $wgUser->editToken() );
120
121 $wgOut->addHTML( "
122 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
123 <table border='0' id='mailheader'><tr>
124 <td align='right'>{$emf}:</td>
125 <td align='left'><strong>{$senderLink}</strong></td>
126 </tr><tr>
127 <td align='right'>{$emt}:</td>
128 <td align='left'><strong>{$recipientLink}</strong></td>
129 </tr><tr>
130 <td align='right'>{$emr}:</td>
131 <td align='left'>
132 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
133 </td>
134 </tr>
135 </table>
136 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
137 <textarea id=\"wpText\" name=\"wpText\" rows='20' cols='80' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
138 "</textarea>
139 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
140 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
141 <input type='hidden' name='wpEditToken' value=\"$token\" />
142 </form>\n" );
143
144 }
145
146 function doSubmit() {
147 global $wgOut, $wgUser, $wgUserEmailUseReplyTo;
148
149 $to = new MailAddress( $this->target );
150 $from = new MailAddress( $wgUser );
151 $subject = $this->subject;
152
153 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
154
155 if( $wgUserEmailUseReplyTo ) {
156 // Put the generic wiki autogenerated address in the From:
157 // header and reserve the user for Reply-To.
158 //
159 // This is a bit ugly, but will serve to differentiate
160 // wiki-borne mails from direct mails and protects against
161 // SPF and bounce problems with some mailers (see below).
162 global $wgPasswordSender;
163 $mailFrom = new MailAddress( $wgPasswordSender );
164 $replyTo = $from;
165 } else {
166 // Put the sending user's e-mail address in the From: header.
167 //
168 // This is clean-looking and convenient, but has issues.
169 // One is that it doesn't as clearly differentiate the wiki mail
170 // from "directly" sent mails.
171 //
172 // Another is that some mailers (like sSMTP) will use the From
173 // address as the envelope sender as well. For open sites this
174 // can cause mails to be flunked for SPF violations (since the
175 // wiki server isn't an authorized sender for various users'
176 // domains) as well as creating a privacy issue as bounces
177 // containing the recipient's e-mail address may get sent to
178 // the sending user.
179 $mailFrom = $from;
180 $replyTo = null;
181 }
182
183 $this->text .= wfMsgForContent( 'emailuser-footer' );
184
185 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
186
187 if( WikiError::isError( $mailResult ) ) {
188 $wgOut->addHTML( wfMsg( "usermailererror" ) .
189 ' ' . htmlspecialchars( $mailResult->getMessage() ) );
190 } else {
191
192 // if the user requested a copy of this mail, do this now,
193 // unless they are emailing themselves, in which case one copy of the message is sufficient.
194 if ($this->cc_me && $to != $from) {
195 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
196 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
197 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
198 if( WikiError::isError( $ccResult ) ) {
199 // At this stage, the user's CC mail has failed, but their
200 // original mail has succeeded. It's unlikely, but still, what to do?
201 // We can either show them an error, or we can say everything was fine,
202 // or we can say we sort of failed AND sort of succeeded. Of these options,
203 // simply saying there was an error is probably best.
204 $wgOut->addHTML( wfMsg( "usermailererror" ) .
205 ' ' . htmlspecialchars( $ccResult->getMessage() ) );
206 return;
207 }
208 }
209 }
210
211 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
212 $encTarget = wfUrlencode( $this->target->getName() );
213 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
214 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
215 }
216 }
217 }
218
219 function showSuccess( &$user ) {
220 global $wgOut;
221
222 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
223 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
224
225 $wgOut->returnToMain( false, $user->getUserPage() );
226 }
227 }