22bc254a4d1d15f102506620439dfdef0e2d2226
[lhc/web/wiklou.git] / includes / specials / SpecialEmailuser.php
1 <?php
2 /**
3 * Implements Special:Emailuser
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page that allows users to send e-mails to other users
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialEmailUser extends UnlistedSpecialPage {
30 protected $mTarget;
31
32 public function __construct() {
33 parent::__construct( 'Emailuser' );
34 }
35
36 protected function getFormFields() {
37 global $wgUser;
38 return array(
39 'From' => array(
40 'type' => 'info',
41 'raw' => 1,
42 'default' => $wgUser->getSkin()->link(
43 $wgUser->getUserPage(),
44 htmlspecialchars( $wgUser->getName() )
45 ),
46 'label-message' => 'emailfrom',
47 'id' => 'mw-emailuser-sender',
48 ),
49 'To' => array(
50 'type' => 'info',
51 'raw' => 1,
52 'default' => $wgUser->getSkin()->link(
53 $this->mTargetObj->getUserPage(),
54 htmlspecialchars( $this->mTargetObj->getName() )
55 ),
56 'label-message' => 'emailto',
57 'id' => 'mw-emailuser-recipient',
58 ),
59 'Target' => array(
60 'type' => 'hidden',
61 'default' => $this->mTargetObj->getName(),
62 ),
63 'Subject' => array(
64 'type' => 'text',
65 'default' => wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) ),
66 'label-message' => 'emailsubject',
67 'maxlength' => 200,
68 'size' => 60,
69 'required' => 1,
70 ),
71 'Text' => array(
72 'type' => 'textarea',
73 'rows' => 20,
74 'cols' => 80,
75 'label-message' => 'emailmessage',
76 'required' => 1,
77 ),
78 'CCMe' => array(
79 'type' => 'check',
80 'label-message' => 'emailccme',
81 'default' => $wgUser->getBoolOption( 'ccmeonemails' ),
82 ),
83 );
84 }
85
86 public function execute( $par ) {
87 global $wgRequest, $wgOut, $wgUser;
88
89 $this->setHeaders();
90 $this->outputHeader();
91 $wgOut->addModuleStyles( 'mediawiki.special' );
92 $this->mTarget = is_null( $par )
93 ? $wgRequest->getVal( 'wpTarget', $wgRequest->getVal( 'target', '' ) )
94 : $par;
95 // error out if sending user cannot do this
96 $error = self::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
97 switch ( $error ) {
98 case null:
99 # Wahey!
100 break;
101 case 'badaccess':
102 $wgOut->permissionRequired( 'sendemail' );
103 return;
104 case 'blockedemailuser':
105 $wgOut->blockedPage();
106 return;
107 case 'actionthrottledtext':
108 $wgOut->rateLimited();
109 return;
110 case 'mailnologin':
111 case 'usermaildisabled':
112 $wgOut->showErrorPage( $error, "{$error}text" );
113 return;
114 default:
115 # It's a hook error
116 list( $title, $msg, $params ) = $error;
117 $wgOut->showErrorPage( $title, $msg, $params );
118 return;
119 }
120 // Got a valid target user name? Else ask for one.
121 $ret = self::getTarget( $this->mTarget );
122 if( !$ret instanceof User ) {
123 if( $this->mTarget != '' ) {
124 $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
125 $wgOut->addHTML( '<p class="error">' . wfMessage( $ret )->parse() . '</p>' );
126 }
127 $wgOut->addHTML( self::userForm( $this->mTarget ) );
128 return false;
129 }
130
131 $this->mTargetObj = $ret;
132
133 $form = new HTMLForm( $this->getFormFields() );
134 $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
135 $form->setSubmitText( wfMsg( 'emailsend' ) );
136 $form->setTitle( $this->getTitle() );
137 $form->setSubmitCallback( array( __CLASS__, 'submit' ) );
138 $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
139 $form->loadData();
140
141 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
142 return false;
143 }
144
145 $wgOut->setPageTitle( wfMsg( 'emailpage' ) );
146 $result = $form->show();
147
148 if( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
149 $wgOut->setPageTitle( wfMsg( 'emailsent' ) );
150 $wgOut->addWikiMsg( 'emailsenttext' );
151 if ( $status->successCount > 1 ) $wgOut->addWikiMsg( 'emailyougotcopy' );
152 } elseif ( $result instanceof Status ) {
153 if ( $status->successCount == 0 ) {
154 $wgOut->setPageTitle( wfMsg( 'emailnotsent' ) );
155 $wgOut->addWikiMsg( $result->getWikiText( 'emailfailed' ) );
156 } elseif ( $status->failCount ) {
157 $wgOut->setPageTitle( wfMsg( 'emailsent' ) );
158 $wgOut->addWikiMsg( 'emailsenttext' );
159 $wgOut->addWikiMsg( $result->getWikiText( 'emailccfailed' ) );
160 }
161 }
162 $wgOut->returnToMain( false, $this->mTargetObj->getUserPage() );
163 }
164
165 /**
166 * Validate target User
167 *
168 * @param $target String: target user name
169 * @return User object on success or a string on error
170 */
171 public static function getTarget( $target ) {
172 if ( $target == '' ) {
173 wfDebug( "Target is empty.\n" );
174 return 'notarget';
175 }
176
177 $nu = User::newFromName( $target );
178 if( !$nu instanceof User || !$nu->getId() ) {
179 wfDebug( "Target is invalid user.\n" );
180 return 'notarget';
181 } elseif ( !$nu->isEmailConfirmed() ) {
182 wfDebug( "User has no valid email.\n" );
183 return 'noemail';
184 } elseif ( !$nu->canReceiveEmail() ) {
185 wfDebug( "User does not allow user emails.\n" );
186 return 'nowikiemail';
187 }
188
189 return $nu;
190 }
191
192 /**
193 * Check whether a user is allowed to send email
194 *
195 * @param $user User object
196 * @param $editToken String: edit token
197 * @return null on success or string on error
198 */
199 public static function getPermissionsError( $user, $editToken ) {
200 global $wgEnableEmail, $wgEnableUserEmail;
201 if( !$wgEnableEmail || !$wgEnableUserEmail ) {
202 return 'usermaildisabled';
203 }
204
205 if( !$user->isAllowed( 'sendemail' ) ) {
206 return 'badaccess';
207 }
208
209 if( !$user->isEmailConfirmed() ) {
210 return 'mailnologin';
211 }
212
213 if( $user->isBlockedFromEmailuser() ) {
214 wfDebug( "User is blocked from sending e-mail.\n" );
215 return "blockedemailuser";
216 }
217
218 if( $user->pingLimiter( 'emailuser' ) ) {
219 wfDebug( "Ping limiter triggered.\n" );
220 return 'actionthrottledtext';
221 }
222
223 $hookErr = false;
224 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
225 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
226 if ( $hookErr ) {
227 return $hookErr;
228 }
229
230 return null;
231 }
232
233 /**
234 * Form to ask for target user name.
235 *
236 * @param $name String: user name submitted.
237 * @return String: form asking for user name.
238 */
239 static function userForm( $name ) {
240 $string = Xml::openElement( 'form', array( 'method' => 'get', 'action' => '', 'id' => 'askusername' ) ) .
241 Xml::openElement( 'fieldset' ) .
242 Html::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
243 Xml::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
244 Xml::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
245 Xml::closeElement( 'fieldset' ) .
246 Xml::closeElement( 'form' ) . "\n";
247 return $string;
248 }
249
250 /**
251 * Really send a mail. Permissions should have been checked using
252 * getPermissionsError(). It is probably also a good
253 * idea to check the edit token and ping limiter in advance.
254 *
255 * @return Mixed: Status object, or potentially a String on error
256 * or maybe even true on success if anything uses the EmailUser hook.
257 */
258 public static function submit( $data ) {
259 global $wgUser, $wgUserEmailUseReplyTo;
260
261 $target = self::getTarget( $data['Target'] );
262 if( !$target instanceof User ) {
263 return wfMsgExt( $target . 'text', 'parse' );
264 }
265 $to = new MailAddress( $target );
266 $from = new MailAddress( $wgUser );
267 $subject = $data['Subject'];
268 $text = $data['Text'];
269
270 // Add a standard footer and trim up trailing newlines
271 $text = rtrim( $text ) . "\n\n-- \n";
272 $text .= wfMsgExt(
273 'emailuserfooter',
274 array( 'content', 'parsemag' ),
275 array( $from->name, $to->name )
276 );
277
278 $error = '';
279 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
280 return $error;
281 }
282
283 if( $wgUserEmailUseReplyTo ) {
284 // Put the generic wiki autogenerated address in the From:
285 // header and reserve the user for Reply-To.
286 //
287 // This is a bit ugly, but will serve to differentiate
288 // wiki-borne mails from direct mails and protects against
289 // SPF and bounce problems with some mailers (see below).
290 global $wgPasswordSender, $wgPasswordSenderName;
291 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
292 $replyTo = $from;
293 } else {
294 // Put the sending user's e-mail address in the From: header.
295 //
296 // This is clean-looking and convenient, but has issues.
297 // One is that it doesn't as clearly differentiate the wiki mail
298 // from "directly" sent mails.
299 //
300 // Another is that some mailers (like sSMTP) will use the From
301 // address as the envelope sender as well. For open sites this
302 // can cause mails to be flunked for SPF violations (since the
303 // wiki server isn't an authorized sender for various users'
304 // domains) as well as creating a privacy issue as bounces
305 // containing the recipient's e-mail address may get sent to
306 // the sending user.
307 $mailFrom = $from;
308 $replyTo = null;
309 }
310
311 $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
312
313 if( !$status->isGood() ) {
314 return $status;
315 } else {
316 $status->successCount++;
317
318 // if the user requested a copy of this mail, do this now,
319 // unless they are emailing themselves, in which case one
320 // copy of the message is sufficient.
321 if ( $data['CCMe'] && $to != $from ) {
322 $cc_subject = wfMsg(
323 'emailccsubject',
324 $target->getName(),
325 $subject
326 );
327 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
328 $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
329 if ( $ccStatus->isGood() ) {
330 $ccStatus->successCount++;
331 } else {
332 $ccStatus->failCount++;
333 }
334 $status->merge( $ccStatus );
335 }
336
337 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
338 return $status;
339 }
340 }
341 }