Tweak for r29561: don't grab a database object until we need it
[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;
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 $mailResult = userMailer( $to, $from, $subject, $this->text );
155
156 if( WikiError::isError( $mailResult ) ) {
157 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
158 } else {
159
160 // if the user requested a copy of this mail, do this now,
161 // unless they are emailing themselves, in which case one copy of the message is sufficient.
162 if ($this->cc_me && $to != $from) {
163 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
164 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
165 $ccResult = userMailer( $from, $from, $cc_subject, $this->text );
166 if( WikiError::isError( $ccResult ) ) {
167 // At this stage, the user's CC mail has failed, but their
168 // original mail has succeeded. It's unlikely, but still, what to do?
169 // We can either show them an error, or we can say everything was fine,
170 // or we can say we sort of failed AND sort of succeeded. Of these options,
171 // simply saying there was an error is probably best.
172 $wgOut->addHTML( wfMsg( "usermailererror" ) . $ccResult);
173 return;
174 }
175 }
176 }
177
178 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
179 $encTarget = wfUrlencode( $this->target->getName() );
180 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
181 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
182 }
183 }
184 }
185
186 function showSuccess( &$user ) {
187 global $wgOut;
188
189 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
190 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
191
192 $wgOut->returnToMain( false, $user->getUserPage() );
193 }
194 }