From a9fd7beaef20a07154850a181de05f80cbe2dbe1 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Wed, 27 Aug 2008 13:36:39 +0000 Subject: [PATCH] * Fixup validation methods in UserEmailForm a bit so that they don't return arrays when it's not necessary. * Add email errors to the API's message map --- includes/api/ApiBase.php | 4 +++ includes/api/ApiEmailUser.php | 9 +++++-- includes/specials/SpecialEmailuser.php | 36 ++++++++++++++------------ 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index ea652fbc0b..445451f703 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -633,7 +633,11 @@ abstract class ApiBase { 'ipb_blocked_as_range' => array('code' => 'blockedasrange', 'info' => "IP address ``\$1'' was blocked as part of range ``\$2''. You can't unblock the IP invidually, but you can unblock the range as a whole."), 'ipb_cant_unblock' => array('code' => 'cantunblock', 'info' => "The block you specified was not found. It may have been unblocked already"), 'mailnologin' => array('code' => 'cantsend', 'info' => "You're not logged in or you don't have a confirmed e-mail address, so you can't send e-mail"), + 'usermaildisabled' => array('code' => 'usermaildisabled' 'info' => "User email has been disabled"), 'blockedemailuser' => array('code' => 'blockedfrommail', 'info' => "You have been blocked from sending e-mail"), + 'notarget' => array('code' => 'notarget', 'info' => "You have not specified a valid target for this action"), + 'noemail' => array('code' => 'noemail', 'info' => "The user has not specified a valid e-mail address, or has chosen not to receive e-mail from other users"), + // API-specific messages diff --git a/includes/api/ApiEmailUser.php b/includes/api/ApiEmailUser.php index 7e083536c9..5e2f64a0ae 100644 --- a/includes/api/ApiEmailUser.php +++ b/includes/api/ApiEmailUser.php @@ -39,6 +39,11 @@ class ApiEmailUser extends ApiBase { public function execute() { global $wgUser; + + // Check whether email is enabled + if ( !EmailUserForm::userEmailEnabled() ) + $this->dieUsageMsg( array( 'usermaildisabled' ) ); + $this->getMain()->requestWriteMode(); $params = $this->extractRequestParams(); @@ -53,12 +58,12 @@ class ApiEmailUser extends ApiBase { // Validate target $targetUser = EmailUserForm::validateEmailTarget( $params['target'] ); if ( !( $targetUser instanceof User ) ) - $this->dieUsageMsg( array( $targetUser[0] ) ); + $this->dieUsageMsg( array( $targetUser ) ); // Check permissions $error = EmailUserForm::getPermissionsError( $wgUser, $params['token'] ); if ( $error ) - $this->dieUsageMsg( array( $error[0] ) ); + $this->dieUsageMsg( array( $error ) ); $form = new EmailUserForm( $targetUser, $params['text'], $params['subject'], $params['ccme'] ); diff --git a/includes/specials/SpecialEmailuser.php b/includes/specials/SpecialEmailuser.php index d557c847d2..678119ae95 100644 --- a/includes/specials/SpecialEmailuser.php +++ b/includes/specials/SpecialEmailuser.php @@ -10,12 +10,17 @@ function wfSpecialEmailuser( $par ) { global $wgRequest, $wgUser, $wgOut; + if ( !EmailUserForm::userEmailEnabled() ) { + $wgOut->showErrorPage( 'nosuchspecialpage', 'nospecialpagetext' ); + return; + } + $action = $wgRequest->getVal( 'action' ); $target = isset($par) ? $par : $wgRequest->getVal( 'target' ); $targetUser = EmailUserForm::validateEmailTarget( $target ); if ( !( $targetUser instanceof User ) ) { - $wgOut->showErrorPage( $targetUser[0], $targetUser[1] ); + $wgOut->showErrorPage( $targetUser.'title', $targetUser.'text' ); return; } @@ -30,7 +35,7 @@ function wfSpecialEmailuser( $par ) { $error = EmailUserForm::getPermissionsError( $wgUser, $wgRequest->getVal( 'wpEditToken' ) ); if ( $error ) { - switch ( $error[0] ) { + switch ( $error ) { case 'blockedemailuser': $wgOut->blockedPage(); return; @@ -40,12 +45,11 @@ function wfSpecialEmailuser( $par ) { case 'sessionfailure': $form->showForm(); return; - default: - $wgOut->showErrorPage( $error[0], $error[1] ); + case 'mailnologin': + $wgOut->showErrorPage( 'mailnologin', 'mailnologintext' ); return; } } - if ( "submit" == $action && $wgRequest->wasPosted() ) { $result = $form->doSubmit(); @@ -228,27 +232,27 @@ class EmailUserForm { return $this->target; } - static function validateEmailTarget ( $target ) { + static function userEmailEnabled() { global $wgEnableEmail, $wgEnableUserEmail; - - if( !( $wgEnableEmail && $wgEnableUserEmail ) ) - return array( "nosuchspecialpage", "nospecialpagetext" ); + return $wgEnableEmail && $wgEnableEmail; + } + static function validateEmailTarget ( $target ) { if ( "" == $target ) { wfDebug( "Target is empty.\n" ); - return array( "notargettitle", "notargettext" ); + return "notarget"; } $nt = Title::newFromURL( $target ); if ( is_null( $nt ) ) { wfDebug( "Target is invalid title.\n" ); - return array( "notargettitle", "notargettext" ); + return "notarget"; } $nu = User::newFromName( $nt->getText() ); if( is_null( $nu ) || !$nu->canReceiveEmail() ) { wfDebug( "Target is invalid user or can't receive.\n" ); - return array( "noemailtitle", "noemailtext" ); + return "noemail"; } return $nu; @@ -256,22 +260,22 @@ class EmailUserForm { static function getPermissionsError ( $user, $editToken ) { if( !$user->canSendEmail() ) { wfDebug( "User can't send.\n" ); - return array( "mailnologin", "mailnologintext" ); + return "mailnologin"; } if( $user->isBlockedFromEmailuser() ) { wfDebug( "User is blocked from sending e-mail.\n" ); - return array( "blockedemailuser", "" ); + return "blockedemailuser"; } if( $user->pingLimiter( 'emailuser' ) ) { wfDebug( "Ping limiter triggered.\n" ); - return array( 'actionthrottledtext', '' ); + return 'actionthrottledtext'; } if( !$user->matchEditToken( $editToken ) ) { wfDebug( "Matching edit token failed.\n" ); - return array( 'sessionfailure', '' ); + return 'sessionfailure'; } return; -- 2.20.1