00001 <?php
00010 function wfSpecialEmailuser( $par ) {
00011 global $wgRequest, $wgUser, $wgOut;
00012
00013 if ( !EmailUserForm::userEmailEnabled() ) {
00014 $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' );
00015 return;
00016 }
00017
00018 $action = $wgRequest->getVal( 'action' );
00019 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
00020 $targetUser = EmailUserForm::validateEmailTarget( $target );
00021
00022 if ( !( $targetUser instanceof User ) ) {
00023 $wgOut->showErrorPage( $targetUser.'title', $targetUser.'text' );
00024 return;
00025 }
00026
00027 $form = new EmailUserForm( $targetUser,
00028 $wgRequest->getText( 'wpText' ),
00029 $wgRequest->getText( 'wpSubject' ),
00030 $wgRequest->getBool( 'wpCCMe' ) );
00031 if ( $action == 'success' ) {
00032 $form->showSuccess();
00033 return;
00034 }
00035
00036 $error = EmailUserForm::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) );
00037 if ( $error ) {
00038 switch ( $error ) {
00039 case 'blockedemailuser':
00040 $wgOut->blockedPage();
00041 return;
00042 case 'actionthrottledtext':
00043 $wgOut->rateLimited();
00044 return;
00045 case 'sessionfailure':
00046 $form->showForm();
00047 return;
00048 case 'mailnologin':
00049 $wgOut->showErrorPage( 'mailnologin', 'mailnologintext' );
00050 return;
00051 }
00052 }
00053
00054 if ( "submit" == $action && $wgRequest->wasPosted() ) {
00055 $result = $form->doSubmit();
00056
00057 if ( !is_null( $result ) ) {
00058 $wgOut->addHTML( wfMsg( "usermailererror" ) .
00059 ' ' . htmlspecialchars( $result->getMessage() ) );
00060 } else {
00061 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
00062 $encTarget = wfUrlencode( $form->getTarget()->getName() );
00063 $wgOut->redirect( $titleObj->getFullURL( "target={$encTarget}&action=success" ) );
00064 }
00065 } else {
00066 $form->showForm();
00067 }
00068 }
00069
00074 class EmailUserForm {
00075
00076 var $target;
00077 var $text, $subject;
00078 var $cc_me;
00079
00083 function EmailUserForm( $target, $text, $subject, $cc_me ) {
00084 $this->target = $target;
00085 $this->text = $text;
00086 $this->subject = $subject;
00087 $this->cc_me = $cc_me;
00088 }
00089
00090 function showForm() {
00091 global $wgOut, $wgUser;
00092 $skin = $wgUser->getSkin();
00093
00094 $wgOut->setPagetitle( wfMsg( "emailpage" ) );
00095 $wgOut->addWikiMsg( "emailpagetext" );
00096
00097 if ( $this->subject === "" ) {
00098 $this->subject = wfMsgExt( 'defemailsubject', array( 'content', 'parsemag' ) );
00099 }
00100
00101 $titleObj = SpecialPage::getTitleFor( "Emailuser" );
00102 $action = $titleObj->getLocalURL( "target=" .
00103 urlencode( $this->target->getName() ) . "&action=submit" );
00104
00105 $wgOut->addHTML(
00106 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'emailuser' ) ) .
00107 Xml::openElement( 'fieldset' ) .
00108 Xml::element( 'legend', null, wfMsgExt( 'email-legend', 'parsemag' ) ) .
00109 Xml::openElement( 'table', array( 'class' => 'mw-emailuser-table' ) ) .
00110 "<tr>
00111 <td class='mw-label'>" .
00112 Xml::label( wfMsg( 'emailfrom' ), 'emailfrom' ) .
00113 "</td>
00114 <td class='mw-input' id='mw-emailuser-sender'>" .
00115 $skin->link( $wgUser->getUserPage(), htmlspecialchars( $wgUser->getName() ) ) .
00116 "</td>
00117 </tr>
00118 <tr>
00119 <td class='mw-label'>" .
00120 Xml::label( wfMsg( 'emailto' ), 'emailto' ) .
00121 "</td>
00122 <td class='mw-input' id='mw-emailuser-recipient'>" .
00123 $skin->link( $this->target->getUserPage(), htmlspecialchars( $this->target->getName() ) ) .
00124 "</td>
00125 </tr>
00126 <tr>
00127 <td class='mw-label'>" .
00128 Xml::label( wfMsg( 'emailsubject' ), 'wpSubject' ) .
00129 "</td>
00130 <td class='mw-input'>" .
00131 Xml::input( 'wpSubject', 60, $this->subject, array( 'type' => 'text', 'maxlength' => 200 ) ) .
00132 "</td>
00133 </tr>
00134 <tr>
00135 <td class='mw-label'>" .
00136 Xml::label( wfMsg( 'emailmessage' ), 'wpText' ) .
00137 "</td>
00138 <td class='mw-input'>" .
00139 Xml::textarea( 'wpText', $this->text, 80, 20, array( 'id' => 'wpText' ) ) .
00140 "</td>
00141 </tr>
00142 <tr>
00143 <td></td>
00144 <td class='mw-input'>" .
00145 Xml::checkLabel( wfMsg( 'emailccme' ), 'wpCCMe', 'wpCCMe', $wgUser->getBoolOption( 'ccmeonemails' ) ) .
00146 "</td>
00147 </tr>
00148 <tr>
00149 <td></td>
00150 <td class='mw-submit'>" .
00151 Xml::submitButton( wfMsg( 'emailsend' ), array( 'name' => 'wpSend', 'accesskey' => 's' ) ) .
00152 "</td>
00153 </tr>" .
00154 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
00155 Xml::closeElement( 'table' ) .
00156 Xml::closeElement( 'fieldset' ) .
00157 Xml::closeElement( 'form' )
00158 );
00159 }
00160
00161
00162
00163
00164
00165
00166 function doSubmit() {
00167 global $wgUser, $wgUserEmailUseReplyTo, $wgSiteName;
00168
00169 $to = new MailAddress( $this->target );
00170 $from = new MailAddress( $wgUser );
00171 $subject = $this->subject;
00172
00173
00174 $this->text = rtrim($this->text) . "\n\n-- \n" . wfMsgExt( 'emailuserfooter',
00175 array( 'content', 'parsemag' ), array( $from->name, $to->name ) );
00176
00177 if( wfRunHooks( 'EmailUser', array( &$to, &$from, &$subject, &$this->text ) ) ) {
00178
00179 if( $wgUserEmailUseReplyTo ) {
00180
00181
00182
00183
00184
00185
00186 global $wgPasswordSender;
00187 $mailFrom = new MailAddress( $wgPasswordSender );
00188 $replyTo = $from;
00189 } else {
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203 $mailFrom = $from;
00204 $replyTo = null;
00205 }
00206
00207 $mailResult = UserMailer::send( $to, $mailFrom, $subject, $this->text, $replyTo );
00208
00209 if( WikiError::isError( $mailResult ) ) {
00210 return $mailResult;
00211
00212 } else {
00213
00214
00215
00216 if ($this->cc_me && $to != $from) {
00217 $cc_subject = wfMsg('emailccsubject', $this->target->getName(), $subject);
00218 if( wfRunHooks( 'EmailUser', array( &$from, &$from, &$cc_subject, &$this->text ) ) ) {
00219 $ccResult = UserMailer::send( $from, $from, $cc_subject, $this->text );
00220 if( WikiError::isError( $ccResult ) ) {
00221
00222
00223
00224
00225
00226 return $ccResult;
00227 }
00228 }
00229 }
00230
00231 wfRunHooks( 'EmailUserComplete', array( $to, $from, $subject, $this->text ) );
00232 return;
00233 }
00234 }
00235 }
00236
00237 function showSuccess( &$user = null ) {
00238 global $wgOut;
00239
00240 if ( is_null($user) )
00241 $user = $this->target;
00242
00243 $wgOut->setPagetitle( wfMsg( "emailsent" ) );
00244 $wgOut->addWikiMsg( 'emailsenttext' );
00245
00246 $wgOut->returnToMain( false, $user->getUserPage() );
00247 }
00248
00249 function getTarget() {
00250 return $this->target;
00251 }
00252
00253 static function userEmailEnabled() {
00254 global $wgEnableEmail, $wgEnableUserEmail;
00255 return $wgEnableEmail && $wgEnableUserEmail;
00256
00257 }
00258 static function validateEmailTarget ( $target ) {
00259 if ( "" == $target ) {
00260 wfDebug( "Target is empty.\n" );
00261 return "notarget";
00262 }
00263
00264 $nt = Title::newFromURL( $target );
00265 if ( is_null( $nt ) ) {
00266 wfDebug( "Target is invalid title.\n" );
00267 return "notarget";
00268 }
00269
00270 $nu = User::newFromName( $nt->getText() );
00271 if( is_null( $nu ) || !$nu->getId() ) {
00272 wfDebug( "Target is invalid user.\n" );
00273 return "notarget";
00274 } else if ( !$nu->isEmailConfirmed() ) {
00275 wfDebug( "User has no valid email.\n" );
00276 return "noemail";
00277 } else if ( !$nu->canReceiveEmail() ) {
00278 wfDebug( "User does not allow user emails.\n" );
00279 return "nowikiemail";
00280 }
00281
00282 return $nu;
00283 }
00284 static function getPermissionsError ( $user, $editToken ) {
00285 if( !$user->canSendEmail() ) {
00286 wfDebug( "User can't send.\n" );
00287 return "mailnologin";
00288 }
00289
00290 if( $user->isBlockedFromEmailuser() ) {
00291 wfDebug( "User is blocked from sending e-mail.\n" );
00292 return "blockedemailuser";
00293 }
00294
00295 if( $user->pingLimiter( 'emailuser' ) ) {
00296 wfDebug( "Ping limiter triggered.\n" );
00297 return 'actionthrottledtext';
00298 }
00299
00300 if( !$user->matchEditToken( $editToken ) ) {
00301 wfDebug( "Matching edit token failed.\n" );
00302 return 'sessionfailure';
00303 }
00304
00305 return;
00306 }
00307
00308 static function newFromURL( $target, $text, $subject, $cc_me )
00309 {
00310 $nt = Title::newFromURL( $target );
00311 $nu = User::newFromName( $nt->getText() );
00312 return new EmailUserForm( $nu, $text, $subject, $cc_me );
00313 }
00314 }