Completed
Pull Request — master (#1200)
by Christoph
09:35
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
 * ownCloud
5
 *
6
 * @author Thomas Müller
7
 * @copyright 2014 Thomas Müller [email protected]
8
 *
9
 * You should have received a copy of the GNU Affero General Public
10
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
11
 *
12
 */
13
14
namespace OCA\Mail\Service\HtmlPurify;
15
16
use HTMLPurifier_Config;
17
use HTMLPurifier_URI;
18
use HTMLPurifier_URIFilter;
19
use HTMLPurifier_URIParser;
20
use OCP\IRequest;
21
use OCP\IURLGenerator;
22
23
class TransformURLScheme extends HTMLPurifier_URIFilter {
24
25
	public $name = 'TransformURLScheme';
26
	public $post = true;
27
28
	/** @var IURLGenerator */
29
	private $urlGenerator;
30
31
	/** @var IRequest */
32
	private $request;
33
34
	/**
35
	 * @var \Closure
36
	 */
37
	private $mapCidToAttachmentId;
38
39 1
	public function __construct($messageParameters, \Closure $mapCidToAttachmentId,
40
		IURLGenerator $urlGenerator, IRequest $request) {
41 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...
42 1
		$this->mapCidToAttachmentId = $mapCidToAttachmentId;
43 1
		$this->urlGenerator = $urlGenerator;
44 1
		$this->request = $request;
45 1
	}
46
47
	/**
48
	 * Transformator which will rewrite all HTTPS and HTTP urls to
49
	 * @param \HTMLPurifier_URI $uri
50
	 * @param HTMLPurifier_Config $config
51
	 * @param \HTMLPurifier_Context $context
52
	 * @return bool
53
	 */
54 1
	public function filter(&$uri, $config, $context) {
55
		/** @var \HTMLPurifier_Context $context */
56
		/** @var \HTMLPurifier_Config $config */
57
		// Only HTTPS and HTTP urls should get rewritten
58 1
		if ($uri->scheme === 'https' || $uri->scheme === 'http') {
59 1
			$uri = $this->filterHttp($uri, $context);
60 1
		}
61
62 1
		if ($uri->scheme === 'cid') {
63
			$attachmentId = $this->mapCidToAttachmentId->__invoke($uri->path);
64
			if (is_null($attachmentId)) {
65
				return true;
66
			}
67
			$this->messageParameters['attachmentId'] = $attachmentId;
68
69
			$imgUrl = $this->urlGenerator->linkToRouteAbsolute('mail.messages.downloadAttachment',
70
				$this->messageParameters);
71
			$parser = new HTMLPurifier_URIParser();
72
			$uri = $parser->parse($imgUrl);
73
		}
74
75 1
		return true;
76
	}
77
78
	/**
79
	 * @param $uri
80
	 * @param $context
81
	 * @return HTMLPurifier_URI
82
	 */
83 1
	private function filterHttp(&$uri, $context) {
84 1
		$originalURL = urlencode($uri->scheme . '://' . $uri->host . $uri->path);
85 1
		if ($uri->query !== null) {
86 1
			$originalURL = $originalURL . urlencode('?' . $uri->query);
87 1
		}
88
89
		// Get the HTML attribute
90 1
		$element = $context->get('CurrentAttr');
91
92
		// If element is of type "href" it is most likely a link that should get redirected
93
		// otherwise it's an element that we send through our proxy
94 1
		if ($element === 'href') {
95 1
			$uri = new \HTMLPurifier_URI(
96 1
				$this->getServerProtocol(), null, $this->getServerHost(), null,
97 1
				$this->urlGenerator->linkToRoute('mail.proxy.redirect'),
98 1
				'src=' . $originalURL, null);
99 1
			return $uri;
100
		} else {
101 1
			$uri = new \HTMLPurifier_URI(
102 1
				$this->getServerProtocol(), null, $this->getServerHost(), null,
103 1
				$this->urlGenerator->linkToRoute('mail.proxy.proxy'),
104 1
				'src=' . $originalURL . '&requesttoken=' . \OC::$server->getSession()->get('requesttoken'),
105 1
				null);
106 1
			return $uri;
107
		}
108
	}
109
110
	/**
111
	 * @todo remove version-hack once core 8.1+ is supported
112
	 * @return string
113
	 */
114 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...
115 1
		$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0');
116 1
		if (version_compare($ocVersion, '8.2.0', '<')) {
117
			return \Util::getServerProtocol();
118
		}
119 1
		return $this->request->getServerProtocol();
120
	}
121
122
	/**
123
	 * @todo remove version-hack once core 8.1+ is supported
124
	 * @return string
125
	 */
126 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...
127 1
		$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0');
128 1
		if (version_compare($ocVersion, '8.2.0', '<')) {
129
			return \Util::getServerHost();
130
		}
131 1
		return $this->request->getServerHost();
132
	}
133
134
}
135