-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBBCode.php
More file actions
703 lines (649 loc) · 21.3 KB
/
BBCode.php
File metadata and controls
703 lines (649 loc) · 21.3 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<?php
namespace ChrisKonnertz\BBCode;
use Closure;
/*
* BBCode to HTML converter
*
* Inspired by Kai Mallea (kmallea@gmail.com)
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*/
class BBCode
{
/**
* Constants with the names of the built-in default tags
* Call getDefaultTagNames() to get them as an array.
*/
const TAG_NAME_B = 'b';
const TAG_NAME_I = 'i';
const TAG_NAME_S = 's';
const TAG_NAME_U = 'u';
const TAG_NAME_CODE = 'code';
const TAG_NAME_EMAIL = 'email';
const TAG_NAME_URL = 'url';
const TAG_NAME_IMG = 'img';
const TAG_NAME_LIST = 'list';
const TAG_NAME_LI_STAR = '*';
const TAG_NAME_LI = 'li';
const TAG_NAME_QUOTE = 'quote';
const TAG_NAME_YOUTUBE = 'youtube';
const TAG_NAME_FONT = 'font';
const TAG_NAME_SIZE = 'size';
const TAG_NAME_COLOR = 'color';
const TAG_NAME_LEFT = 'left';
const TAG_NAME_CENTER = 'center';
const TAG_NAME_RIGHT = 'right';
const TAG_NAME_SPOILER = 'spoiler';
/**
* The current version number
*/
const VERSION = '1.1.2';
/**
* The text with BBCodes
*
* @var string|null
*/
protected $text = null;
/**
* Array with custom tag Closures
*
* @var Closure[]
*/
protected $customTagClosures = array();
/**
* Array of (name of) tags that are ignored
*
* @var string[]
*/
protected $ignoredTags = array();
/**
* Widht (in pixels) of the YouTube iframe element
*
* @var int
*/
protected $youTubeWidth = 640;
/**
* Height (in pixels) of the YouTube iframe element
*
* @var int
*/
protected $youTubeHeight = 385;
/**
* BBCode constructor.
*
* @param string|null $text The text - might include BBCode tags
*/
public function __construct($text = null)
{
$this->setText($text);
}
/**
* Set the raw text - might include BBCode tags
*
* @param string $text The text
* @retun void
*/
public function setText($text)
{
$this->text = $text;
}
/**
* Renders only the text without any BBCode tags.
*
* @param string $text Optional: Render the passed BBCode string instead of the internally stored one
* @return string
*/
public function renderPlain($text = null)
{
if ($this->text !== null and $text === null) {
$text = $this->text;
}
return preg_replace("/\[(.*?)\]/is", '', $text);
}
/**
* Renders only the text without any BBCode tags.
* Alias for renderRaw().
*
* @deprecated Deprecated since 1.1.0
*
* @param string $text Optional: Render the passed BBCode string instead of the internally stored one
* @return string
*/
public function renderRaw($text = null)
{
return $this->renderPlain($text);
}
/**
* Renders BBCode to HTML
*
* @param string $text Optional: Render the passed BBCode string instead of the internally stored one
* @param bool $escape Escape HTML entities? (Only "<" and ">"!)
* @param bool $keepLines Keep line breaks by replacing them with <br>?
* @return string
*/
public function render($text = null, $escape = true, $keepLines = true)
{
if ($this->text !== null and $text === null) {
$text = $this->text;
}
$html = '';
$len = mb_strlen($text);
$inTag = false; // True if current position is inside a tag
$inName = false; // True if current pos is inside a tag name
$inStr = false; // True if current pos is inside a string
/** @var Tag|null $tag */
$tag = null;
$openTags = array();
/*
* Loop over each character of the text
*/
for ($i = 0; $i < $len; $i++) {
$char = mb_substr($text, $i, 1);
if ($keepLines) {
if ($char == "\n") {
$html .= '<br/>';
}
if ($char == "\r") {
continue;
}
}
if (! $escape or ($char != '<' and $char != '>')) {
/*
* $inTag == true means the current position is inside a tag definition
* (= inside the brackets of a tag)
*/
if ($inTag) {
if ($char == '"') {
if ($inStr) {
$inStr = false;
} else {
if ($inName) {
$tag->valid = false;
} else {
$inStr = true;
}
}
} else {
/*
* This closes a tag
*/
if ($char == ']' and ! $inStr) {
$inTag = false;
$inName = false;
if ($tag->valid) {
$code = null;
if ($tag->opening) {
$code = $this->generateTag($tag, $html, null, $openTags);
} else {
$openingTag = $this->popTag($openTags, $tag);
if ($openingTag) {
$code = $this->generateTag($tag, $html, $openingTag, $openTags);
}
}
if ($code !== null and $tag->opening) {
$openTags[$tag->name][] = $tag;
}
$html .= $code;
}
continue;
}
if ($inName and ! $inStr) {
/*
* This makes the current tag a closing tag
*/
if ($char == '/') {
if ($tag->name) {
$tag->valid = false;
} else {
$tag->opening = false;
}
} else {
/*
* This means a property starts
*/
if ($char == '=') {
if ($tag->name) {
$inName = false;
} else {
$tag->valid = false;
}
} else {
$tag->name .= mb_strtolower($char);
}
}
} else { // If we are not inside the name we are inside a property
$tag->property .= $char;
}
}
} else {
/*
* This opens a tag
*/
if ($char == '[') {
$inTag = true;
$inName = true;
$tag = new Tag();
$tag->position = mb_strlen($html);
} else {
$html .= $char;
}
}
} else {
$html .= htmlspecialchars($char);
}
}
/*
* Check for tags that are not closed and close them.
*/
foreach ($openTags as $name => $openTagsByType) {
$closingTag = new Tag($name, false);
foreach ($openTagsByType as $openTag) {
$html .= $this->generateTag($closingTag, $html, $openTag);
}
}
return $html;
}
/**
* Generates and returns the HTML code of the current tag
*
* @param Tag $tag The current tag
* @param string $html The current HTML code passed by reference - might be altered!
* @param Tag|null $openingTag The opening tag that is linked to the tag (or null)
* @param Tag[] $openTags Array with tags that are opned but not closed
* @return string
*/
protected function generateTag(Tag $tag, &$html, Tag $openingTag = null, array $openTags = [])
{
$code = null;
if (in_array($tag->name, $this->ignoredTags)) {
return $code;
}
if ($tag->name && isset($this->customTagClosures[$tag->name])) {
$closure = $this->customTagClosures[$tag->name];
return $closure($tag, $html, $openingTag);
}
switch ($tag->name) {
case self::TAG_NAME_B:
if ($tag->opening) {
$code = '<strong>';
} else {
$code = '</strong>';
}
break;
case self::TAG_NAME_I:
if ($tag->opening) {
$code = '<em>';
} else {
$code = '</em>';
}
break;
case self::TAG_NAME_S:
if ($tag->opening) {
$code = '<del>';
} else {
$code = '</del>';
}
break;
case self::TAG_NAME_U:
if ($tag->opening) {
$code = '<span style="text-decoration: underline">';
} else {
$code = '</span>';
}
break;
case self::TAG_NAME_CODE:
if ($tag->opening) {
$code = '<pre><code>';
} else {
$code = '</code></pre>';
}
break;
case self::TAG_NAME_EMAIL:
if ($tag->opening) {
if ($tag->property) {
$code = '<a href="mailto:'.$tag->property.'">';
} else {
$code = '<a href="mailto:';
}
} else {
if ($openingTag->property) {
$code = '</a>';
} else {
$code .= '">'.mb_substr($html, $openingTag->position + 16).'</a>';
}
}
break;
case self::TAG_NAME_URL:
if ($tag->opening) {
if ($tag->property) {
$code = '<a href="'.$tag->property.'">';
} else {
$code = '<a href="';
}
} else {
if ($openingTag->property) {
$code = '</a>';
} else {
$partial = mb_substr($html, $openingTag->position + 9);
$html = mb_substr($html, 0, $openingTag->position + 9)
.strip_tags($partial).'">'.$partial.'</a>';
}
}
break;
case self::TAG_NAME_IMG:
if ($tag->opening) {
$code = '<img src="';
} else {
$code = '" />';
}
break;
case self::TAG_NAME_LIST:
if ($tag->opening) {
$listType = '<ul>';
if ($tag->property) {
$listType = '<ol>';
if ($tag->property == 'i') {
$listType = '<ol style="list-style-type: lower-roman">';
} elseif ($tag->property == 'a') {
$listType = '<ol style="list-style-type: lower-alpha">';
}
}
$code = $listType;
} else {
if ($this->endsWith($html, '<ul>')) {
$code = '</ul>';
} elseif ($this->endsWith($html, '<ol>')) {
$code = '</ol>';
} elseif ($this->endsWith($html, '<ol style="list-style-type: lower-roman">')) {
$code = '</ol>';
} elseif ($this->endsWith($html, '<ol style="list-style-type: lower-alpha">')) {
$code = '</ol>';
} elseif ($this->endsWith($html, '</li>') and $openingTag->property) {
$code = '</ol>';
} elseif ($this->endsWith($html, '</li>') and ! $openingTag->property) {
$code = '</ul>';
} elseif ($openingTag->property) {
$code = '</li></ol>';
} else {
$code = '</li></ul>';
}
}
break;
case self::TAG_NAME_LI_STAR:
if ($tag->opening) {
/*
* We require that the list item is inside a list
*/
if (isset($openTags['list']) and sizeof($openTags['list']) > 0) {
$tag->opening = false;
if ($this->endsWith($html, '<ul>')) {
$code = '<li>';
} else {
$code = '</li><li>';
}
}
}
break;
case self::TAG_NAME_LI:
if ($tag->opening) {
/*
* We require that the list item is inside a list
*/
if (isset($openTags['list']) and sizeof($openTags['list']) > 0) {
$code = '<li>';
}
} else {
$code = '</li>';
}
break;
case self::TAG_NAME_QUOTE:
if ($tag->opening) {
if ($tag->property) {
$code = '<blockquote><span class="author">'.$tag->property.':</span><br/>';
} else {
$code = '<blockquote>';
}
} else {
$code = '</blockquote>';
}
break;
case self::TAG_NAME_YOUTUBE:
if ($tag->opening) {
$code = '<iframe class="youtube-player" type="text/html" width="' . $this->youTubeWidth . '"\
height="' . $this->youTubeHeight . '" src="https://www.youtube.com/embed/';
} else {
$code = '" frameborder="0"></iframe>';
}
break;
case self::TAG_NAME_FONT:
if ($tag->opening) {
if ($tag->property) {
$code = '<span style="font-family: '.$tag->property.'">';
}
} else {
$code = '</span>';
}
break;
case self::TAG_NAME_SIZE:
if ($tag->opening) {
if ($tag->property) {
$code = '<span style="font-size: '.$tag->property.'%">';
}
} else {
$code = '</span>';
}
break;
case self::TAG_NAME_COLOR:
if ($tag->opening) {
if ($tag->property) {
$code = '<span style="color: '.$tag->property.'">';
}
} else {
$code = '</span>';
}
break;
case self::TAG_NAME_LEFT:
if ($tag->opening) {
$code = '<div style="text-align: left">';
} else {
$code = '</div>';
}
break;
case self::TAG_NAME_CENTER:
if ($tag->opening) {
$code = '<div style="text-align: center">';
} else {
$code = '</div>';
}
break;
case self::TAG_NAME_RIGHT:
if ($tag->opening) {
$code = '<div style="text-align: right">';
} else {
$code = '</div>';
}
break;
case self::TAG_NAME_SPOILER:
if ($tag->opening) {
$code = '<div class="spoiler">';
} else {
$code = '</div>';
}
break;
}
return $code;
}
/**
* Magic method __toString()
*
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* Returns the last tag of a given type and removes it from the array.
*
* @param Tag[] $tags Array of tags
* @param Tag $tag Return the last tag of the type of this tag
* @return Tag|null
*/
protected function popTag(array &$tags, $tag)
{
if (! isset($tags[$tag->name])) {
return null;
}
$size = sizeof($tags[$tag->name]);
if ($size === 0) {
return null;
} else {
return array_pop($tags[$tag->name]);
}
}
/**
* Adds a custom tag (with name and a Closure)
*
* Example:
*
* $bbcode->addTag('example', function($tag, &$html, $openingTag) {
* if ($tag->opening) {
* return '<span class="example">';
* } else {
* return '</span>';
* }
* });
*
* @param string $name The name of the tag
* @param Closure $closure The Closure that renders the tag
* @return void
*/
public function addTag($name, Closure $closure)
{
$this->customTagClosures[$name] = $closure;
}
/**
* Remove the custom tag with the given name
*
* @param string $name
* @return void
*/
public function forgetTag($name)
{
unset($this->customTagClosures[$name]);
}
/**
* Add a tag to the array of ignored tags
*
* @param string $name The name of the tag
* @return void
*/
public function ignoreTag($name)
{
if (! in_array($name, $this->ignoredTags)) {
$this->ignoredTags[] = $name;
}
}
/**
* Remove a tag from the array of ignored tags
*
* @param string $name The name of the tag
* @return void
*/
public function permitTag($name)
{
$key = array_search($name, $this->ignoredTags);
if ($key !== false) {
unset($this->ignoredTags[$key]);
}
}
/**
* Returns an array with the name of the tags that are ignored
*
* @return string[]
*/
public function getIgnoredTags()
{
return $this->ignoredTags;
}
/**
* Get the width of the YouTube iframe element
*
* @return int
*/
public function getYouTubeWidth()
{
return $this->youTubeWidth;
}
/**
* Set the width of the YouTube iframe element
*
* @param int $youTubeWidth
* @return void
*/
public function setYouTubeWidth($youTubeWidth)
{
$this->youTubeWidth = $youTubeWidth;
}
/**
* Get the height of the YouTube iframe element
*
* @return int
*/
public function getYouTubeHeight()
{
return $this->youTubeHeight;
}
/**
* Set the height of the YouTube iframe element
*
* @param int $youTubeHeight
* @return void
*/
public function setYouTubeHeight($youTubeHeight)
{
$this->youTubeHeight = $youTubeHeight;
}
/**
* Returns an array with the names of all default tags
*
* @return string[]
*/
public function getDefaultTagNames()
{
return [
self::TAG_NAME_B,
self::TAG_NAME_I,
self::TAG_NAME_S,
self::TAG_NAME_U,
self::TAG_NAME_CODE,
self::TAG_NAME_EMAIL,
self::TAG_NAME_URL,
self::TAG_NAME_IMG,
self::TAG_NAME_LIST,
self::TAG_NAME_LI_STAR,
self::TAG_NAME_LI,
self::TAG_NAME_QUOTE,
self::TAG_NAME_YOUTUBE,
self::TAG_NAME_FONT,
self::TAG_NAME_SIZE,
self::TAG_NAME_COLOR,
self::TAG_NAME_LEFT,
self::TAG_NAME_CENTER,
self::TAG_NAME_RIGHT,
self::TAG_NAME_SPOILER,
];
}
/**
* Returns true if $haystack ends with $needle
*
* @param string $haystack
* @param string $needle
* @return bool
*/
protected function endsWith($haystack, $needle)
{
return ($needle === '' or mb_substr($haystack, -mb_strlen($needle)) === $needle);
}
}