Kill some trailing whitespace
[lhc/web/wiklou.git] / includes / User.php
index 2da3968..8cdad6c 100644 (file)
@@ -350,14 +350,12 @@ class User {
         * addresses like this, if we allowed accounts like this to be created
         * new users could get the old edits of these anonymous users.
         *
-        * @bug 3631
-        *
         * @static
         * @param string $name Nickname of a user
         * @return bool
         */
        static function isIP( $name ) {
-               return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/',$name);
+               return preg_match('/^\d{1,3}\.\d{1,3}\.\d{1,3}\.(?:xxx|\d{1,3})$/',$name) || User::isIPv6($name);
                /*return preg_match("/^
                        (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
                        (?:[01]?\d{1,2}|2(:?[0-4]\d|5[0-5]))\.
@@ -366,6 +364,27 @@ class User {
                $/x", $name);*/
        }
 
+       /**
+        * Check if $name is an IPv6 IP.
+        */
+       static function isIPv6($name) {
+               /* 
+                * if it has any non-valid characters, it can't be a valid IPv6  
+                * address.
+                */
+               if (preg_match("/[^:a-fA-F0-9]/", $name))
+                       return false;
+
+               $parts = explode(":", $name);
+               if (count($parts) < 3)
+                       return false;
+               foreach ($parts as $part) {
+                       if (!preg_match("/^[0-9a-fA-F]{0,4}$/", $part))
+                               return false;
+               }
+               return true;
+       }
+
        /**
         * Is the input a valid username?
         *
@@ -468,12 +487,7 @@ class User {
                $result = null;
                if( !wfRunHooks( 'isValidPassword', array( $password, &$result ) ) ) return $result;
                if ($result === false) return false; 
-               if (strlen( $password ) >= $wgMinimalPasswordLength) {
-                       return true;
-               }
-               else {
-                       return false;
-               }
+               return (strlen( $password ) >= $wgMinimalPasswordLength);
        }
 
        /**
@@ -483,8 +497,7 @@ class User {
         * rejected valid addresses. Actually just check if there is '@' somewhere
         * in the given address.
         *
-        * @todo Check for RFC 2822 compilance
-        * @bug 959
+        * @todo Check for RFC 2822 compilance (bug 959)
         *
         * @param string $addr email address
         * @static
@@ -586,7 +599,7 @@ class User {
 
        /**
         * Return a random password. Sourced from mt_rand, so it's not particularly secure.
-        * @todo: hash random numbers to improve security, like generateToken()
+        * @todo hash random numbers to improve security, like generateToken()
         *
         * @return string
         * @static
@@ -865,7 +878,8 @@ class User {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__.": checking...\n" );
 
-               $this->mBlockedby = 0;
+               $this->mBlockedby = 0; 
+               $this->mHideName = 0;
                $ip = wfGetIP();
 
                if ($this->isAllowed( 'ipblock-exempt' ) ) {
@@ -880,6 +894,7 @@ class User {
                        wfDebug( __METHOD__.": Found block.\n" );
                        $this->mBlockedby = $this->mBlock->mBy;
                        $this->mBlockreason = $this->mBlock->mReason;
+                       $this->mHideName = $this->mBlock->mHideName;
                        if ( $this->isLoggedIn() ) {
                                $this->spreadBlock();
                        }
@@ -954,6 +969,7 @@ class User {
         * @return bool
         */
        public function isPingLimitable() {
+               global $wgRateLimitsExcludedGroups;
                return array_intersect($this->getEffectiveGroups(), $wgRateLimitsExcludedGroups) != array();
        }
 
@@ -968,22 +984,22 @@ class User {
         * @public
         */
        function pingLimiter( $action='edit' ) {
-       
+
                # Call the 'PingLimiter' hook
                $result = false;
                if( !wfRunHooks( 'PingLimiter', array( &$this, $action, $result ) ) ) {
                        return $result;
                }
-               
+
                global $wgRateLimits, $wgRateLimitsExcludedGroups;
                if( !isset( $wgRateLimits[$action] ) ) {
                        return false;
                }
-               
+
                # Some groups shouldn't trigger the ping limiter, ever
                if( !$this->isPingLimitable() )
                        return false;
-               
+
                global $wgMemc, $wgRateLimitLog;
                wfProfileIn( __METHOD__ );
 
@@ -1057,14 +1073,13 @@ class User {
                wfProfileIn( __METHOD__ );
                wfDebug( __METHOD__.": enter\n" );
 
-               if ( $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
-                 $title->getNamespace() == NS_USER_TALK )
-               {
+               wfDebug( __METHOD__.": asking isBlocked()\n" );
+               $blocked = $this->isBlocked( $bFromSlave );
+               # If a user's name is suppressed, they cannot make edits anywhere
+               if ( !$this->mHideName && $wgBlockAllowsUTEdit && $title->getText() === $this->getName() &&
+                 $title->getNamespace() == NS_USER_TALK ) {
                        $blocked = false;
                        wfDebug( __METHOD__.": self-talk page, ignoring any blocks\n" );
-               } else {
-                       wfDebug( __METHOD__.": asking isBlocked()\n" );
-                       $blocked = $this->isBlocked( $bFromSlave );
                }
                wfProfileOut( __METHOD__ );
                return $blocked;
@@ -1117,7 +1132,8 @@ class User {
                        if ( $this->mName === false ) {
                                $this->mName = wfGetIP();
                        }
-                       return $this->mName;
+                       # Clean up IPs
+                       return IP::sanitizeIP($this->mName);
                }
        }
 
@@ -1377,11 +1393,23 @@ class User {
                                        $wgMinimalPasswordLength ) );
                        }
                }
-               
+
                if( !$wgAuth->setPassword( $this, $str ) ) {
                        throw new PasswordError( wfMsg( 'externaldberror' ) );
                }
                
+               $this->setInternalPassword( $str );
+
+               return true;
+       }
+
+       /**
+        * Set the password and reset the random token no matter
+        * what.
+        *
+        * @param string $str
+        */
+       function setInternalPassword( $str ) {
                $this->load();
                $this->setToken();
                
@@ -1393,10 +1421,7 @@ class User {
                }
                $this->mNewpassword = '';
                $this->mNewpassTime = null;
-               
-               return true;
        }
-
        /**
         * Set the random token (used for persistent authentication)
         * Called from loadDefaults() among other places.
@@ -1623,7 +1648,7 @@ class User {
        /**
         * Add the user to the given group.
         * This takes immediate effect.
-        * @string $group
+        * @param string $group
         */
        function addGroup( $group ) {
                $this->load();
@@ -1647,7 +1672,7 @@ class User {
        /**
         * Remove the user from the given group.
         * This takes immediate effect.
-        * @string $group
+        * @param string $group
         */
        function removeGroup( $group ) {
                $this->load();
@@ -1918,7 +1943,7 @@ class User {
 
        /**
         * Save object settings into database
-        * @fixme Only rarely do all these fields need to be set!
+        * @todo Only rarely do all these fields need to be set!
         */
        function saveSettings() {
                $this->load();