Completed
Pull Request — master (#1351)
by Christoph
09:04
created

ProxyController::redirect()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 8.7972
cc 4
eloc 12
nc 3
nop 1
crap 4
1
<?php
2
3
/**
4
 * @author Christoph Wurst <[email protected]>
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * ownCloud - Mail
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Mail\Controller;
26
27
use Exception;
28
use OCP\IRequest;
29
use OCP\ISession;
30
use OCP\IURLGenerator;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http\TemplateResponse;
33
use OCA\Mail\Http\ProxyDownloadResponse;
34
35
class ProxyController extends Controller {
36
37
	/**
38
	 * @var \OCP\IURLGenerator
39
	 */
40
	private $urlGenerator;
41
42
	/**
43
	 * @var \OCP\ISession
44
	 */
45
	private $session;
46
47
	/**
48
	 * @var string
49
	 */
50
	private $referrer;
51
52
	/**
53
	 * @var string
54
	 */
55
	private $hostname;
56
57
	/**
58
	 * @param string $appName
59
	 * @param \OCP\IRequest $request
60
	 * @param IURLGenerator $urlGenerator
61
	 * @param \OCP\ISession $session
62
	 */
63 5
	public function __construct($appName, IRequest $request,
64
		IURLGenerator $urlGenerator, ISession $session, $referrer, $hostname) {
65 5
		parent::__construct($appName, $request);
66 5
		$this->urlGenerator = $urlGenerator;
67 5
		$this->session = $session;
68 5
		$this->referrer = $referrer;
69 5
		$this->hostname = $hostname;
70 5
	}
71
72
	/**
73
	 * @NoAdminRequired
74
	 * @NoCSRFRequired
75
	 *
76
	 * @param string $src
77
	 *
78
	 * @throws \Exception If the URL is not valid.
79
	 * @return TemplateResponse
80
	 */
81 5
	public function redirect($src) {
82 5
		$authorizedRedirect = false;
83
84 5
		if (strpos($src, 'http://') !== 0 && strpos($src, 'https://') !== 0) {
85 1
			throw new Exception('URL is not valid.', 1);
86
		}
87
88
		// If the request has a referrer from this domain redirect the user without interaction
89
		// this is there to prevent an open redirector.
90
		// Since we can't prevent the referrer from being added with a HTTP only header we rely on an
91
		// additional JS file here.
92 4
		if (parse_url($this->referrer, PHP_URL_HOST) === $this->hostname) {
93 2
			$authorizedRedirect = true;
94 2
		}
95
96
		$params = [
97 4
			'authorizedRedirect' => $authorizedRedirect,
98 4
			'url' => $src,
99 4
			'urlHost' => parse_url($src, PHP_URL_HOST),
100 4
			'mailURL' => $this->urlGenerator->linkToRoute('mail.page.index'),
101 4
		];
102 4
		return new TemplateResponse($this->appName, 'redirect', $params, 'guest');
103
	}
104
105
	/**
106
	 * @NoAdminRequired
107
	 * @NoCSRFRequired
108
	 *
109
	 * @param string $src
110
	 *
111
	 * TODO: Cache the proxied content to prevent unnecessary requests from the oC server
112
	 *       The caching should also already happen in a cronjob so that the sender of the
113
	 *       mail does not know whether the mail has been opened.
114
	 *
115
	 * @return ProxyDownloadResponse
116
	 */
117
	public function proxy($src) {
118
		// close the session to allow parallel downloads
119
		$this->session->close();
120
121
		$content = $this->getUrlContent($src);
122
		return new ProxyDownloadResponse($content, $src, 'application/octet-stream');
123
	}
124
125
	/**
126
	 * Version hack for \OCP\IHelper
127
	 *
128
	 * @todo remove version-hack once core 8.1+ is supported
129
	 *
130
	 * @param type $src
131
	 * @return type
132
	 */
133
	private function getUrlContent($src) {
134
		$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0');
135
		if (version_compare($ocVersion, '8.2.0', '<')) {
136
			return \OC::$server->getHTTPClientService()->newClient()->get($src);
137
		} else {
138
			return \OC::$server->getHelper()->getUrlContent($src);
139
		}
140
	}
141
142
}
143