Completed
Push — master ( ac21a8...212d32 )
by Jan-Christoph
22:23
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 0
Metric Value
dl 7
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 2.032
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
23
namespace OCA\Mail\Service\HtmlPurify;
24
25
use HTMLPurifier_Config;
26
use HTMLPurifier_URI;
27
use HTMLPurifier_URIFilter;
28
use HTMLPurifier_URIParser;
29
use OCP\IRequest;
30
use OCP\IURLGenerator;
31
use OCP\Util;
32
33
class TransformURLScheme extends HTMLPurifier_URIFilter {
34
35
	public $name = 'TransformURLScheme';
36
	public $post = true;
37
38
	/** @var IURLGenerator */
39
	private $urlGenerator;
40
41
	/** @var IRequest */
42
	private $request;
43
44
	/**
45
	 * @var \Closure
46
	 */
47
	private $mapCidToAttachmentId;
48
49 1
	public function __construct($messageParameters, \Closure $mapCidToAttachmentId,
50
		IURLGenerator $urlGenerator, IRequest $request) {
51 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...
52 1
		$this->mapCidToAttachmentId = $mapCidToAttachmentId;
53 1
		$this->urlGenerator = $urlGenerator;
54 1
		$this->request = $request;
55 1
	}
56
57
	/**
58
	 * Transformator which will rewrite all HTTPS and HTTP urls to
59
	 * @param \HTMLPurifier_URI $uri
60
	 * @param HTMLPurifier_Config $config
61
	 * @param \HTMLPurifier_Context $context
62
	 * @return bool
63
	 */
64 1
	public function filter(&$uri, $config, $context) {
65
		/** @var \HTMLPurifier_Context $context */
66
		/** @var \HTMLPurifier_Config $config */
67
		// Only HTTPS and HTTP urls should get rewritten
68 1
		if ($uri->scheme === 'https' || $uri->scheme === 'http') {
69 1
			$uri = $this->filterHttp($uri, $context);
70 1
		}
71
72 1
		if ($uri->scheme === 'cid') {
73
			$attachmentId = $this->mapCidToAttachmentId->__invoke($uri->path);
74
			if (is_null($attachmentId)) {
75
				return true;
76
			}
77
			$this->messageParameters['attachmentId'] = $attachmentId;
78
79
			$imgUrl = $this->urlGenerator->linkToRouteAbsolute('mail.messages.downloadAttachment',
80
				$this->messageParameters);
81
			$parser = new HTMLPurifier_URIParser();
82
			$uri = $parser->parse($imgUrl);
83
		}
84
85 1
		return true;
86
	}
87
88
	/**
89
	 * @param HTMLPurifier_URI $uri
90
	 * @param \HTMLPurifier_Context $context
91
	 * @return HTMLPurifier_URI
92
	 */
93 1
	private function filterHttp(&$uri, $context) {
94 1
		$originalURL = urlencode($uri->scheme . '://' . $uri->host . $uri->path);
95 1
		if ($uri->query !== null) {
96 1
			$originalURL = $originalURL . urlencode('?' . $uri->query);
97 1
		}
98
99
		// Get the HTML attribute
100 1
		$element = $context->get('CurrentAttr');
101
102
		// If element is of type "href" it is most likely a link that should get redirected
103
		// otherwise it's an element that we send through our proxy
104 1
		if ($element === 'href') {
105 1
			$uri = new \HTMLPurifier_URI(
106 1
				$this->request->getServerProtocol(), null, $this->request->getServerHost(), null,
107 1
				$this->urlGenerator->linkToRoute('mail.proxy.redirect'),
108 1
				'src=' . $originalURL, null);
109 1
			return $uri;
110
		} else {
111 1
			$uri = new \HTMLPurifier_URI(
112 1
				$this->request->getServerProtocol(), null, $this->request->getServerHost(), null,
113 1
				$this->urlGenerator->linkToRoute('mail.proxy.proxy'),
114 1
				'src=' . $originalURL . '&requesttoken=' . \OC::$server->getSession()->get('requesttoken'),
115 1
				null);
116 1
			return $uri;
117
		}
118
	}
119
120
}
121