* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / User.php
index 3520a2e..42a6a89 100644 (file)
@@ -13,6 +13,9 @@ require_once( 'WatchedItem.php' );
 # Number of characters in user_token field
 define( 'USER_TOKEN_LENGTH', 32 );
 
+# Serialized record version
+define( 'MW_USER_VERSION', 2 );
+
 /**
  *
  * @package MediaWiki
@@ -32,10 +35,12 @@ class User {
        var $mRealName;
        var $mHash;
        var $mGroups;
+       var $mVersion; // serialized version
 
        /** Construct using User:loadDefaults() */
        function User() {
                $this->loadDefaults();
+               $this->mVersion = MW_USER_VERSION;
        }
 
        /**
@@ -47,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;
        }
        
        /**
@@ -82,6 +96,17 @@ class User {
                        return null;
                }
        }
+       
+       /**
+        * Serialze sleep function, for better cache efficiency and avoidance of 
+        * silly "incomplete type" errors when skins are cached
+        */
+       function __sleep() {
+               return array( 'mId', 'mName', 'mPassword', 'mEmail', 'mNewtalk',
+                       'mEmailAuthenticated', 'mRights', 'mOptions', 'mDataLoaded', 
+                       'mNewpassword', 'mBlockedby', 'mBlockreason', 'mTouched', 
+                       'mToken', 'mRealName', 'mHash', 'mGroups' );
+       }
 
        /**
         * Get username given an id.
@@ -147,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
@@ -162,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
@@ -320,7 +400,7 @@ class User {
                        }
 
                        # DNSBL
-                       if ( !$this->mBlockedby && $wgEnableSorbs ) {
+                       if ( !$this->mBlockedby && $wgEnableSorbs && !$this->getID() ) {
                                if ( $this->inSorbsBlacklist( $wgIP ) ) {
                                        $this->mBlockedby = wfMsg( 'sorbs' );
                                        $this->mBlockreason = wfMsg( 'sorbsreason' );
@@ -449,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
@@ -515,6 +609,10 @@ class User {
 
                $passwordCorrect = FALSE;
                $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
+               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();
@@ -628,6 +726,7 @@ class User {
        }
        
        function getNewtalk() {
+               global $wgUseEnotif;
                $fname = 'User::getNewtalk';
                $this->loadFromDatabase();
                
@@ -645,20 +744,33 @@ class User {
                                        $this->mNewtalk = $newtalk ? 1 : 0;
                                        return (bool)$this->mNewtalk;
                                }
-                       }
+                       } 
                        
                        $dbr =& wfGetDB( DB_SLAVE );
-                       $res = $dbr->select( 'watchlist',
-                               array( 'wl_user' ),
-                               array( 'wl_title'     => $this->getTitleKey(),
-                                          'wl_namespace' => NS_USER_TALK,
-                                          'wl_user'      => $this->mId,
-                                          'wl_notificationtimestamp != 0' ),
-                               'User::getNewtalk' );
-                       if( $dbr->numRows($res) > 0 ) {
-                               $this->mNewtalk = 1;
+                       if ( $wgUseEnotif ) {
+                               $res = $dbr->select( 'watchlist',
+                                       array( 'wl_user' ),
+                                       array( 'wl_title'     => $this->getTitleKey(),
+                                                  'wl_namespace' => NS_USER_TALK,
+                                                  'wl_user'      => $this->mId,
+                                                  'wl_notificationtimestamp != 0' ),
+                                       'User::getNewtalk' );
+                               if( $dbr->numRows($res) > 0 ) {
+                                       $this->mNewtalk = 1;
+                               }
+                               $dbr->freeResult( $res );
+                       } elseif ( $this->mId ) {
+                               $res = $dbr->select( 'user_newtalk', 1, array( 'user_id' => $this->mId ), $fname );
+
+                               if ( $dbr->numRows($res)>0 ) {
+                                       $this->mNewtalk= 1;
+                               }
+                               $dbr->freeResult( $res );
+                       } else {
+                               $res = $dbr->select( 'user_newtalk', 1, array( 'user_ip' => $this->mName ), $fname );
+                               $this->mNewtalk = $dbr->numRows( $res ) > 0 ? 1 : 0;
+                               $dbr->freeResult( $res );
                        }
-                       $dbr->freeResult( $res );
                        
                        if( !$this->mId ) {
                                $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
@@ -730,7 +842,7 @@ class User {
                        } else {
                                $key = microtime();
                        }
-                       $this->mToken = md5( $wgSecretKey . mt_rand( 0, 0x7fffffff ) . $wgDBname . $this->mId );
+                       $this->mToken = md5( $key . mt_rand( 0, 0x7fffffff ) . $wgDBname . $this->mId );
                } else {
                        $this->mToken = $token;
                }
@@ -775,7 +887,7 @@ class User {
        function getOption( $oname ) {
                $this->loadFromDatabase();
                if ( array_key_exists( $oname, $this->mOptions ) ) {
-                       return $this->mOptions[$oname];
+                       return trim( $this->mOptions[$oname] );
                } else {
                        return '';
                }
@@ -1018,11 +1130,16 @@ class User {
         * the next change of the page if it's watched etc.
         */
        function clearNotification( &$title ) {
-               global $wgUser;
+               global $wgUser, $wgUseEnotif;
+
+               if ( !$wgUseEnotif ) {
+                       return;
+               }
 
                $userid = $this->getID();
-               if ($userid==0)
+               if ($userid==0) {
                        return;
+               }
                
                // Only update the timestamp if the page is being watched. 
                // The query to find out if it is watched is cached both in memcached and per-invocation,
@@ -1065,6 +1182,10 @@ class User {
         * @access public
         */
        function clearAllNotifications( $currentUser ) {
+               global $wgUseEnotif;
+               if ( !$wgUseEnotif ) {
+                       return;
+               }
                if( $currentUser != 0 )  {
        
                        $dbw =& wfGetDB( DB_MASTER );
@@ -1148,26 +1269,14 @@ class User {
         * Save object settings into database
         */
        function saveSettings() {
-               global $wgMemc, $wgDBname;
+               global $wgMemc, $wgDBname, $wgUseEnotif;
                $fname = 'User::saveSettings';
 
-               $dbw =& wfGetDB( DB_MASTER );
-               if ( ! $this->getNewtalk() ) {
-                       # Delete the watchlist entry for user_talk page X watched by user X
-                       $dbw->delete( 'watchlist',
-                               array( 'wl_user'      => $this->mId,
-                                          'wl_title'     => $this->getTitleKey(),
-                                          'wl_namespace' => NS_USER_TALK ),
-                               $fname );
-                       if( !$this->mId ) {
-                               # Anon users have a separate memcache space for newtalk
-                               # since they don't store their own info. Trim...
-                               $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
-                       }
-               }
-
+               if ( wfReadOnly() ) { return; }
+               $this->saveNewtalk();
                if ( 0 == $this->mId ) { return; }
                
+               $dbw =& wfGetDB( DB_MASTER );
                $dbw->update( 'user',
                        array( /* SET */
                                'user_name' => $this->mName,
@@ -1185,6 +1294,80 @@ class User {
                );
                $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
        }
+       
+       /**
+        * Save value of new talk flag.
+        */
+       function saveNewtalk() {
+               global $wgDBname, $wgMemc, $wgUseEnotif;
+               
+               $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
+                               $dbw->delete( 'watchlist',
+                                       array( 'wl_user'      => $this->mId,
+                                                  'wl_title'     => $this->getTitleKey(),
+                                                  'wl_namespace' => NS_USER_TALK ),
+                                       $fname );
+                               if ( $dbw->affectedRows() ) {
+                                       $changed = true;
+                               }
+                               if( !$this->mId ) {
+                                       # Anon users have a separate memcache space for newtalk
+                                       # since they don't store their own info. Trim...
+                                       $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
+                               }
+                       }
+               } else {
+                       if ($this->getID() != 0) {
+                               $field = 'user_id';
+                               $value = $this->getID();
+                               $key = false;
+                       } else {
+                               $field = 'user_ip';
+                               $value = $this->mName;
+                               $key = "$wgDBname:newtalk:ip:$this->mName";
+                       }
+                       
+                       $dbr =& wfGetDB( DB_SLAVE );
+                       $dbw =& wfGetDB( DB_MASTER );
+
+                       $res = $dbr->selectField('user_newtalk', $field,
+                                                                        array($field => $value), $fname);
+
+                       $changed = true;
+                       if ($res !== false && $this->mNewtalk == 0) {
+                               $dbw->delete('user_newtalk', array($field => $value), $fname);
+                               if ( $key ) {
+                                       $wgMemc->set( $key, 0 );
+                               }
+                       } else if ($res === false && $this->mNewtalk == 1) {
+                               $dbw->insert('user_newtalk', array($field => $value), $fname);
+                               if ( $key ) {
+                                       $wgMemc->set( $key, 1 );
+                               }
+                       } else {
+                               $changed = false;
+                       }
+               }
+
+               # Update user_touched, so that newtalk notifications in the client cache are invalidated
+               if ( $changed && $this->getID() ) {
+                       $dbw->update('user', 
+                               /*SET*/ array( 'user_touched' => $this->mTouched ),
+                               /*WHERE*/ array( 'user_id' => $this->getID() ),
+                               $fname);
+                       $wgMemc->set( "$wgDBname:user:id:{$this->mId}", $this, 86400 );
+               }
+       }
 
        /**
         * Checks if a user with the given name exists, returns the ID
@@ -1283,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' );
@@ -1578,7 +1760,8 @@ class User {
                $rights = array();
                foreach( $groups as $group ) {
                        if( isset( $wgGroupPermissions[$group] ) ) {
-                               $rights = array_merge( $rights, $wgGroupPermissions[$group] );
+                               $rights = array_merge( $rights,
+                                       array_keys( array_filter( $wgGroupPermissions[$group] ) ) );
                        }
                }
                return $rights;