merging latest master
[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 public function getDescription() {
37 $target = self::getTarget( $this->mTarget );
38 if( !$target instanceof User ) {
39 return $this->msg( 'emailuser-title-notarget' )->text();
40 }
41
42 return $this->msg( 'emailuser-title-target', $target->getName() )->text();
43 }
44
45 protected function getFormFields() {
46 return array(
47 'From' => array(
48 'type' => 'info',
49 'raw' => 1,
50 'default' => Linker::link(
51 $this->getUser()->getUserPage(),
52 htmlspecialchars( $this->getUser()->getName() )
53 ),
54 'label-message' => 'emailfrom',
55 'id' => 'mw-emailuser-sender',
56 ),
57 'To' => array(
58 'type' => 'info',
59 'raw' => 1,
60 'default' => Linker::link(
61 $this->mTargetObj->getUserPage(),
62 htmlspecialchars( $this->mTargetObj->getName() )
63 ),
64 'label-message' => 'emailto',
65 'id' => 'mw-emailuser-recipient',
66 ),
67 'Target' => array(
68 'type' => 'hidden',
69 'default' => $this->mTargetObj->getName(),
70 ),
71 'Subject' => array(
72 'type' => 'text',
73 'default' => $this->msg( 'defemailsubject',
74 $this->getUser()->getName() )->inContentLanguage()->text(),
75 'label-message' => 'emailsubject',
76 'maxlength' => 200,
77 'size' => 60,
78 'required' => true,
79 ),
80 'Text' => array(
81 'type' => 'textarea',
82 'rows' => 20,
83 'cols' => 80,
84 'label-message' => 'emailmessage',
85 'required' => true,
86 ),
87 'CCMe' => array(
88 'type' => 'check',
89 'label-message' => 'emailccme',
90 'default' => $this->getUser()->getBoolOption( 'ccmeonemails' ),
91 ),
92 );
93 }
94
95 public function execute( $par ) {
96 $out = $this->getOutput();
97 $out->addModuleStyles( 'mediawiki.special' );
98
99 $this->mTarget = is_null( $par )
100 ? $this->getRequest()->getVal( 'wpTarget', $this->getRequest()->getVal( 'target', '' ) )
101 : $par;
102
103 // This needs to be below assignment of $this->mTarget because
104 // getDescription() needs it to determine the correct page title.
105 $this->setHeaders();
106 $this->outputHeader();
107
108 // error out if sending user cannot do this
109 $error = self::getPermissionsError( $this->getUser(), $this->getRequest()->getVal( 'wpEditToken' ) );
110 switch ( $error ) {
111 case null:
112 # Wahey!
113 break;
114 case 'badaccess':
115 throw new PermissionsError( 'sendemail' );
116 case 'blockedemailuser':
117 throw new UserBlockedError( $this->getUser()->mBlock );
118 case 'actionthrottledtext':
119 throw new ThrottledError;
120 case 'mailnologin':
121 case 'usermaildisabled':
122 throw new ErrorPageError( $error, "{$error}text" );
123 default:
124 # It's a hook error
125 list( $title, $msg, $params ) = $error;
126 throw new ErrorPageError( $title, $msg, $params );
127 }
128 // Got a valid target user name? Else ask for one.
129 $ret = self::getTarget( $this->mTarget );
130 if( !$ret instanceof User ) {
131 if( $this->mTarget != '' ) {
132 $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
133 $out->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
134 }
135 $out->addHTML( $this->userForm( $this->mTarget ) );
136 return false;
137 }
138
139 $this->mTargetObj = $ret;
140
141 $form = new HTMLForm( $this->getFormFields(), $this->getContext() );
142 $form->addPreText( $this->msg( 'emailpagetext' )->parse() );
143 $form->setSubmitTextMsg( 'emailsend' );
144 $form->setTitle( $this->getTitle() );
145 $form->setSubmitCallback( array( __CLASS__, 'uiSubmit' ) );
146 $form->setWrapperLegendMsg( 'email-legend' );
147 $form->loadData();
148
149 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
150 return false;
151 }
152
153 $result = $form->show();
154
155 if( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
156 $out->setPageTitle( $this->msg( 'emailsent' ) );
157 $out->addWikiMsg( 'emailsenttext' );
158 $out->returnToMain( false, $this->mTargetObj->getUserPage() );
159 }
160 }
161
162 /**
163 * Validate target User
164 *
165 * @param $target String: target user name
166 * @return User object on success or a string on error
167 */
168 public static function getTarget( $target ) {
169 if ( $target == '' ) {
170 wfDebug( "Target is empty.\n" );
171 return 'notarget';
172 }
173
174 $nu = User::newFromName( $target );
175 if( !$nu instanceof User || !$nu->getId() ) {
176 wfDebug( "Target is invalid user.\n" );
177 return 'notarget';
178 } elseif ( !$nu->isEmailConfirmed() ) {
179 wfDebug( "User has no valid email.\n" );
180 return 'noemail';
181 } elseif ( !$nu->canReceiveEmail() ) {
182 wfDebug( "User does not allow user emails.\n" );
183 return 'nowikiemail';
184 }
185
186 return $nu;
187 }
188
189 /**
190 * Check whether a user is allowed to send email
191 *
192 * @param $user User object
193 * @param $editToken String: edit token
194 * @return null on success or string on error
195 */
196 public static function getPermissionsError( $user, $editToken ) {
197 global $wgEnableEmail, $wgEnableUserEmail;
198 if( !$wgEnableEmail || !$wgEnableUserEmail ) {
199 return 'usermaildisabled';
200 }
201
202 if( !$user->isAllowed( 'sendemail' ) ) {
203 return 'badaccess';
204 }
205
206 if( !$user->isEmailConfirmed() ) {
207 return 'mailnologin';
208 }
209
210 if( $user->isBlockedFromEmailuser() ) {
211 wfDebug( "User is blocked from sending e-mail.\n" );
212 return "blockedemailuser";
213 }
214
215 if( $user->pingLimiter( 'emailuser' ) ) {
216 wfDebug( "Ping limiter triggered.\n" );
217 return 'actionthrottledtext';
218 }
219
220 $hookErr = false;
221 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
222 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
223 if ( $hookErr ) {
224 return $hookErr;
225 }
226
227 return null;
228 }
229
230 /**
231 * Form to ask for target user name.
232 *
233 * @param $name String: user name submitted.
234 * @return String: form asking for user name.
235 */
236 protected function userForm( $name ) {
237 global $wgScript;
238 $string = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' ) ) .
239 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
240 Xml::openElement( 'fieldset' ) .
241 Html::rawElement( 'legend', null, $this->msg( 'emailtarget' )->parse() ) .
242 Xml::inputLabel( $this->msg( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
243 Xml::submitButton( $this->msg( 'emailusernamesubmit' )->text() ) .
244 Xml::closeElement( 'fieldset' ) .
245 Xml::closeElement( 'form' ) . "\n";
246 return $string;
247 }
248
249 /**
250 * Submit callback for an HTMLForm object, will simply call submit().
251 *
252 * @since 1.20
253 * @param $data array
254 * @param $form HTMLForm object
255 * @return Status|string|bool
256 */
257 public static function uiSubmit( array $data, HTMLForm $form ) {
258 return self::submit( $data, $form->getContext() );
259 }
260
261 /**
262 * Really send a mail. Permissions should have been checked using
263 * getPermissionsError(). It is probably also a good
264 * idea to check the edit token and ping limiter in advance.
265 *
266 * @return Mixed: Status object, or potentially a String on error
267 * or maybe even true on success if anything uses the EmailUser hook.
268 */
269 public static function submit( array $data, IContextSource $context ) {
270 global $wgUserEmailUseReplyTo;
271
272 $target = self::getTarget( $data['Target'] );
273 if( !$target instanceof User ) {
274 return $context->msg( $target . 'text' )->parseAsBlock();
275 }
276 $to = new MailAddress( $target );
277 $from = new MailAddress( $context->getUser() );
278 $subject = $data['Subject'];
279 $text = $data['Text'];
280
281 // Add a standard footer and trim up trailing newlines
282 $text = rtrim( $text ) . "\n\n-- \n";
283 $text .= $context->msg( 'emailuserfooter',
284 $from->name, $to->name )->inContentLanguage()->text();
285
286 $error = '';
287 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
288 return $error;
289 }
290
291 if( $wgUserEmailUseReplyTo ) {
292 // Put the generic wiki autogenerated address in the From:
293 // header and reserve the user for Reply-To.
294 //
295 // This is a bit ugly, but will serve to differentiate
296 // wiki-borne mails from direct mails and protects against
297 // SPF and bounce problems with some mailers (see below).
298 global $wgPasswordSender, $wgPasswordSenderName;
299 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
300 $replyTo = $from;
301 } else {
302 // Put the sending user's e-mail address in the From: header.
303 //
304 // This is clean-looking and convenient, but has issues.
305 // One is that it doesn't as clearly differentiate the wiki mail
306 // from "directly" sent mails.
307 //
308 // Another is that some mailers (like sSMTP) will use the From
309 // address as the envelope sender as well. For open sites this
310 // can cause mails to be flunked for SPF violations (since the
311 // wiki server isn't an authorized sender for various users'
312 // domains) as well as creating a privacy issue as bounces
313 // containing the recipient's e-mail address may get sent to
314 // the sending user.
315 $mailFrom = $from;
316 $replyTo = null;
317 }
318
319 $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
320
321 if( !$status->isGood() ) {
322 return $status;
323 } else {
324 // if the user requested a copy of this mail, do this now,
325 // unless they are emailing themselves, in which case one
326 // copy of the message is sufficient.
327 if ( $data['CCMe'] && $to != $from ) {
328 $cc_subject = $context->msg( 'emailccsubject' )->rawParams(
329 $target->getName(), $subject )->text();
330 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
331 $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
332 $status->merge( $ccStatus );
333 }
334
335 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
336 return $status;
337 }
338 }
339 }