Completed
Push — master ( ff9c3c...ab8637 )
by
unknown
04:59
created

TransformURLScheme::getServerHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2.032
1
<?php
2
3
/**
4
 * @author Thomas Müller <[email protected]>
5
 *
6
 * ownCloud - Mail
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Mail\Service\HtmlPurify;
23
24
use HTMLPurifier_Config;
25
use HTMLPurifier_URI;
26
use HTMLPurifier_URIFilter;
27
use HTMLPurifier_URIParser;
28
use OCP\IRequest;
29
use OCP\IURLGenerator;
30
use OCP\Util;
31
32
class TransformURLScheme extends HTMLPurifier_URIFilter {
33
34
	public $name = 'TransformURLScheme';
35
	public $post = true;
36
37
	/** @var IURLGenerator */
38
	private $urlGenerator;
39
40
	/** @var IRequest */
41
	private $request;
42
43
	/**
44
	 * @var \Closure
45
	 */
46
	private $mapCidToAttachmentId;
47
48 1
	public function __construct($messageParameters, \Closure $mapCidToAttachmentId,
49
		IURLGenerator $urlGenerator, IRequest $request) {
50 1
		$this->messageParameters = $messageParameters;
0 ignored issues
show
Bug introduced by
The property messageParameters does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51 1
		$this->mapCidToAttachmentId = $mapCidToAttachmentId;
52 1
		$this->urlGenerator = $urlGenerator;
53 1
		$this->request = $request;
54 1
	}
55
56
	/**
57
	 * Transformator which will rewrite all HTTPS and HTTP urls to
58
	 * @param \HTMLPurifier_URI $uri
59
	 * @param HTMLPurifier_Config $config
60
	 * @param \HTMLPurifier_Context $context
61
	 * @return bool
62
	 */
63 1
	public function filter(&$uri, $config, $context) {
64
		/** @var \HTMLPurifier_Context $context */
65
		/** @var \HTMLPurifier_Config $config */
66
		// Only HTTPS and HTTP urls should get rewritten
67 1
		if ($uri->scheme === 'https' || $uri->scheme === 'http') {
68 1
			$uri = $this->filterHttp($uri, $context);
69 1
		}
70
71 1
		if ($uri->scheme === 'cid') {
72
			$attachmentId = $this->mapCidToAttachmentId->__invoke($uri->path);
73
			if (is_null($attachmentId)) {
74
				return true;
75
			}
76
			$this->messageParameters['attachmentId'] = $attachmentId;
77
78
			$imgUrl = $this->urlGenerator->linkToRouteAbsolute('mail.messages.downloadAttachment',
79
				$this->messageParameters);
80
			$parser = new HTMLPurifier_URIParser();
81
			$uri = $parser->parse($imgUrl);
82
		}
83
84 1
		return true;
85
	}
86
87
	/**
88
	 * @param $uri
89
	 * @param $context
90
	 * @return HTMLPurifier_URI
91
	 */
92 1
	private function filterHttp(&$uri, $context) {
93 1
		$originalURL = urlencode($uri->scheme . '://' . $uri->host . $uri->path);
94 1
		if ($uri->query !== null) {
95 1
			$originalURL = $originalURL . urlencode('?' . $uri->query);
96 1
		}
97
98
		// Get the HTML attribute
99 1
		$element = $context->get('CurrentAttr');
100
101
		// If element is of type "href" it is most likely a link that should get redirected
102
		// otherwise it's an element that we send through our proxy
103 1
		if ($element === 'href') {
104 1
			$uri = new \HTMLPurifier_URI(
105 1
				$this->getServerProtocol(), null, $this->getServerHost(), null,
106 1
				$this->urlGenerator->linkToRoute('mail.proxy.redirect'),
107 1
				'src=' . $originalURL, null);
108 1
			return $uri;
109
		} else {
110 1
			$uri = new \HTMLPurifier_URI(
111 1
				$this->getServerProtocol(), null, $this->getServerHost(), null,
112 1
				$this->urlGenerator->linkToRoute('mail.proxy.proxy'),
113 1
				'src=' . $originalURL . '&requesttoken=' . \OC::$server->getSession()->get('requesttoken'),
114 1
				null);
115 1
			return $uri;
116
		}
117
	}
118
119
	/**
120
	 * @todo remove version-hack once core 8.1+ is supported
121
	 * @return string
122
	 */
123 1 View Code Duplication
	private function getServerProtocol() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124 1
		$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0');
125 1
		if (version_compare($ocVersion, '8.2.0', '<')) {
126
			return Util::getServerProtocol();
127
		}
128 1
		return $this->request->getServerProtocol();
129
	}
130
131
	/**
132
	 * @todo remove version-hack once core 8.1+ is supported
133
	 * @return string
134
	 */
135 1 View Code Duplication
	private function getServerHost() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136 1
		$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0');
137 1
		if (version_compare($ocVersion, '8.2.0', '<')) {
138
			return Util::getServerHost();
139
		}
140 1
		return $this->request->getServerHost();
141
	}
142
143
}
144