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