MediaWiki  master
LoginHelper.php
Go to the documentation of this file.
1 <?php
2 
3 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
5 
11 class LoginHelper extends ContextSource {
12  use ProtectedHookAccessorTrait;
13 
26  public static $validErrorMessages = [
27  'exception-nologin-text',
28  'watchlistanontext',
29  'changeemail-no-info',
30  'resetpass-no-info',
31  'confirmemail_needlogin',
32  'prefsnologintext2',
33  'specialmute-login-required',
34  ];
35 
42  public static function getValidErrorMessages() {
43  static $messages = null;
44  if ( !$messages ) {
45  $messages = self::$validErrorMessages;
46  Hooks::runner()->onLoginFormValidErrorMessages( $messages );
47  }
48 
49  return $messages;
50  }
51 
52  public function __construct( IContextSource $context ) {
53  $this->setContext( $context );
54  }
55 
72  public function showReturnToPage(
73  $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false, $returnToAnchor = ''
74  ) {
75  $config = $this->getConfig();
76  if ( $type !== 'error' && $config->get( MainConfigNames::RedirectOnLogin ) !== null ) {
77  $returnTo = $config->get( MainConfigNames::RedirectOnLogin );
78  $returnToQuery = [];
79  } elseif ( is_string( $returnToQuery ) ) {
80  $returnToQuery = wfCgiToArray( $returnToQuery );
81  }
82 
83  // Allow modification of redirect behavior
84  $this->getHookRunner()->onPostLoginRedirect( $returnTo, $returnToQuery, $type );
85 
86  $returnToTitle = Title::newFromText( $returnTo ) ?: Title::newMainPage();
87 
88  if ( $config->get( MainConfigNames::ForceHTTPS )
89  || ( $config->get( MainConfigNames::SecureLogin ) && $stickHTTPS )
90  ) {
91  $options = [ 'https' ];
92  $proto = PROTO_HTTPS;
93  } elseif ( $config->get( MainConfigNames::SecureLogin ) && !$stickHTTPS ) {
94  $options = [ 'http' ];
95  $proto = PROTO_HTTP;
96  } else {
97  $options = [];
98  $proto = PROTO_RELATIVE;
99  }
100 
101  if ( $type === 'successredirect' ) {
102  $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto )
103  . $returnToAnchor;
104  $this->getOutput()->redirect( $redirectUrl );
105  } else {
106  $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );
107  }
108  }
109 }
const PROTO_HTTPS
Definition: Defines.php:193
const PROTO_HTTP
Definition: Defines.php:192
const PROTO_RELATIVE
Definition: Defines.php:194
wfCgiToArray( $query)
This is the logical opposite of wfArrayToCgi(): it accepts a query string as its argument and returns...
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
IContextSource $context
setContext(IContextSource $context)
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
Helper functions for the login form that need to be shared with other special pages (such as CentralA...
Definition: LoginHelper.php:11
static getValidErrorMessages()
Returns an array of all valid error messages.
Definition: LoginHelper.php:42
__construct(IContextSource $context)
Definition: LoginHelper.php:52
showReturnToPage( $type, $returnTo='', $returnToQuery='', $stickHTTPS=false, $returnToAnchor='')
Show a return link or redirect to it.
Definition: LoginHelper.php:72
static string[] $validErrorMessages
Valid error and warning messages.
Definition: LoginHelper.php:26
A class containing constants representing the names of configuration variables.
static newMainPage(MessageLocalizer $localizer=null)
Create a new Title for the Main Page.
Definition: Title.php:700
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:370
Interface for objects which can provide a MediaWiki context on request.