Debian Codegraph @

includes/specials/helpers/LoginHelper.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Context\ContextSource;
4 use MediaWiki\Context\IContextSource;
5 use MediaWiki\HookContainer\HookRunner;
6 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
7 use MediaWiki\MainConfigNames;
8 use MediaWiki\MediaWikiServices;
9 use 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 */
16 class 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 'exception-nologin-text-for-temp-user',
34 'watchlistanontext',
35 'watchlistanontext-for-temp-user',
36 'changeemail-no-info',
37 'confirmemail_needlogin',
38 'prefsnologintext2',
39 'prefsnologintext2-for-temp-user',
40 'specialmute-login-required',
41 'specialmute-login-required-for-temp-user',
42 ];
43
44 /**
45 * Returns an array of all valid error messages.
46 *
47 * @return array
48 * @see LoginHelper::$validErrorMessages
49 */
50 public static function getValidErrorMessages() {
51 static $messages = null;
52 if ( !$messages ) {
53 $messages = self::$validErrorMessages;
54 ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )
55 ->onLoginFormValidErrorMessages( $messages );
56 }
57
58 return $messages;
59 }
60
61 public function __construct( IContextSource $context ) {
62 $this->setContext( $context );
63 }
64
65 /**
66 * Show a return link or redirect to it.
67 * Extensions can change where the link should point or inject content into the page
68 * (which will change it from redirect to link mode).
69 *
70 * @param string $type One of the following:
71 * - error: display a return to link ignoring $wgRedirectOnLogin
72 * - success: display a return to link using $wgRedirectOnLogin if needed
73 * - successredirect: send an HTTP redirect using $wgRedirectOnLogin if needed
74 * - signup: used during signup, functionally identical to 'success'
75 * @param string $returnTo Title of page to return to. Overriden by $wgRedirectOnLogin
76 * when that is set (and $type is not 'error').
77 * @param array|string $returnToQuery Query parameters to return to.
78 * @param bool $stickHTTPS Keep redirect link on HTTPS. Ignored (treated as
79 * true) if $wgForceHTTPS is true.
80 * @param string $returnToAnchor A string to append to the URL, presumed to
81 * be either a fragment including the leading hash or an empty string.
82 */
83 public function showReturnToPage(
84 $type, $returnTo = '', $returnToQuery = '', $stickHTTPS = false, $returnToAnchor = ''
85 ) {
86 $config = $this->getConfig();
87 if ( $type !== 'error' && $config->get( MainConfigNames::RedirectOnLogin ) !== null ) {
88 $returnTo = $config->get( MainConfigNames::RedirectOnLogin );
89 $returnToQuery = [];
90 } elseif ( is_string( $returnToQuery ) ) {
91 $returnToQuery = wfCgiToArray( $returnToQuery );
92 }
93 if ( $returnToAnchor !== '' && $returnToAnchor[0] !== '#' ) {
94 $returnToAnchor = '';
95 }
96
97 // Allow modification of redirect behavior
98 $oldReturnTo = $returnTo;
99 $oldReturnToQuery = $returnToQuery;
100 $this->getHookRunner()->onPostLoginRedirect( $returnTo, $returnToQuery, $type );
101 if ( $returnTo !== $oldReturnTo || $returnToQuery !== $oldReturnToQuery ) {
102 // PostLoginRedirect does not handle $returnToAnchor, and changing hooks is hard.
103 // At least don't add the anchor if the hook changed the URL.
104 $returnToAnchor = '';
105 }
106
107 $returnToTitle = Title::newFromText( $returnTo ) ?: Title::newMainPage();
108
109 if ( $config->get( MainConfigNames::ForceHTTPS )
110 || ( $config->get( MainConfigNames::SecureLogin ) && $stickHTTPS )
111 ) {
112 $options = [ 'https' ];
113 $proto = PROTO_HTTPS;
114 } elseif ( $config->get( MainConfigNames::SecureLogin ) && !$stickHTTPS ) {
115 $options = [ 'http' ];
116 $proto = PROTO_HTTP;
117 } else {
118 $options = [];
119 $proto = PROTO_RELATIVE;
120 }
121
122 if ( $type === 'successredirect' ) {
123 $redirectUrl = $returnToTitle->getFullUrlForRedirect( $returnToQuery, $proto )
124 . $returnToAnchor;
125 $this->getOutput()->redirect( $redirectUrl );
126 } else {
127 $this->getOutput()->addReturnTo( $returnToTitle, $returnToQuery, null, $options );
128 }
129 }
130 }