* (bug 28444) Fix regression: edit-on-doubleclick retains revision id again
[mediawiki.git] / includes / specials / SpecialEmailuser.php
blob098d936c4614083623fd5c36f4051fc23b5bf0ab
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 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() )
46 'label-message' => 'emailfrom',
47 'id' => 'mw-emailuser-sender',
49 'To' => array(
50 'type' => 'info',
51 'raw' => 1,
52 'default' => $wgUser->getSkin()->link(
53 $this->mTargetObj->getUserPage(),
54 htmlspecialchars( $this->mTargetObj->getName() )
56 'label-message' => 'emailto',
57 'id' => 'mw-emailuser-recipient',
59 'Target' => array(
60 'type' => 'hidden',
61 'default' => $this->mTargetObj->getName(),
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,
71 'Text' => array(
72 'type' => 'textarea',
73 'rows' => 20,
74 'cols' => 80,
75 'label-message' => 'emailmessage',
76 'required' => 1,
78 'CCMe' => array(
79 'type' => 'check',
80 'label-message' => 'emailccme',
81 'default' => $wgUser->getBoolOption( 'ccmeonemails' ),
86 public function execute( $par ) {
87 global $wgRequest, $wgOut, $wgUser;
89 $this->setHeaders();
90 $this->outputHeader();
91 $wgOut->addModuleStyles( 'mediawiki.special' );
93 $this->mTarget = is_null( $par )
94 ? $wgRequest->getVal( 'wpTarget', $wgRequest->getVal( 'target', '' ) )
95 : $par;
97 $ret = self::getTarget( $this->mTarget );
98 if( $ret instanceof User ){
99 $this->mTargetObj = $ret;
100 } else {
101 $wgOut->showErrorPage( "{$ret}title", "{$ret}text" );
102 return false;
105 $error = self::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
106 switch ( $error ) {
107 case null:
108 # Wahey!
109 break;
110 case 'badaccess':
111 $wgOut->permissionRequired( 'sendemail' );
112 return;
113 case 'blockedemailuser':
114 $wgOut->blockedPage();
115 return;
116 case 'actionthrottledtext':
117 $wgOut->rateLimited();
118 return;
119 case 'mailnologin':
120 case 'usermaildisabled':
121 $wgOut->showErrorPage( $error, "{$error}text" );
122 return;
123 default:
124 # It's a hook error
125 list( $title, $msg, $params ) = $error;
126 $wgOut->showErrorPage( $title, $msg, $params );
127 return;
130 $form = new HTMLForm( $this->getFormFields() );
131 $form->addPreText( wfMsgExt( 'emailpagetext', 'parseinline' ) );
132 $form->setSubmitText( wfMsg( 'emailsend' ) );
133 $form->setTitle( $this->getTitle() );
134 $form->setSubmitCallback( array( __CLASS__, 'submit' ) );
135 $form->setWrapperLegend( wfMsgExt( 'email-legend', 'parsemag' ) );
136 $form->loadData();
138 if( !wfRunHooks( 'EmailUserForm', array( &$form ) ) ){
139 return false;
142 $wgOut->setPagetitle( wfMsg( 'emailpage' ) );
143 $result = $form->show();
145 if( $result === true || ( $result instanceof Status && $result->isGood() ) ){
146 $wgOut->setPagetitle( wfMsg( 'emailsent' ) );
147 $wgOut->addWikiMsg( 'emailsenttext' );
148 $wgOut->returnToMain( false, $this->mTargetObj->getUserPage() );
153 * Validate target User
155 * @param $target String: target user name
156 * @return User object on success or a string on error
158 public static function getTarget( $target ) {
159 if ( $target == '' ) {
160 wfDebug( "Target is empty.\n" );
161 return 'notarget';
164 $nu = User::newFromName( $target );
165 if( !$nu instanceof User || !$nu->getId() ) {
166 wfDebug( "Target is invalid user.\n" );
167 return 'notarget';
168 } else if ( !$nu->isEmailConfirmed() ) {
169 wfDebug( "User has no valid email.\n" );
170 return 'noemail';
171 } else if ( !$nu->canReceiveEmail() ) {
172 wfDebug( "User does not allow user emails.\n" );
173 return 'nowikiemail';
176 return $nu;
180 * Check whether a user is allowed to send email
182 * @param $user User object
183 * @param $editToken String: edit token
184 * @return null on success or string on error
186 public static function getPermissionsError( $user, $editToken ) {
187 global $wgEnableEmail, $wgEnableUserEmail;
188 if( !$wgEnableEmail || !$wgEnableUserEmail ){
189 return 'usermaildisabled';
192 if( !$user->isAllowed( 'sendemail' ) ) {
193 return 'badaccess';
196 if( !$user->isEmailConfirmed() ){
197 return 'mailnologin';
200 if( $user->isBlockedFromEmailuser() ) {
201 wfDebug( "User is blocked from sending e-mail.\n" );
202 return "blockedemailuser";
205 if( $user->pingLimiter( 'emailuser' ) ) {
206 wfDebug( "Ping limiter triggered.\n" );
207 return 'actionthrottledtext';
210 $hookErr = false;
211 wfRunHooks( 'UserCanSendEmail', array( &$user, &$hookErr ) );
212 wfRunHooks( 'EmailUserPermissionsErrors', array( $user, $editToken, &$hookErr ) );
213 if ( $hookErr ) {
214 return $hookErr;
217 return null;
221 * Really send a mail. Permissions should have been checked using
222 * getPermissionsError(). It is probably also a good
223 * idea to check the edit token and ping limiter in advance.
225 * @return Mixed: Status object, or potentially a String on error
226 * or maybe even true on success if anything uses the EmailUser hook.
228 public static function submit( $data ) {
229 global $wgUser, $wgUserEmailUseReplyTo;
231 $target = self::getTarget( $data['Target'] );
232 if( !$target instanceof User ){
233 return wfMsgExt( $target . 'text', 'parse' );
235 $to = new MailAddress( $target );
236 $from = new MailAddress( $wgUser );
237 $subject = $data['Subject'];
238 $text = $data['Text'];
240 // Add a standard footer and trim up trailing newlines
241 $text = rtrim( $text ) . "\n\n-- \n";
242 $text .= wfMsgExt(
243 'emailuserfooter',
244 array( 'content', 'parsemag' ),
245 array( $from->name, $to->name )
248 $error = '';
249 if( !wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$text, &$error ) ) ) {
250 return $error;
253 if( $wgUserEmailUseReplyTo ) {
254 // Put the generic wiki autogenerated address in the From:
255 // header and reserve the user for Reply-To.
257 // This is a bit ugly, but will serve to differentiate
258 // wiki-borne mails from direct mails and protects against
259 // SPF and bounce problems with some mailers (see below).
260 global $wgPasswordSender, $wgPasswordSenderName;
261 $mailFrom = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
262 $replyTo = $from;
263 } else {
264 // Put the sending user's e-mail address in the From: header.
266 // This is clean-looking and convenient, but has issues.
267 // One is that it doesn't as clearly differentiate the wiki mail
268 // from "directly" sent mails.
270 // Another is that some mailers (like sSMTP) will use the From
271 // address as the envelope sender as well. For open sites this
272 // can cause mails to be flunked for SPF violations (since the
273 // wiki server isn't an authorized sender for various users'
274 // domains) as well as creating a privacy issue as bounces
275 // containing the recipient's e-mail address may get sent to
276 // the sending user.
277 $mailFrom = $from;
278 $replyTo = null;
281 $status = UserMailer::send( $to, $mailFrom, $subject, $text, $replyTo );
283 if( !$status->isGood() ) {
284 return $status;
285 } else {
286 // if the user requested a copy of this mail, do this now,
287 // unless they are emailing themselves, in which case one
288 // copy of the message is sufficient.
289 if ( $data['CCMe'] && $to != $from ) {
290 $cc_subject = wfMsg(
291 'emailccsubject',
292 $target->getName(),
293 $subject
295 wfRunHooks( 'EmailUserCC', array( &$from, &$from, &$cc_subject, &$text ) );
296 $ccStatus = UserMailer::send( $from, $from, $cc_subject, $text );
297 $status->merge( $ccStatus );
300 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $text ) );
301 return $status;