Merge "(bug 18195) Allow changing preferences via API"
[lhc/web/wiklou.git] / includes / User.php
index 566dcc7..793714d 100644 (file)
@@ -2041,7 +2041,6 @@ class User {
         * @param $token String|bool If specified, set the token to this value
         */
        public function setToken( $token = false ) {
-               global $wgSecretKey, $wgProxyKey;
                $this->load();
                if ( !$token ) {
                        $this->mToken = MWCryptRand::generateHex( USER_TOKEN_LENGTH );
@@ -2113,6 +2112,42 @@ class User {
                wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail ) );
        }
 
+       /**
+        * Set the user's e-mail address and a confirmation mail if needed.
+        *
+        * @since 1.20
+        * @param $str String New e-mail address
+        * @return Status
+        */
+       public function setEmailWithConfirmation( $str ) {
+               global $wgEnableEmail, $wgEmailAuthentication;
+
+               if ( !$wgEnableEmail ) {
+                       return Status::newFatal( 'emaildisabled' );
+               }
+
+               $oldaddr = $this->getEmail();
+               if ( $str === $oldaddr ) {
+                       return Status::newGood( true );
+               }
+
+               $this->setEmail( $str );
+
+               if ( $str !== '' && $wgEmailAuthentication ) {
+                       # Send a confirmation request to the new address if needed
+                       $type = $oldaddr != '' ? 'changed' : 'set';
+                       $result = $this->sendConfirmationMail( $type );
+                       if ( $result->isGood() ) {
+                               # Say the the caller that a confirmation mail has been sent
+                               $result->value = 'eauth';
+                       }
+               } else {
+                       $result = Status::newGood( true );
+               }
+
+               return $result;
+       }
+
        /**
         * Get the user's real name
         * @return String User's real name
@@ -2246,7 +2281,10 @@ class User {
         * Reset all options to the site defaults
         */
        public function resetOptions() {
+               $this->load();
+
                $this->mOptions = self::getDefaultOptions();
+               $this->mOptionsLoaded = true;
        }
 
        /**
@@ -3303,11 +3341,12 @@ class User {
                global $wgUserEmailConfirmationTokenExpiry;
                $now = time();
                $expires = $now + $wgUserEmailConfirmationTokenExpiry;
+               $expiration = wfTimestamp( TS_MW, $expires );
                $this->load();
                $token = MWCryptRand::generateHex( 32 );
                $hash = md5( $token );
                $this->mEmailToken = $hash;
-               $this->mEmailTokenExpires = wfTimestamp( TS_MW, $expires );
+               $this->mEmailTokenExpires = $expiration;
                return $token;
        }
 
@@ -3888,7 +3927,7 @@ class User {
                } elseif ( $type == ':B:' ) {
                        # Salted
                        list( $salt, $realHash ) = explode( ':', substr( $hash, 3 ), 2 );
-                       return md5( $salt.'-'.md5( $password ) ) == $realHash;
+                       return md5( $salt.'-'.md5( $password ) ) === $realHash;
                } else {
                        # Old-style
                        return self::oldCrypt( $password, $userId ) === $hash;