Merge "(bug 34876) improve $.makeCollapsible performance on the initial collapsing"
[lhc/web/wiklou.git] / includes / specials / SpecialChangeEmail.php
index 62be543..eae8e3a 100644 (file)
  * @ingroup SpecialPage
  */
 class SpecialChangeEmail extends UnlistedSpecialPage {
+
+       /**
+        * Users password
+        * @var string
+        */
+       protected $mPassword;
+
+       /**
+        * Users new email address
+        * @var string
+        */
+       protected $mNewEmail;
+
        public function __construct() {
                parent::__construct( 'ChangeEmail' );
        }
 
+       /**
+        * @return Bool
+        */
        function isListed() {
                global $wgAuth;
                return $wgAuth->allowPropChange( 'emailaddress' );
@@ -41,27 +57,24 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
         */
        function execute( $par ) {
                global $wgAuth;
-               $this->checkReadOnly();
-
-               $request = $this->getRequest();
-               $this->mPassword = $request->getVal( 'wpPassword' );
-               $this->mNewEmail = $request->getVal( 'wpNewEmail' );
 
                $this->setHeaders();
                $this->outputHeader();
 
                $out = $this->getOutput();
                $out->disallowUserJs();
-
-               $user = $this->getUser();
+               $out->addModules( 'mediawiki.special.changeemail' );
 
                if ( !$wgAuth->allowPropChange( 'emailaddress' ) ) {
-                       $this->error( wfMsgExt( 'cannotchangeemail', 'parseinline' ) );
+                       $this->error( 'cannotchangeemail' );
                        return;
                }
 
+               $user = $this->getUser();
+               $request = $this->getRequest();
+
                if ( !$request->wasPosted() && !$user->isLoggedIn() ) {
-                       $this->error( wfMsg( 'changeemail-no-info' ) );
+                       $this->error( 'changeemail-no-info' );
                        return;
                }
 
@@ -70,6 +83,11 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                        return;
                }
 
+               $this->checkReadOnly();
+
+               $this->mPassword = $request->getVal( 'wpPassword' );
+               $this->mNewEmail = $request->getVal( 'wpNewEmail' );
+
                if ( $request->wasPosted()
                        && $user->matchEditToken( $request->getVal( 'token' ) ) )
                {
@@ -88,6 +106,9 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                $this->showForm();
        }
 
+       /**
+        * @param $type string
+        */
        protected function doReturnTo( $type = 'hard' ) {
                $titleObj = Title::newFromText( $this->getRequest()->getVal( 'returnto' ) );
                if ( !$titleObj instanceof Title ) {
@@ -100,19 +121,23 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                }
        }
 
+       /**
+        * @param $msg string
+        */
        protected function error( $msg ) {
-               $this->getOutput()->addHTML( Xml::element('p', array( 'class' => 'error' ), $msg ) );
+               $this->getOutput()->wrapWikiMsg( "<p class='error'>\n$1\n</p>", $msg );
        }
 
        protected function showForm() {
+               global $wgRequirePasswordforEmailChange;
                $user = $this->getUser();
 
                $oldEmailText = $user->getEmail()
                        ? $user->getEmail()
-                       : wfMsg( 'changeemail-none' );
+                       : $this->msg( 'changeemail-none' )->text();
 
                $this->getOutput()->addHTML(
-                       Xml::fieldset( wfMsg( 'changeemail-header' ) ) .
+                       Xml::fieldset( $this->msg( 'changeemail-header' )->text() ) .
                        Xml::openElement( 'form',
                                array(
                                        'method' => 'post',
@@ -120,19 +145,26 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                                        'id' => 'mw-changeemail-form' ) ) . "\n" .
                        Html::hidden( 'token', $user->getEditToken() ) . "\n" .
                        Html::hidden( 'returnto', $this->getRequest()->getVal( 'returnto' ) ) . "\n" .
-                       wfMsgExt( 'changeemail-text', array( 'parse' ) ) . "\n" .
-                       Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n" .
-                       $this->pretty( array(
-                               array( 'wpName', 'username', 'text', $user->getName() ),
-                               array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
-                               array( 'wpNewEmail', 'changeemail-newemail', 'input', $this->mNewEmail ),
-                               array( 'wpPassword', 'yourpassword', 'password', $this->mPassword ),
-                       ) ) . "\n" .
+                       $this->msg( 'changeemail-text' )->parseAsBlock() . "\n" .
+                       Xml::openElement( 'table', array( 'id' => 'mw-changeemail-table' ) ) . "\n"
+               );
+               $items = array(
+                       array( 'wpName', 'username', 'text', $user->getName() ),
+                       array( 'wpOldEmail', 'changeemail-oldemail', 'text', $oldEmailText ),
+                       array( 'wpNewEmail', 'changeemail-newemail', 'email', $this->mNewEmail ),
+               );
+               if ( $wgRequirePasswordforEmailChange ) {
+                       $items[] = array( 'wpPassword', 'changeemail-password', 'password', $this->mPassword );
+               }
+
+               $this->getOutput()->addHTML(
+                       $this->pretty( $items ) .
+                       "\n" .
                        "<tr>\n" .
                                "<td></td>\n" .
                                '<td class="mw-input">' .
-                                       Xml::submitButton( wfMsg( 'changeemail-submit' ) ) .
-                                       Xml::submitButton( wfMsg( 'changeemail-cancel' ), array( 'name' => 'wpCancel' ) ) .
+                                       Xml::submitButton( $this->msg( 'changeemail-submit' )->text() ) .
+                                       Xml::submitButton( $this->msg( 'changeemail-cancel' )->text(), array( 'name' => 'wpCancel' ) ) .
                                "</td>\n" .
                        "</tr>\n" .
                        Xml::closeElement( 'table' ) .
@@ -141,6 +173,10 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                );
        }
 
+       /**
+        * @param $fields array
+        * @return string
+        */
        protected function pretty( $fields ) {
                $out = '';
                foreach ( $fields as $list ) {
@@ -157,9 +193,9 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                        $out .= "<tr>\n";
                        $out .= "\t<td class='mw-label'>";
                        if ( $type != 'text' ) {
-                               $out .= Xml::label( wfMsg( $label ), $name );
+                               $out .= Xml::label( $this->msg( $label )->text(), $name );
                        } else {
-                               $out .=  wfMsgHtml( $label );
+                               $out .=  $this->msg( $label )->escaped();
                        }
                        $out .= "</td>\n";
                        $out .= "\t<td class='mw-input'>";
@@ -171,22 +207,28 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
        }
 
        /**
+        * @param $user User
+        * @param $pass string
+        * @param $newaddr string
         * @return bool|string true or string on success, false on failure
         */
        protected function attemptChange( User $user, $pass, $newaddr ) {
+               global $wgAuth;
+
                if ( $newaddr != '' && !Sanitizer::validateEmail( $newaddr ) ) {
-                       $this->error( wfMsgExt( 'invalidemailaddress', 'parseinline' ) );
+                       $this->error( 'invalidemailaddress' );
                        return false;
                }
 
                $throttleCount = LoginForm::incLoginThrottle( $user->getName() );
                if ( $throttleCount === true ) {
-                       $this->error( wfMsgHtml( 'login-throttled' ) );
+                       $this->error( 'login-throttled' );
                        return false;
                }
 
-               if ( !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
-                       $this->error( wfMsgHtml( 'wrongpassword' ) );
+               global $wgRequirePasswordforEmailChange;
+               if ( $wgRequirePasswordforEmailChange && !$user->checkTemporaryPassword( $pass ) && !$user->checkPassword( $pass ) ) {
+                       $this->error( 'wrongpassword' );
                        return false;
                }
 
@@ -194,18 +236,22 @@ class SpecialChangeEmail extends UnlistedSpecialPage {
                        LoginForm::clearLoginThrottle( $user->getName() );
                }
 
-               list( $status, $info ) = Preferences::trySetUserEmail( $user, $newaddr );
-               if ( $status !== true ) {
-                       if ( $status instanceof Status ) {
-                               $this->getOutput()->addHTML(
-                                       '<p class="error">' .
-                                       $this->getOutput()->parseInline( $status->getWikiText( $info ) ) .
-                                       '</p>' );
-                       }
+               $oldaddr = $user->getEmail();
+               $status = $user->setEmailWithConfirmation( $newaddr );
+               if ( !$status->isGood() ) {
+                       $this->getOutput()->addHTML(
+                               '<p class="error">' .
+                               $this->getOutput()->parseInline( $status->getWikiText( 'mailerror' ) ) .
+                               '</p>' );
                        return false;
                }
 
+               wfRunHooks( 'PrefsEmailAudit', array( $user, $oldaddr, $newaddr ) );
+
                $user->saveSettings();
-               return $info ? $info : true;
+
+               $wgAuth->updateExternalDB( $user );
+
+               return $status->value;
        }
 }