fix some spacing
[lhc/web/wiklou.git] / includes / User.php
index c2af93c..4daa5d6 100644 (file)
@@ -574,7 +574,6 @@ class User {
                        return false;
                }
 
-
                // Ensure that the name can't be misresolved as a different title,
                // such as with extra namespace keys at the start.
                $parsed = Title::newFromText( $name );
@@ -857,7 +856,7 @@ class User {
         * @note This no longer clears uncached lazy-initialised properties;
         *       the constructor does that instead.
         *
-        * @param $name string
+        * @param $name string|bool
         */
        public function loadDefaults( $name = false ) {
                wfProfileIn( __METHOD__ );
@@ -980,10 +979,13 @@ class User {
                }
 
                if ( $request->getSessionData( 'wsToken' ) ) {
-                       $passwordCorrect = $proposedUser->getToken( false ) === $request->getSessionData( 'wsToken' );
+                       $passwordCorrect = ( $proposedUser->getToken( false ) === $request->getSessionData( 'wsToken' ) );
                        $from = 'session';
                } elseif ( $request->getCookie( 'Token' ) ) {
-                       $passwordCorrect = $proposedUser->getToken( false ) === $request->getCookie( 'Token' );
+                       # Get the token from DB/cache and clean it up to remove garbage padding.
+                       # This deals with historical problems with bugs and the default column value.
+                       $token = rtrim( $proposedUser->getToken( false ) ); // correct token
+                       $passwordCorrect = ( strlen( $token ) && $token === $request->getCookie( 'Token' ) );
                        $from = 'cookie';
                } else {
                        # No session or persistent login cookie
@@ -1262,7 +1264,6 @@ class User {
                }
        }
 
-
        /**
         * Get blocking information
         * @param $bFromSlave Bool Whether to check the slave database first. To
@@ -2046,7 +2047,9 @@ class User {
        /**
         * Set the password and reset the random token unconditionally.
         *
-        * @param $str String New password to set
+        * @param $str string|null New password to set or null to set an invalid
+        *        password hash meaning that the user will not be able to log in
+        *        through the web interface.
         */
        public function setInternalPassword( $str ) {
                $this->load();
@@ -2064,7 +2067,7 @@ class User {
 
        /**
         * Get the user's current token.
-        * @param $forceCreation Force the generation of a new token if the user doesn't have one (default=true for backwards compatibility)
+        * @param $forceCreation bool Force the generation of a new token if the user doesn't have one (default=true for backwards compatibility)
         * @return String Token
         */
        public function getToken( $forceCreation = true ) {
@@ -2317,6 +2320,7 @@ class User {
         * - 'registered' - preferences which are registered in core MediaWiki or
         *                  by extensions using the UserGetDefaultOptions hook.
         * - 'registered-multiselect' - as above, using the 'multiselect' type.
+        * - 'registered-checkmatrix' - as above, using the 'checkmatrix' type.
         * - 'userjs' - preferences with names starting with 'userjs-', intended to
         *              be used by user scripts.
         * - 'unused' - preferences about which MediaWiki doesn't know anything.
@@ -2333,6 +2337,7 @@ class User {
                return array(
                        'registered',
                        'registered-multiselect',
+                       'registered-checkmatrix',
                        'userjs',
                        'unused'
                );
@@ -2358,8 +2363,8 @@ class User {
                $prefs = Preferences::getPreferences( $this, $context );
                $mapping = array();
 
-               // Multiselect options are stored in the database with one key per
-               // option, each having a boolean value. Extract those keys.
+               // Multiselect and checkmatrix options are stored in the database with
+               // one key per option, each having a boolean value. Extract those keys.
                $multiselectOptions = array();
                foreach ( $prefs as $name => $info ) {
                        if ( ( isset( $info['type'] ) && $info['type'] == 'multiselect' ) ||
@@ -2374,6 +2379,23 @@ class User {
                                unset( $prefs[$name] );
                        }
                }
+               $checkmatrixOptions = array();
+               foreach ( $prefs as $name => $info ) {
+                       if ( ( isset( $info['type'] ) && $info['type'] == 'checkmatrix' ) ||
+                                       ( isset( $info['class'] ) && $info['class'] == 'HTMLCheckMatrix' ) ) {
+                               $columns = HTMLFormField::flattenOptions( $info['columns'] );
+                               $rows = HTMLFormField::flattenOptions( $info['rows'] );
+                               $prefix = isset( $info['prefix'] ) ? $info['prefix'] : $name;
+
+                               foreach ( $columns as $column ) {
+                                       foreach ( $rows as $row ) {
+                                               $checkmatrixOptions["$prefix-$column-$row"] = true;
+                                       }
+                               }
+
+                               unset( $prefs[$name] );
+                       }
+               }
 
                // $value is ignored
                foreach ( $options as $key => $value ) {
@@ -2381,6 +2403,8 @@ class User {
                                $mapping[$key] = 'registered';
                        } elseif( isset( $multiselectOptions[$key] ) ) {
                                $mapping[$key] = 'registered-multiselect';
+                       } elseif( isset( $checkmatrixOptions[$key] ) ) {
+                               $mapping[$key] = 'registered-checkmatrix';
                        } elseif ( substr( $key, 0, 7 ) === 'userjs-' ) {
                                $mapping[$key] = 'userjs';
                        } else {
@@ -2399,14 +2423,14 @@ class User {
         * and 'all', which forces a reset of *all* preferences and overrides everything else.
         *
         * @param $resetKinds array|string which kinds of preferences to reset. Defaults to
-        *                                 array( 'registered', 'registered-multiselect', 'unused' )
-        *                                 for backwards-compatibility.
+        *             array( 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused' )
+        *             for backwards-compatibility.
         * @param $context IContextSource|null context source used when $resetKinds
-        *                                     does not contain 'all', passed to getOptionKinds().
-        *                                     Defaults to RequestContext::getMain() when null.
+        *             does not contain 'all', passed to getOptionKinds().
+        *             Defaults to RequestContext::getMain() when null.
         */
        public function resetOptions(
-               $resetKinds = array( 'registered', 'registered-multiselect', 'unused' ),
+               $resetKinds = array( 'registered', 'registered-multiselect', 'registered-checkmatrix', 'unused' ),
                IContextSource $context = null
        ) {
                $this->load();
@@ -2469,7 +2493,7 @@ class User {
         */
        public function getStubThreshold() {
                global $wgMaxArticleSize; # Maximum article size, in Kb
-               $threshold = intval( $this->getOption( 'stubthreshold' ) );
+               $threshold = $this->getIntOption( 'stubthreshold' );
                if ( $threshold > $wgMaxArticleSize * 1024 ) {
                        # If they have set an impossible value, disable the preference
                        # so we can use the parser cache again.
@@ -2954,7 +2978,7 @@ class User {
         *
         * @param $request WebRequest object to use; $wgRequest will be used if null
         *        is passed.
-        * @param $secure Whether to force secure/insecure cookies or use default
+        * @param $secure bool Whether to force secure/insecure cookies or use default
         */
        public function setCookies( $request = null, $secure = null ) {
                if ( $request === null ) {
@@ -2962,7 +2986,9 @@ class User {
                }
 
                $this->load();
-               if ( 0 == $this->mId ) return;
+               if ( 0 == $this->mId ) {
+                       return;
+               }
                if ( !$this->mToken ) {
                        // When token is empty or NULL generate a new one and then save it to the database
                        // This allows a wiki to re-secure itself after a leak of it's user table or $wgSecretKey
@@ -3112,6 +3138,7 @@ class User {
        public static function createNew( $name, $params = array() ) {
                $user = new User;
                $user->load();
+               $user->setToken(); // init token
                if ( isset( $params['options'] ) ) {
                        $user->mOptions = $params['options'] + (array)$user->mOptions;
                        unset( $params['options'] );
@@ -3173,6 +3200,9 @@ class User {
         */
        public function addToDatabase() {
                $this->load();
+               if ( !$this->mToken ) {
+                       $this->setToken(); // init token
+               }
 
                $this->mTouched = self::newTouchedTimestamp();
 
@@ -3277,7 +3307,7 @@ class User {
                // since it disables the parser cache, its value will always
                // be 0 when this function is called by parsercache.
 
-               $confstr =        $this->getOption( 'math' );
+               $confstr = $this->getOption( 'math' );
                $confstr .= '!' . $this->getStubThreshold();
                $confstr .= '!' . ( $this->getOption( 'numberheadings' ) ? '1' : '' );
                $confstr .= '!' . $wgLang->getCode();
@@ -3480,11 +3510,10 @@ class User {
        /**
         * Generate a looking random token for various uses.
         *
-        * @param $salt String Optional salt value
         * @return String The new random token
         * @deprecated since 1.20; Use MWCryptRand for secure purposes or wfRandomString for pesudo-randomness
         */
-       public static function generateToken( $salt = '' ) {
+       public static function generateToken() {
                return MWCryptRand::generateHex( 32 );
        }
 
@@ -3746,8 +3775,9 @@ class User {
        /**
         * Get the timestamp of account creation.
         *
-        * @return String|Bool Timestamp of account creation, or false for
-        *     non-existent/anonymous user accounts.
+        * @return String|Bool|Null Timestamp of account creation, false for
+        *     non-existent/anonymous user accounts, or null if existing account
+        *     but information is not in database.
         */
        public function getRegistration() {
                if ( $this->isAnon() ) {
@@ -4291,7 +4321,7 @@ class User {
        /**
         * Load the user options either from cache, the database or an array
         *
-        * @param $data Rows for the current user out of the user_properties table
+        * @param $data array Rows for the current user out of the user_properties table
         */
        protected function loadOptions( $data = null ) {
                global $wgContLang;