Nov. branch merge. Various features backported from stable, various bug fixes.
[lhc/web/wiklou.git] / includes / User.php
index 8306392..697354f 100644 (file)
@@ -1,6 +1,8 @@
 <?
 # See user.doc
 
+include_once( "WatchedItem.php" );
+
 class User {
        /* private */ var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
        /* private */ var $mRights, $mOptions;
@@ -38,7 +40,7 @@ class User {
                $nt = Title::newFromText( $name );
                $sql = "SELECT user_id FROM user WHERE user_name='" .
                  wfStrencode( $nt->getText() ) . "'";
-               $res = wfQuery( $sql, "User::idFromName" );
+               $res = wfQuery( $sql, DB_READ, "User::idFromName" );
 
                if ( 0 == wfNumRows( $res ) ) { return 0; }
                else {
@@ -94,22 +96,15 @@ class User {
        {
                if ( -1 != $this->mBlockedby ) { return; }
 
-               $remaddr = getenv( "REMOTE_ADDR" );
-               if ( 0 == $this->mId ) {
-                       $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
-                         "ipb_address='$remaddr'";
-               } else {
-                       $sql = "SELECT ipb_by,ipb_reason FROM ipblocks WHERE " .
-                         "(ipb_address='$remaddr' OR ipb_user={$this->mId})";
-               }
-               $res = wfQuery( $sql, "User::getBlockedStatus" );
-               if ( 0 == wfNumRows( $res ) ) {
+               $block = new Block();
+               if ( !$block->load( getenv( "REMOTE_ADDR" ), $this->mId ) ) {
+                       wfDebug( getenv( "REMOTE_ADDR" ) ." is not blocked\n" );
                        $this->mBlockedby = 0;
                        return;
                }
-               $s = wfFetchObject( $res );
-               $this->mBlockedby = $s->ipb_by;
-               $this->mBlockreason = $s->ipb_reason;
+               
+               $this->mBlockedby = $block->mBy;
+               $this->mBlockreason = $block->mReason;
        }
 
        function isBlocked()
@@ -129,23 +124,22 @@ class User {
                return $this->mBlockreason;
        }
 
-       function loadFromSession()
+       /* static */ function loadFromSession()
        {
                global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
+               global $wgMemc, $wgDBname;
 
                if ( isset( $wsUserID ) ) {
                        if ( 0 != $wsUserID ) {
                                $sId = $wsUserID;
                        } else {
-                               $this->mId = 0;
-                               return;
+                               return new User();
                        }
                } else if ( isset( $HTTP_COOKIE_VARS["wcUserID"] ) ) {
                        $sId = $HTTP_COOKIE_VARS["wcUserID"];
                        $wsUserID = $sId;
                } else {
-                       $this->mId = 0;
-                       return;
+                       return new User();
                }
                if ( isset( $wsUserName ) ) {
                        $sName = $wsUserName;
@@ -153,30 +147,41 @@ class User {
                        $sName = $HTTP_COOKIE_VARS["wcUserName"];
                        $wsUserName = $sName;
                } else {
-                       $this->mId = 0;
-                       return;
+                       return new User();
                }
 
                $passwordCorrect = FALSE;
-               $this->mId = $sId;
-               $this->loadFromDatabase();
+               $user = $wgMemc->get( $key = "$wgDBname:user:id:$sId" );
+               if($makenew = !$user) {
+                       wfDebug( "User::loadFromSession() unable to load from memcached\n" );
+                       $user = new User();
+                       $user->mId = $sId;
+                       $user->loadFromDatabase();
+               } else {
+                       wfDebug( "User::loadFromSession() got from cache!\n" );
+               }
 
                if ( isset( $wsUserPassword ) ) {
-                       $passwordCorrect = $wsUserPassword == $this->mPassword;
+                       $passwordCorrect = $wsUserPassword == $user->mPassword;
                } else if ( isset( $HTTP_COOKIE_VARS["wcUserPassword"] ) ) {
-                       $this->mCookiePassword = $HTTP_COOKIE_VARS["wcUserPassword"];
-                       $wsUserPassword = $this->addSalt( $this->mCookiePassword );
-                       $passwordCorrect = $wsUserPassword == $this->mPassword;
+                       $user->mCookiePassword = $HTTP_COOKIE_VARS["wcUserPassword"];
+                       $wsUserPassword = $user->addSalt( $user->mCookiePassword );
+                       $passwordCorrect = $wsUserPassword == $user->mPassword;
                } else {
-                       $this->mId = 0;
-                       $this->loadDefaults(); # Can't log in from session
-                       return;
+                       return new User(); # Can't log in from session
                }
 
-               if ( ( $sName == $this->mName ) && $passwordCorrect ) {
-                       return;
+               if ( ( $sName == $user->mName ) && $passwordCorrect ) {
+                       if($makenew) {
+                               if($wgMemc->set( $key, $user ))
+                                       wfDebug( "User::loadFromSession() successfully saved user\n" );
+                               else
+                                       wfDebug( "User::loadFromSession() unable to save to memcached\n" );
+                       }
+                       $user->spreadBlock();
+                       return $user;
                }
-               $this->loadDefaults(); # Can't log in from session
+               return new User(); # Can't log in from session
        }
 
        function loadFromDatabase()
@@ -186,20 +191,28 @@ class User {
                $this->mNewtalk=0; # reset talk page status
                if($this->mId) {
                        $sql = "SELECT 1 FROM user_newtalk WHERE user_id={$this->mId}";
-                       $res = wfQuery ($sql,  "User::loadFromDatabase" );
+                       $res = wfQuery ($sql, DB_READ, "User::loadFromDatabase" );
 
                        if (wfNumRows($res)>0) {
                                $this->mNewtalk= 1;
                        }
                        wfFreeResult( $res );
                } else {
-                       $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
-                       $res = wfQuery ($sql,  "User::loadFromDatabase" );
-
-                       if (wfNumRows($res)>0) {
-                               $this->mNewtalk= 1;
+                       # TEST THIS @@@
+                       global $wgDBname, $wgMemc;
+                       $key = "$wgDBname:newtalk:ip:{$this->mName}";
+                       $newtalk = $wgMemc->get( $key );
+                       if($newtalk === false) {
+                               $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
+                               $res = wfQuery ($sql, DB_READ, "User::loadFromDatabase" );
+
+                               $this->mNewtalk = (wfNumRows($res)>0) ? 1 : 0;
+                               wfFreeResult( $res );
+
+                               $wgMemc->set( $key, $this->mNewtalk, time() ); // + 1800 );
+                       } else {
+                               $this->mNewtalk = $newtalk ? 1 : 0;
                        }
-                       wfFreeResult( $res );
                }
                if(!$this->mId) {
                        $this->mDataLoaded = true;
@@ -209,7 +222,7 @@ class User {
                $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
                  "user_options,user_rights,user_touched FROM user WHERE user_id=" .
                  "{$this->mId}";
-               $res = wfQuery( $sql, "User::loadFromDatabase" );
+               $res = wfQuery( $sql, DB_READ, "User::loadFromDatabase" );
 
                if ( wfNumRows( $res ) > 0 ) {
                        $s = wfFetchObject( $res );
@@ -394,45 +407,21 @@ class User {
                return $this->mSkin;
        }
 
-       function isWatched( $title )
-       {
-               # Note - $title should be a Title _object_
-               # Pages and their talk pages are considered equivalent for watching;
-               # remember that talk namespaces are numbered as page namespace+1.
-               if( $this->mId ) {
-                       $sql = "SELECT 1 FROM watchlist
-                         WHERE wl_user={$this->mId} AND
-                         wl_namespace = " . ($title->getNamespace() & ~1) . " AND
-                         wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
-                       $res = wfQuery( $sql );
-                       return (wfNumRows( $res ) > 0);
-               } else {
-                       return false;
-               }
+       function isWatched( $title ) {
+               $wl = WatchedItem::fromUserTitle( $this, $title );
+               return $wl->isWatched();
        }
-
-       function addWatch( $title )
-       {
-               if( $this->mId ) {
-                       # REPLACE instead of INSERT because occasionally someone
-                       # accidentally reloads a watch-add operation.
-                       $sql = "REPLACE INTO watchlist (wl_user, wl_namespace,wl_title)
-                         VALUES ({$this->mId}," . (($title->getNamespace() | 1) - 1) .
-                         ",'" . wfStrencode( $title->getDBkey() ) . "')";
-                       wfQuery( $sql );
-                       $this->invalidateCache();
-               }
+       
+       function addWatch( $title ) {
+               $wl = WatchedItem::fromUserTitle( $this, $title );
+               $wl->addWatch();
+               $this->invalidateCache();
        }
-
-       function removeWatch( $title )
-       {
-               if( $this->mId ) {
-                       $sql = "DELETE FROM watchlist WHERE wl_user={$this->mId} AND
-                         wl_namespace=" . (($title->getNamespace() | 1) - 1) .
-                         " AND wl_title='" . wfStrencode( $title->getDBkey() ) . "'";
-                       wfQuery( $sql );
-            $this->invalidateCache();
-               }
+       
+       function removeWatch( $title ) {
+               $wl = WatchedItem::fromUserTitle( $this, $title );
+               $wl->removeWatch();
+               $this->invalidateCache();
        }
 
 
@@ -491,15 +480,16 @@ class User {
 
        function saveSettings()
        {
-               global $wgUser;
+               global $wgMemc, $wgDBname;
 
                if ( ! $this->mNewtalk ) {
                        if( $this->mId ) {
                                $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
-                               wfQuery ($sql,"User::saveSettings");
+                               wfQuery ($sql, DB_WRITE, "User::saveSettings");
                        } else {
                                $sql="DELETE FROM user_newtalk WHERE user_ip='{$this->mName}'";
-                               wfQuery ($sql,"User::saveSettings");
+                               wfQuery ($sql, DB_WRITE, "User::saveSettings");
+                               $wgMemc->delete( "$wgDBname:newtalk:ip:{$this->mName}" );
                        }
                }
                if ( 0 == $this->mId ) { return; }
@@ -510,11 +500,11 @@ class User {
                  "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
                  "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
                  "user_options= '" . $this->encodeOptions() . "', " .
-                 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " 
-.
+                 "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " .
                  "user_touched= '" . wfStrencode( $this->mTouched ) .
                  "' WHERE user_id={$this->mId}";
-               wfQuery( $sql, "User::saveSettings" );
+               wfQuery( $sql, DB_WRITE, "User::saveSettings" );
+               $wgMemc->delete( "$wgDBname:user:id:$this->mId" );
        }
 
        # Checks if a user with the given name exists
@@ -527,7 +517,7 @@ class User {
 
                $sql = "SELECT user_id FROM user WHERE user_name='" .
                  wfStrencode( $s ) . "'";
-               $res = wfQuery( $sql, "User::idForName" );
+               $res = wfQuery( $sql, DB_READ, "User::idForName" );
                if ( 0 == wfNumRows( $res ) ) { return 0; }
 
                $s = wfFetchObject( $res );
@@ -548,9 +538,66 @@ class User {
                  wfStrencode( $this->mEmail ) . "', '" .
                  wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
                  $this->encodeOptions() . "')";
-               wfQuery( $sql, "User::addToDatabase" );
+               wfQuery( $sql, DB_WRITE, "User::addToDatabase" );
                $this->mId = $this->idForName();
        }
+
+       function spreadBlock()
+       {
+               # If the (non-anonymous) user is blocked, this function will block any IP address
+               # that they successfully log on from.
+               $fname = "User::spreadBlock";
+               
+               wfDebug( "User:spreadBlock()\n" );
+               if ( $this->mId == 0 ) {
+                       return;
+               }
+               
+               $userblock = Block::newFromDB( "", $this->mId );
+               if ( !$userblock->isValid() ) {
+                       return;
+               }
+               
+               # Check if this IP address is already blocked
+               $addr = getenv( "REMOTE_ADDR" );
+               $ipblock = Block::newFromDB( $addr );
+               if ( $ipblock->isValid() ) {
+                       # Just update the timestamp
+                       $ipblock->updateTimestamp();
+                       return;
+               }
+               
+               # Make a new block object with the desired properties
+               wfDebug( "Autoblocking {$this->mUserName}@{$addr}\n" );
+               $ipblock->mAddress = $addr;
+               $ipblock->mUser = 0;
+               $ipblock->mBy = $userblock->mBy;
+               $ipblock->mReason = str_replace( "$1", $this->getName(), wfMsg( "autoblocker" ) );
+               $ipblock->mReason = str_replace( "$2", $userblock->mReason, $ipblock->mReason );
+               $ipblock->mTimestamp = wfTimestampNow();
+               $ipblock->mAuto = 1;
+
+               # Insert it
+               $ipblock->insert();
+       
+       }
+
+
+       function isAllowedToCreateAccount() 
+       {
+               global $wgWhitelistAccount;
+               $allowed = false;
+               
+               if (!$wgWhitelistAccount) { return 1; }; // default behaviour
+               foreach ($wgWhitelistAccount as $right => $ok) {
+                       $userHasRight = (!strcmp($right, "user") || in_array($right, $this->getRights()));
+                       $allowed |= ($ok && $userHasRight);
+               }
+               return $allowed;
+       }
+
+
+
 }
 
 ?>