MediaWiki  1.34.0
SpecialConfirmEmail.php
Go to the documentation of this file.
1 <?php
25 
35  public function __construct() {
36  parent::__construct( 'Confirmemail', 'editmyprivateinfo' );
37  }
38 
39  public function doesWrites() {
40  return true;
41  }
42 
51  function execute( $code ) {
52  // Ignore things like master queries/connections on GET requests.
53  // It's very convenient to just allow formless link usage.
54  $trxProfiler = Profiler::instance()->getTransactionProfiler();
55 
56  $this->setHeaders();
57  $this->checkReadOnly();
58  $this->checkPermissions();
59 
60  // This could also let someone check the current email address, so
61  // require both permissions.
62  if ( !MediaWikiServices::getInstance()
64  ->userHasRight( $this->getUser(), 'viewmyprivateinfo' )
65  ) {
66  throw new PermissionsError( 'viewmyprivateinfo' );
67  }
68 
69  if ( $code === null || $code === '' ) {
70  $this->requireLogin( 'confirmemail_needlogin' );
71  if ( Sanitizer::validateEmail( $this->getUser()->getEmail() ) ) {
72  $this->showRequestForm();
73  } else {
74  $this->getOutput()->addWikiMsg( 'confirmemail_noemail' );
75  }
76  } else {
77  $old = $trxProfiler->setSilenced( true );
78  $this->attemptConfirm( $code );
79  $trxProfiler->setSilenced( $old );
80  }
81  }
82 
86  function showRequestForm() {
87  $user = $this->getUser();
88  $out = $this->getOutput();
89 
90  if ( !$user->isEmailConfirmed() ) {
91  $descriptor = [];
92  if ( $user->isEmailConfirmationPending() ) {
93  $descriptor += [
94  'pending' => [
95  'type' => 'info',
96  'raw' => true,
97  'default' => "<div class=\"error mw-confirmemail-pending\">\n" .
98  $this->msg( 'confirmemail_pending' )->escaped() .
99  "\n</div>",
100  ],
101  ];
102  }
103 
104  $out->addWikiMsg( 'confirmemail_text' );
105  $form = HTMLForm::factory( 'ooui', $descriptor, $this->getContext() );
106  $form
107  ->setMethod( 'post' )
108  ->setAction( $this->getPageTitle()->getLocalURL() )
109  ->setSubmitTextMsg( 'confirmemail_send' )
110  ->setSubmitCallback( [ $this, 'submitSend' ] );
111 
112  $retval = $form->show();
113 
114  if ( $retval === true ) {
115  // should never happen, but if so, don't let the user without any message
116  $out->addWikiMsg( 'confirmemail_sent' );
117  } elseif ( $retval instanceof Status && $retval->isGood() ) {
118  $out->addWikiTextAsInterface( $retval->getValue() );
119  }
120  } else {
121  // date and time are separate parameters to facilitate localisation.
122  // $time is kept for backward compat reasons.
123  // 'emailauthenticated' is also used in SpecialPreferences.php
124  $lang = $this->getLanguage();
125  $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
126  $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
127  $d = $lang->userDate( $emailAuthenticated, $user );
128  $t = $lang->userTime( $emailAuthenticated, $user );
129  $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
130  }
131  }
132 
138  public function submitSend() {
139  $status = $this->getUser()->sendConfirmationMail();
140  if ( $status->isGood() ) {
141  return Status::newGood( $this->msg( 'confirmemail_sent' )->text() );
142  } else {
143  return Status::newFatal( new RawMessage(
144  $status->getWikiText( 'confirmemail_sendfailed', false, $this->getLanguage() )
145  ) );
146  }
147  }
148 
155  private function attemptConfirm( $code ) {
156  $user = User::newFromConfirmationCode( $code, User::READ_EXCLUSIVE );
157  if ( !is_object( $user ) ) {
158  $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
159 
160  return;
161  }
162 
163  // rate limit email confirmations
164  if ( $user->pingLimiter( 'confirmemail' ) ) {
165  $this->getOutput()->addWikiMsg( 'actionthrottledtext' );
166 
167  return;
168  }
169 
170  $user->confirmEmail();
171  $user->saveSettings();
172  $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
173  $this->getOutput()->addWikiMsg( $message );
174 
175  if ( !$this->getUser()->isLoggedIn() ) {
176  $title = SpecialPage::getTitleFor( 'Userlogin' );
177  $this->getOutput()->returnToMain( true, $title );
178  }
179  }
180 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:672
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:63
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
UnlistedSpecialPage
Shortcut to construct a special page which is unlisted by default.
Definition: UnlistedSpecialPage.php:29
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:315
SpecialConfirmEmail\showRequestForm
showRequestForm()
Show a nice form for the user to request a confirmation mail.
Definition: SpecialConfirmEmail.php:86
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:83
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:30
SpecialConfirmEmail\attemptConfirm
attemptConfirm( $code)
Attempt to confirm the user's email address and show success or failure as needed; if successful,...
Definition: SpecialConfirmEmail.php:155
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:749
SpecialConfirmEmail
Special page allows users to request email confirmation message, and handles processing of the confir...
Definition: SpecialConfirmEmail.php:34
SpecialConfirmEmail\submitSend
submitSend()
Callback for HTMLForm send confirmation mail.
Definition: SpecialConfirmEmail.php:138
SpecialConfirmEmail\__construct
__construct()
Definition: SpecialConfirmEmail.php:35
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
StatusValue\isGood
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: StatusValue.php:121
getPermissionManager
getPermissionManager()
$t
$t
Definition: make-normalization-table.php:143
$title
$title
Definition: testCompression.php:34
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:729
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:345
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
SpecialConfirmEmail\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialConfirmEmail.php:39
$status
return $status
Definition: SyntaxHighlight.php:347
SpecialConfirmEmail\execute
execute( $code)
Main execution point.
Definition: SpecialConfirmEmail.php:51
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:328
User\newFromConfirmationCode
static newFromConfirmationCode( $code, $flags=0)
Factory method to fetch whichever user has a given email confirmation code.
Definition: User.php:650
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:303