typo
[lhc/web/wiklou.git] / includes / User.php
index a6f1fb5..572060b 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 # See user.doc
 
-include_once( "WatchedItem.php" );
+require_once( "WatchedItem.php" );
 
 class User {
        /* private */ var $mId, $mName, $mPassword, $mEmail, $mNewtalk;
@@ -11,6 +11,8 @@ class User {
        /* private */ var $mBlockedby, $mBlockreason;
        /* private */ var $mTouched;
        /* private */ var $mCookiePassword;
+        /* private */ var $mRealName;
+       /* private */ var $mUserTable;
 
        function User()
        {
@@ -35,16 +37,29 @@ 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 )
        {
+               global $wgIsPg;
                $nt = Title::newFromText( $name );
-               $sql = "SELECT user_id FROM user WHERE user_name='" .
+               if( is_null( $nt ) ) {
+                       # Illegal name
+                       return null;
+               }
+               $usertable=$wgIsPg?'"user"':'user';
+               $sql = "SELECT user_id FROM $usertable WHERE user_name='" .
                  wfStrencode( $nt->getText() ) . "'";
                $res = wfQuery( $sql, DB_READ, "User::idFromName" );
 
-               if ( 0 == wfNumRows( $res ) ) { return 0; }
-               else {
+               if ( 0 == wfNumRows( $res ) ) {
+                       return 0;
+               } else {
                        $s = wfFetchObject( $res );
+                       wfFreeResult( $res );
                        return $s->user_id;
                }
        }
@@ -70,7 +85,7 @@ class User {
 
        function loadDefaults()
        {
-               global $wgLang, $wgIP;
+               global $wgLang, $wgIP, $wgIsPg;
                global $wgNamespacesToBeSearchedDefault;
 
                $this->mId = $this->mNewtalk = 0;
@@ -90,6 +105,9 @@ class User {
                $this->mBlockedby = -1; # Unset
                $this->mTouched = '0'; # Allow any pages to be cached
                $this->cookiePassword = "";
+
+               /* avoid reserved keyword usage for PostgreSQL */
+               $this->mUserTable=$wgIsPg?'"user"':'user';
        }
 
        /* private */ function getBlockedStatus()
@@ -139,11 +157,11 @@ class User {
        function SetupSession() {
                global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain;
                if( $wgSessionsInMemcached ) {
-                       include_once( "MemcachedSessions.php" );
+                       require_once( "MemcachedSessions.php" );
                }
                session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain );
                session_cache_limiter( "private, must-revalidate" );
-               session_start();
+               @session_start();
        }
 
        /* static */ function loadFromSession()
@@ -226,7 +244,6 @@ class User {
                        }
                        wfFreeResult( $res );
                } else {
-                       # TEST THIS @@@
                        global $wgDBname, $wgMemc;
                        $key = "$wgDBname:newtalk:ip:{$this->mName}";
                        $newtalk = $wgMemc->get( $key );
@@ -248,14 +265,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 {$this->mUserTable} 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 );
@@ -367,6 +385,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();
@@ -380,6 +410,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();
        }
@@ -440,10 +474,14 @@ class User {
 
                        if ( !isset( $skinNames[$s] ) ) {
                                $fallback = array(
-                                       'standard' => "Standard",
-                                       'nostalgia' => "Nostalgia",
-                                       'cologneblue' => "Cologne Blue");
-                               if(is_int($s) && isset( $fallback[$s]) ){
+                                       0 => "SkinStandard",
+                                       1 => "SkinNostalgia",
+                                       2 => "SkinCologneBlue");
+                               if ( isset( $skinNames['monobook'] ) ) {
+                                       $fallback[0] = "SkinMonoBook";
+                               }
+                               
+                               if(is_numeric($s) && isset( $fallback[$s]) ){
                                        $sn = $fallback[$s];
                                } else {
                                        $sn = "SkinStandard";
@@ -529,7 +567,6 @@ class User {
        function saveSettings()
        {
                global $wgMemc, $wgDBname;
-
                if ( ! $this->mNewtalk ) {
                        if( $this->mId ) {
                                $sql="DELETE FROM user_newtalk WHERE user_id={$this->mId}";
@@ -542,10 +579,11 @@ class User {
                }
                if ( 0 == $this->mId ) { return; }
 
-               $sql = "UPDATE user SET " .
+               $sql = "UPDATE {$this->mUserTable} SET " .
                  "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 ) ) . "', " .
@@ -563,7 +601,7 @@ class User {
                $s = trim( $this->mName );
                if ( 0 == strcmp( "", $s ) ) return 0;
 
-               $sql = "SELECT user_id FROM user WHERE user_name='" .
+               $sql = "SELECT user_id FROM {$this->mUserTable} WHERE user_name='" .
                  wfStrencode( $s ) . "'";
                $res = wfQuery( $sql, DB_READ, "User::idForName" );
                if ( 0 == wfNumRows( $res ) ) { return 0; }
@@ -578,12 +616,13 @@ class User {
 
        function addToDatabase()
        {
-               $sql = "INSERT INTO user (user_name,user_password,user_newpassword," .
-                 "user_email, user_rights, user_options) " .
+               $sql = "INSERT INTO {$this->mUserTable} (user_name,user_password,user_newpassword," .
+                 "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" );
@@ -616,14 +655,20 @@ class User {
                }
                
                # Make a new block object with the desired properties
-               wfDebug( "Autoblocking {$this->mUserName}@{$wgIP}\n" );
+               wfDebug( "Autoblocking {$this->mName}@{$wgIP}\n" );
                $ipblock->mAddress = $wgIP;
                $ipblock->mUser = 0;
                $ipblock->mBy = $userblock->mBy;
                $ipblock->mReason = wfMsg( "autoblocker", $this->getName(), $userblock->mReason );
                $ipblock->mTimestamp = wfTimestampNow();
                $ipblock->mAuto = 1;
-               $ipblock->mExpiry = Block::getAutoblockExpiry( $ipblock->mTimestamp );
+               # 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();
@@ -677,6 +722,10 @@ class User {
        {
                wfSetVar( $this->mDataLoaded, $loaded );
        }
+       
+       function getUserPage() {
+               return Title::makeTitle( NS_USER, $this->mName );
+       }
 }
 
 ?>