Html::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Jakob Sack <[email protected]>
6
 * @author Jakob Sack <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * Mail
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\Mail\Service;
27
28
use Closure;
29
use HTMLPurifier;
30
use HTMLPurifier_Config;
31
use HTMLPurifier_HTMLDefinition;
32
use HTMLPurifier_URISchemeRegistry;
33
use Kwi\UrlLinker;
34
use OCA\Mail\Service\HtmlPurify\CidURIScheme;
35
use OCA\Mail\Service\HtmlPurify\TransformCSSBackground;
36
use OCA\Mail\Service\HtmlPurify\TransformHTMLLinks;
37
use OCA\Mail\Service\HtmlPurify\TransformImageSrc;
38
use OCA\Mail\Service\HtmlPurify\TransformNoReferrer;
39
use OCA\Mail\Service\HtmlPurify\TransformURLScheme;
40
use OCP\IRequest;
41
use OCP\IURLGenerator;
42
use OCP\Util;
43
44
class Html {
45
46
	/** @var IURLGenerator */
47
	private $urlGenerator;
48
49
	/** @var IRequest */
50
	private $request;
51
52 27
	public function __construct(IURLGenerator $urlGenerator, IRequest $request) {
53 27
		$this->urlGenerator = $urlGenerator;
54 27
		$this->request = $request;
55 27
	}
56
57
	/**
58
	 * @param string $data
59
	 * @return string
60
	 */
61 15
	public function convertLinks($data) {
62 15
		$linker = new UrlLinker(true, false);
63 15
		$data = $linker->linkUrlsInTrustedHtml($data);
64
65 15
		$config = HTMLPurifier_Config::createDefault();
66
67
		// Append target="_blank" to all link (a) elements
68 15
		$config->set('HTML.TargetBlank', true);
69
70
		// allow cid, http and ftp
71 15
		$config->set('URI.AllowedSchemes', ['http' => true, 'https' => true, 'ftp' => true, 'mailto' => true]);
72 15
		$config->set('URI.Host', Util::getServerHostName());
73
74
		// Disable the cache since ownCloud has no really appcache
75
		// TODO: Fix this - requires https://github.com/owncloud/core/issues/10767 to be fixed
76 15
		$config->set('Cache.DefinitionImpl', null);
77
78
		/** @var HTMLPurifier_HTMLDefinition $uri */
79 15
		$uri = $config->getDefinition('HTML');
80 15
		$uri->info_attr_transform_post['noreferrer'] = new TransformNoReferrer();
81
82 15
		$purifier = new HTMLPurifier($config);
83
84 15
		return $purifier->purify($data);
85
	}
86
87
	/**
88
	 * split off the signature
89
	 *
90
	 * @param string $body
91
	 * @return array
92
	 */
93 3
	public function parseMailBody($body) {
94 3
		$signature = null;
95 3
		$parts = explode("-- \r\n", $body);
96 3
		if (count($parts) > 1) {
97 2
			$signature = nl2br(array_pop($parts));
98 2
			$body = implode("-- \r\n", $parts);
99 2
		}
100
101
		return [
102 3
			$body,
103
			$signature
104 3
		];
105
	}
106
107 1
	public function sanitizeHtmlMailBody($mailBody, array $messageParameters, Closure $mapCidToAttachmentId) {
108 1
		$config = HTMLPurifier_Config::createDefault();
109
110
		// Append target="_blank" to all link (a) elements
111 1
		$config->set('HTML.TargetBlank', true);
112
113
		// allow cid, http and ftp
114 1
		$config->set('URI.AllowedSchemes', ['cid' => true, 'http' => true, 'https' => true, 'ftp' => true, 'mailto' => true]);
115 1
		$config->set('URI.Host', Util::getServerHostName());
116
117
		// Disable the cache since ownCloud has no really appcache
118
		// TODO: Fix this - requires https://github.com/owncloud/core/issues/10767 to be fixed
119 1
		$config->set('Cache.DefinitionImpl', null);
120
121
		// Rewrite URL for redirection and proxying of content
122 1
		$html = $config->getDefinition('HTML');
123 1
		$html->info_attr_transform_post['imagesrc'] = new TransformImageSrc($this->urlGenerator);
0 ignored issues
show
Bug introduced by
The property info_attr_transform_post does not seem to exist in HTMLPurifier_Definition.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
124 1
		$html->info_attr_transform_post['cssbackground'] = new TransformCSSBackground($this->urlGenerator);
125 1
		$html->info_attr_transform_post['htmllinks'] = new TransformHTMLLinks();
126
127 1
		$uri = $config->getDefinition('URI');
128 1
		$uri->addFilter(new TransformURLScheme($messageParameters, $mapCidToAttachmentId, $this->urlGenerator, $this->request), $config);
129
130 1
		HTMLPurifier_URISchemeRegistry::instance()->register('cid', new CidURIScheme());
131
132 1
		$purifier = new HTMLPurifier($config);
133
134 1
		$result = $purifier->purify($mailBody);
135
		// eat xml parse errors within HTMLPurifier
136 1
		libxml_clear_errors();
137 1
		return $result;
138
	}
139
140
}
141