Drop ancient PHP4 wrapper for cloning, completely unused
[lhc/web/wiklou.git] / includes / User.php
index e577dde..ee148b0 100644 (file)
@@ -44,7 +44,7 @@ class User {
 
        /**
         * \type{\arrayof{\string}} A list of default user toggles, i.e., boolean user
-         * preferences that are displayed by Special:Preferences as checkboxes.
+        * preferences that are displayed by Special:Preferences as checkboxes.
         * This list can be extended via the UserToggles hook or by
         * $wgContLang::getExtraUserToggles().
         * @showinitializer
@@ -216,7 +216,7 @@ class User {
                $mBlockreason, $mBlock, $mEffectiveGroups, $mBlockedGlobally,
                $mLocked, $mHideName, $mOptions;
        //@}
-       
+
        static $idCacheByName = array();
 
        /**
@@ -354,9 +354,7 @@ class User {
                        $validate = 'valid';
                }
                $name = self::getCanonicalName( $name, $validate );
-               if ( WikiError::isError( $name ) ) {
-                       return $name;
-               } elseif ( $name === false ) {
+               if ( $name === false ) {
                        return false;
                } else {
                        # Create unloaded user object
@@ -636,12 +634,12 @@ class User {
         */
        function getPasswordValidity( $password ) {
                global $wgMinimalPasswordLength, $wgContLang;
-               
+
                $result = false; //init $result to false for the internal checks
-               
+
                if( !wfRunHooks( 'isValidPassword', array( $password, &$result, $this ) ) )
                        return $result;
-               
+
                if ( $result === false ) {
                        if( strlen( $password ) < $wgMinimalPasswordLength ) {
                                return 'passwordtooshort';
@@ -693,14 +691,15 @@ class User {
         *                - 'creatable'  Valid for batch processes, login and account creation
         */
        static function getCanonicalName( $name, $validate = 'valid' ) {
-               # Maybe force usernames to capital
-               $name = Title::capitalize( $name, NS_USER );
+               # Force usernames to capital
+               global $wgContLang;
+               $name = $wgContLang->ucfirst( $name );
 
                # Reject names containing '#'; these will be cleaned up
                # with title normalisation, but then it's too late to
                # check elsewhere
                if( strpos( $name, '#' ) !== false )
-                       return new WikiError( 'usernamehasherror' );
+                       return false;
 
                # Clean up name according to title rules
                $t = ( $validate === 'valid' ) ?
@@ -849,7 +848,7 @@ class User {
         * @return \bool True if the user is logged in, false otherwise.
         */
        private function loadFromSession() {
-               global $wgMemc, $wgCookiePrefix;
+               global $wgMemc, $wgCookiePrefix, $wgExternalAuthType, $wgAutocreatePolicy;
 
                $result = null;
                wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) );
@@ -857,6 +856,14 @@ class User {
                        return $result;
                }
 
+               if ( $wgExternalAuthType && $wgAutocreatePolicy == 'view' ) {
+                       $extUser = ExternalUser::newFromCookie();
+                       if ( $extUser ) {
+                               # TODO: Automatically create the user here (or probably a bit
+                               # lower down, in fact)
+                       }
+               }
+
                if ( isset( $_COOKIE["{$wgCookiePrefix}UserID"] ) ) {
                        $sId = intval( $_COOKIE["{$wgCookiePrefix}UserID"] );
                        if( isset( $_SESSION['wsUserID'] ) && $sId != $_SESSION['wsUserID'] ) {
@@ -895,6 +902,13 @@ class User {
                        return false;
                }
 
+               global $wgBlockDisablesLogin;
+               if( $wgBlockDisablesLogin && $this->isBlocked() ) {
+                       # User blocked and we've disabled blocked user logins
+                       $this->loadDefaults();
+                       return false;
+               }
+
                if ( isset( $_SESSION['wsToken'] ) ) {
                        $passwordCorrect = $_SESSION['wsToken'] == $this->mToken;
                        $from = 'session';
@@ -1046,7 +1060,7 @@ class User {
                        $defOpt['searchNs'.$nsnum] = !empty( $wgNamespacesToBeSearchedDefault[$nsnum] );
                }
                $defOpt['skin'] = $wgDefaultSkin;
-               
+
                return $defOpt;
        }
 
@@ -1091,7 +1105,7 @@ class User {
         *                    done against master.
         */
        function getBlockedStatus( $bFromSlave = true ) {
-               global $wgEnableSorbs, $wgProxyWhitelist, $wgUser;
+               global $wgProxyWhitelist, $wgUser;
 
                if ( -1 != $this->mBlockedby ) {
                        wfDebug( "User::getBlockedStatus: already loaded.\n" );
@@ -1114,7 +1128,7 @@ class User {
 
                # Check if we are looking at an IP or a logged-in user
                if ( $this->isIP( $this->getName() ) ) {
-                       $ip = $this->getName(); 
+                       $ip = $this->getName();
                } else {
                        # Check if we are looking at the current user
                        # If we don't, and the user is logged in, we don't know about
@@ -1138,6 +1152,8 @@ class User {
                if ( $this->mBlock->load( $ip , $this->mId ) ) {
                        wfDebug( __METHOD__ . ": Found block.\n" );
                        $this->mBlockedby = $this->mBlock->mBy;
+                       if( $this->mBlockedby == "0" )
+                               $this->mBlockedby = $this->mBlock->mByName;
                        $this->mBlockreason = $this->mBlock->mReason;
                        $this->mHideName = $this->mBlock->mHideName;
                        $this->mAllowUsertalk = $this->mBlock->mAllowUsertalk;
@@ -1159,8 +1175,8 @@ class User {
                        }
 
                        # DNSBL
-                       if ( !$this->mBlockedby && $wgEnableSorbs && !$this->getID() ) {
-                               if ( $this->inSorbsBlacklist( $ip ) ) {
+                       if ( !$this->mBlockedby && !$this->getID() ) {
+                               if ( $this->isDnsBlacklisted( $ip ) ) {
                                        $this->mBlockedby = wfMsg( 'sorbs' );
                                        $this->mBlockreason = wfMsg( 'sorbsreason' );
                                }
@@ -1174,16 +1190,24 @@ class User {
        }
 
        /**
-        * Whether the given IP is in the SORBS blacklist.
+        * Whether the given IP is in a DNS blacklist.
         *
         * @param $ip \string IP to check
+        * @param $checkWhitelist Boolean: whether to check the whitelist first
         * @return \bool True if blacklisted.
         */
-       function inSorbsBlacklist( $ip ) {
-               global $wgEnableSorbs, $wgSorbsUrl;
+       function isDnsBlacklisted( $ip, $checkWhitelist = false ) {
+               global $wgEnableSorbs, $wgEnableDnsBlacklist,
+                       $wgSorbsUrl, $wgDnsBlacklistUrls, $wgProxyWhitelist;
 
-               return $wgEnableSorbs &&
-                       $this->inDnsBlacklist( $ip, $wgSorbsUrl );
+               if ( !$wgEnableDnsBlacklist && !$wgEnableSorbs )
+                       return false;
+
+               if ( $checkWhitelist && in_array( $ip, $wgProxyWhitelist ) )
+                       return false;
+
+               $urls = array_merge( $wgDnsBlacklistUrls, (array)$wgSorbsUrl );
+               return $this->inDnsBlacklist( $ip, $urls );
        }
 
        /**
@@ -1331,11 +1355,11 @@ class User {
                                } else {
                                        wfDebug( __METHOD__ . ": ok. $key at $count $summary\n" );
                                }
-                               $wgMemc->incr( $key );
                        } else {
                                wfDebug( __METHOD__ . ": adding record for $key $summary\n" );
-                               $wgMemc->set( $key, 1, intval( $period ) ); // first ping
+                               $wgMemc->add( $key, 0, intval( $period ) ); // first ping
                        }
+                       $wgMemc->incr( $key );
                }
 
                wfProfileOut( __METHOD__ );
@@ -1375,9 +1399,9 @@ class User {
                        $blocked = false;
                        wfDebug( __METHOD__ . ": self-talk page, ignoring any blocks\n" );
                }
-               
+
                wfRunHooks( 'UserIsBlockedFrom', array( $this, $title, &$blocked, &$allowUsertalk ) );
-               
+
                wfProfileOut( __METHOD__ );
                return $blocked;
        }
@@ -1772,7 +1796,7 @@ class User {
                        if( !$wgAuth->allowPasswordChange() ) {
                                throw new PasswordError( wfMsg( 'password-change-forbidden' ) );
                        }
+
                        if( !$this->isValidPassword( $str ) ) {
                                global $wgMinimalPasswordLength;
                                $valid = $this->getPasswordValidity( $str );
@@ -1956,6 +1980,16 @@ class User {
                }
        }
 
+       /**
+        * Get all user's options
+        *
+        * @return array
+        */
+       public function getOptions() {
+               $this->loadOptions();
+               return $this->mOptions;
+       }
+
        /**
         * Get the user's current setting for a given option, as a boolean value.
         *
@@ -2326,7 +2360,7 @@ class User {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'watchlist',
                                        array( /* SET */
-                                               'wl_notificationtimestamp' => NULL
+                                               'wl_notificationtimestamp' => null
                                        ), array( /* WHERE */
                                                'wl_title' => $title->getDBkey(),
                                                'wl_namespace' => $title->getNamespace(),
@@ -2353,7 +2387,7 @@ class User {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'watchlist',
                                array( /* SET */
-                                       'wl_notificationtimestamp' => NULL
+                                       'wl_notificationtimestamp' => null
                                ), array( /* WHERE */
                                        'wl_user' => $currentUser
                                ), __METHOD__
@@ -2498,9 +2532,9 @@ class User {
                                'user_id' => $this->mId
                        ), __METHOD__
                );
-               
+
                $this->saveOptions();
-               
+
                wfRunHooks( 'UserSaveSettings', array( $this ) );
                $this->clearSharedCache();
                $this->getUserPage()->invalidateCache();
@@ -2599,7 +2633,7 @@ class User {
 
                // Clear instance cache other than user table data, which is already accurate
                $this->clearInstanceCache();
-               
+
                $this->saveOptions();
        }
 
@@ -3023,8 +3057,8 @@ class User {
         * @return \bool True if allowed
         */
        function canSendEmail() {
-               global $wgEnableEmail, $wgEnableUserEmail, $wgUser;
-               if( !$wgEnableEmail || !$wgEnableUserEmail || !$wgUser->isAllowed( 'sendemail' ) ) {
+               global $wgEnableEmail, $wgEnableUserEmail;
+               if( !$wgEnableEmail || !$wgEnableUserEmail || !$this->isAllowed( 'sendemail' ) ) {
                        return false;
                }
                $canSend = $this->isEmailConfirmed();
@@ -3536,18 +3570,18 @@ class User {
         * @param $byEmail Boolean: account made by email?
         */
        public function addNewUserLogEntry( $byEmail = false ) {
-               global $wgUser, $wgContLang, $wgNewUserLog;
+               global $wgUser, $wgNewUserLog;
                if( empty( $wgNewUserLog ) ) {
                        return true; // disabled
                }
-               $talk = $wgContLang->getFormattedNsText( NS_TALK );
+
                if( $this->getName() == $wgUser->getName() ) {
                        $action = 'create';
                        $message = '';
                } else {
                        $action = 'create2';
-                       $message = $byEmail 
-                               ? wfMsgForContent( 'newuserlog-byemail' ) 
+                       $message = $byEmail
+                               ? wfMsgForContent( 'newuserlog-byemail' )
                                : '';
                }
                $log = new LogPage( 'newusers' );
@@ -3575,7 +3609,6 @@ class User {
        }
 
        protected function loadOptions() {
-               global $wgCookiePrefix;
                $this->load();
                if ( $this->mOptionsLoaded || !$this->getId() )
                        return;
@@ -3584,7 +3617,7 @@ class User {
 
                // Maybe load from the object
                if ( !is_null( $this->mOptionOverrides ) ) {
-                       wfDebug( "Loading options for user " . $this->getId() . " from override cache.\n" ); 
+                       wfDebug( "Loading options for user " . $this->getId() . " from override cache.\n" );
                        foreach( $this->mOptionOverrides as $key => $value ) {
                                $this->mOptions[$key] = $value;
                        }
@@ -3604,11 +3637,6 @@ class User {
                                $this->mOptionOverrides[$row->up_property] = $row->up_value;
                                $this->mOptions[$row->up_property] = $row->up_value;
                        }
-
-                       //null skin if User::mId is loaded out of session data without persistant credentials
-                       if ( !isset( $_SESSION['wsToken'] ) && !isset( $_COOKIE["{$wgCookiePrefix}Token"] ) )
-                               $this->mOptions['skin'] = null;
-
                }
 
                $this->mOptionsLoaded = true;
@@ -3623,11 +3651,11 @@ class User {
 
                $this->loadOptions();
                $dbw = wfGetDB( DB_MASTER );
-               
+
                $insert_rows = array();
-               
+
                $saveOptions = $this->mOptions;
-               
+
                // Allow hooks to abort, for instance to save to a global profile.
                // Reset options to default state before saving.
                if( !wfRunHooks( 'UserSaveOptions', array( $this, &$saveOptions ) ) )
@@ -3663,7 +3691,7 @@ class User {
        }
 
        /**
-        * Provide an array of HTML 5 attributes to put on an input element
+        * Provide an array of HTML5 attributes to put on an input element
         * intended for the user to enter a new password.  This may include
         * required, title, and/or pattern, depending on $wgMinimalPasswordLength.
         *