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
|
|
|
|
24
|
|
|
namespace OCA\Mail\Service\HtmlPurify; |
25
|
|
|
use HTMLPurifier_AttrTransform; |
26
|
|
|
use HTMLPurifier_Config; |
27
|
|
|
use HTMLPurifier_Context; |
28
|
|
|
use OCP\IURLGenerator; |
29
|
|
|
use OCP\Util; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Adds copies src to data-src on all img tags. |
33
|
|
|
*/ |
34
|
|
|
class TransformCSSBackground extends HTMLPurifier_AttrTransform { |
35
|
|
|
/** @var IURLGenerator */ |
36
|
|
|
private $urlGenerator; |
37
|
|
|
|
38
|
1 |
|
public function __construct(IURLGenerator $urlGenerator) { |
39
|
1 |
|
$this->urlGenerator = $urlGenerator; |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $attr |
44
|
|
|
* @param HTMLPurifier_Config $config |
45
|
|
|
* @param HTMLPurifier_Context $context |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
1 |
|
public function transform($attr, $config, $context) { |
49
|
1 |
|
if (!isset($attr['style']) || |
50
|
1 |
|
strpos($attr['style'], 'background') === false) { |
51
|
1 |
|
return $attr; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Check if there is a background image given |
55
|
1 |
|
$cssAttributes = explode(';', $attr['style']); |
56
|
|
|
|
57
|
1 |
|
$func = function($cssAttribute) { |
58
|
1 |
|
if (preg_match('/\S/', $cssAttribute) === 0) { |
59
|
|
|
// empty or whitespace |
60
|
1 |
|
return ''; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
list($name, $value) = explode(':', $cssAttribute, 2); |
64
|
1 |
|
if(strpos($name, 'background') !== false && |
65
|
1 |
|
strpos($value, 'url(') !== false) { |
66
|
|
|
// Replace image URL |
67
|
|
|
$value = preg_replace('/url\("?http.*\)/i', |
68
|
|
|
'url(' . $this->urlGenerator->imagePath('mail', 'blocked-image.png') . ')', |
69
|
|
|
$value); |
70
|
|
|
return $name.':'.$value; |
71
|
|
|
} else { |
72
|
1 |
|
return $cssAttribute; |
73
|
|
|
} |
74
|
1 |
|
}; |
75
|
|
|
|
76
|
|
|
// Reassemble style |
77
|
1 |
|
$cssAttributes = array_map($func, $cssAttributes); |
78
|
1 |
|
$newStyle = implode(';', $cssAttributes); |
79
|
|
|
|
80
|
|
|
// Replace style if required |
81
|
1 |
|
if ($newStyle !== $attr['style']) { |
82
|
|
|
$attr['data-original-style'] = $attr['style']; |
83
|
|
|
$attr['style'] = $newStyle; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $attr; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|