Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoginHelper
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
210
0.00% covered (danger)
0.00%
0 / 1
 getValidErrorMessages
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 showReturnToPage
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
132
1<?php
2
3use MediaWiki\Context\ContextSource;
4use MediaWiki\Context\IContextSource;
5use MediaWiki\HookContainer\HookRunner;
6use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
7use MediaWiki\MainConfigNames;
8use MediaWiki\MediaWikiServices;
9use MediaWiki\Title\Title;
10
11/**
12 * Helper functions for the login form that need to be shared with other special pages
13 * (such as CentralAuth's SpecialCentralLogin).
14 * @since 1.27
15 */
16class LoginHelper extends ContextSource {
17    use ProtectedHookAccessorTrait;
18
19    /**
20     * Valid error and warning messages
21     *
22     * Special:Userlogin can show an error or warning message on the form when
23     * coming from another page. This is done via the ?error= or ?warning= GET
24     * parameters.
25     *
26     * This array is the list of valid message keys. Further keys can be added by the
27     * LoginFormValidErrorMessages hook. All other values will be ignored.
28     *
29     * @var string[]
30     */
31    public static $validErrorMessages = [
32        'exception-nologin-text',
33        'watchlistanontext',
34        'changeemail-no-info',
35        'resetpass-no-info',
36        'confirmemail_needlogin',
37        'prefsnologintext2',
38        'specialmute-login-required',
39    ];
40
41    /**
42     * Returns an array of all valid error messages.
43     *
44     * @return array
45     * @see LoginHelper::$validErrorMessages
46     */
47    public static function getValidErrorMessages() {
48        static $messages = null;
49        if ( !$messages ) {
50            $messages = self::$validErrorMessages;
51            ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
52                ->onLoginFormValidErrorMessages( $messages );
53        }
54
55        return $messages;
56    }
57
58    public function __construct( IContextSource $context ) {
59        $this->setContext( $context );
60    }
61
62    /**
63     * Show a return link or redirect to it.
64     * Extensions can change where the link should point or inject content into the page
65     * (which will change it from redirect to link mode).
66     *
67     * @param string $type One of the following:
68     *    - error: display a return to link ignoring $wgRedirectOnLogin
69     *    - success: display a return to link using $wgRedirectOnLogin if needed
70     *    - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed
71     *    - signup: used during signup, functionally identical to 'success'
72     * @param string $returnTo Title of page to return to. Overriden by $wgRedirectOnLogin
73     *   when that is set (and $type is not 'error').
74     * @param array|string $returnToQuery Query parameters to return to.
75     * @param bool $stickHTTPS Keep redirect link on HTTPS. Ignored (treated as
76     *   true) if $wgForceHTTPS is true.
77     * @param string $returnToAnchor A string to append to the URL, presumed to
78     *   be either a fragment including the leading hash or an empty string.
79     */
80    public function showReturnToPage(
81        $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false, $returnToAnchor = ''
82    ) {
83        $config = $this->getConfig();
84        if ( $type !== 'error' && $config->get( MainConfigNames::RedirectOnLogin ) !== null ) {
85            $returnTo = $config->get( MainConfigNames::RedirectOnLogin );
86            $returnToQuery = [];
87        } elseif ( is_string( $returnToQuery ) ) {
88            $returnToQuery = wfCgiToArray( $returnToQuery );
89        }
90
91        // Allow modification of redirect behavior
92        $this->getHookRunner()->onPostLoginRedirect( $returnTo, $returnToQuery, $type );
93
94        $returnToTitle = Title::newFromText( $returnTo ) ?: Title::newMainPage();
95
96        if ( $config->get( MainConfigNames::ForceHTTPS )
97            || ( $config->get( MainConfigNames::SecureLogin ) && $stickHTTPS )
98        ) {
99            $options = [ 'https' ];
100            $proto = PROTO_HTTPS;
101        } elseif ( $config->get( MainConfigNames::SecureLogin ) && !$stickHTTPS ) {
102            $options = [ 'http' ];
103            $proto = PROTO_HTTP;
104        } else {
105            $options = [];
106            $proto = PROTO_RELATIVE;
107        }
108
109        if ( $type === 'successredirect' ) {
110            $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto )
111                . $returnToAnchor;
112            $this->getOutput()->redirect( $redirectUrl );
113        } else {
114            $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );
115        }
116    }
117}