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
|
|
|
* 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 OC; |
29
|
|
|
use OCA\Mail\Http\ProxyDownloadResponse; |
30
|
|
|
use OCP\AppFramework\Controller; |
31
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
32
|
|
|
use OCP\Http\Client\IClientService; |
33
|
|
|
use OCP\IRequest; |
34
|
|
|
use OCP\ISession; |
35
|
|
|
use OCP\IURLGenerator; |
36
|
|
|
|
37
|
|
|
class ProxyController extends Controller { |
38
|
|
|
|
39
|
|
|
/** @var IURLGenerator */ |
40
|
|
|
private $urlGenerator; |
41
|
|
|
|
42
|
|
|
/** @var ISession */ |
43
|
|
|
private $session; |
44
|
|
|
|
45
|
|
|
/** @var IClientService */ |
46
|
|
|
private $clientService; |
47
|
|
|
|
48
|
|
|
/** @var string */ |
49
|
|
|
private $referrer; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
private $hostname; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $appName |
56
|
|
|
* @param IRequest $request |
57
|
|
|
* @param IURLGenerator $urlGenerator |
58
|
|
|
* @param ISession $session |
59
|
|
|
* @param IClientService $clientService |
60
|
|
|
*/ |
61
|
5 |
|
public function __construct($appName, IRequest $request, |
62
|
|
|
IURLGenerator $urlGenerator, ISession $session, IClientService $clientService, $referrer, $hostname) { |
63
|
5 |
|
parent::__construct($appName, $request); |
64
|
5 |
|
$this->urlGenerator = $urlGenerator; |
65
|
5 |
|
$this->session = $session; |
66
|
5 |
|
$this->clientService = $clientService; |
67
|
5 |
|
$this->referrer = $referrer; |
68
|
5 |
|
$this->hostname = $hostname; |
69
|
5 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @NoAdminRequired |
73
|
|
|
* @NoCSRFRequired |
74
|
|
|
* |
75
|
|
|
* @param string $src |
76
|
|
|
* |
77
|
|
|
* @throws \Exception If the URL is not valid. |
78
|
|
|
* @return TemplateResponse |
79
|
|
|
*/ |
80
|
5 |
|
public function redirect($src) { |
81
|
5 |
|
$authorizedRedirect = false; |
82
|
|
|
|
83
|
5 |
|
if (strpos($src, 'http://') !== 0 && strpos($src, 'https://') !== 0) { |
84
|
1 |
|
throw new Exception('URL is not valid.', 1); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// If the request has a referrer from this domain redirect the user without interaction |
88
|
|
|
// this is there to prevent an open redirector. |
89
|
|
|
// Since we can't prevent the referrer from being added with a HTTP only header we rely on an |
90
|
|
|
// additional JS file here. |
91
|
4 |
|
if (parse_url($this->referrer, PHP_URL_HOST) === $this->hostname) { |
92
|
2 |
|
$authorizedRedirect = true; |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
$params = [ |
96
|
4 |
|
'authorizedRedirect' => $authorizedRedirect, |
97
|
4 |
|
'url' => $src, |
98
|
4 |
|
'urlHost' => parse_url($src, PHP_URL_HOST), |
99
|
4 |
|
'mailURL' => $this->urlGenerator->linkToRoute('mail.page.index'), |
100
|
4 |
|
]; |
101
|
4 |
|
return new TemplateResponse($this->appName, 'redirect', $params, 'guest'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @NoAdminRequired |
106
|
|
|
* @NoCSRFRequired |
107
|
|
|
* |
108
|
|
|
* @param string $src |
109
|
|
|
* |
110
|
|
|
* TODO: Cache the proxied content to prevent unnecessary requests from the oC server |
111
|
|
|
* The caching should also already happen in a cronjob so that the sender of the |
112
|
|
|
* mail does not know whether the mail has been opened. |
113
|
|
|
* |
114
|
|
|
* @return ProxyDownloadResponse |
115
|
|
|
*/ |
116
|
|
|
public function proxy($src) { |
117
|
|
|
// close the session to allow parallel downloads |
118
|
|
|
$this->session->close(); |
119
|
|
|
|
120
|
|
|
$client = $this->clientService->newClient(); |
121
|
|
|
$content = $client->get($src); |
122
|
|
|
return new ProxyDownloadResponse($content, $src, 'application/octet-stream'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|