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