Merge "SpecialMediaStatistics: Add <thead> and <tbody> in the table"
[lhc/web/wiklou.git] / includes / specials / helpers / LoginHelper.php
1 <?php
2
3 /**
4 * Helper functions for the login form that need to be shared with other special pages
5 * (such as CentralAuth's SpecialCentralLogin).
6 * @since 1.27
7 */
8 class LoginHelper extends ContextSource {
9 /**
10 * Valid error and warning messages
11 *
12 * Special:Userlogin can show an error or warning message on the form when
13 * coming from another page. This is done via the ?error= or ?warning= GET
14 * parameters.
15 *
16 * This array is the list of valid message keys. Further keys can be added by the
17 * LoginFormValidErrorMessages hook. All other values will be ignored.
18 *
19 * @var string[]
20 */
21 public static $validErrorMessages = [
22 'exception-nologin-text',
23 'watchlistanontext',
24 'changeemail-no-info',
25 'resetpass-no-info',
26 'confirmemail_needlogin',
27 'prefsnologintext2',
28 ];
29
30 /**
31 * Returns an array of all valid error messages.
32 *
33 * @return array
34 * @see LoginHelper::$validErrorMessages
35 */
36 public static function getValidErrorMessages() {
37 static $messages = null;
38 if ( !$messages ) {
39 $messages = self::$validErrorMessages;
40 Hooks::run( 'LoginFormValidErrorMessages', [ &$messages ] );
41 }
42
43 return $messages;
44 }
45
46 public function __construct( IContextSource $context ) {
47 $this->setContext( $context );
48 }
49
50 /**
51 * Show a return link or redirect to it.
52 * Extensions can change where the link should point or inject content into the page
53 * (which will change it from redirect to link mode).
54 *
55 * @param string $type One of the following:
56 * - error: display a return to link ignoring $wgRedirectOnLogin
57 * - success: display a return to link using $wgRedirectOnLogin if needed
58 * - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed
59 * @param string $returnTo
60 * @param array|string $returnToQuery
61 * @param bool $stickHTTPS Keep redirect link on HTTPS
62 */
63 public function showReturnToPage(
64 $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false
65 ) {
66 $config = $this->getConfig();
67 if ( $type !== 'error' && $config->get( 'RedirectOnLogin' ) !== null ) {
68 $returnTo = $config->get( 'RedirectOnLogin' );
69 $returnToQuery = [];
70 } elseif ( is_string( $returnToQuery ) ) {
71 $returnToQuery = wfCgiToArray( $returnToQuery );
72 }
73
74 // Allow modification of redirect behavior
75 Hooks::run( 'PostLoginRedirect', [ &$returnTo, &$returnToQuery, &$type ] );
76
77 $returnToTitle = Title::newFromText( $returnTo ) ?: Title::newMainPage();
78
79 if ( $config->get( 'SecureLogin' ) && !$stickHTTPS ) {
80 $options = [ 'http' ];
81 $proto = PROTO_HTTP;
82 } elseif ( $config->get( 'SecureLogin' ) ) {
83 $options = [ 'https' ];
84 $proto = PROTO_HTTPS;
85 } else {
86 $options = [];
87 $proto = PROTO_RELATIVE;
88 }
89
90 if ( $type === 'successredirect' ) {
91 $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto );
92 $this->getOutput()->redirect( $redirectUrl );
93 } else {
94 $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );
95 }
96 }
97 }