(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / specials / SpecialEmailuser.php
blob314da7274c955dbcf94486234902a830e6c12ddc
1 <?php
2 /**
3 * Implements Special:Emailuser
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page that allows users to send e-mails to other users
27 * @ingroup SpecialPage
29 class SpecialEmailUser extends UnlistedSpecialPage {
30 protected $mTarget;
32 public function __construct() {
33 parent::__construct( 'Emailuser' );
36 protected function getFormFields() {
37 return array(
38 'From' => array(
39 'type' => 'info',
40 'raw' => 1,
41 'default' => Linker::link(
42 $this->getUser()->getUserPage(),
43 htmlspecialchars( $this->getUser()->getName() )
45 'label-message' => 'emailfrom',
46 'id' => 'mw-emailuser-sender',
48 'To' => array(
49 'type' => 'info',
50 'raw' => 1,
51 'default' => Linker::link(
52 $this->mTargetObj->getUserPage(),
53 htmlspecialchars( $this->mTargetObj->getName() )
55 'label-message' => 'emailto',
56 'id' => 'mw-emailuser-recipient',
58 'Target' => array(
59 'type' => 'hidden',
60 'default' => $this->mTargetObj->getName(),
62 'Subject' => array(
63 'type' => 'text',
64 'default' => wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ), $this->getUser()->getName() ),
65 'label-message' => 'emailsubject',
66 'maxlength' => 200,
67 'size' => 60,
68 'required' => 1,
70 'Text' => array(
71 'type' => 'textarea',
72 'rows' => 20,
73 'cols' => 80,
74 'label-message' => 'emailmessage',
75 'required' => 1,
77 'CCMe' => array(
78 'type' => 'check',
79 'label-message' => 'emailccme',
80 'default' => $this->getUser()->getBoolOption( 'ccmeonemails' ),
85 public function execute( $par ) {
86 $this->setHeaders();
87 $this->outputHeader();
88 $out = $this->getOutput();
89 $out->addModuleStyles( 'mediawiki.special' );
90 $this->mTarget = is_null( $par )
91 ? $this->getRequest()->getVal( 'wpTarget', $this->getRequest()->getVal( 'target', '' ) )
92 : $par;
93 // error out if sending user cannot do this
94 $error = self::getPermissionsError( $this->getUser(), $this->getRequest()->getVal( 'wpEditToken' ) );
95 switch ( $error ) {
96 case null:
97 # Wahey!
98 break;
99 case 'badaccess':
100 throw new PermissionsError( 'sendemail' );
101 case 'blockedemailuser':
102 throw new UserBlockedError( $this->getUser()->mBlock );
103 case 'actionthrottledtext':
104 throw new ThrottledError;
105 case 'mailnologin':
106 case 'usermaildisabled':
107 throw new ErrorPageError( $error, "{$error}text" );
108 default:
109 # It's a hook error
110 list( $title, $msg, $params ) = $error;
111 throw new ErrorPageError( $title, $msg, $params );
113 // Got a valid target user name? Else ask for one.
114 $ret = self::getTarget( $this->mTarget );
115 if( !$ret instanceof User ) {
116 if( $this->mTarget != '' ) {
117 $ret = ( $ret == 'notarget' ) ? 'emailnotarget' : ( $ret . 'text' );
118 $out->wrapWikiMsg( "<p class='error'>$1</p>", $ret );
120 $out->addHTML( $this->userForm( $this->mTarget ) );
121 return false;
124 $this->mTargetObj = $ret;
126 $form = new HTMLForm( $this->getFormFields(), $this->getContext() );
127 $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
128 $form->setSubmitText( wfMsg( 'emailsend' ) );
129 $form->setTitle( $this->getTitle() );
130 $form->setSubmitCallback( array( __CLASS__, 'submit' ) );
131 $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
132 $form->loadData();
134 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ) {
135 return false;
138 $out->setPageTitle( $this->msg( 'emailpage' ) );
139 $result = $form->show();
141 if( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
142 $out->setPageTitle( $this->msg( 'emailsent' ) );
143 $out->addWikiMsg( 'emailsenttext' );
144 $out->returnToMain( false, $this->mTargetObj->getUserPage() );
149 * Validate target User
151 * @param $target String: target user name
152 * @return User object on success or a string on error
154 public static function getTarget( $target ) {
155 if ( $target == '' ) {
156 wfDebug( "Target is empty.\n" );
157 return 'notarget';
160 $nu = User::newFromName( $target );
161 if( !$nu instanceof User || !$nu->getId() ) {
162 wfDebug( "Target is invalid user.\n" );
163 return 'notarget';
164 } elseif ( !$nu->isEmailConfirmed() ) {
165 wfDebug( "User has no valid email.\n" );
166 return 'noemail';
167 } elseif ( !$nu->canReceiveEmail() ) {
168 wfDebug( "User does not allow user emails.\n" );
169 return 'nowikiemail';
172 return $nu;
176 * Check whether a user is allowed to send email
178 * @param $user User object
179 * @param $editToken String: edit token
180 * @return null on success or string on error
182 public static function getPermissionsError( $user, $editToken ) {
183 global $wgEnableEmail, $wgEnableUserEmail;
184 if( !$wgEnableEmail || !$wgEnableUserEmail ) {
185 return 'usermaildisabled';
188 if( !$user->isAllowed( 'sendemail' ) ) {
189 return 'badaccess';
192 if( !$user->isEmailConfirmed() ) {
193 return 'mailnologin';
196 if( $user->isBlockedFromEmailuser() ) {
197 wfDebug( "User is blocked from sending e-mail.\n" );
198 return "blockedemailuser";
201 if( $user->pingLimiter( 'emailuser' ) ) {
202 wfDebug( "Ping limiter triggered.\n" );
203 return 'actionthrottledtext';
206 $hookErr = false;
207 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
208 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
209 if ( $hookErr ) {
210 return $hookErr;
213 return null;
217 * Form to ask for target user name.
219 * @param $name String: user name submitted.
220 * @return String: form asking for user name.
222 protected function userForm( $name ) {
223 global $wgScript;
224 $string = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'askusername' ) ) .
225 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
226 Xml::openElement( 'fieldset' ) .
227 Html::rawElement( 'legend', null, wfMessage( 'emailtarget' )->parse() ) .
228 Xml::inputLabel( wfMessage( 'emailusername' )->text(), 'target', 'emailusertarget', 30, $name ) . ' ' .
229 Xml::submitButton( wfMessage( 'emailusernamesubmit' )->text() ) .
230 Xml::closeElement( 'fieldset' ) .
231 Xml::closeElement( 'form' ) . "\n";
232 return $string;
236 * Really send a mail. Permissions should have been checked using
237 * getPermissionsError(). It is probably also a good
238 * idea to check the edit token and ping limiter in advance.
240 * @return Mixed: Status object, or potentially a String on error
241 * or maybe even true on success if anything uses the EmailUser hook.
243 public static function submit( $data ) {
244 global $wgUser, $wgUserEmailUseReplyTo;
246 $target = self::getTarget( $data['Target'] );
247 if( !$target instanceof User ) {
248 return wfMsgExt( $target . 'text', 'parse' );
250 $to = new MailAddress( $target );
251 $from = new MailAddress( $wgUser );
252 $subject = $data['Subject'];
253 $text = $data['Text'];
255 // Add a standard footer and trim up trailing newlines
256 $text = rtrim( $text ) . "\n\n-- \n";
257 $text .= wfMsgExt(
258 'emailuserfooter',
259 array( 'content', 'parsemag' ),
260 array( $from->name, $to->name )
263 $error = '';
264 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
265 return $error;
268 if( $wgUserEmailUseReplyTo ) {
269 // Put the generic wiki autogenerated address in the From:
270 // header and reserve the user for Reply-To.
272 // This is a bit ugly, but will serve to differentiate
273 // wiki-borne mails from direct mails and protects against
274 // SPF and bounce problems with some mailers (see below).
275 global $wgPasswordSender, $wgPasswordSenderName;
276 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
277 $replyTo = $from;
278 } else {
279 // Put the sending user's e-mail address in the From: header.
281 // This is clean-looking and convenient, but has issues.
282 // One is that it doesn't as clearly differentiate the wiki mail
283 // from "directly" sent mails.
285 // Another is that some mailers (like sSMTP) will use the From
286 // address as the envelope sender as well. For open sites this
287 // can cause mails to be flunked for SPF violations (since the
288 // wiki server isn't an authorized sender for various users'
289 // domains) as well as creating a privacy issue as bounces
290 // containing the recipient's e-mail address may get sent to
291 // the sending user.
292 $mailFrom = $from;
293 $replyTo = null;
296 $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
298 if( !$status->isGood() ) {
299 return $status;
300 } else {
301 // if the user requested a copy of this mail, do this now,
302 // unless they are emailing themselves, in which case one
303 // copy of the message is sufficient.
304 if ( $data['CCMe'] && $to != $from ) {
305 $cc_subject = wfMsg(
306 'emailccsubject',
307 $target->getName(),
308 $subject
310 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
311 $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
312 $status->merge( $ccStatus );
315 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
316 return $status;