| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use MediaWiki\HTMLForm\HTMLForm; |
| 24 |
use MediaWiki\Language\RawMessage; |
| 25 |
use MediaWiki\Parser\Sanitizer; |
| 26 |
use MediaWiki\SpecialPage\SpecialPage; |
| 27 |
use MediaWiki\SpecialPage\UnlistedSpecialPage; |
| 28 |
use MediaWiki\Status\Status; |
| 29 |
use MediaWiki\User\User; |
| 30 |
use MediaWiki\User\UserFactory; |
| 31 |
use PermissionsError; |
| 32 |
use Profiler; |
| 33 |
use ReadOnlyError; |
| 34 |
use UserNotLoggedIn; |
| 35 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 36 |
use Wikimedia\ScopedCallback; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
class SpecialConfirmEmail extends UnlistedSpecialPage { |
| 52 |
|
| 53 |
private UserFactory $userFactory; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function __construct( UserFactory $userFactory ) { |
| 59 |
parent::__construct( 'Confirmemail', 'editmyprivateinfo' ); |
| 60 |
|
| 61 |
$this->userFactory = $userFactory; |
| 62 |
} |
| 63 |
|
| 64 |
public function doesWrites() { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function execute( $code ) { |
| 77 |
|
| 78 |
|
| 79 |
$trxProfiler = Profiler::instance()->getTransactionProfiler(); |
| 80 |
|
| 81 |
$this->setHeaders(); |
| 82 |
$this->checkReadOnly(); |
| 83 |
$this->checkPermissions(); |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if ( !$this->getAuthority()->isAllowed( 'viewmyprivateinfo' ) ) { |
| 88 |
throw new PermissionsError( 'viewmyprivateinfo' ); |
| 89 |
} |
| 90 |
|
| 91 |
if ( $code === null || $code === '' ) { |
| 92 |
$this->requireNamedUser( 'confirmemail_needlogin', 'exception-nologin', true ); |
| 93 |
if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) { |
| 94 |
$this->showRequestForm(); |
| 95 |
} else { |
| 96 |
$this->getOutput()->addWikiMsg( 'confirmemail_noemail' ); |
| 97 |
} |
| 98 |
} else { |
| 99 |
$scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY ); |
| 100 |
$this->attemptConfirm( $code ); |
| 101 |
ScopedCallback::consume( $scope ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private function showRequestForm() { |
| 109 |
$user = $this->getUser(); |
| 110 |
$out = $this->getOutput(); |
| 111 |
|
| 112 |
if ( !$user->isEmailConfirmed() ) { |
| 113 |
$descriptor = []; |
| 114 |
if ( $user->isEmailConfirmationPending() ) { |
| 115 |
$descriptor += [ |
| 116 |
'pending' => [ |
| 117 |
'type' => 'info', |
| 118 |
'raw' => true, |
| 119 |
'default' => "<div class=\"error mw-confirmemail-pending\">\n" . |
| 120 |
$this->msg( 'confirmemail_pending' )->escaped() . |
| 121 |
"\n</div>", |
| 122 |
], |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
$out->addWikiMsg( 'confirmemail_text' ); |
| 127 |
$form = HTMLForm::factory( 'ooui', $descriptor, $this->getContext() ); |
| 128 |
$form |
| 129 |
->setAction( $this->getPageTitle()->getLocalURL() ) |
| 130 |
->setSubmitTextMsg( 'confirmemail_send' ) |
| 131 |
->setSubmitCallback( [ $this, 'submitSend' ] ); |
| 132 |
|
| 133 |
$retval = $form->show(); |
| 134 |
|
| 135 |
if ( $retval === true ) { |
| 136 |
|
| 137 |
$out->addWikiMsg( 'confirmemail_sent' ); |
| 138 |
} elseif ( $retval instanceof Status && $retval->isGood() ) { |
| 139 |
$out->addWikiTextAsInterface( $retval->getValue() ); |
| 140 |
} |
| 141 |
} else { |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
$lang = $this->getLanguage(); |
| 146 |
$emailAuthenticated = $user->getEmailAuthenticationTimestamp(); |
| 147 |
$time = $lang->userTimeAndDate( $emailAuthenticated, $user ); |
| 148 |
$d = $lang->userDate( $emailAuthenticated, $user ); |
| 149 |
$t = $lang->userTime( $emailAuthenticated, $user ); |
| 150 |
$out->addWikiMsg( 'emailauthenticated', $time, $d, $t ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function submitSend() { |
| 160 |
$status = $this->getUser()->sendConfirmationMail(); |
| 161 |
if ( $status->isGood() ) { |
| 162 |
return Status::newGood( $this->msg( 'confirmemail_sent' )->text() ); |
| 163 |
} else { |
| 164 |
return Status::newFatal( new RawMessage( |
| 165 |
$status->getWikiText( 'confirmemail_sendfailed', false, $this->getLanguage() ) |
| 166 |
) ); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private function attemptConfirm( $code ) { |
| 177 |
$user = $this->userFactory->newFromConfirmationCode( |
| 178 |
$code, |
| 179 |
IDBAccessObject::READ_LATEST |
| 180 |
); |
| 181 |
|
| 182 |
if ( !is_object( $user ) ) { |
| 183 |
if ( User::isWellFormedConfirmationToken( $code ) ) { |
| 184 |
$this->getOutput()->addWikiMsg( 'confirmemail_invalid' ); |
| 185 |
} else { |
| 186 |
$this->getOutput()->addWikiMsg( 'confirmemail_invalid_format' ); |
| 187 |
} |
| 188 |
|
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
$this->authorizeAction( 'confirmemail' )->throwErrorPageError(); |
| 194 |
|
| 195 |
$userLatest = $user->getInstanceForUpdate(); |
| 196 |
$userLatest->confirmEmail(); |
| 197 |
$userLatest->saveSettings(); |
| 198 |
$message = $this->getUser()->isNamed() ? 'confirmemail_loggedin' : 'confirmemail_success'; |
| 199 |
$this->getOutput()->addWikiMsg( $message ); |
| 200 |
|
| 201 |
if ( !$this->getUser()->isNamed() ) { |
| 202 |
$title = SpecialPage::getTitleFor( 'Userlogin' ); |
| 203 |
$this->getOutput()->returnToMain( true, $title ); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
class_alias( SpecialConfirmEmail::class, 'SpecialConfirmEmail' ); |