|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
6
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class MediaTokenTransformer. |
|
10
|
|
|
*/ |
|
11
|
|
|
class MediaTokenTransformer implements DataTransformerInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param mixed $content |
|
15
|
|
|
* |
|
16
|
|
|
* @return string |
|
17
|
|
|
*/ |
|
18
|
2 |
|
public function transform($content) |
|
19
|
|
|
{ |
|
20
|
2 |
|
if (!trim($content)) { |
|
21
|
1 |
|
return ''; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
$crawler = new Crawler(); |
|
25
|
1 |
|
$crawler->addHtmlContent($content); |
|
26
|
|
|
|
|
27
|
1 |
|
$crawler->filter('img,a')->each( |
|
28
|
|
|
function (Crawler $node) { |
|
29
|
1 |
|
$element = $node->getNode(0); |
|
30
|
1 |
|
$attribute = $element->nodeName === 'img' ? 'src' : 'href'; |
|
31
|
1 |
|
if ($element->hasAttribute('data-' . $attribute)) { |
|
32
|
1 |
|
$attributeValue = $element->getAttribute('data-' . $attribute); |
|
33
|
1 |
|
$element->setAttribute($attribute, $attributeValue); |
|
34
|
|
|
$element->removeAttribute('data-' . $attribute); |
|
35
|
1 |
|
} |
|
36
|
|
|
} |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
1 |
|
try { |
|
40
|
|
|
return $crawler->html(); |
|
41
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
42
|
|
|
return $content; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param mixed $content |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
2 |
|
*/ |
|
51
|
|
|
public function reverseTransform($content) |
|
52
|
2 |
|
{ |
|
53
|
1 |
|
if (!trim($content)) { |
|
54
|
|
|
return ''; |
|
55
|
|
|
} |
|
56
|
2 |
|
|
|
57
|
2 |
|
$crawler = new Crawler(); |
|
58
|
|
|
$crawler->addHtmlContent($content); |
|
59
|
|
|
|
|
60
|
2 |
|
// Get all img and a tags and parse the token. |
|
61
|
|
|
$crawler->filter('img,a')->each( |
|
62
|
1 |
|
function (Crawler $node) { |
|
63
|
1 |
|
$element = $node->getNode(0); |
|
64
|
1 |
|
$attribute = $element->nodeName === 'img' ? 'src' : 'href'; |
|
65
|
1 |
|
$attributeValue = $element->getAttribute($attribute); |
|
66
|
|
|
$parsed = parse_url($attributeValue, PHP_URL_QUERY); |
|
67
|
1 |
|
parse_str($parsed, $query); |
|
68
|
|
|
|
|
69
|
|
|
if (isset($query['token'])) { |
|
70
|
1 |
|
$element->setAttribute($attribute, $query['token']); |
|
71
|
2 |
|
} |
|
72
|
|
|
$element->setAttribute('data-' . $attribute, $attributeValue); |
|
73
|
|
|
} |
|
74
|
|
|
); |
|
75
|
2 |
|
|
|
76
|
|
|
try { |
|
77
|
|
|
$html = $crawler->filter('body')->html(); |
|
78
|
2 |
|
|
|
79
|
2 |
|
// URL-decode square brackets in img and a tags |
|
80
|
|
|
$html = preg_replace_callback( |
|
81
|
1 |
|
'/<(img|a)\s+[^>]*>/', |
|
82
|
2 |
|
function ($matches) { |
|
83
|
2 |
|
return str_replace(['%5B', '%5D'], ['[', ']'], $matches[0]); |
|
84
|
|
|
}, |
|
85
|
|
|
$html |
|
86
|
2 |
|
); |
|
87
|
|
|
|
|
88
|
|
|
return $html; |
|
89
|
|
|
} catch (\InvalidArgumentException $exception) { |
|
90
|
|
|
return $content; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|