mLoadedRequest = false; } /** * Load basic request parameters for this Special page. */ private function loadRequestParameters() { if ( $this->mLoadedRequest ) { return; } $this->mLoadedRequest = true; $request = $this->getRequest(); $this->mPosted = $request->wasPosted(); $this->mAction = $request->getVal( 'action' ); $this->mFromHTTP = $request->getBool( 'fromhttp', false ) || $request->getBool( 'wpFromhttp', false ); $this->mStickHTTPS = ( !$this->mFromHTTP && $request->getProtocol() === 'https' ) || $request->getBool( 'wpForceHttps', false ); $this->mLanguage = $request->getText( 'uselang' ); $this->mReturnTo = $request->getVal( 'returnto', '' ); $this->mReturnToQuery = $request->getVal( 'returntoquery', '' ); } /** * Load data from request. * @private * @param string $subPage Subpage of Special:Userlogin */ protected function load( $subPage ) { global $wgSecureLogin; $this->loadRequestParameters(); if ( $this->mLoaded ) { return; } $this->mLoaded = true; $request = $this->getRequest(); $securityLevel = $this->getRequest()->getText( 'force' ); if ( $securityLevel && AuthManager::singleton()->securitySensitiveOperationStatus( $securityLevel ) === AuthManager::SEC_REAUTH ) { $this->securityLevel = $securityLevel; } $this->loadAuth( $subPage ); $this->mToken = $request->getVal( $this->getTokenName() ); // Show an error or warning passed on from a previous page $entryError = $this->msg( $request->getVal( 'error', '' ) ); $entryWarning = $this->msg( $request->getVal( 'warning', '' ) ); // bc: provide login link as a parameter for messages where the translation // was not updated $loginreqlink = $this->getLinkRenderer()->makeKnownLink( $this->getPageTitle(), $this->msg( 'loginreqlink' )->text(), [], [ 'returnto' => $this->mReturnTo, 'returntoquery' => $this->mReturnToQuery, 'uselang' => $this->mLanguage ?: null, 'fromhttp' => $wgSecureLogin && $this->mFromHTTP ? '1' : null, ] ); // Only show valid error or warning messages. if ( $entryError->exists() && in_array( $entryError->getKey(), LoginHelper::getValidErrorMessages(), true ) ) { $this->mEntryErrorType = 'error'; $this->mEntryError = $entryError->rawParams( $loginreqlink )->parse(); } elseif ( $entryWarning->exists() && in_array( $entryWarning->getKey(), LoginHelper::getValidErrorMessages(), true ) ) { $this->mEntryErrorType = 'warning'; $this->mEntryError = $entryWarning->rawParams( $loginreqlink )->parse(); } # 1. When switching accounts, it sucks to get automatically logged out # 2. Do not return to PasswordReset after a successful password change # but goto Wiki start page (Main_Page) instead ( T35997 ) $returnToTitle = Title::newFromText( $this->mReturnTo ); if ( is_object( $returnToTitle ) && ( $returnToTitle->isSpecial( 'Userlogout' ) || $returnToTitle->isSpecial( 'PasswordReset' ) ) ) { $this->mReturnTo = ''; $this->mReturnToQuery = ''; } } protected function getPreservedParams( $withToken = false ) { global $wgSecureLogin; $params = parent::getPreservedParams( $withToken ); $params += [ 'returnto' => $this->mReturnTo ?: null, 'returntoquery' => $this->mReturnToQuery ?: null, ]; if ( $wgSecureLogin && !$this->isSignup() ) { $params['fromhttp'] = $this->mFromHTTP ? '1' : null; } return $params; } protected function beforeExecute( $subPage ) { // finish initializing the class before processing the request - T135924 $this->loadRequestParameters(); return parent::beforeExecute( $subPage ); } /** * @param string|null $subPage * @suppress PhanTypeObjectUnsetDeclaredProperty */ public function execute( $subPage ) { if ( $this->mPosted ) { $time = microtime( true ); $profilingScope = new ScopedCallback( function () use ( $time ) { $time = microtime( true ) - $time; $statsd = MediaWikiServices::getInstance()->getStatsdDataFactory(); $statsd->timing( "timing.login.ui.{$this->authAction}", $time * 1000 ); } ); } $authManager = AuthManager::singleton(); $session = SessionManager::getGlobalSession(); // Session data is used for various things in the authentication process, so we must make // sure a session cookie or some equivalent mechanism is set. $session->persist(); $this->load( $subPage ); $this->setHeaders(); $this->checkPermissions(); // Make sure the system configuration allows log in / sign up if ( !$this->isSignup() && !$authManager->canAuthenticateNow() ) { if ( !$session->canSetUser() ) { throw new ErrorPageError( 'cannotloginnow-title', 'cannotloginnow-text', [ $session->getProvider()->describe( RequestContext::getMain()->getLanguage() ) ] ); } throw new ErrorPageError( 'cannotlogin-title', 'cannotlogin-text' ); } elseif ( $this->isSignup() && !$authManager->canCreateAccounts() ) { throw new ErrorPageError( 'cannotcreateaccount-title', 'cannotcreateaccount-text' ); } /* * In the case where the user is already logged in, and was redirected to * the login form from a page that requires login, do not show the login * page. The use case scenario for this is when a user opens a large number * of tabs, is redirected to the login page on all of them, and then logs * in on one, expecting all the others to work properly. * * However, do show the form if it was visited intentionally (no 'returnto' * is present). People who often switch between several accounts have grown * accustomed to this behavior. * * Also make an exception when force= is set in the URL, which means the user must * reauthenticate for security reasons. */ if ( !$this->isSignup() && !$this->mPosted && !$this->securityLevel && ( $this->mReturnTo !== '' || $this->mReturnToQuery !== '' ) && $this->getUser()->isLoggedIn() ) { $this->successfulAction(); return; } // If logging in and not on HTTPS, either redirect to it or offer a link. global $wgSecureLogin; if ( $this->getRequest()->getProtocol() !== 'https' ) { $title = $this->getFullTitle(); $query = $this->getPreservedParams( false ) + [ 'title' => null, ( $this->mEntryErrorType === 'error' ? 'error' : 'warning' ) => $this->mEntryError, ] + $this->getRequest()->getQueryValues(); $url = $title->getFullURL( $query, false, PROTO_HTTPS ); if ( $wgSecureLogin && !$this->mFromHTTP && wfCanIPUseHTTPS( $this->getRequest()->getIP() ) ) { // Avoid infinite redirect $url = wfAppendQuery( $url, 'fromhttp=1' ); $this->getOutput()->redirect( $url ); // Since we only do this redir to change proto, always vary $this->getOutput()->addVaryHeader( 'X-Forwarded-Proto' ); return; } else { // A wiki without HTTPS login support should set $wgServer to // http://somehost, in which case the secure URL generated // above won't actually start with https:// if ( substr( $url, 0, 8 ) === 'https://' ) { $this->mSecureLoginUrl = $url; } } } if ( !$this->isActionAllowed( $this->authAction ) ) { // FIXME how do we explain this to the user? can we handle session loss better? // messages used: authpage-cannot-login, authpage-cannot-login-continue, // authpage-cannot-create, authpage-cannot-create-continue $this->mainLoginForm( [], 'authpage-cannot-' . $this->authAction ); return; } if ( $this->canBypassForm( $button_name ) ) { $this->setRequest( [], true ); $this->getRequest()->setVal( $this->getTokenName(), $this->getToken() ); if ( $button_name ) { $this->getRequest()->setVal( $button_name, true ); } } $status = $this->trySubmit(); if ( !$status || !$status->isGood() ) { $this->mainLoginForm( $this->authRequests, $status ? $status->getMessage() : '', 'error' ); return; } /** @var AuthenticationResponse $response */ $response = $status->getValue(); $returnToUrl = $this->getPageTitle( 'return' ) ->getFullURL( $this->getPreservedParams( true ), false, PROTO_HTTPS ); switch ( $response->status ) { case AuthenticationResponse::PASS: $this->logAuthResult( true ); $this->proxyAccountCreation = $this->isSignup() && !$this->getUser()->isAnon(); $this->targetUser = User::newFromName( $response->username ); if ( !$this->proxyAccountCreation && $response->loginRequest && $authManager->canAuthenticateNow() ) { // successful registration; log the user in instantly $response2 = $authManager->beginAuthentication( [ $response->loginRequest ], $returnToUrl ); if ( $response2->status !== AuthenticationResponse::PASS ) { LoggerFactory::getInstance( 'login' ) ->error( 'Could not log in after account creation' ); $this->successfulAction( true, Status::newFatal( 'createacct-loginerror' ) ); break; } } if ( !$this->proxyAccountCreation ) { // Ensure that the context user is the same as the session user. $this->setSessionUserForCurrentRequest(); } $this->successfulAction( true ); break; case AuthenticationResponse::FAIL: // fall through case AuthenticationResponse::RESTART: unset( $this->authForm ); if ( $response->status === AuthenticationResponse::FAIL ) { $action = $this->getDefaultAction( $subPage ); $messageType = 'error'; } else { $action = $this->getContinueAction( $this->authAction ); $messageType = 'warning'; } $this->logAuthResult( false, $response->message ? $response->message->getKey() : '-' ); $this->loadAuth( $subPage, $action, true ); $this->mainLoginForm( $this->authRequests, $response->message, $messageType ); break; case AuthenticationResponse::REDIRECT: unset( $this->authForm ); $this->getOutput()->redirect( $response->redirectTarget ); break; case AuthenticationResponse::UI: unset( $this->authForm ); $this->authAction = $this->isSignup() ? AuthManager::ACTION_CREATE_CONTINUE : AuthManager::ACTION_LOGIN_CONTINUE; $this->authRequests = $response->neededRequests; $this->mainLoginForm( $response->neededRequests, $response->message, $response->messageType ); break; default: throw new LogicException( 'invalid AuthenticationResponse' ); } } /** * Determine if the login form can be bypassed. This will be the case when no more than one * button is present and no other user input fields that are not marked as 'skippable' are * present. If the login form were not bypassed, the user would be presented with a * superfluous page on which they must press the single button to proceed with login. * Not only does this cause an additional mouse click and page load, it confuses users, * especially since there are a help link and forgotten password link that are * provided on the login page that do not apply to this situation. * * @param string|null &$button_name if the form has a single button, returns * the name of the button; otherwise, returns null * @return bool */ private function canBypassForm( &$button_name ) { $button_name = null; if ( $this->isContinued() ) { return false; } $fields = AuthenticationRequest::mergeFieldInfo( $this->authRequests ); foreach ( $fields as $fieldname => $field ) { if ( !isset( $field['type'] ) ) { return false; } if ( !empty( $field['skippable'] ) ) { continue; } if ( $field['type'] === 'button' ) { if ( $button_name !== null ) { $button_name = null; return false; } else { $button_name = $fieldname; } } elseif ( $field['type'] !== 'null' ) { return false; } } return true; } /** * Show the success page. * * @param string $type Condition of return to; see `executeReturnTo` * @param string|Message $title Page's title * @param string $msgname * @param string $injected_html * @param StatusValue|null $extraMessages */ protected function showSuccessPage( $type, $title, $msgname, $injected_html, $extraMessages ) { $out = $this->getOutput(); $out->setPageTitle( $title ); if ( $msgname ) { $out->addWikiMsg( $msgname, wfEscapeWikiText( $this->getUser()->getName() ) ); } if ( $extraMessages ) { $extraMessages = Status::wrap( $extraMessages ); $out->addWikiTextAsInterface( $extraMessages->getWikiText( false, false, $this->getLanguage() ) ); } $out->addHTML( $injected_html ); $helper = new LoginHelper( $this->getContext() ); $helper->showReturnToPage( $type, $this->mReturnTo, $this->mReturnToQuery, $this->mStickHTTPS ); } /** * Add a "return to" link or redirect to it. * Extensions can use this to reuse the "return to" logic after * inject steps (such as redirection) into the login process. * * @param string $type One of the following: * - error: display a return to link ignoring $wgRedirectOnLogin * - signup: display a return to link using $wgRedirectOnLogin if needed * - success: display a return to link using $wgRedirectOnLogin if needed * - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed * @param string $returnTo * @param array|string $returnToQuery * @param bool $stickHTTPS Keep redirect link on HTTPS * @since 1.22 */ public function showReturnToPage( $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false ) { $helper = new LoginHelper( $this->getContext() ); $helper->showReturnToPage( $type, $returnTo, $returnToQuery, $stickHTTPS ); } /** * Replace some globals to make sure the fact that the user has just been logged in is * reflected in the current request. */ protected function setSessionUserForCurrentRequest() { global $wgUser, $wgLang; $context = RequestContext::getMain(); $localContext = $this->getContext(); if ( $context !== $localContext ) { // remove AuthManagerSpecialPage context hack $this->setContext( $context ); } $user = $context->getRequest()->getSession()->getUser(); $wgUser = $user; $context->setUser( $user ); $wgLang = $context->getLanguage(); } /** * @param AuthenticationRequest[] $requests A list of AuthorizationRequest objects, * used to generate the form fields. An empty array means a fatal error * (authentication cannot continue). * @param string|Message $msg * @param string $msgtype * @throws ErrorPageError * @throws Exception * @throws FatalError * @throws MWException * @throws PermissionsError * @throws ReadOnlyError * @private */ protected function mainLoginForm( array $requests, $msg = '', $msgtype = 'error' ) { $user = $this->getUser(); $out = $this->getOutput(); // FIXME how to handle empty $requests - restart, or no form, just an error message? // no form would be better for no session type errors, restart is better when can* fails. if ( !$requests ) { $this->authAction = $this->getDefaultAction( $this->subPage ); $this->authForm = null; $requests = AuthManager::singleton()->getAuthenticationRequests( $this->authAction, $user ); } // Generic styles and scripts for both login and signup form $out->addModuleStyles( [ 'mediawiki.ui', 'mediawiki.ui.button', 'mediawiki.ui.checkbox', 'mediawiki.ui.input', 'mediawiki.special.userlogin.common.styles' ] ); if ( $this->isSignup() ) { // XXX hack pending RL or JS parse() support for complex content messages T27349 $out->addJsConfigVars( 'wgCreateacctImgcaptchaHelp', $this->msg( 'createacct-imgcaptcha-help' )->parse() ); // Additional styles and scripts for signup form $out->addModules( [ 'mediawiki.special.userlogin.signup.js' ] ); $out->addModuleStyles( [ 'mediawiki.special.userlogin.signup.styles' ] ); } else { // Additional styles for login form $out->addModuleStyles( [ 'mediawiki.special.userlogin.login.styles' ] ); } $out->disallowUserJs(); // just in case... $form = $this->getAuthForm( $requests, $this->authAction, $msg, $msgtype ); $form->prepareForm(); $submitStatus = Status::newGood(); if ( $msg && $msgtype === 'warning' ) { $submitStatus->warning( $msg ); } elseif ( $msg && $msgtype === 'error' ) { $submitStatus->fatal( $msg ); } // warning header for non-standard workflows (e.g. security reauthentication) if ( !$this->isSignup() && $this->getUser()->isLoggedIn() && $this->authAction !== AuthManager::ACTION_LOGIN_CONTINUE ) { $reauthMessage = $this->securityLevel ? 'userlogin-reauth' : 'userlogin-loggedin'; $submitStatus->warning( $reauthMessage, $this->getUser()->getName() ); } $formHtml = $form->getHTML( $submitStatus ); $out->addHTML( $this->getPageHtml( $formHtml ) ); } /** * Add page elements which are outside the form. * FIXME this should probably be a template, but use a sane language (handlebars?) * @param string $formHtml * @return string */ protected function getPageHtml( $formHtml ) { global $wgLoginLanguageSelector; $loginPrompt = $this->isSignup() ? '' : Html::rawElement( 'div', [ 'id' => 'userloginprompt' ], $this->msg( 'loginprompt' )->parseAsBlock() ); $languageLinks = $wgLoginLanguageSelector ? $this->makeLanguageSelector() : ''; $signupStartMsg = $this->msg( 'signupstart' ); $signupStart = ( $this->isSignup() && !$signupStartMsg->isDisabled() ) ? Html::rawElement( 'div', [ 'id' => 'signupstart' ], $signupStartMsg->parseAsBlock() ) : ''; if ( $languageLinks ) { $languageLinks = Html::rawElement( 'div', [ 'id' => 'languagelinks' ], Html::rawElement( 'p', [], $languageLinks ) ); } $benefitsContainer = ''; if ( $this->isSignup() && $this->showExtraInformation() ) { // messages used: // createacct-benefit-icon1 createacct-benefit-head1 createacct-benefit-body1 // createacct-benefit-icon2 createacct-benefit-head2 createacct-benefit-body2 // createacct-benefit-icon3 createacct-benefit-head3 createacct-benefit-body3 $benefitCount = 3; $benefitList = ''; for ( $benefitIdx = 1; $benefitIdx <= $benefitCount; $benefitIdx++ ) { $headUnescaped = $this->msg( "createacct-benefit-head$benefitIdx" )->text(); $iconClass = $this->msg( "createacct-benefit-icon$benefitIdx" )->text(); $benefitList .= Html::rawElement( 'div', [ 'class' => "mw-number-text $iconClass" ], Html::rawElement( 'h3', [], $this->msg( "createacct-benefit-head$benefitIdx" )->escaped() ) . Html::rawElement( 'p', [], $this->msg( "createacct-benefit-body$benefitIdx" )->params( $headUnescaped )->escaped() ) ); } $benefitsContainer = Html::rawElement( 'div', [ 'class' => 'mw-createacct-benefits-container' ], Html::rawElement( 'h2', [], $this->msg( 'createacct-benefit-heading' )->escaped() ) . Html::rawElement( 'div', [ 'class' => 'mw-createacct-benefits-list' ], $benefitList ) ); } $html = Html::rawElement( 'div', [ 'class' => 'mw-ui-container' ], $loginPrompt . $languageLinks . $signupStart . Html::rawElement( 'div', [ 'id' => 'userloginForm' ], $formHtml ) . $benefitsContainer ); return $html; } /** * Generates a form from the given request. * @param AuthenticationRequest[] $requests * @param string $action AuthManager action name * @param string|Message $msg * @param string $msgType * @return HTMLForm */ protected function getAuthForm( array $requests, $action, $msg = '', $msgType = 'error' ) { global $wgSecureLogin; // FIXME merge this with parent if ( isset( $this->authForm ) ) { return $this->authForm; } $usingHTTPS = $this->getRequest()->getProtocol() === 'https'; // get basic form description from the auth logic $fieldInfo = AuthenticationRequest::mergeFieldInfo( $requests ); // this will call onAuthChangeFormFields() $formDescriptor = static::fieldInfoToFormDescriptor( $requests, $fieldInfo, $this->authAction ); $this->postProcessFormDescriptor( $formDescriptor, $requests ); $context = $this->getContext(); if ( $context->getRequest() !== $this->getRequest() ) { // We have overridden the request, need to make sure the form uses that too. $context = new DerivativeContext( $this->getContext() ); $context->setRequest( $this->getRequest() ); } $form = HTMLForm::factory( 'vform', $formDescriptor, $context ); $form->addHiddenField( 'authAction', $this->authAction ); if ( $this->mLanguage ) { $form->addHiddenField( 'uselang', $this->mLanguage ); } $form->addHiddenField( 'force', $this->securityLevel ); $form->addHiddenField( $this->getTokenName(), $this->getToken()->toString() ); if ( $wgSecureLogin ) { // If using HTTPS coming from HTTP, then the 'fromhttp' parameter must be preserved if ( !$this->isSignup() ) { $form->addHiddenField( 'wpForceHttps', (int)$this->mStickHTTPS ); $form->addHiddenField( 'wpFromhttp', $usingHTTPS ); } } // set properties of the form itself $form->setAction( $this->getPageTitle()->getLocalURL( $this->getReturnToQueryStringFragment() ) ); $form->setName( 'userlogin' . ( $this->isSignup() ? '2' : '' ) ); if ( $this->isSignup() ) { $form->setId( 'userlogin2' ); } $form->suppressDefaultSubmit(); $this->authForm = $form; return $form; } public function onAuthChangeFormFields( array $requests, array $fieldInfo, array &$formDescriptor, $action ) { $coreFieldDescriptors = $this->getFieldDefinitions(); // keep the ordering from getCoreFieldDescriptors() where there is no explicit weight foreach ( $coreFieldDescriptors as $fieldName => $coreField ) { $requestField = $formDescriptor[$fieldName] ?? []; // remove everything that is not in the fieldinfo, is not marked as a supplemental field // to something in the fieldinfo, and is not an info field or a submit button if ( !isset( $fieldInfo[$fieldName] ) && ( !isset( $coreField['baseField'] ) || !isset( $fieldInfo[$coreField['baseField']] ) ) && ( !isset( $coreField['type'] ) || !in_array( $coreField['type'], [ 'submit', 'info' ], true ) ) ) { $coreFieldDescriptors[$fieldName] = null; continue; } // core message labels should always take priority if ( isset( $coreField['label'] ) || isset( $coreField['label-message'] ) || isset( $coreField['label-raw'] ) ) { unset( $requestField['label'], $requestField['label-message'], $coreField['label-raw'] ); } $coreFieldDescriptors[$fieldName] += $requestField; } $formDescriptor = array_filter( $coreFieldDescriptors + $formDescriptor ); return true; } /** * Show extra information such as password recovery information, link from login to signup, * CTA etc? Such information should only be shown on the "landing page", ie. when the user * is at the first step of the authentication process. * @return bool */ protected function showExtraInformation() { return $this->authAction !== $this->getContinueAction( $this->authAction ) && !$this->securityLevel; } /** * Create a HTMLForm descriptor for the core login fields. * @return array */ protected function getFieldDefinitions() { global $wgEmailConfirmToEdit; $isLoggedIn = $this->getUser()->isLoggedIn(); $continuePart = $this->isContinued() ? 'continue-' : ''; $anotherPart = $isLoggedIn ? 'another-' : ''; // @phan-suppress-next-line PhanUndeclaredMethod $expiration = $this->getRequest()->getSession()->getProvider()->getRememberUserDuration(); $expirationDays = ceil( $expiration / ( 3600 * 24 ) ); $secureLoginLink = ''; if ( $this->mSecureLoginUrl ) { $secureLoginLink = Html::element( 'a', [ 'href' => $this->mSecureLoginUrl, 'class' => 'mw-ui-flush-right mw-secure', ], $this->msg( 'userlogin-signwithsecure' )->text() ); } $usernameHelpLink = ''; if ( !$this->msg( 'createacct-helpusername' )->isDisabled() ) { $usernameHelpLink = Html::rawElement( 'span', [ 'class' => 'mw-ui-flush-right', ], $this->msg( 'createacct-helpusername' )->parse() ); } if ( $this->isSignup() ) { $fieldDefinitions = [ 'statusarea' => [ // used by the mediawiki.special.userlogin.signup.js module for error display // FIXME merge this with HTMLForm's normal status (error) area 'type' => 'info', 'raw' => true, 'default' => Html::element( 'div', [ 'id' => 'mw-createacct-status-area' ] ), 'weight' => -105, ], 'username' => [ 'label-raw' => $this->msg( 'userlogin-yourname' )->escaped() . $usernameHelpLink, 'id' => 'wpName2', 'placeholder-message' => $isLoggedIn ? 'createacct-another-username-ph' : 'userlogin-yourname-ph', ], 'mailpassword' => [ // create account without providing password, a temporary one will be mailed 'type' => 'check', 'label-message' => 'createaccountmail', 'name' => 'wpCreateaccountMail', 'id' => 'wpCreateaccountMail', ], 'password' => [ 'id' => 'wpPassword2', 'placeholder-message' => 'createacct-yourpassword-ph', 'hide-if' => [ '===', 'wpCreateaccountMail', '1' ], ], 'domain' => [], 'retype' => [ 'baseField' => 'password', 'type' => 'password', 'label-message' => 'createacct-yourpasswordagain', 'id' => 'wpRetype', 'cssclass' => 'loginPassword', 'size' => 20, 'validation-callback' => function ( $value, $alldata ) { if ( empty( $alldata['mailpassword'] ) && !empty( $alldata['password'] ) ) { if ( !$value ) { return $this->msg( 'htmlform-required' ); } elseif ( $value !== $alldata['password'] ) { return $this->msg( 'badretype' ); } } return true; }, 'hide-if' => [ '===', 'wpCreateaccountMail', '1' ], 'placeholder-message' => 'createacct-yourpasswordagain-ph', ], 'email' => [ 'type' => 'email', 'label-message' => $wgEmailConfirmToEdit ? 'createacct-emailrequired' : 'createacct-emailoptional', 'id' => 'wpEmail', 'cssclass' => 'loginText', 'size' => '20', // FIXME will break non-standard providers 'required' => $wgEmailConfirmToEdit, 'validation-callback' => function ( $value, $alldata ) { global $wgEmailConfirmToEdit; // AuthManager will check most of these, but that will make the auth // session fail and this won't, so nicer to do it this way if ( !$value && $wgEmailConfirmToEdit ) { // no point in allowing registration without email when email is // required to edit return $this->msg( 'noemailtitle' ); } elseif ( !$value && !empty( $alldata['mailpassword'] ) ) { // cannot send password via email when there is no email address return $this->msg( 'noemailcreate' ); } elseif ( $value && !Sanitizer::validateEmail( $value ) ) { return $this->msg( 'invalidemailaddress' ); } return true; }, 'placeholder-message' => 'createacct-' . $anotherPart . 'email-ph', ], 'realname' => [ 'type' => 'text', 'help-message' => $isLoggedIn ? 'createacct-another-realname-tip' : 'prefs-help-realname', 'label-message' => 'createacct-realname', 'cssclass' => 'loginText', 'size' => 20, 'id' => 'wpRealName', ], 'reason' => [ // comment for the user creation log 'type' => 'text', 'label-message' => 'createacct-reason', 'cssclass' => 'loginText', 'id' => 'wpReason', 'size' => '20', 'placeholder-message' => 'createacct-reason-ph', ], 'createaccount' => [ // submit button 'type' => 'submit', 'default' => $this->msg( 'createacct-' . $anotherPart . $continuePart . 'submit' )->text(), 'name' => 'wpCreateaccount', 'id' => 'wpCreateaccount', 'weight' => 100, ], ]; } else { $fieldDefinitions = [ 'username' => [ 'label-raw' => $this->msg( 'userlogin-yourname' )->escaped() . $secureLoginLink, 'id' => 'wpName1', 'placeholder-message' => 'userlogin-yourname-ph', ], 'password' => [ 'id' => 'wpPassword1', 'placeholder-message' => 'userlogin-yourpassword-ph', ], 'domain' => [], 'rememberMe' => [ // option for saving the user token to a cookie 'type' => 'check', 'name' => 'wpRemember', 'label-message' => $this->msg( 'userlogin-remembermypassword' ) ->numParams( $expirationDays ), 'id' => 'wpRemember', ], 'loginattempt' => [ // submit button 'type' => 'submit', 'default' => $this->msg( 'pt-login-' . $continuePart . 'button' )->text(), 'id' => 'wpLoginAttempt', 'weight' => 100, ], 'linkcontainer' => [ // help link 'type' => 'info', 'cssclass' => 'mw-form-related-link-container mw-userlogin-help', // 'id' => 'mw-userlogin-help', // FIXME HTMLInfoField ignores this 'raw' => true, 'default' => Html::element( 'a', [ 'href' => Skin::makeInternalOrExternalUrl( $this->msg( 'helplogin-url' ) ->inContentLanguage() ->text() ), ], $this->msg( 'userlogin-helplink2' )->text() ), 'weight' => 200, ], // button for ResetPasswordSecondaryAuthenticationProvider 'skipReset' => [ 'weight' => 110, 'flags' => [], ], ]; } $fieldDefinitions['username'] += [ 'type' => 'text', 'name' => 'wpName', 'cssclass' => 'loginText', 'size' => 20, // 'required' => true, ]; $fieldDefinitions['password'] += [ 'type' => 'password', // 'label-message' => 'userlogin-yourpassword', // would override the changepassword label 'name' => 'wpPassword', 'cssclass' => 'loginPassword', 'size' => 20, // 'required' => true, ]; if ( $this->mEntryError ) { $fieldDefinitions['entryError'] = [ 'type' => 'info', 'default' => Html::rawElement( 'div', [ 'class' => $this->mEntryErrorType . 'box', ], $this->mEntryError ), 'raw' => true, 'rawrow' => true, 'weight' => -100, ]; } if ( !$this->showExtraInformation() ) { unset( $fieldDefinitions['linkcontainer'], $fieldDefinitions['signupend'] ); } if ( $this->isSignup() && $this->showExtraInformation() ) { // blank signup footer for site customization // uses signupend-https for HTTPS requests if it's not blank, signupend otherwise $signupendMsg = $this->msg( 'signupend' ); $signupendHttpsMsg = $this->msg( 'signupend-https' ); if ( !$signupendMsg->isDisabled() ) { $usingHTTPS = $this->getRequest()->getProtocol() === 'https'; $signupendText = ( $usingHTTPS && !$signupendHttpsMsg->isBlank() ) ? $signupendHttpsMsg->parse() : $signupendMsg->parse(); $fieldDefinitions['signupend'] = [ 'type' => 'info', 'raw' => true, 'default' => Html::rawElement( 'div', [ 'id' => 'signupend' ], $signupendText ), 'weight' => 225, ]; } } if ( !$this->isSignup() && $this->showExtraInformation() ) { $passwordReset = MediaWikiServices::getInstance()->getPasswordReset(); if ( $passwordReset->isAllowed( $this->getUser() )->isGood() ) { $fieldDefinitions['passwordReset'] = [ 'type' => 'info', 'raw' => true, 'cssclass' => 'mw-form-related-link-container', 'default' => $this->getLinkRenderer()->makeLink( SpecialPage::getTitleFor( 'PasswordReset' ), $this->msg( 'userlogin-resetpassword-link' )->text() ), 'weight' => 230, ]; } // Don't show a "create account" link if the user can't. if ( $this->showCreateAccountLink() ) { // link to the other action $linkTitle = $this->getTitleFor( $this->isSignup() ? 'Userlogin' : 'CreateAccount' ); $linkq = $this->getReturnToQueryStringFragment(); // Pass any language selection on to the mode switch link if ( $this->mLanguage ) { $linkq .= '&uselang=' . urlencode( $this->mLanguage ); } $loggedIn = $this->getUser()->isLoggedIn(); $fieldDefinitions['createOrLogin'] = [ 'type' => 'info', 'raw' => true, 'linkQuery' => $linkq, 'default' => function ( $params ) use ( $loggedIn, $linkTitle ) { return Html::rawElement( 'div', [ 'id' => 'mw-createaccount' . ( !$loggedIn ? '-cta' : '' ), 'class' => ( $loggedIn ? 'mw-form-related-link-container' : 'mw-ui-vform-field' ) ], ( $loggedIn ? '' : $this->msg( 'userlogin-noaccount' )->escaped() ) . Html::element( 'a', [ 'id' => 'mw-createaccount-join' . ( $loggedIn ? '-loggedin' : '' ), 'href' => $linkTitle->getLocalURL( $params['linkQuery'] ), 'class' => ( $loggedIn ? '' : 'mw-ui-button' ), 'tabindex' => 100, ], $this->msg( $loggedIn ? 'userlogin-createanother' : 'userlogin-joinproject' )->text() ) ); }, 'weight' => 235, ]; } } return $fieldDefinitions; } /** * Check if a session cookie is present. * * This will not pick up a cookie set during _this_ request, but is meant * to ensure that the client is returning the cookie which was set on a * previous pass through the system. * * @return bool */ protected function hasSessionCookie() { global $wgDisableCookieCheck, $wgInitialSessionId; return $wgDisableCookieCheck || ( $wgInitialSessionId && $this->getRequest()->getSession()->getId() === (string)$wgInitialSessionId ); } /** * Returns a string that can be appended to the URL (without encoding) to preserve the * return target. Does not include leading '?'/'&'. * @return string */ protected function getReturnToQueryStringFragment() { $returnto = ''; if ( $this->mReturnTo !== '' ) { $returnto = 'returnto=' . wfUrlencode( $this->mReturnTo ); if ( $this->mReturnToQuery !== '' ) { $returnto .= '&returntoquery=' . wfUrlencode( $this->mReturnToQuery ); } } return $returnto; } /** * Whether the login/create account form should display a link to the * other form (in addition to whatever the skin provides). * @return bool */ private function showCreateAccountLink() { if ( $this->isSignup() ) { return true; } elseif ( MediaWikiServices::getInstance() ->getPermissionManager() ->userHasRight( $this->getUser(), 'createaccount' ) ) { return true; } else { return false; } } protected function getTokenName() { return $this->isSignup() ? 'wpCreateaccountToken' : 'wpLoginToken'; } /** * Produce a bar of links which allow the user to select another language * during login/registration but retain "returnto" * * @return string */ protected function makeLanguageSelector() { $msg = $this->msg( 'loginlanguagelinks' )->inContentLanguage(); if ( $msg->isBlank() ) { return ''; } $langs = explode( "\n", $msg->text() ); $links = []; foreach ( $langs as $lang ) { $lang = trim( $lang, '* ' ); $parts = explode( '|', $lang ); if ( count( $parts ) >= 2 ) { $links[] = $this->makeLanguageSelectorLink( $parts[0], trim( $parts[1] ) ); } } return count( $links ) > 0 ? $this->msg( 'loginlanguagelabel' )->rawParams( $this->getLanguage()->pipeList( $links ) )->escaped() : ''; } /** * Create a language selector link for a particular language * Links back to this page preserving type and returnto * * @param string $text Link text * @param string $lang Language code * @return string */ protected function makeLanguageSelectorLink( $text, $lang ) { if ( $this->getLanguage()->getCode() == $lang ) { // no link for currently used language return htmlspecialchars( $text ); } $query = [ 'uselang' => $lang ]; if ( $this->mReturnTo !== '' ) { $query['returnto'] = $this->mReturnTo; $query['returntoquery'] = $this->mReturnToQuery; } $attr = []; $targetLanguage = Language::factory( $lang ); $attr['lang'] = $attr['hreflang'] = $targetLanguage->getHtmlCode(); return $this->getLinkRenderer()->makeKnownLink( $this->getPageTitle(), $text, $attr, $query ); } protected function getGroupName() { return 'login'; } /** * @param array &$formDescriptor * @param array $requests */ protected function postProcessFormDescriptor( &$formDescriptor, $requests ) { // Pre-fill username (if not creating an account, T46775). if ( isset( $formDescriptor['username'] ) && !isset( $formDescriptor['username']['default'] ) && !$this->isSignup() ) { $user = $this->getUser(); if ( $user->isLoggedIn() ) { $formDescriptor['username']['default'] = $user->getName(); } else { $formDescriptor['username']['default'] = $this->getRequest()->getSession()->suggestLoginUsername(); } } // don't show a submit button if there is nothing to submit (i.e. the only form content // is other submit buttons, for redirect flows) if ( !$this->needsSubmitButton( $requests ) ) { unset( $formDescriptor['createaccount'], $formDescriptor['loginattempt'] ); } if ( !$this->isSignup() ) { // FIXME HACK don't focus on non-empty field // maybe there should be an autofocus-if similar to hide-if? if ( isset( $formDescriptor['username'] ) && empty( $formDescriptor['username']['default'] ) && !$this->getRequest()->getCheck( 'wpName' ) ) { $formDescriptor['username']['autofocus'] = true; } elseif ( isset( $formDescriptor['password'] ) ) { $formDescriptor['password']['autofocus'] = true; } } $this->addTabIndex( $formDescriptor ); } }