1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Thomas Müller <[email protected]> |
5
|
|
|
* |
6
|
|
|
* ownCloud - Mail |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Mail\Service\HtmlPurify; |
23
|
|
|
|
24
|
|
|
use HTMLPurifier_Config; |
25
|
|
|
use HTMLPurifier_URI; |
26
|
|
|
use HTMLPurifier_URIFilter; |
27
|
|
|
use HTMLPurifier_URIParser; |
28
|
|
|
use OCP\IRequest; |
29
|
|
|
use OCP\IURLGenerator; |
30
|
|
|
|
31
|
|
|
class TransformURLScheme extends HTMLPurifier_URIFilter { |
32
|
|
|
|
33
|
|
|
public $name = 'TransformURLScheme'; |
34
|
|
|
public $post = true; |
35
|
|
|
|
36
|
|
|
/** @var IURLGenerator */ |
37
|
|
|
private $urlGenerator; |
38
|
|
|
|
39
|
|
|
/** @var IRequest */ |
40
|
|
|
private $request; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Closure |
44
|
|
|
*/ |
45
|
|
|
private $mapCidToAttachmentId; |
46
|
|
|
|
47
|
1 |
|
public function __construct($messageParameters, \Closure $mapCidToAttachmentId, |
48
|
|
|
IURLGenerator $urlGenerator, IRequest $request) { |
49
|
1 |
|
$this->messageParameters = $messageParameters; |
|
|
|
|
50
|
1 |
|
$this->mapCidToAttachmentId = $mapCidToAttachmentId; |
51
|
1 |
|
$this->urlGenerator = $urlGenerator; |
52
|
1 |
|
$this->request = $request; |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Transformator which will rewrite all HTTPS and HTTP urls to |
57
|
|
|
* @param \HTMLPurifier_URI $uri |
58
|
|
|
* @param HTMLPurifier_Config $config |
59
|
|
|
* @param \HTMLPurifier_Context $context |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
1 |
|
public function filter(&$uri, $config, $context) { |
63
|
|
|
/** @var \HTMLPurifier_Context $context */ |
64
|
|
|
/** @var \HTMLPurifier_Config $config */ |
65
|
|
|
// Only HTTPS and HTTP urls should get rewritten |
66
|
1 |
|
if ($uri->scheme === 'https' || $uri->scheme === 'http') { |
67
|
1 |
|
$uri = $this->filterHttp($uri, $context); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
if ($uri->scheme === 'cid') { |
71
|
|
|
$attachmentId = $this->mapCidToAttachmentId->__invoke($uri->path); |
72
|
|
|
if (is_null($attachmentId)) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
$this->messageParameters['attachmentId'] = $attachmentId; |
76
|
|
|
|
77
|
|
|
$imgUrl = $this->urlGenerator->linkToRouteAbsolute('mail.messages.downloadAttachment', |
78
|
|
|
$this->messageParameters); |
79
|
|
|
$parser = new HTMLPurifier_URIParser(); |
80
|
|
|
$uri = $parser->parse($imgUrl); |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param $uri |
88
|
|
|
* @param $context |
89
|
|
|
* @return HTMLPurifier_URI |
90
|
|
|
*/ |
91
|
1 |
|
private function filterHttp(&$uri, $context) { |
92
|
1 |
|
$originalURL = urlencode($uri->scheme . '://' . $uri->host . $uri->path); |
93
|
1 |
|
if ($uri->query !== null) { |
94
|
1 |
|
$originalURL = $originalURL . urlencode('?' . $uri->query); |
95
|
1 |
|
} |
96
|
|
|
|
97
|
|
|
// Get the HTML attribute |
98
|
1 |
|
$element = $context->get('CurrentAttr'); |
99
|
|
|
|
100
|
|
|
// If element is of type "href" it is most likely a link that should get redirected |
101
|
|
|
// otherwise it's an element that we send through our proxy |
102
|
1 |
|
if ($element === 'href') { |
103
|
1 |
|
$uri = new \HTMLPurifier_URI( |
104
|
1 |
|
$this->getServerProtocol(), null, $this->getServerHost(), null, |
105
|
1 |
|
$this->urlGenerator->linkToRoute('mail.proxy.redirect'), |
106
|
1 |
|
'src=' . $originalURL, null); |
107
|
1 |
|
return $uri; |
108
|
|
|
} else { |
109
|
1 |
|
$uri = new \HTMLPurifier_URI( |
110
|
1 |
|
$this->getServerProtocol(), null, $this->getServerHost(), null, |
111
|
1 |
|
$this->urlGenerator->linkToRoute('mail.proxy.proxy'), |
112
|
1 |
|
'src=' . $originalURL . '&requesttoken=' . \OC::$server->getSession()->get('requesttoken'), |
113
|
1 |
|
null); |
114
|
1 |
|
return $uri; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @todo remove version-hack once core 8.1+ is supported |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
1 |
View Code Duplication |
private function getServerProtocol() { |
|
|
|
|
123
|
1 |
|
$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0'); |
124
|
1 |
|
if (version_compare($ocVersion, '8.2.0', '<')) { |
125
|
|
|
return Util::getServerProtocol(); |
126
|
|
|
} |
127
|
1 |
|
return $this->request->getServerProtocol(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @todo remove version-hack once core 8.1+ is supported |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
1 |
View Code Duplication |
private function getServerHost() { |
|
|
|
|
135
|
1 |
|
$ocVersion = \OC::$server->getConfig()->getSystemValue('version', '0.0.0'); |
136
|
1 |
|
if (version_compare($ocVersion, '8.2.0', '<')) { |
137
|
|
|
return Util::getServerHost(); |
138
|
|
|
} |
139
|
1 |
|
return $this->request->getServerHost(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: