Follow-up to r64903; fix field names broken by r65040.
[lhc/web/wiklou.git] / includes / specials / SpecialEmailuser.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 class SpecialEmailUser extends UnlistedSpecialPage {
8 protected $mTarget;
9
10 public function __construct(){
11 parent::__construct( 'Emailuser' );
12 }
13
14 protected function getFormFields(){
15 global $wgUser;
16 return array(
17 'From' => array(
18 'type' => 'info',
19 'raw' => 1,
20 'default' => $wgUser->getSkin()->link(
21 $wgUser->getUserPage(),
22 htmlspecialchars( $wgUser->getName() )
23 ),
24 'label-message' => 'emailfrom',
25 'id' => 'mw-emailuser-sender',
26 ),
27 'To' => array(
28 'type' => 'info',
29 'raw' => 1,
30 'default' => $wgUser->getSkin()->link(
31 $this->mTargetObj->getUserPage(),
32 htmlspecialchars( $this->mTargetObj->getName() )
33 ),
34 'label-message' => 'emailto',
35 'id' => 'mw-emailuser-recipient',
36 ),
37 'Target' => array(
38 'name' => 'wpTarget',
39 'type' => 'hidden',
40 'default' => $this->mTargetObj->getName(),
41 ),
42 'Subject' => array(
43 'type' => 'text',
44 'default' => wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) ),
45 'label-message' => 'emailsubject',
46 'maxlength' => 200,
47 'size' => 60,
48 'required' => 1,
49 ),
50 'Text' => array(
51 'type' => 'textarea',
52 'rows' => 20,
53 'cols' => 80,
54 'label-message' => 'emailmessage',
55 'required' => 1,
56 ),
57 'CCMe' => array(
58 'type' => 'check',
59 'label-message' => 'emailccme',
60 'default' => $wgUser->getBoolOption( 'ccmeonemails' ),
61 ),
62 );
63 }
64
65 public function execute( $par ) {
66 global $wgRequest, $wgOut, $wgUser;
67 $this->mTarget = is_null( $par )
68 ? $wgRequest->getVal( 'wpTarget', '' )
69 : $par;
70
71 $ret = self::getTarget( $this->mTarget );
72 if( $ret instanceof User ){
73 $this->mTargetObj = $ret;
74 } else {
75 $wgOut->showErrorPage( "{$ret}title", "{$ret}text" );
76 return false;
77 }
78
79 $error = self::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
80 switch ( $error ) {
81 case null:
82 # Wahey!
83 break;
84 case 'badaccess':
85 $wgOut->permissionRequired( 'sendemail' );
86 return;
87 case 'blockedemailuser':
88 $wgOut->blockedPage();
89 return;
90 case 'actionthrottledtext':
91 $wgOut->rateLimited();
92 return;
93 case 'mailnologin':
94 case 'usermaildisabled':
95 $wgOut->showErrorPage( $error, "{$error}text" );
96 return;
97 default:
98 # It's a hook error
99 list( $title, $msg, $params ) = $error;
100 $wgOut->showErrorPage( $title, $msg, $params );
101 return;
102 }
103
104 $form = new HTMLForm( $this->getFormFields() );
105 $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
106 $form->setSubmitText( wfMsg( 'emailsend' ) );
107 $form->setTitle( $this->getTitle() );
108 $form->setSubmitCallback( array( __CLASS__, 'submit' ) );
109 $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
110 $form->loadData();
111
112 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ){
113 return false;
114 }
115
116 $wgOut->setPagetitle( wfMsg( 'emailpage' ) );
117 $result = $form->show();
118
119 if( $result === true ){
120 $wgOut->setPagetitle( wfMsg( 'emailsent' ) );
121 $wgOut->addWikiMsg( 'emailsenttext' );
122 $wgOut->returnToMain( false, $this->mTargetObj->getUserPage() );
123 }
124 }
125
126 /**
127 * Validate target User
128 *
129 * @param $target String: target user name
130 * @return User object on success or a string on error
131 */
132 public static function getTarget( $target ) {
133 if ( $target == '' ) {
134 wfDebug( "Target is empty.\n" );
135 return 'notarget';
136 }
137
138 $nu = User::newFromName( $target );
139 if( !$nu instanceof User || !$nu->getId() ) {
140 wfDebug( "Target is invalid user.\n" );
141 return 'notarget';
142 } else if ( !$nu->isEmailConfirmed() ) {
143 wfDebug( "User has no valid email.\n" );
144 return 'noemail';
145 } else if ( !$nu->canReceiveEmail() ) {
146 wfDebug( "User does not allow user emails.\n" );
147 return 'nowikiemail';
148 }
149
150 return $nu;
151 }
152
153 /**
154 * Check whether a user is allowed to send email
155 *
156 * @param $user User object
157 * @param $editToken String: edit token
158 * @return null on success or string on error
159 */
160 public static function getPermissionsError( $user, $editToken ) {
161 global $wgEnableEmail, $wgEnableUserEmail;
162 if( !$wgEnableEmail || !$wgEnableUserEmail ){
163 return 'usermaildisabled';
164 }
165
166 if( !$user->isAllowed( 'sendemail' ) ) {
167 return 'badaccess';
168 }
169
170 if( !$user->isEmailConfirmed() ){
171 return 'mailnologin';
172 }
173
174 if( $user->isBlockedFromEmailuser() ) {
175 wfDebug( "User is blocked from sending e-mail.\n" );
176 return "blockedemailuser";
177 }
178
179 if( $user->pingLimiter( 'emailuser' ) ) {
180 wfDebug( "Ping limiter triggered.\n" );
181 return 'actionthrottledtext';
182 }
183
184 $hookErr = false;
185 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
186 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
187 if ( $hookErr ) {
188 return $hookErr;
189 }
190
191 return null;
192 }
193
194 /**
195 * Really send a mail. Permissions should have been checked using
196 * getPermissionsError(). It is probably also a good
197 * idea to check the edit token and ping limiter in advance.
198 *
199 * @return Mixed: True on success, String on error
200 */
201 public static function submit( $data ) {
202 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
203
204 $target = self::getTarget( $data['Target'] );
205 if( !$target instanceof User ){
206 return wfMsgExt( $target . 'text', 'parse' );
207 }
208 $to = new MailAddress( $target );
209 $from = new MailAddress( $wgUser );
210 $subject = $data['Subject'];
211 $text = $data['Text'];
212
213 // Add a standard footer and trim up trailing newlines
214 $text = rtrim( $text ) . "\n\n-- \n";
215 $text .= wfMsgExt(
216 'emailuserfooter',
217 array( 'content', 'parsemag' ),
218 array( $from->name, $to->name )
219 );
220
221 $error = '';
222 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
223 return $error;
224 }
225
226 if( $wgUserEmailUseReplyTo ) {
227 // Put the generic wiki autogenerated address in the From:
228 // header and reserve the user for Reply-To.
229 //
230 // This is a bit ugly, but will serve to differentiate
231 // wiki-borne mails from direct mails and protects against
232 // SPF and bounce problems with some mailers (see below).
233 global $wgPasswordSender;
234 $mailFrom = new MailAddress( $wgPasswordSender );
235 $replyTo = $from;
236 } else {
237 // Put the sending user's e-mail address in the From: header.
238 //
239 // This is clean-looking and convenient, but has issues.
240 // One is that it doesn't as clearly differentiate the wiki mail
241 // from "directly" sent mails.
242 //
243 // Another is that some mailers (like sSMTP) will use the From
244 // address as the envelope sender as well. For open sites this
245 // can cause mails to be flunked for SPF violations (since the
246 // wiki server isn't an authorized sender for various users'
247 // domains) as well as creating a privacy issue as bounces
248 // containing the recipient's e-mail address may get sent to
249 // the sending user.
250 $mailFrom = $from;
251 $replyTo = null;
252 }
253
254 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
255
256 if( WikiError::isError( $mailResult ) && false ) {
257 return $mailResult->getMessage();
258 } else {
259 // if the user requested a copy of this mail, do this now,
260 // unless they are emailing themselves, in which case one
261 // copy of the message is sufficient.
262 if ( $data['CCMe'] && $to != $from ) {
263 $cc_subject = wfMsg(
264 'emailccsubject',
265 $target->getName(),
266 $subject
267 );
268 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
269 $ccResult = UserMailer::send( $from, $from, $cc_subject, $text );
270 if( WikiError::isError( $ccResult ) ) {
271 // At this stage, the user's CC mail has failed, but their
272 // original mail has succeeded. It's unlikely, but still,
273 // what to do? We can either show them an error, or we can
274 // say everything was fine, or we can say we sort of failed
275 // AND sort of succeeded. Of these options, simply saying
276 // there was an error is probably best.
277 return $ccResult->getMessage();
278 }
279 }
280
281 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
282 return true;
283 }
284 }
285 }