Fix SpecialUpload::unsaveUploadedFile(). Move UploadVerification hook so that 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 $f = new EmailUserForm( $nu );
49
50 if ( "success" == $action ) {
51 $f->showSuccess( $nu );
52 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
53 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
54 {
55 # Check against the rate limiter
56 if( $wgUser->pingLimiter( 'emailuser' ) ) {
57 $wgOut->rateLimited();
58 return;
59 }
60
61 $f->doSubmit();
62 } else {
63 $f->showForm();
64 }
65 }
66
67 /**
68 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
69 * @addtogroup SpecialPage
70 */
71 class EmailUserForm {
72
73 var $target;
74 var $text, $subject;
75 var $cc_me; // Whether user requested to be sent a separate copy of their email.
76
77 /**
78 * @param User $target
79 */
80 function EmailUserForm( $target ) {
81 global $wgRequest;
82 $this->target = $target;
83 $this->text = $wgRequest->getText( 'wpText' );
84 $this->subject = $wgRequest->getText( 'wpSubject' );
85 $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
86 }
87
88 function showForm() {
89 global $wgOut, $wgUser;
90
91 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
92 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
93
94 if ( $this->subject === "" ) {
95 $this->subject = wfMsg( "defemailsubject" );
96 }
97
98 $emf = wfMsg( "emailfrom" );
99 $sender = $wgUser->getName();
100 $emt = wfMsg( "emailto" );
101 $rcpt = $this->target->getName();
102 $emr = wfMsg( "emailsubject" );
103 $emm = wfMsg( "emailmessage" );
104 $ems = wfMsg( "emailsend" );
105 $emc = wfMsg( "emailccme" );
106 $encSubject = htmlspecialchars( $this->subject );
107
108 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
109 $action = $titleObj->escapeLocalURL( "target=" .
110 urlencode( $this->target->getName() ) . "&action=submit" );
111 $token = $wgUser->editToken();
112
113 $wgOut->addHTML( "
114 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
115 <table border='0' id='mailheader'><tr>
116 <td align='right'>{$emf}:</td>
117 <td align='left'><strong>" . htmlspecialchars( $sender ) . "</strong></td>
118 </tr><tr>
119 <td align='right'>{$emt}:</td>
120 <td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</strong></td>
121 </tr><tr>
122 <td align='right'>{$emr}:</td>
123 <td align='left'>
124 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
125 </td>
126 </tr>
127 </table>
128 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
129 <textarea name=\"wpText\" rows='20' cols='80' wrap='virtual' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
130 "</textarea>
131 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
132 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
133 <input type='hidden' name='wpEditToken' value=\"$token\" />
134 </form>\n" );
135
136 }
137
138 function doSubmit() {
139 global $wgOut, $wgUser;
140
141 $to = new MailAddress( $this->target );
142 $from = new MailAddress( $wgUser );
143 $subject = $this->subject;
144
145 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
146
147 $mailResult = userMailer( $to, $from, $subject, $this->text );
148
149 if( WikiError::isError( $mailResult ) ) {
150 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
151 } else {
152
153 // if the user requested a copy of this mail, do this now,
154 // unless they are emailing themselves, in which case one copy of the message is sufficient.
155 if ($this->cc_me && $to != $from) {
156 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
157 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
158 $ccResult = userMailer( $from, $from, $cc_subject, $this->text );
159 if( WikiError::isError( $ccResult ) ) {
160 // At this stage, the user's CC mail has failed, but their
161 // original mail has succeeded. It's unlikely, but still, what to do?
162 // We can either show them an error, or we can say everything was fine,
163 // or we can say we sort of failed AND sort of succeeded. Of these options,
164 // simply saying there was an error is probably best.
165 $wgOut->addHTML( wfMsg( "usermailererror" ) . $ccResult);
166 return;
167 }
168 }
169 }
170
171 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
172 $encTarget = wfUrlencode( $this->target->getName() );
173 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
174 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
175 }
176 }
177 }
178
179 function showSuccess( &$user ) {
180 global $wgOut;
181
182 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
183 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
184
185 $wgOut->returnToMain( false, $user->getUserPage() );
186 }
187 }
188 ?>