MediaWiki  master
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  public 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  private 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  ->setAction( $this->getPageTitle()->getLocalURL() )
108  ->setSubmitTextMsg( 'confirmemail_send' )
109  ->setSubmitCallback( [ $this, 'submitSend' ] );
110 
111  $retval = $form->show();
112 
113  if ( $retval === true ) {
114  // should never happen, but if so, don't let the user without any message
115  $out->addWikiMsg( 'confirmemail_sent' );
116  } elseif ( $retval instanceof Status && $retval->isGood() ) {
117  $out->addWikiTextAsInterface( $retval->getValue() );
118  }
119  } else {
120  // date and time are separate parameters to facilitate localisation.
121  // $time is kept for backward compat reasons.
122  // 'emailauthenticated' is also used in SpecialPreferences.php
123  $lang = $this->getLanguage();
124  $emailAuthenticated = $user->getEmailAuthenticationTimestamp();
125  $time = $lang->userTimeAndDate( $emailAuthenticated, $user );
126  $d = $lang->userDate( $emailAuthenticated, $user );
127  $t = $lang->userTime( $emailAuthenticated, $user );
128  $out->addWikiMsg( 'emailauthenticated', $time, $d, $t );
129  }
130  }
131 
137  public function submitSend() {
138  $status = $this->getUser()->sendConfirmationMail();
139  if ( $status->isGood() ) {
140  return Status::newGood( $this->msg( 'confirmemail_sent' )->text() );
141  } else {
142  return Status::newFatal( new RawMessage(
143  $status->getWikiText( 'confirmemail_sendfailed', false, $this->getLanguage() )
144  ) );
145  }
146  }
147 
154  private function attemptConfirm( $code ) {
155  $user = User::newFromConfirmationCode( $code, User::READ_EXCLUSIVE );
156  if ( !is_object( $user ) ) {
157  $this->getOutput()->addWikiMsg( 'confirmemail_invalid' );
158 
159  return;
160  }
161 
162  // rate limit email confirmations
163  if ( $user->pingLimiter( 'confirmemail' ) ) {
164  $this->getOutput()->addWikiMsg( 'actionthrottledtext' );
165 
166  return;
167  }
168 
169  $user->confirmEmail();
170  $user->saveSettings();
171  $message = $this->getUser()->isLoggedIn() ? 'confirmemail_loggedin' : 'confirmemail_success';
172  $this->getOutput()->addWikiMsg( $message );
173 
174  if ( !$this->getUser()->isLoggedIn() ) {
175  $title = SpecialPage::getTitleFor( 'Userlogin' );
176  $this->getOutput()->returnToMain( true, $title );
177  }
178  }
179 }
SpecialPage\getPageTitle
getPageTitle( $subpage=false)
Get a self-referential title object.
Definition: SpecialPage.php:669
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:800
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:716
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:152
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
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:328
Sanitizer\validateEmail
static validateEmail( $addr)
Does a string look like an e-mail address?
Definition: Sanitizer.php:1984
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:90
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:154
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:746
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:137
SpecialConfirmEmail\__construct
__construct()
Definition: SpecialConfirmEmail.php:35
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
StatusValue\isGood
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Definition: StatusValue.php:122
getPermissionManager
getPermissionManager()
$title
$title
Definition: testCompression.php:38
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:551
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:726
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:689
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:358
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
SpecialConfirmEmail\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialConfirmEmail.php:39
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:341
$t
$t
Definition: testCompression.php:74
User\newFromConfirmationCode
static newFromConfirmationCode( $code, $flags=0)
Factory method to fetch whichever user has a given email confirmation code.
Definition: User.php:669
RawMessage
Variant of the Message class.
Definition: RawMessage.php:35
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:311