MediaWiki master
SpecialConfirmEmail.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
9use LogicException;
21use Profiler;
23use Wikimedia\ScopedCallback;
24
39
40 private UserFactory $userFactory;
41
42 public function __construct( UserFactory $userFactory ) {
43 parent::__construct( 'Confirmemail', 'editmyprivateinfo' );
44
45 $this->userFactory = $userFactory;
46 }
47
49 public function doesWrites() {
50 return true;
51 }
52
61 public function execute( $code ) {
62 // Ignore things like primary queries/connections on GET requests.
63 // It's very convenient to just allow formless link usage.
64 $trxProfiler = Profiler::instance()->getTransactionProfiler();
65
66 $this->setHeaders();
67 $this->checkReadOnly();
68 $this->checkPermissions();
69
70 // This could also let someone check the current email address, so
71 // require both permissions.
72 if ( !$this->getAuthority()->isAllowed( 'viewmyprivateinfo' ) ) {
73 throw new PermissionsError( 'viewmyprivateinfo' );
74 }
75
76 if ( $code === null || $code === '' ) {
77 $this->requireNamedUser( 'confirmemail_needlogin', 'exception-nologin', true );
78 if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
79 $this->showRequestForm();
80 } else {
81 $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
82 }
83 } else {
84 $scope = $trxProfiler->silenceForScope( $trxProfiler::EXPECTATION_REPLICAS_ONLY );
85 $this->attemptConfirm( $code );
86 ScopedCallback::consume( $scope );
87 }
88 }
89
93 private function showRequestForm() {
94 $user = $this->getUser();
95 $out = $this->getOutput();
96
97 if ( !$user->isEmailConfirmed() ) {
98 $descriptor = [];
99 if ( $user->isEmailConfirmationPending() ) {
100 $descriptor += [
101 'pending' => [
102 'type' => 'info',
103 'raw' => true,
104 'default' => "<div class=\"error mw-confirmemail-pending\">\n" .
105 $this->msg( 'confirmemail_pending' )->escaped() .
106 "\n</div>",
107 ],
108 ];
109 }
110
111 $out->addWikiMsg( 'confirmemail_text' );
112 $form = HTMLForm::factory( 'ooui', $descriptor, $this->getContext() );
113 $form
114 ->setAction( $this->getPageTitle()->getLocalURL() )
115 ->setSubmitTextMsg( 'confirmemail_send' )
116 ->setSubmitCallback( $this->submitSend( ... ) );
117
118 $retval = $form->show();
119
120 if ( $retval === true || ( $retval instanceof Status && $retval->isGood() ) ) {
121 $out->addWikiMsg( 'confirmemail_sent' );
122 }
123 } else {
124 // date and time are separate parameters to facilitate localisation.
125 // $time is kept for backward compat reasons.
126 // 'emailauthenticated' is also used in SpecialPreferences.php
127 $lang = $this->getLanguage();
128 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
129 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
130 $d = $lang->userDate( $emailAuthenticated, $user );
131 $t = $lang->userTime( $emailAuthenticated, $user );
132 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
133 }
134 }
135
141 private function submitSend() {
142 $status = $this->getUser()->sendConfirmationMail();
143 if ( $status->isGood() ) {
144 return Status::newGood();
145 } else {
146 return Status::newFatal( new RawMessage(
147 $status->getWikiText( 'confirmemail_sendfailed', false, $this->getLanguage() )
148 ) );
149 }
150 }
151
158 private function attemptConfirm( $code ) {
159 $user = $this->userFactory->newFromConfirmationCode(
160 $code,
161 IDBAccessObject::READ_LATEST
162 );
163
164 if ( !is_object( $user ) ) {
165 if ( User::isWellFormedConfirmationToken( $code ) ) {
166 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
167 } else {
168 $this->getOutput()->addWikiMsg( 'confirmemail_invalid_format' );
169 }
170
171 return;
172 }
173
174 // Enforce permissions, user blocks, and rate limits
175 $this->authorizeAction( 'confirmemail' )->throwErrorPageError();
176
177 $userLatest = $user->getInstanceFromPrimary() ?? throw new LogicException( 'No user' );
178 $userLatest->confirmEmail();
179 $userLatest->saveSettings();
180 $message = $this->getUser()->isNamed() ? 'confirmemail_loggedin' : 'confirmemail_success';
181 $this->getOutput()->addWikiMsg( $message );
182
183 if ( !$this->getUser()->isNamed() ) {
184 $title = SpecialPage::getTitleFor( 'Userlogin' );
185 $this->getOutput()->returnToMain( true, $title );
186 }
187 }
188}
189
191class_alias( SpecialConfirmEmail::class, 'SpecialConfirmEmail' );
Show an error when a user tries to do something they do not have the necessary permissions for.
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
Redirect a user to the login page or account creation page.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:195
Variant of the Message class.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:32
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getUser()
Shortcut to get the User executing this instance.
authorizeAction(?string $action=null)
Utility function for authorizing an action to be performed by the special page.
requireNamedUser( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin', bool $alwaysRedirectToLoginPage=false)
If the user is not logged in or is a temporary user, throws UserNotLoggedIn.
getPageTitle( $subpage=false)
Get a self-referential title object.
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getAuthority()
Shortcut to get the Authority executing this instance.
getLanguage()
Shortcut to get user's language.
Shortcut to construct a special page which is unlisted by default.
Email confirmation for registered users.
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki....
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Create User objects.
User class for the MediaWiki software.
Definition User.php:109
Profiler base class that defines the interface and some shared functionality.
Definition Profiler.php:22
static instance()
Definition Profiler.php:90
Interface for database access objects.