1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jakob Sack <[email protected]> |
5
|
|
|
* @author Jakob Sack <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Mail |
8
|
|
|
* |
9
|
|
|
* This code is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
11
|
|
|
* as published by the Free Software Foundation. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU Affero General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OCA\Mail\Service\HtmlPurify; |
24
|
|
|
use HTMLPurifier_AttrTransform; |
25
|
|
|
use HTMLPurifier_Config; |
26
|
|
|
use HTMLPurifier_Context; |
27
|
|
|
use HTMLPurifier_URI; |
28
|
|
|
use HTMLPurifier_URIFilter; |
29
|
|
|
use HTMLPurifier_URIParser; |
30
|
|
|
use OCP\IURLGenerator; |
31
|
|
|
use OCP\Util; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Adds copies src to data-src on all img tags. |
35
|
|
|
*/ |
36
|
|
|
class TransformImageSrc extends HTMLPurifier_AttrTransform { |
37
|
|
|
/** |
38
|
|
|
* @type HTMLPurifier_URIParser |
39
|
|
|
*/ |
40
|
|
|
private $parser; |
41
|
|
|
|
42
|
|
|
/** @var IURLGenerator */ |
43
|
|
|
private $urlGenerator; |
44
|
|
|
|
45
|
1 |
|
public function __construct(IURLGenerator $urlGenerator) { |
46
|
1 |
|
$this->parser = new HTMLPurifier_URIParser(); |
47
|
1 |
|
$this->urlGenerator = $urlGenerator; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $attr |
52
|
|
|
* @param HTMLPurifier_Config $config |
53
|
|
|
* @param HTMLPurifier_Context $context |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
1 |
|
public function transform($attr, $config, $context) { |
57
|
1 |
|
if ($context->get('CurrentToken')->name !== 'img' || |
58
|
1 |
|
!isset($attr['src'])) { |
59
|
1 |
|
return $attr; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Block tracking pixels |
63
|
1 |
|
if (isset($attr['width']) && isset($attr['height']) && |
64
|
1 |
|
(int)$attr['width'] < 5 && (int)$attr['height'] < 5){ |
65
|
|
|
// Replace with a transparent png in case it's important for the layout |
66
|
|
|
$attr['src'] = $this->urlGenerator->imagePath('mail', 'blocked-image.png'); |
67
|
|
|
$attr = $this->setDisplayNone($attr); |
68
|
|
|
return $attr; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Do not block images attached to the email |
72
|
1 |
|
$url = $this->parser->parse($attr['src']); |
73
|
1 |
|
if ($url->host === Util::getServerHostName() && $url->path === $this->urlGenerator->linkToRoute('mail.proxy.proxy')) { |
74
|
|
|
$attr['data-original-src'] = $attr['src']; |
75
|
|
|
$attr['src'] = $this->urlGenerator->imagePath('mail', 'blocked-image.png'); |
76
|
|
|
$attr = $this->setDisplayNone($attr); |
77
|
|
|
} |
78
|
1 |
|
return $attr; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param array $attr |
83
|
|
|
* @return array |
84
|
|
|
* |
85
|
|
|
* Sets html attribute style="display: none;", keeps old style |
86
|
|
|
* attributes |
87
|
|
|
*/ |
88
|
|
|
private function setDisplayNone($attr) { |
89
|
|
|
if (isset($attr['style'])) { |
90
|
|
|
$attr['style'] = 'display: none;'.$attr['style']; // the space is important for jquery! |
91
|
|
|
} else { |
92
|
|
|
$attr['style'] = 'display: none;'; |
93
|
|
|
} |
94
|
|
|
return $attr; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|