TransformNoReferrer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 63.16%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 41
ccs 12
cts 19
cp 0.6316
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B transform() 0 23 6
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Thomas Müller <[email protected]>
6
 *
7
 * Mail
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
namespace OCA\Mail\Service\HtmlPurify;
23
use HTMLPurifier_AttrTransform;
24
use HTMLPurifier_Config;
25
use HTMLPurifier_Context;
26
use HTMLPurifier_URIParser;
27
28
/**
29
 * Adds rel="noreferrer" to all outbound links.
30
 */
31
class TransformNoReferrer extends HTMLPurifier_AttrTransform
32
{
33
    /**
34
     * @type HTMLPurifier_URIParser
35
     */
36
    private $parser;
37
38 15
    public function __construct() {
39 15
        $this->parser = new HTMLPurifier_URIParser();
40 15
    }
41
42
    /**
43
     * @param array $attr
44
     * @param HTMLPurifier_Config $config
45
     * @param HTMLPurifier_Context $context
46
     * @return array
47
     */
48 13
    public function transform($attr, $config, $context)
49
    {
50 13
        if (!isset($attr['href'])) {
51
            return $attr;
52
        }
53
54
        // XXX Kind of inefficient
55 13
        $url = $this->parser->parse($attr['href']);
56 13
        $scheme = $url->getSchemeObj($config, $context);
57
58 13
        if ($scheme->browsable && !$url->isLocal($config, $context)) {
59 13
            if (isset($attr['rel'])) {
60
                $rels = explode(' ', $attr['rel']);
61
                if (!in_array('noreferrer', $rels)) {
62
                    $rels[] = 'noreferrer';
63
                }
64
                $attr['rel'] = implode(' ', $rels);
65
            } else {
66 13
                $attr['rel'] = 'noreferrer';
67
            }
68 13
        }
69 13
        return $attr;
70
    }
71
}
72