Use local context instead of global variables
[lhc/web/wiklou.git] / includes / specials / SpecialPasswordReset.php
index fe9d924..476ab05 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Implements Special:Blankpage
+ * Implements Special:PasswordReset
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -33,26 +33,13 @@ class SpecialPasswordReset extends FormSpecialPage {
        }
 
        public function userCanExecute( User $user ) {
-               global $wgPasswordResetRoutes, $wgAuth;
-
-               // Maybe password resets are disabled, or there are no allowable routes
-               if ( !is_array( $wgPasswordResetRoutes )
-                       || !in_array( true, array_values( $wgPasswordResetRoutes ) ) )
-               {
-                       throw new ErrorPageError( 'internalerror', 'passwordreset-disabled' );
-               }
-
-               // Maybe the external auth plugin won't allow local password changes
-               if ( !$wgAuth->allowPasswordChange() ) {
+               $error = $this->canChangePassword( $user );
+               if ( is_string( $error ) ) {
+                       throw new ErrorPageError( 'internalerror', $error );
+               } else if ( !$error ) {
                        throw new ErrorPageError( 'internalerror', 'resetpass_forbidden' );
                }
 
-               // Maybe the user is blocked (check this here rather than relying on the parent
-               // method as we have a more specific error message to use here
-               if ( $user->isBlocked() ) {
-                       throw new ErrorPageError( 'internalerror', 'blocked-mailpassword' );
-               }
-
                return parent::userCanExecute( $user );
        }
 
@@ -76,6 +63,10 @@ class SpecialPasswordReset extends FormSpecialPage {
                return $a;
        }
 
+       public function alterForm( HTMLForm $form ) {
+               $form->setSubmitText( wfMessage( "mailmypassword" ) );
+       }
+
        protected function preText() {
                global $wgPasswordResetRoutes;
                $i = 0;
@@ -105,8 +96,6 @@ class SpecialPasswordReset extends FormSpecialPage {
                        && Sanitizer::validateEmail( $data['Email'] ) )
                {
                        $method = 'email';
-
-                       // FIXME: this is an unindexed query
                        $res = wfGetDB( DB_SLAVE )->select(
                                'user',
                                '*',
@@ -143,7 +132,7 @@ class SpecialPasswordReset extends FormSpecialPage {
                }
 
                $firstUser = $users[0];
-               
+
                if ( !$firstUser instanceof User || !$firstUser->getID() ) {
                        return array( array( 'nosuchuser', $data['Username'] ) );
                }
@@ -173,7 +162,7 @@ class SpecialPasswordReset extends FormSpecialPage {
 
                // We need to have a valid IP address for the hook, but per bug 18347, we should
                // send the user's name if they're logged in.
-               $ip = wfGetIP();
+               $ip = $this->getRequest()->getIP();
                if ( !$ip ) {
                        return array( 'badipaddress' );
                }
@@ -212,7 +201,7 @@ class SpecialPasswordReset extends FormSpecialPage {
                if ( $result->isGood() ) {
                        return true;
                } else {
-                       // FIXME: The email didn't send, but we have already set the password throttle
+                       // @todo FIXME: The email didn't send, but we have already set the password throttle
                        // timestamp, so they won't be able to try again until it expires...  :(
                        return array( array( 'mailerror', $result->getMessage() ) );
                }
@@ -222,4 +211,42 @@ class SpecialPasswordReset extends FormSpecialPage {
                $this->getOutput()->addWikiMsg( 'passwordreset-emailsent' );
                $this->getOutput()->returnToMain();
        }
+
+       protected function canChangePassword( User $user ) {
+               global $wgPasswordResetRoutes, $wgAuth;
+
+               // Maybe password resets are disabled, or there are no allowable routes
+               if ( !is_array( $wgPasswordResetRoutes ) ||
+                        !in_array( true, array_values( $wgPasswordResetRoutes ) ) )
+               {
+                       return 'passwordreset-disabled';
+               }
+
+               // Maybe the external auth plugin won't allow local password changes
+               if ( !$wgAuth->allowPasswordChange() ) {
+                       return 'resetpass_forbidden';
+               }
+
+               // Maybe the user is blocked (check this here rather than relying on the parent
+               // method as we have a more specific error message to use here
+               if ( $user->isBlocked() ) {
+                       return 'blocked-mailpassword';
+               }
+
+               return true;
+       }
+
+       /**
+        * Hide the password reset page if resets are disabled.
+        * @return Bool
+        */
+       function isListed() {
+               global $wgUser;
+
+               if ( $this->canChangePassword( $wgUser ) === true ) {
+                       return parent::isListed();
+               }
+
+               return false;
+       }
 }