Changed Metadata to use users' real names, if provided. If not, use their
[lhc/web/wiklou.git] / includes / User.php
index a528e3a..6b0632d 100644 (file)
@@ -1,4 +1,4 @@
-<?
+<?php
 # See user.doc
 
 include_once( "WatchedItem.php" );
@@ -11,6 +11,7 @@ class User {
        /* private */ var $mBlockedby, $mBlockreason;
        /* private */ var $mTouched;
        /* private */ var $mCookiePassword;
+        /* private */ var $mRealName;
 
        function User()
        {
@@ -35,6 +36,11 @@ class User {
                return wfGetSQL( "user", "user_name", "user_id=$id" );
        }
 
+       /* static */ function whoIsReal( $id )
+       {
+               return wfGetSQL( "user", "user_real_name", "user_id=$id" );
+       }
+
        /* static */ function idFromName( $name )
        {
                $nt = Title::newFromText( $name );
@@ -94,45 +100,29 @@ class User {
 
        /* private */ function getBlockedStatus()
        {
-               global $wgBadRanges, $wgBadUserAgents, $wgRangeBlockUser, $wgRangeBlockReason, $wgIP;
+               global $wgIP, $wgBlockCache;
 
                if ( -1 != $this->mBlockedby ) { return; }
+       
+               $this->mBlockedby = 0;
                
-               # Range/user-agent blocking
-               
-               $fBlock = false; # Mmmm, Hungarian
-               if ( ( !is_array( $wgBadUserAgents ) ||
-                                       array_key_exists( getenv( "HTTP_USER_AGENT" ), $wgBadUserAgents ) ) &&
-                               is_array( $wgBadRanges ) )
-               {
-                       $iIp = ip2long( $wgIP );
-                       foreach ( $wgBadRanges as $range ) {
-                               $start = ip2long( $range[0] );
-                               $end = ip2long( $range[1] );
-                               if ( $iIp >= $start && $iIp <= $end ) {
-                                       $fBlock = true;
-                                       break;
-                               }
+               # User blocking
+               if ( $this->mId ) {     
+                       $block = new Block();
+                       if ( $block->load( $wgIP , $this->mId ) ) {
+                               $this->mBlockedby = $block->mBy;
+                               $this->mBlockreason = $block->mReason;
                        }
                }
 
-               if ( $fBlock ) {
-                       $this->mBlockedby = $wgRangeBlockUser;
-                       $this->mBlockreason = $wgRangeBlockReason;
-                       return;
-               }
-
-               # User/IP blocking
-
-               $block = new Block();
-               if ( !$block->load( $wgIP , $this->mId ) ) {
-                       wfDebug( $wgIP ." is not blocked\n" );
-                       $this->mBlockedby = 0;
-                       return;
+               # IP/range blocking
+               if ( !$this->mBlockedby ) {
+                       $block = $wgBlockCache->get( $wgIP );
+                       if ( $block !== false ) {
+                               $this->mBlockedby = $block->mBy;
+                               $this->mBlockreason = $block->mReason;
+                       }
                }
-
-               $this->mBlockedby = $block->mBy;
-               $this->mBlockreason = $block->mReason;
        }
 
        function isBlocked()
@@ -154,41 +144,35 @@ class User {
 
        function SetupSession() {
                global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
-               global $wsUserID, $wsUserName, $wsUserPassword, $wsUploadFiles;
                if( $wgSessionsInMemcached ) {
                        include_once( "MemcachedSessions.php" );
                }
                session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
                session_cache_limiter( "private, must-revalidate" );
-               session_start();
-               session_register( "wsUserID" );
-               session_register( "wsUserName" );
-               session_register( "wsUserPassword" );
-               session_register( "wsUploadFiles" );
+               @session_start();
        }
 
        /* static */ function loadFromSession()
        {
-               global $HTTP_COOKIE_VARS, $wsUserID, $wsUserName, $wsUserPassword;
                global $wgMemc, $wgDBname;
 
-               if ( isset( $wsUserID ) ) {
-                       if ( 0 != $wsUserID ) {
-                               $sId = $wsUserID;
+               if ( isset( $_SESSION['wsUserID'] ) ) {
+                       if ( 0 != $_SESSION['wsUserID'] ) {
+                               $sId = $_SESSION['wsUserID'];
                        } else {
                                return new User();
                        }
-               } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserID"] ) ) {
-                       $sId = $HTTP_COOKIE_VARS["{$wgDBname}UserID"];
-                       $wsUserID = $sId;
+               } else if ( isset( $_COOKIE["{$wgDBname}UserID"] ) ) {
+                       $sId = IntVal( $_COOKIE["{$wgDBname}UserID"] );
+                       $_SESSION['wsUserID'] = $sId;
                } else {
                        return new User();
                }
-               if ( isset( $wsUserName ) ) {
-                       $sName = $wsUserName;
-               } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}UserName"] ) ) {
-                       $sName = $HTTP_COOKIE_VARS["{$wgDBname}UserName"];
-                       $wsUserName = $sName;
+               if ( isset( $_SESSION['wsUserName'] ) ) {
+                       $sName = $_SESSION['wsUserName'];
+               } else if ( isset( $_COOKIE["{$wgDBname}UserName"] ) ) {
+                       $sName = $_COOKIE["{$wgDBname}UserName"];
+                       $_SESSION['wsUserName'] = $sName;
                } else {
                        return new User();
                }
@@ -204,12 +188,12 @@ class User {
                        wfDebug( "User::loadFromSession() got from cache!\n" );
                }
 
-               if ( isset( $wsUserPassword ) ) {
-                       $passwordCorrect = $wsUserPassword == $user->mPassword;
-               } else if ( isset( $HTTP_COOKIE_VARS["{$wgDBname}Password"] ) ) {
-                       $user->mCookiePassword = $HTTP_COOKIE_VARS["{$wgDBname}Password"];
-                       $wsUserPassword = $user->addSalt( $user->mCookiePassword );
-                       $passwordCorrect = $wsUserPassword == $user->mPassword;
+               if ( isset( $_SESSION['wsUserPassword'] ) ) {
+                       $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword;
+               } else if ( isset( $_COOKIE["{$wgDBname}Password"] ) ) {
+                       $user->mCookiePassword = $_COOKIE["{$wgDBname}Password"];
+                       $_SESSION['wsUserPassword'] = $user->addSalt( $user->mCookiePassword );
+                       $passwordCorrect = $_SESSION['wsUserPassword'] == $user->mPassword;
                } else {
                        return new User(); # Can't log in from session
                }
@@ -229,7 +213,14 @@ class User {
 
        function loadFromDatabase()
        {
-               if ( $this->mDataLoaded ) { return; }
+               global $wgCommandLineMode;
+               if ( $this->mDataLoaded || $wgCommandLineMode ) {
+                       return;
+               }
+
+               # Paranoia
+               $this->mId = IntVal( $this->mId );
+
                # check in separate table if there are changes to the talk page
                $this->mNewtalk=0; # reset talk page status
                if($this->mId) {
@@ -241,11 +232,10 @@ class User {
                        }
                        wfFreeResult( $res );
                } else {
-                       # TEST THIS @@@
                        global $wgDBname, $wgMemc;
                        $key = "$wgDBname:newtalk:ip:{$this->mName}";
                        $newtalk = $wgMemc->get( $key );
-                       if( ! is_scalar( $newtalk ) ){
+                       if( ! is_integer( $newtalk ) ){
                                $sql = "SELECT 1 FROM user_newtalk WHERE user_ip='{$this->mName}'";
                                $res = wfQuery ($sql, DB_READ, "User::loadFromDatabase" );
 
@@ -263,14 +253,15 @@ class User {
                } # the following stuff is for non-anonymous users only
 
                $sql = "SELECT user_name,user_password,user_newpassword,user_email," .
-                 "user_options,user_rights,user_touched FROM user WHERE user_id=" .
-                 "{$this->mId}";
+                 "user_real_name,user_options,user_rights,user_touched " . 
+                  " FROM user WHERE user_id=" . $this->mId;
                $res = wfQuery( $sql, DB_READ, "User::loadFromDatabase" );
 
                if ( wfNumRows( $res ) > 0 ) {
                        $s = wfFetchObject( $res );
                        $this->mName = $s->user_name;
                        $this->mEmail = $s->user_email;
+                       $this->mRealName = $s->user_real_name;
                        $this->mPassword = $s->user_password;
                        $this->mNewpassword = $s->user_newpassword;
                        $this->decodeOptions( $s->user_options );
@@ -382,6 +373,18 @@ class User {
                $this->mEmail = $str;
        }
 
+       function getRealName()
+       {
+               $this->loadFromDatabase();
+               return $this->mRealName;
+       }
+
+       function setRealName( $str )
+       {
+               $this->loadFromDatabase();
+               $this->mRealName = $str;
+       }
+
        function getOption( $oname )
        {
                $this->loadFromDatabase();
@@ -395,6 +398,10 @@ class User {
        function setOption( $oname, $val )
        {
                $this->loadFromDatabase();
+               if ( $oname == 'skin' ) {
+                       # Clear cached skin, so the new one displays immediately in Special:Preferences
+                       unset( $this->mSkin );
+               }
                $this->mOptions[$oname] = $val;
                $this->invalidateCache();
        }
@@ -428,11 +435,21 @@ class User {
                return in_array( "developer", $this->mRights );
        }
 
-       function isBot()
+       function isBureaucrat()
        {
                $this->loadFromDatabase();
                if ( 0 == $this->mId ) { return false; }
 
+               return in_array( "bureaucrat", $this->mRights );
+       }
+
+       function isBot()
+       {
+               $this->loadFromDatabase();
+
+               # Why was this here? I need a UID=0 conversion script [TS]
+               # if ( 0 == $this->mId ) { return false; }
+
                return in_array( "bot", $this->mRights );
        }
 
@@ -441,10 +458,21 @@ class User {
                if ( ! isset( $this->mSkin ) ) {
                        $skinNames = Skin::getSkinNames();
                        $s = $this->getOption( "skin" );
-                       if ( "" == $s ) { $s = 0; }
-
-                       if ( $s >= count( $skinNames ) ) { $sn = "SkinStandard"; }
-                       else $sn = "Skin" . $skinNames[$s];
+                       if ( "" == $s ) { $s = 'standard'; }
+
+                       if ( !isset( $skinNames[$s] ) ) {
+                               $fallback = array(
+                                       'standard' => "Standard",
+                                       'nostalgia' => "Nostalgia",
+                                       'cologneblue' => "Cologne Blue");
+                               if(is_int($s) && isset( $fallback[$s]) ){
+                                       $sn = $fallback[$s];
+                               } else {
+                                       $sn = "SkinStandard";
+                               }
+                       } else {
+                               $sn = "Skin" . $skinNames[$s];
+                       }
                        $this->mSkin = new $sn;
                }
                return $this->mSkin;
@@ -490,19 +518,18 @@ class User {
 
        function setCookies()
        {
-               global $wsUserID, $wsUserName, $wsUserPassword;
                global $wgCookieExpiration, $wgCookiePath, $wgCookieDomain, $wgDBname;
                if ( 0 == $this->mId ) return;
                $this->loadFromDatabase();
                $exp = time() + $wgCookieExpiration;
 
-               $wsUserID = $this->mId;
+               $_SESSION['wsUserID'] = $this->mId;
                setcookie( "{$wgDBname}UserID", $this->mId, $exp, $wgCookiePath, $wgCookieDomain );
 
-               $wsUserName = $this->mName;
+               $_SESSION['wsUserName'] = $this->mName;
                setcookie( "{$wgDBname}UserName", $this->mName, $exp, $wgCookiePath, $wgCookieDomain );
 
-               $wsUserPassword = $this->mPassword;
+               $_SESSION['wsUserPassword'] = $this->mPassword;
                if ( 1 == $this->getOption( "rememberpassword" ) ) {
                        setcookie( "{$wgDBname}Password", $this->mCookiePassword, $exp, $wgCookiePath, $wgCookieDomain );
                } else {
@@ -512,10 +539,10 @@ class User {
 
        function logout()
        {
-               global $wsUserID, $wgCookiePath, $wgCookieDomain, $wgDBname;
+               global $wgCookiePath, $wgCookieDomain, $wgDBname;
                $this->mId = 0;
 
-               $wsUserID = 0;
+               $_SESSION['wsUserID'] = 0;
 
                setcookie( "{$wgDBname}UserID", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
                setcookie( "{$wgDBname}Password", "", time() - 3600, $wgCookiePath, $wgCookieDomain );
@@ -541,6 +568,7 @@ class User {
                  "user_name= '" . wfStrencode( $this->mName ) . "', " .
                  "user_password= '" . wfStrencode( $this->mPassword ) . "', " .
                  "user_newpassword= '" . wfStrencode( $this->mNewpassword ) . "', " .
+                 "user_real_name= '" . wfStrencode( $this->mRealName ) . "', " .
                  "user_email= '" . wfStrencode( $this->mEmail ) . "', " .
                  "user_options= '" . $this->encodeOptions() . "', " .
                  "user_rights= '" . wfStrencode( implode( ",", $this->mRights ) ) . "', " .
@@ -574,11 +602,12 @@ class User {
        function addToDatabase()
        {
                $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
-                 "user_email, user_rights, user_options) " .
+                 "user_email, user_real_name, user_rights, user_options) " .
                  " VALUES ('" . wfStrencode( $this->mName ) . "', '" .
                  wfStrencode( $this->mPassword ) . "', '" .
                  wfStrencode( $this->mNewpassword ) . "', '" .
                  wfStrencode( $this->mEmail ) . "', '" .
+                 wfStrencode( $this->mRealName ) . "', '" .
                  wfStrencode( implode( ",", $this->mRights ) ) . "', '" .
                  $this->encodeOptions() . "')";
                wfQuery( $sql, DB_WRITE, "User::addToDatabase" );
@@ -618,6 +647,13 @@ class User {
                $ipblock->mReason = wfMsg( "autoblocker", $this->getName(), $userblock->mReason );
                $ipblock->mTimestamp = wfTimestampNow();
                $ipblock->mAuto = 1;
+               # If the user is already blocked with an expiry date, we don't 
+               # want to pile on top of that!
+               if($userblock->mExpiry) {
+                       $ipblock->mExpiry = min ( $userblock->mExpiry, Block::getAutoblockExpiry( $ipblock->mTimestamp ));
+               } else {
+                       $ipblock->mExpiry = Block::getAutoblockExpiry( $ipblock->mTimestamp );
+               }
 
                # Insert it
                $ipblock->insert();
@@ -665,8 +701,16 @@ class User {
                return $allowed;
        }
 
-
-
+       # Set mDataLoaded, return previous value
+       # Use this to prevent DB access in command-line scripts or similar situations
+       function setLoaded( $loaded ) 
+       {
+               wfSetVar( $this->mDataLoaded, $loaded );
+       }
+       
+       function getUserPage() {
+               return Title::makeTitle( NS_USER, $this->mName );
+       }
 }
 
 ?>