MediaWiki REL1_39
SpecialConfirmEmail.php
Go to the documentation of this file.
1<?php
25use Wikimedia\ScopedCallback;
26
36
38 private $userFactory;
39
43 public function __construct( UserFactory $userFactory ) {
44 parent::__construct( 'Confirmemail', 'editmyprivateinfo' );
45
46 $this->userFactory = $userFactory;
47 }
48
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->requireLogin( 'confirmemail_needlogin' );
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();
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 ) {
121 // should never happen, but if so, don't let the user without any message
122 $out->addWikiMsg( 'confirmemail_sent' );
123 } elseif ( $retval instanceof Status && $retval->isGood() ) {
124 $out->addWikiTextAsInterface( $retval->getValue() );
125 }
126 } else {
127 // date and time are separate parameters to facilitate localisation.
128 // $time is kept for backward compat reasons.
129 // 'emailauthenticated' is also used in SpecialPreferences.php
130 $lang = $this->getLanguage();
131 $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
132 $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
133 $d = $lang->userDate( $emailAuthenticated, $user );
134 $t = $lang->userTime( $emailAuthenticated, $user );
135 $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
136 }
137 }
138
144 public function submitSend() {
145 $status = $this->getUser()->sendConfirmationMail();
146 if ( $status->isGood() ) {
147 return Status::newGood( $this->msg( 'confirmemail_sent' )->text() );
148 } else {
149 return Status::newFatal( new RawMessage(
150 $status->getWikiText( 'confirmemail_sendfailed', false, $this->getLanguage() )
151 ) );
152 }
153 }
154
161 private function attemptConfirm( $code ) {
162 $user = $this->userFactory->newFromConfirmationCode(
163 $code,
164 UserFactory::READ_LATEST
165 );
166
167 if ( !is_object( $user ) ) {
168 $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
169
170 return;
171 }
172
173 // rate limit email confirmations
174 if ( $user->pingLimiter( 'confirmemail' ) ) {
175 $this->getOutput()->addWikiMsg( 'actionthrottledtext' );
176
177 return;
178 }
179
180 $userLatest = $user->getInstanceForUpdate();
181 $userLatest->confirmEmail();
182 $userLatest->saveSettings();
183 $message = $this->getUser()->isRegistered() ? 'confirmemail_loggedin' : 'confirmemail_success';
184 $this->getOutput()->addWikiMsg( $message );
185
186 if ( !$this->getUser()->isRegistered() ) {
187 $title = SpecialPage::getTitleFor( 'Userlogin' );
188 $this->getOutput()->returnToMain( true, $title );
189 }
190 }
191}
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition HTMLForm.php:348
Creates User objects.
Show an error when a user tries to do something they do not have the necessary permissions for.
Variant of the Message class.
Special page allows users to request email confirmation message, and handles processing of the confir...
doesWrites()
Indicates whether this special page may perform database writes.
execute( $code)
Main execution point.
__construct(UserFactory $userFactory)
submitSend()
Callback for HTMLForm send confirmation mail.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
getUser()
Shortcut to get the User executing this instance.
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
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,...
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getAuthority()
Shortcut to get the Authority executing this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
getPageTitle( $subpage=false)
Get a self-referential title object.
getLanguage()
Shortcut to get user's language.
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Shortcut to construct a special page which is unlisted by default.
if(!isset( $args[0])) $lang