2018-07-28 23:32:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2021-04-12 02:10:07 +00:00
|
|
|
* Copyright (C) 2018 Kunal Mehta <legoktm@debian.org>
|
2018-07-28 23:32:15 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\SecureLinkFixer;
|
|
|
|
|
|
|
|
class Hooks {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook: LinkerMakeExternalLink
|
|
|
|
*
|
|
|
|
* Changes the scheme of the URL to HTTPS if necessary
|
|
|
|
*
|
|
|
|
* @param string &$url
|
|
|
|
*/
|
|
|
|
public static function onLinkerMakeExternalLink( &$url ) {
|
|
|
|
if ( strpos( $url, 'https://' ) === 0 ) {
|
|
|
|
// Already HTTPS
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsed = wfParseUrl( $url );
|
|
|
|
if ( !$parsed ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-02 04:40:16 +00:00
|
|
|
if ( $parsed['scheme'] !== 'http' && $parsed['scheme'] !== '' ) {
|
2018-07-28 23:32:15 +00:00
|
|
|
// We only want http:// and proto-rel
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-02 04:40:16 +00:00
|
|
|
if ( HSTSPreloadLookup::getInstance()->isPreloaded( $parsed['host'] ) ) {
|
|
|
|
$parsed['scheme'] = 'https';
|
|
|
|
$parsed['delimiter'] = '://';
|
2018-07-28 23:32:15 +00:00
|
|
|
$url = wfAssembleUrl( $parsed );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|