Completed
Pull Request — master (#1200)
by Christoph
09:35
created

TransformURLScheme   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 112
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 77.55%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 3
dl 14
loc 112
ccs 38
cts 49
cp 0.7755
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B filter() 0 23 5
B filterHttp() 0 26 3
A getServerProtocol() 7 7 2
A getServerHost() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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