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;
|
|
|
|
|
2022-06-05 16:06:20 +00:00
|
|
|
use MediaWiki\Hook\LinkerMakeExternalLinkHook;
|
2024-09-09 05:53:31 +00:00
|
|
|
use MediaWiki\Utils\UrlUtils;
|
2022-06-05 16:06:20 +00:00
|
|
|
|
|
|
|
class Hooks implements LinkerMakeExternalLinkHook {
|
|
|
|
|
2023-06-13 16:24:48 +00:00
|
|
|
private HSTSPreloadLookup $lookup;
|
2024-09-09 05:53:31 +00:00
|
|
|
private UrlUtils $urlUtils;
|
2022-06-05 16:06:20 +00:00
|
|
|
|
2024-09-09 05:53:31 +00:00
|
|
|
public function __construct( HSTSPreloadLookup $lookup, UrlUtils $urlUtils ) {
|
2022-06-05 16:06:20 +00:00
|
|
|
$this->lookup = $lookup;
|
2024-09-09 05:53:31 +00:00
|
|
|
$this->urlUtils = $urlUtils;
|
2022-06-05 16:06:20 +00:00
|
|
|
}
|
2018-07-28 23:32:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook: LinkerMakeExternalLink
|
|
|
|
*
|
|
|
|
* Changes the scheme of the URL to HTTPS if necessary
|
|
|
|
*
|
2022-06-05 16:06:20 +00:00
|
|
|
* @param string &$url Link URL
|
|
|
|
* @param string &$text Link text
|
|
|
|
* @param string &$link New link HTML (if returning false)
|
|
|
|
* @param string[] &$attribs Attributes to be applied
|
|
|
|
* @param string $linkType External link type
|
|
|
|
* @return bool|void True or no return value to continue or false to abort
|
2018-07-28 23:32:15 +00:00
|
|
|
*/
|
2022-06-05 16:06:20 +00:00
|
|
|
public function onLinkerMakeExternalLink(
|
|
|
|
&$url, &$text, &$link, &$attribs, $linkType
|
|
|
|
) {
|
2022-08-26 21:12:31 +00:00
|
|
|
if ( str_starts_with( $url, 'https://' ) ) {
|
2018-07-28 23:32:15 +00:00
|
|
|
// Already HTTPS
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-09-09 05:53:31 +00:00
|
|
|
$parsed = $this->urlUtils->parse( $url );
|
2018-07-28 23:32:15 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-05 16:06:20 +00:00
|
|
|
if ( $this->lookup->isPreloaded( $parsed['host'] ) ) {
|
2018-08-02 04:40:16 +00:00
|
|
|
$parsed['scheme'] = 'https';
|
|
|
|
$parsed['delimiter'] = '://';
|
2024-09-09 05:53:31 +00:00
|
|
|
$url = UrlUtils::assemble( $parsed );
|
2018-07-28 23:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|