* Fixed E_NOTICE..
[mediawiki.git] / includes / SpecialEmailuser.php
blob1d5a964731ae30527cf3c2a93ff4fe1b0d275431
1 <?php
2 /**
4 * @addtogroup SpecialPage
5 */
7 require_once('UserMailer.php');
9 /**
10 * @todo document
12 function wfSpecialEmailuser( $par ) {
13 global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
15 if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
16 $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
17 return;
20 if( !$wgUser->canSendEmail() ) {
21 wfDebug( "User can't send.\n" );
22 $wgOut->showErrorPage( "mailnologin", "mailnologintext" );
23 return;
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;
34 $nt = Title::newFromURL( $target );
35 if ( is_null( $nt ) ) {
36 wfDebug( "Target is invalid title.\n" );
37 $wgOut->showErrorPage( "notargettitle", "notargettext" );
38 return;
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;
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;
55 $f = new EmailUserForm( $nu );
57 if ( "success" == $action ) {
58 $f->showSuccess( $nu );
59 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
60 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) )
62 # Check against the rate limiter
63 if( $wgUser->pingLimiter( 'emailuser' ) ) {
64 $wgOut->rateLimited();
65 return;
68 $f->doSubmit();
69 } else {
70 $f->showForm();
74 /**
75 * Implements the Special:Emailuser web interface, and invokes userMailer for sending the email message.
76 * @addtogroup SpecialPage
78 class EmailUserForm {
80 var $target;
81 var $text, $subject;
82 var $cc_me; // Whether user requested to be sent a separate copy of their email.
84 /**
85 * @param User $target
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' );
95 function showForm() {
96 global $wgOut, $wgUser;
98 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
99 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
101 if ( $this->subject === "" ) {
102 $this->subject = wfMsg( "defemailsubject" );
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 );
115 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
116 $action = $titleObj->escapeLocalURL( "target=" .
117 urlencode( $this->target->getName() ) . "&action=submit" );
118 $token = htmlspecialchars( $wgUser->editToken() );
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 name=\"wpText\" rows='20' cols='80' wrap='virtual' 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" );
145 function doSubmit() {
146 global $wgOut, $wgUser;
148 $to = new MailAddress( $this->target );
149 $from = new MailAddress( $wgUser );
150 $subject = $this->subject;
152 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
154 $mailResult = userMailer( $to, $from, $subject, $this->text );
156 if( WikiError::isError( $mailResult ) ) {
157 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
158 } else {
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;
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 ) );
186 function showSuccess( &$user ) {
187 global $wgOut;
189 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
190 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
192 $wgOut->returnToMain( false, $user->getUserPage() );