-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlistener.php
152 lines (130 loc) · 3.64 KB
/
listener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* @package s9e\mediaembed
* @copyright Copyright (c) 2014-2016 The s9e Authors
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
namespace s9e\mediaembed;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use s9e\TextFormatter\Bundles\MediaPack;
use s9e\TextFormatter\Configurator\Bundles\MediaPack as MediaPackConfigurator;
use s9e\TextFormatter\Configurator\Items\UnsafeTemplate;
class listener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
'core.text_formatter_s9e_configure_after' => 'onConfigure',
'core.modify_format_display_text_after' => 'onPreview',
'core.modify_submit_post_data' => 'onSubmit',
'core.modify_text_for_display_after' => 'onDisplay',
'core.modify_text_for_edit_before' => 'onEdit',
'core.modify_text_for_storage_after' => 'onStorage',
'core.submit_pm_before' => 'onPmStorage'
);
}
public function onConfigure($event)
{
$configurator = $event['configurator'];
$templates = array();
foreach ($configurator->tags as $tagName => $tag)
{
if (isset($configurator->BBCodes[$tagName]))
{
$templates[$tagName] = (string) $tag->template;
}
}
$bundleConfigurator = new MediaPackConfigurator;
$bundleConfigurator->configure($configurator);
foreach ($templates as $tagName => $template)
{
$tag = $configurator->tags[$tagName];
if ($tag->template == $template)
{
continue;
}
$configurator->BBCodes[$tagName]->defaultAttribute = 'url';
$configurator->BBCodes[$tagName]->contentAttributes = array('url');
$tag->template = new UnsafeTemplate('<xsl:choose><xsl:when test="@url">' . $tag->template . '</xsl:when><xsl:otherwise>' . $template . '</xsl:otherwise></xsl:choose>');
}
}
public function onDisplay($event)
{
$event['text'] = $this->render($event['text']);
}
public function onEdit($event)
{
$event['text'] = $this->unparse($event['text']);
}
public function onPmStorage($event)
{
$data = $event['data'];
$data['message'] = $this->parse($data['message']);
$event['data'] = $data;
}
public function onPreview($event)
{
$event['text'] = $this->render($this->parse($event['text']));
}
public function onStorage($event)
{
$event['text'] = $this->parse($event['text']);
}
public function onSubmit($event)
{
$data = $event['data'];
$data['message'] = $this->parse($data['message']);
$event['data'] = $data;
}
public function parse($text)
{
if (strpos($text, '<!-- m -->') === false)
{
return $text;
}
if (!class_exists('s9e\\TextFormatter\\Parser', false))
{
include_once __DIR__ . '/bundle.php';
include __DIR__ . '/parsing.php';
}
return preg_replace_callback(
'(<!-- m -->.*?href="([^"]+).*?<!-- m -->)',
function ($m)
{
$xml = MediaPack::parse(htmlspecialchars_decode($m[1]));
return ($xml[1] === 'r')
? '<!-- s9e:mediaembed:' . base64_encode($xml) . ' -->' . $m[0]
: $m[0];
},
$text
);
}
public function render($text)
{
if (strpos($text, '<!-- s9e:mediaembed') === false)
{
return $text;
}
if (!class_exists('s9e\\TextFormatter\\Renderer', false))
{
include_once __DIR__ . '/bundle.php';
include __DIR__ . '/rendering.php';
}
return preg_replace_callback(
'(<!-- s9e:mediaembed:([^ ]+) --><!-- m -->.*?<!-- m -->)',
function ($m)
{
return MediaPack::render(base64_decode($m[1]));
},
$text
);
}
public function unparse($text)
{
if (strpos($text, '<!-- s9e:mediaembed') === false)
{
return $text;
}
return preg_replace('(<!-- s9e:mediaembed:([^ ]+) -->)', '', $text);
}
}