* re-commit r58172 with the fix for the issue mentioned where users would not be...
[lhc/web/wiklou.git] / includes / User.php
index 9fd2598..8435414 100644 (file)
@@ -354,8 +354,10 @@ class User {
                        $validate = 'valid';
                }
                $name = self::getCanonicalName( $name, $validate );
-               if ( $name === false ) {
-                       return null;
+               if ( WikiError::isError( $name ) ) {
+                       return $name;
+               } elseif ( $name === false ) {
+                       return false;
                } else {
                        # Create unloaded user object
                        $u = new User;
@@ -622,16 +624,8 @@ class User {
         * @return bool True or false
         */
        function isValidPassword( $password ) {
-               global $wgMinimalPasswordLength, $wgContLang;
-
-               if( !wfRunHooks( 'isValidPassword', array( $password, &$result, $this ) ) )
-                       return $result;
-               if( $result === false )
-                       return false;
-               // Password needs to be long enough, and can't be the same as the username
-               return strlen( $password ) >= $wgMinimalPasswordLength
-                       && $wgContLang->lc( $password ) !== $wgContLang->lc( $this->mName );
+               //simple boolean wrapper for getPasswordValidity
+               return $this->getPasswordValidity( $password ) === true;
        }
 
        /**
@@ -640,18 +634,30 @@ class User {
         * @param $password String Desired password
         * @return mixed: true on success, string of error message on failure
         */
-       static function getPasswordValidity( $password ) {
+       function getPasswordValidity( $password ) {
                global $wgMinimalPasswordLength, $wgContLang;
                
-               if(!$this->isValidPassword( $password ))        {
+               $result = false; //init $result to false for the internal checks
+               
+               if( !wfRunHooks( 'isValidPassword', array( $password, &$result, $this ) ) )
+                       return $result;
+               
+               if ( $result === false ) {
                        if( strlen( $password ) < $wgMinimalPasswordLength ) {
                                return 'passwordtooshort';
-                       } elseif( $wgContLang->lc( $password ) == $wgContLang->lc( $this->mName ) ) {
+                       } elseif ( $wgContLang->lc( $password ) == $wgContLang->lc( $this->mName ) ) {
                                return 'password-name-match';
+                       } else {
+                               //it seems weird returning true here, but this is because of the
+                               //initialization of $result to false above. If the hook is never run or it
+                               //doesn't modify $result, then we will likely get down into this if with
+                               //a valid password.
+                               return true;
                        }
-               }
-               else    {
+               } elseif( $result === true ) {
                        return true;
+               } else {
+                       return $result; //the isValidPassword hook set a string $result and returned true
                }
        }
 
@@ -694,7 +700,7 @@ class User {
                # with title normalisation, but then it's too late to
                # check elsewhere
                if( strpos( $name, '#' ) !== false )
-                       return false;
+                       return new WikiError( 'usernamehasherror' );
 
                # Clean up name according to title rules
                $t = ( $validate === 'valid' ) ?
@@ -1184,27 +1190,33 @@ class User {
         * Whether the given IP is in a given DNS blacklist.
         *
         * @param $ip \string IP to check
-        * @param $base \string URL of the DNS blacklist
+        * @param $bases \string or Array of Strings: URL of the DNS blacklist
         * @return \bool True if blacklisted.
         */
-       function inDnsBlacklist( $ip, $base ) {
+       function inDnsBlacklist( $ip, $bases ) {
                wfProfileIn( __METHOD__ );
 
                $found = false;
                $host = '';
                // FIXME: IPv6 ???  (http://bugs.php.net/bug.php?id=33170)
                if( IP::isIPv4( $ip ) ) {
-                       # Make hostname
-                       $host = "$ip.$base";
+                       # Reverse IP, bug 21255
+                       $ipReversed = implode( '.', array_reverse( explode( '.', $ip ) ) );
 
-                       # Send query
-                       $ipList = gethostbynamel( $host );
+                       foreach( (array)$bases as $base ) {
+                               # Make hostname
+                               $host = "$ipReversed.$base";
 
-                       if( $ipList ) {
-                               wfDebug( "Hostname $host is {$ipList[0]}, it's a proxy says $base!\n" );
-                               $found = true;
-                       } else {
-                               wfDebug( "Requested $host, not found in $base.\n" );
+                               # Send query
+                               $ipList = gethostbynamel( $host );
+
+                               if( $ipList ) {
+                                       wfDebug( "Hostname $host is {$ipList[0]}, it's a proxy says $base!\n" );
+                                       $found = true;
+                                       break;
+                               } else {
+                                       wfDebug( "Requested $host, not found in $base.\n" );
+                               }
                        }
                }
 
@@ -2054,6 +2066,7 @@ class User {
         */
        function getEffectiveGroups( $recache = false ) {
                if ( $recache || is_null( $this->mEffectiveGroups ) ) {
+                       wfProfileIn( __METHOD__ );
                        $this->mEffectiveGroups = $this->getGroups();
                        $this->mEffectiveGroups[] = '*';
                        if( $this->getId() ) {
@@ -2067,6 +2080,7 @@ class User {
                                # Hook for additional groups
                                wfRunHooks( 'UserEffectiveGroups', array( &$this, &$this->mEffectiveGroups ) );
                        }
+                       wfProfileOut( __METHOD__ );
                }
                return $this->mEffectiveGroups;
        }
@@ -2612,7 +2626,7 @@ class User {
         * Generate a string which will be different for any combination of
         * user options which would produce different parser output.
         * This will be used as part of the hash key for the parser cache,
-        * so users will the same options can share the same cached data
+        * so users with the same options can share the same cached data
         * safely.
         *
         * Extensions which require it should install 'PageRenderingHash' hook,