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