1 | <?php |
||
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) { |
|
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) { |
||
96 | } |
||
97 |