Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
Hooks | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
onLinkerMakeExternalLink | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | /** |
3 | * Copyright (C) 2018 Kunal Mehta <legoktm@debian.org> |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | */ |
18 | |
19 | namespace MediaWiki\SecureLinkFixer; |
20 | |
21 | use MediaWiki\Hook\LinkerMakeExternalLinkHook; |
22 | |
23 | class Hooks implements LinkerMakeExternalLinkHook { |
24 | |
25 | private HSTSPreloadLookup $lookup; |
26 | |
27 | public function __construct( HSTSPreloadLookup $lookup ) { |
28 | $this->lookup = $lookup; |
29 | } |
30 | |
31 | /** |
32 | * Hook: LinkerMakeExternalLink |
33 | * |
34 | * Changes the scheme of the URL to HTTPS if necessary |
35 | * |
36 | * @param string &$url Link URL |
37 | * @param string &$text Link text |
38 | * @param string &$link New link HTML (if returning false) |
39 | * @param string[] &$attribs Attributes to be applied |
40 | * @param string $linkType External link type |
41 | * @return bool|void True or no return value to continue or false to abort |
42 | */ |
43 | public function onLinkerMakeExternalLink( |
44 | &$url, &$text, &$link, &$attribs, $linkType |
45 | ) { |
46 | if ( str_starts_with( $url, 'https://' ) ) { |
47 | // Already HTTPS |
48 | return; |
49 | } |
50 | |
51 | $parsed = wfParseUrl( $url ); |
52 | if ( !$parsed ) { |
53 | return; |
54 | } |
55 | |
56 | if ( $parsed['scheme'] !== 'http' && $parsed['scheme'] !== '' ) { |
57 | // We only want http:// and proto-rel |
58 | return; |
59 | } |
60 | |
61 | if ( $this->lookup->isPreloaded( $parsed['host'] ) ) { |
62 | $parsed['scheme'] = 'https'; |
63 | $parsed['delimiter'] = '://'; |
64 | $url = wfAssembleUrl( $parsed ); |
65 | } |
66 | } |
67 | } |