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
|
1 |
|
$element->removeAttribute('data-' . $attribute); |
35
|
|
|
} |
36
|
1 |
|
} |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
try { |
40
|
1 |
|
return $crawler->html(); |
41
|
|
|
} catch (\InvalidArgumentException $exception) { |
42
|
|
|
return $content; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param mixed $content |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
2 |
|
public function reverseTransform($content) |
52
|
|
|
{ |
53
|
2 |
|
if (!trim($content)) { |
54
|
1 |
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$crawler = new Crawler(); |
58
|
2 |
|
$crawler->addHtmlContent($content); |
59
|
|
|
|
60
|
|
|
// Get all img and a tags and parse the token. |
61
|
2 |
|
$crawler->filter('img,a')->each( |
62
|
|
|
function (Crawler $node) { |
63
|
1 |
|
$element = $node->getNode(0); |
64
|
1 |
|
$attribute = $element->nodeName === 'img' ? 'src' : 'href'; |
65
|
1 |
|
$attributeValue = $element->getAttribute($attribute); |
66
|
1 |
|
$parsed = parse_url($attributeValue, PHP_URL_QUERY); |
67
|
1 |
|
parse_str($parsed, $query); |
68
|
|
|
|
69
|
1 |
|
if (isset($query['token'])) { |
70
|
|
|
$element->setAttribute($attribute, $query['token']); |
71
|
|
|
} |
72
|
1 |
|
$element->setAttribute('data-' . $attribute, $attributeValue); |
73
|
2 |
|
} |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
try { |
77
|
2 |
|
$html = $crawler->filter('body')->html(); |
78
|
|
|
|
79
|
|
|
// URL-decode square brackets in img and a tags |
80
|
2 |
|
$html = preg_replace_callback( |
81
|
2 |
|
'/<(img|a)\s+[^>]*>/', |
82
|
|
|
function ($matches) { |
83
|
1 |
|
return str_replace(['%5B', '%5D'], ['[', ']'], $matches[0]); |
84
|
2 |
|
}, |
85
|
|
|
$html |
86
|
|
|
); |
87
|
|
|
|
88
|
2 |
|
return $html; |
89
|
|
|
} catch (\InvalidArgumentException $exception) { |
90
|
|
|
return $content; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.