Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageDisplayHookHandler
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 onBeforePageDisplay
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\CentralAuth\Hooks\Handlers;
22
23use CentralAuthTokenSessionProvider;
24use MediaWiki\Config\Config;
25use MediaWiki\Extension\CentralAuth\CentralAuthHooks;
26use MediaWiki\Extension\CentralAuth\Special\SpecialCentralAutoLogin;
27use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
28use MediaWiki\Hook\BeforePageDisplayHook;
29use MediaWiki\Html\Html;
30use MediaWiki\Logger\LoggerFactory;
31use MediaWiki\Output\OutputPage;
32use MediaWiki\ResourceLoader\Module;
33use MediaWiki\WikiMap\WikiMap;
34use Skin;
35
36class PageDisplayHookHandler implements
37    BeforePageDisplayHook
38{
39    private Config $config;
40
41    /**
42     * @param Config $config
43     */
44    public function __construct( Config $config ) {
45        $this->config = $config;
46    }
47
48    /**
49     * This does several things; the most important one is to trigger autologin if the user
50     * is not logged in.
51     *
52     * @param OutputPage $out
53     * @param Skin $skin
54     * @todo Add 1x1 images somewhere besides page content
55     *
56     * @see SpecialCentralAutoLogin
57     */
58    public function onBeforePageDisplay( $out, $skin ): void {
59        $logger = LoggerFactory::getInstance( 'CentralAuth' );
60
61        if ( $out->getRequest()->getSession()->getProvider()
62            instanceof CentralAuthTokenSessionProvider
63        ) {
64            // Prevent user scripts and styles when centralauthtoken is in use
65            $out->reduceAllowedModules(
66                Module::TYPE_SCRIPTS, Module::ORIGIN_USER_SITEWIDE
67            );
68            $out->reduceAllowedModules(
69                Module::TYPE_STYLES, Module::ORIGIN_USER_SITEWIDE
70            );
71        }
72
73        if ( !$out->getUser()->isRegistered() ) {
74            $wikiId = WikiMap::getCurrentWikiId();
75            $loginWikiId = $this->config->get( 'CentralAuthLoginWiki' );
76            if ( $loginWikiId && $wikiId !== $loginWikiId ) {
77                // Let the frontend know if this is a mobile domain, T100413
78                $out->addJsConfigVars(
79                    'wgCentralAuthMobileDomain',
80                    CentralAuthHooks::isMobileDomain()
81                );
82
83                $logger->debug( 'CentralAutoLogin triggered in BeforePageDisplay' );
84
85                $out->addModules( 'ext.centralauth.centralautologin' );
86
87                $wiki = WikiMap::getWiki( $wikiId );
88                $loginWiki = WikiMap::getWiki( $loginWikiId );
89                if ( $wiki->getCanonicalServer() !== $loginWiki->getCanonicalServer() ) {
90                    $out->addHeadItem( 'centralauth-dns-prefetch', Html::element( 'link', [
91                        'rel' => 'dns-prefetch',
92                        'href' => preg_replace( '/^https?:/', '', $loginWiki->getCanonicalServer() ),
93                    ] ) );
94                }
95
96                // For non-JS clients.
97                $params = [
98                    'type' => '1x1',
99                ];
100                if ( CentralAuthHooks::isMobileDomain() ) {
101                    $params['mobile'] = 1;
102                }
103                $out->addHTML( '<noscript>' . CentralAuthHooks::getAuthIconHtml(
104                    $loginWikiId, 'Special:CentralAutoLogin/start', $params, null
105                ) . '</noscript>' );
106            }
107        } else {
108            $centralUser = CentralAuthUser::getInstance( $out->getUser() );
109            if ( $centralUser->exists() && $centralUser->isAttached() ) {
110                $out->addModules( 'ext.centralauth.centralautologin.clearcookie' );
111            }
112
113            if ( $out->getRequest()->getSessionData( 'CentralAuthDoEdgeLogin' ) ) {
114                $logger->debug( 'Edge login triggered in BeforePageDisplay' );
115                $out->getRequest()->setSessionData( 'CentralAuthDoEdgeLogin', null );
116                $out->addHTML( CentralAuthHooks::getEdgeLoginHTML() );
117            }
118        }
119    }
120}