* (bug 8437) Make Title::loadRestrictions() initialise $mRestrictions properly
[lhc/web/wiklou.git] / includes / SpecialEmailuser.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once('UserMailer.php');
12
13 function wfSpecialEmailuser( $par ) {
14 global $wgUser, $wgOut, $wgRequest, $wgEnableEmail, $wgEnableUserEmail;
15
16 if( !( $wgEnableEmail && $wgEnableUserEmail ) ) {
17 $wgOut->showErrorPage( "nosuchspecialpage", "nospecialpagetext" );
18 return;
19 }
20
21 if( !$wgUser->canSendEmail() ) {
22 wfDebug( "User can't send.\n" );
23 $wgOut->showErrorPage( "mailnologin", "mailnologintext" );
24 return;
25 }
26
27 $action = $wgRequest->getVal( 'action' );
28 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
29 if ( "" == $target ) {
30 wfDebug( "Target is empty.\n" );
31 $wgOut->showErrorPage( "notargettitle", "notargettext" );
32 return;
33 }
34
35 $nt = Title::newFromURL( $target );
36 if ( is_null( $nt ) ) {
37 wfDebug( "Target is invalid title.\n" );
38 $wgOut->showErrorPage( "notargettitle", "notargettext" );
39 return;
40 }
41
42 $nu = User::newFromName( $nt->getText() );
43 if( is_null( $nu ) || !$nu->canReceiveEmail() ) {
44 wfDebug( "Target is invalid user or can't receive.\n" );
45 $wgOut->showErrorPage( "noemailtitle", "noemailtext" );
46 return;
47 }
48
49 $f = new EmailUserForm( $nu );
50
51 if ( "success" == $action ) {
52 $f->showSuccess( $nu );
53 } else if ( "submit" == $action && $wgRequest->wasPosted() &&
54 $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
55 $f->doSubmit();
56 } else {
57 $f->showForm();
58 }
59 }
60
61 /**
62 * @todo document
63 * @package MediaWiki
64 * @subpackage SpecialPage
65 */
66 class EmailUserForm {
67
68 var $target;
69 var $text, $subject;
70 var $cc_me; // Whether user requested to be sent a separate copy of their email.
71
72 /**
73 * @param User $target
74 */
75 function EmailUserForm( $target ) {
76 global $wgRequest;
77 $this->target = $target;
78 $this->text = $wgRequest->getText( 'wpText' );
79 $this->subject = $wgRequest->getText( 'wpSubject' );
80 $this->cc_me = $wgRequest->getBool( 'wpCCMe' );
81 }
82
83 function showForm() {
84 global $wgOut, $wgUser;
85
86 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
87 $wgOut->addWikiText( wfMsg( "emailpagetext" ) );
88
89 if ( $this->subject === "" ) {
90 $this->subject = wfMsg( "defemailsubject" );
91 }
92
93 $emf = wfMsg( "emailfrom" );
94 $sender = $wgUser->getName();
95 $emt = wfMsg( "emailto" );
96 $rcpt = $this->target->getName();
97 $emr = wfMsg( "emailsubject" );
98 $emm = wfMsg( "emailmessage" );
99 $ems = wfMsg( "emailsend" );
100 $emc = wfMsg( "emailccme" );
101 $encSubject = htmlspecialchars( $this->subject );
102
103 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
104 $action = $titleObj->escapeLocalURL( "target=" .
105 urlencode( $this->target->getName() ) . "&action=submit" );
106 $token = $wgUser->editToken();
107
108 $wgOut->addHTML( "
109 <form id=\"emailuser\" method=\"post\" action=\"{$action}\">
110 <table border='0' id='mailheader'><tr>
111 <td align='right'>{$emf}:</td>
112 <td align='left'><strong>" . htmlspecialchars( $sender ) . "</strong></td>
113 </tr><tr>
114 <td align='right'>{$emt}:</td>
115 <td align='left'><strong>" . htmlspecialchars( $rcpt ) . "</strong></td>
116 </tr><tr>
117 <td align='right'>{$emr}:</td>
118 <td align='left'>
119 <input type='text' size='60' maxlength='200' name=\"wpSubject\" value=\"{$encSubject}\" />
120 </td>
121 </tr>
122 </table>
123 <span id='wpTextLabel'><label for=\"wpText\">{$emm}:</label><br /></span>
124 <textarea name=\"wpText\" rows='20' cols='80' wrap='virtual' style=\"width: 100%;\">" . htmlspecialchars( $this->text ) .
125 "</textarea>
126 " . wfCheckLabel( $emc, 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) . "<br />
127 <input type='submit' name=\"wpSend\" value=\"{$ems}\" />
128 <input type='hidden' name='wpEditToken' value=\"$token\" />
129 </form>\n" );
130
131 }
132
133 function doSubmit() {
134 global $wgOut, $wgUser;
135
136 $to = new MailAddress( $this->target );
137 $from = new MailAddress( $wgUser );
138 $subject = $this->subject;
139
140 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
141
142 $mailResult = userMailer( $to, $from, $subject, $this->text );
143
144 if( WikiError::isError( $mailResult ) ) {
145 $wgOut->addHTML( wfMsg( "usermailererror" ) . $mailResult);
146 } else {
147
148 // if the user requested a copy of this mail, do this now,
149 // unless they are emailing themselves, in which case one copy of the message is sufficient.
150 if ($this->cc_me && $to != $from) {
151 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
152 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
153 $ccResult = userMailer( $from, $from, $cc_subject, $this->text );
154 if( WikiError::isError( $ccResult ) ) {
155 // At this stage, the user's CC mail has failed, but their
156 // original mail has succeeded. It's unlikely, but still, what to do?
157 // We can either show them an error, or we can say everything was fine,
158 // or we can say we sort of failed AND sort of succeeded. Of these options,
159 // simply saying there was an error is probably best.
160 $wgOut->addHTML( wfMsg( "usermailererror" ) . $ccResult);
161 return;
162 }
163 }
164 }
165
166 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
167 $encTarget = wfUrlencode( $this->target->getName() );
168 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
169 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
170 }
171 }
172 }
173
174 function showSuccess( &$user ) {
175 global $wgOut;
176
177 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
178 $wgOut->addHTML( wfMsg( "emailsenttext" ) );
179
180 $wgOut->returnToMain( false, $user->getUserPage() );
181 }
182 }
183 ?>