-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathParser.php
More file actions
224 lines (190 loc) · 7.13 KB
/
Parser.php
File metadata and controls
224 lines (190 loc) · 7.13 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Copyright (C) 2015-2019 FeatherBB
* based on code by (C) 2008-2015 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace FeatherBB\Core;
use FeatherBB\Core\Interfaces\Cache;
use FeatherBB\Core\Interfaces\ForumEnv;
use FeatherBB\Core\Interfaces\ForumSettings;
use FeatherBB\Core\Interfaces\User;
use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Unparser;
class Parser
{
private $parser;
private $renderer;
private $cacheDir;
private $smilies;
public function __construct()
{
$this->cacheDir = ForumEnv::get('FORUM_CACHE_DIR').'/parser';
// Load smilies
if (!Cache::isCached('smilies')) {
Cache::store('smilies', \FeatherBB\Model\Cache::getSmilies());
}
$this->smilies = Cache::retrieve('smilies');
if (Cache::isCached('s9eparser') && Cache::isCached('s9erenderer')) {
$this->parser = unserialize(Cache::retrieve('s9eparser'));
$this->renderer = unserialize(Cache::retrieve('s9erenderer'));
} else {
$this->configureParser();
}
}
/**
* TODO Build bundles depend forum config and user group rights
*/
private function configureParser()
{
$renderer = $parser = null;
$configurator = new Configurator;
$configurator->plugins->load('Autoemail');//Fatdown & Forum default
$configurator->plugins->load('Autolink');//Fatdown & Forum default
$configurator->plugins->load('Escaper');//Fatdown default
$configurator->plugins->load('Litedown');//Fatdown default
$configurator->plugins->load('PipeTables');//Fatdown default
// Load BBCodes
$configurator->plugins->load('BBCodes');//Forum default
$configurator->BBCodes->addFromRepository('B');
$configurator->BBCodes->addFromRepository('U');
$configurator->BBCodes->addFromRepository('I');
$configurator->BBCodes->addFromRepository('S');
$configurator->BBCodes->addFromRepository('DEL');
$configurator->BBCodes->addFromRepository('INS');
$configurator->BBCodes->addFromRepository('EM');
$configurator->BBCodes->addFromRepository('COLOR');
$configurator->BBCodes->addFromRepository('H1');
$configurator->BBCodes->addFromRepository('URL');
$configurator->BBCodes->addFromRepository('IMG');
$configurator->BBCodes->addFromRepository('QUOTE');
$configurator->BBCodes->addFromRepository('CODE');
$configurator->BBCodes->addFromRepository('LIST');
$configurator->BBCodes->addFromRepository('CENTER');
$configurator->BBCodes->addFromRepository('RIGHT');
$configurator->BBCodes->addFromRepository('LEFT');
$configurator->BBCodes->addFromRepository('JUSTIFY');
// Alias COLOUR to COLOR
$configurator->BBCodes->add('COLOUR', ['defaultAttribute' => 'color'])->tagName = 'COLOR';
// Add some default limits
$configurator->tags['QUOTE']->nestingLimit = 3;
$configurator->tags['LIST']->nestingLimit = 5;
$configurator->registeredVars['cacheDir'] = ForumEnv::get('FORUM_CACHE_DIR');
// Get an instance of the parser and the renderer
extract($configurator->finalize());
// We save the parser and the renderer to the disk for easy reuse
Cache::store('s9eparser', serialize($parser));
Cache::store('s9erenderer', serialize($renderer));
$this->parser = $parser;
$this->renderer = $renderer;
}
/**
* Parse post or signature message text.
*
* @param string &$text
* @param integer $hideSmilies
* @return string
*/
public function parseBbcode(&$text, $hideSmilies = 0)
{
$xml = $this->parser->parse($text);
$html = $this->renderer->render($xml);
if (!$hideSmilies) {
$html = $this->doSmilies($html);
}
return $html;
}
/**
* Parse message text
*
* @param string $text
* @param integer $hideSmilies
* @return string
*/
public function parseMessage($text, $hideSmilies)
{
if (ForumSettings::get('o_censoring') == '1') {
$text = Utils::censor($text);
}
if (ForumSettings::get('p_message_bbcode') == '1') {
if (ForumSettings::get('p_message_img_tag') !== '1' || User::getPref('show.img') !== '1') {
$this->parser->disablePlugin('Autoimage');// enable after parsing?
$this->parser->disablePlugin('Autovideo');
}
$xml = $this->parser->parse($text);
$html = $this->renderer->render($xml);
}
else {
$html = Utils::escape($text);
}
if (User::getPref('show.smilies') == '1' && ForumSettings::get('o_smilies') == '1' && $hideSmilies == 0) {
$html = $this->doSmilies($html);
}
return $html;
}
/**
* Parse signature text
*
* @param string $text
* @return string
*/
public function parseSignature($text)
{
if (ForumSettings::get('o_censoring') == '1') {
$text = Utils::censor($text);
}
if (ForumSettings::get('p_sig_bbcode') == '1') {
if (ForumSettings::get('p_sig_img_tag') !== '1' || User::getPref('show.img.sig') !== '1') {
$this->parser->disablePlugin('Autoimage');// enable after parsing?
$this->parser->disablePlugin('Autovideo');
}
$xml = $this->parser->parse($text);
$html = $this->renderer->render($xml);
}
else {
$html = Utils::escape($text);
}
if (User::getPref('show.smilies') == '1' && ForumSettings::get('o_smilies') == '1') {
$html = $this->doSmilies($html);
}
return $html;
}
public function parseForSave($text, &$errors)
{
$xml = $this->parser->parse($text);
return Unparser::unparse($xml);
}
/**
* Pre-process text containing BBCodes. Check for integrity,
* well-formedness, nesting, etc. Flag errors by wrapping offending
* tags in a special [err] tag.
*
* @param string $text
* @param array &$errors
* @param integer $isSignature
* @return string
*/
public function preparseBbcode($text, &$errors, $isSignature = false)
{
$xml = $this->parser->parse($text);
return Unparser::unparse($xml);
}
/**
* Display smilies
* Credits: FluxBB
* @param $text string containing smilies to parse
* @return string text "smilied" :-)
*/
private function doSmilies($text)
{
$text = ' '.$text.' ';
foreach ($this->smilies as $smileyText => $smileyImg)
{
if (strpos($text, $smileyText) !== false) {
$text = Utils::ucpPregReplace('%(?<=[>\s])'.preg_quote($smileyText, '%').'(?=[^\p{L}\p{N}])%um', '<img src="'.Utils::escape(URL::base().'/style/img/smilies/'.$smileyImg).'" alt="'.substr($smileyImg, 0, strrpos($smileyImg, '.')).'" />', $text);
}
}
return substr($text, 1, -1);
}
}