* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / User.php
index 5f18a22..42a6a89 100644 (file)
@@ -52,16 +52,25 @@ class User {
        function newFromName( $name ) {
                $u = new User();
 
+               # Force usernames to capital
+               global $wgContLang;
+               $name = $wgContLang->ucfirst( $name );
+               
                # Clean up name according to title rules
-
                $t = Title::newFromText( $name );
                if( is_null( $t ) ) {
-                       return NULL;
-               } else {
-                       $u->setName( $t->getText() );
-                       $u->setId( $u->idFromName( $t->getText() ) );
-                       return $u;
+                       return null;
+               }
+               
+               # Reject various classes of invalid names
+               $canonicalName = $t->getText();
+               if( !User::isValidUserName( $canonicalName ) ) {
+                       return null;
                }
+               
+               $u->setName( $canonicalName );
+               $u->setId( $u->idFromName( $t->getText() ) );
+               return $u;
        }
        
        /**
@@ -163,8 +172,46 @@ class User {
        }
 
        /**
+        * Is the input a valid username?
+        *
+        * Checks if the input is a valid username, we don't want an empty string,
+        * an IP address, anything that containins slashes (would mess up subpages),
+        * is longer than the maximum allowed username size or doesn't begin with
+        * a capital letter.
+        *
+        * @param string $name
+        * @return bool
+        * @static
+        */
+       function isValidUserName( $name ) {
+               global $wgContLang, $wgMaxNameChars;
+               
+               if ( $name == ''
+               || User::isIP( $name )
+               || strpos( $name, '/' ) !== false
+               || strlen( $name ) > $wgMaxNameChars
+               || $name != $wgContLang->ucfirst( $name ) )
+                       return false;
+               else
+                       return true;
+       }
+
+       /**
+        * Is the input a valid password?
+        *
+        * @param string $password
+        * @return bool
+        * @static
+        */
+       function isValidPassword( $password ) {
+               global $wgMinimalPasswordLength;
+               return strlen( $password ) >= $wgMinimalPasswordLength;
+       }
+
+       /**     
         * does the string match roughly an email address ?
         *
+        * @todo Check for RFC 2822 compilance
         * @bug 959
         *
         * @param string $addr email address
@@ -178,6 +225,23 @@ class User {
                        (false !== strpos( $addr, '@' ) );
        }
 
+       /**
+        * Count the number of edits of a user 
+        *
+        * @param int $uid The user ID to check
+        * @return int
+        */
+       function edits( $uid ) {
+               $fname = 'User::editCount';
+               
+               $dbr =& wfGetDB( DB_SLAVE );
+               return $dbr->selectField(
+                       'revision', 'count(*)',
+                       array( 'rev_user' => $uid ),
+                       $fname
+               );
+       }
+
        /**
         * probably return a random password
         * @return string probably a random password
@@ -465,6 +529,20 @@ class User {
                $this->getBlockedStatus( $bFromSlave );
                return $this->mBlockedby !== 0;
        }
+
+       /**
+        * Check if user is blocked from editing a particular article
+        */
+       function isBlockedFrom( $title, $bFromSlave = false ) {
+               global $wgBlockAllowsUTEdit;
+               if ( $wgBlockAllowsUTEdit && $title->getText() === $this->getName() && 
+                 $title->getNamespace() == NS_USER_TALK )
+               {
+                       return false;
+               } else {
+                       return $this->isBlocked( $bFromSlave );
+               }
+       }
        
        /**
         * Get name of blocker
@@ -531,7 +609,10 @@ class User {
 
                $passwordCorrect = FALSE;
                $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
-               if( $user->mVersion < MW_USER_VERSION ) $user = false;
+               if( !is_object( $user ) || $user->mVersion < MW_USER_VERSION ) {
+                       # Expire old serialized objects; they may be corrupt.
+                       $user = false;
+               }
                if($makenew = !$user) {
                        wfDebug( "User::loadFromSession() unable to load from memcached\n" );
                        $user = new User();
@@ -1222,9 +1303,12 @@ class User {
                
                $fname = 'User::saveNewtalk';
 
+               $changed = false;
+
                if ( wfReadOnly() ) { return ; }
                $dbr =& wfGetDB( DB_SLAVE );
                $dbw =& wfGetDB( DB_MASTER );
+               $changed = false;
                if ( $wgUseEnotif ) {
                        if ( ! $this->getNewtalk() ) {
                                # Delete the watchlist entry for user_talk page X watched by user X
@@ -1382,7 +1466,6 @@ class User {
 
                $confstr =        $this->getOption( 'math' );
                $confstr .= '!' . $this->getOption( 'stubthreshold' );
-               $confstr .= '!' . $this->getOption( 'editsection' );
                $confstr .= '!' . $this->getOption( 'date' );
                $confstr .= '!' . $this->getOption( 'numberheadings' );
                $confstr .= '!' . $this->getOption( 'language' );